2021-11-26 17:08:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 flyinghead
|
|
|
|
|
|
|
|
This file is part of Flycast.
|
|
|
|
|
|
|
|
Flycast is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Flycast is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Flycast. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "rend/imgui_driver.h"
|
2023-05-03 07:50:37 +00:00
|
|
|
#include "imgui/backends/imgui_impl_dx11.h"
|
2021-11-26 17:08:41 +00:00
|
|
|
#include "dx11context.h"
|
2022-02-14 17:38:58 +00:00
|
|
|
#include "rend/gui.h"
|
2022-06-15 19:22:12 +00:00
|
|
|
#include <unordered_map>
|
2021-11-26 17:08:41 +00:00
|
|
|
|
|
|
|
class DX11Driver final : public ImGuiDriver
|
|
|
|
{
|
|
|
|
public:
|
2022-04-13 16:06:19 +00:00
|
|
|
DX11Driver(ID3D11Device* device, ID3D11DeviceContext* deviceContext) {
|
|
|
|
ImGui_ImplDX11_Init(device, deviceContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DX11Driver() {
|
|
|
|
ImGui_ImplDX11_Shutdown();
|
|
|
|
}
|
|
|
|
|
2021-11-26 17:08:41 +00:00
|
|
|
void newFrame() override {
|
|
|
|
ImGui_ImplDX11_NewFrame();
|
|
|
|
}
|
|
|
|
|
2022-12-16 19:34:30 +00:00
|
|
|
void renderDrawData(ImDrawData *drawData, bool gui_open) override {
|
2021-11-26 17:08:41 +00:00
|
|
|
theDX11Context.EndImGuiFrame();
|
2022-12-16 19:34:30 +00:00
|
|
|
if (gui_open)
|
2022-02-14 17:38:58 +00:00
|
|
|
frameRendered = true;
|
2021-11-26 17:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void present() override {
|
2022-02-14 17:38:58 +00:00
|
|
|
if (frameRendered)
|
|
|
|
theDX11Context.Present();
|
|
|
|
frameRendered = false;
|
2021-11-26 17:08:41 +00:00
|
|
|
}
|
2022-02-14 17:38:58 +00:00
|
|
|
|
|
|
|
void setFrameRendered() override {
|
|
|
|
frameRendered = true;
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:22:12 +00:00
|
|
|
ImTextureID getTexture(const std::string& name) override {
|
|
|
|
auto it = textures.find(name);
|
|
|
|
if (it != textures.end())
|
|
|
|
return (ImTextureID)it->second.textureView.get();
|
|
|
|
else
|
|
|
|
return ImTextureID{};
|
|
|
|
}
|
|
|
|
|
|
|
|
ImTextureID updateTexture(const std::string& name, const u8 *data, int width, int height) override
|
|
|
|
{
|
|
|
|
Texture& texture = textures[name];
|
|
|
|
texture.texture.reset();
|
|
|
|
texture.textureView.reset();
|
|
|
|
|
|
|
|
D3D11_TEXTURE2D_DESC desc{};
|
|
|
|
desc.Width = width;
|
|
|
|
desc.Height = height;
|
|
|
|
desc.ArraySize = 1;
|
|
|
|
desc.SampleDesc.Count = 1;
|
|
|
|
desc.Usage = D3D11_USAGE_DEFAULT;
|
|
|
|
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
desc.MipLevels = 1;
|
|
|
|
theDX11Context.getDevice()->CreateTexture2D(&desc, nullptr, &texture.texture.get());
|
|
|
|
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc{};
|
|
|
|
viewDesc.Format = desc.Format;
|
|
|
|
viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
viewDesc.Texture2D.MipLevels = desc.MipLevels;
|
|
|
|
theDX11Context.getDevice()->CreateShaderResourceView(texture.texture, &viewDesc, &texture.textureView.get());
|
|
|
|
|
|
|
|
theDX11Context.getDeviceContext()->UpdateSubresource(texture.texture, 0, nullptr, data, width * 4, width * 4 * height);
|
|
|
|
|
|
|
|
return (ImTextureID)texture.textureView.get();
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:38:58 +00:00
|
|
|
private:
|
2022-06-15 19:22:12 +00:00
|
|
|
struct Texture
|
|
|
|
{
|
|
|
|
ComPtr<ID3D11Texture2D> texture;
|
|
|
|
ComPtr<ID3D11ShaderResourceView> textureView;
|
|
|
|
};
|
|
|
|
|
2022-02-14 17:38:58 +00:00
|
|
|
bool frameRendered = false;
|
2022-06-15 19:22:12 +00:00
|
|
|
std::unordered_map<std::string, Texture> textures;
|
2021-11-26 17:08:41 +00:00
|
|
|
};
|