mirror of
https://github.com/WinampDesktop/winamp.git
synced 2025-06-18 07:15:45 -04:00
GPU/HW: Add D3D11 renderer and refactor host interface/display
This commit is contained in:
@ -1,12 +1,21 @@
|
||||
set(SRCS
|
||||
add_executable(duckstation
|
||||
icon.cpp
|
||||
main.cpp
|
||||
opengl_host_display.cpp
|
||||
opengl_host_display.h
|
||||
sdl_audio_stream.cpp
|
||||
sdl_audio_stream.h
|
||||
sdl_interface.cpp
|
||||
sdl_interface.h
|
||||
sdl_host_interface.cpp
|
||||
sdl_host_interface.h
|
||||
)
|
||||
|
||||
add_executable(duckstation ${SRCS})
|
||||
target_include_directories(duckstation PRIVATE "${SDL2_INCLUDE_DIRS}")
|
||||
target_link_libraries(duckstation core imgui nativefiledialog "${SDL2_LIBRARIES}")
|
||||
target_link_libraries(duckstation PRIVATE YBaseLib core common imgui nativefiledialog glad "${SDL2_LIBRARIES}")
|
||||
|
||||
if(WIN32)
|
||||
target_sources(duckstation PRIVATE
|
||||
d3d11_host_display.cpp
|
||||
d3d11_host_display.h
|
||||
)
|
||||
target_link_libraries(duckstation PRIVATE d3d11.lib)
|
||||
endif()
|
||||
|
394
src/duckstation/d3d11_host_display.cpp
Normal file
394
src/duckstation/d3d11_host_display.cpp
Normal file
@ -0,0 +1,394 @@
|
||||
#include "d3d11_host_display.h"
|
||||
#include "YBaseLib/Log.h"
|
||||
#include "common/d3d11/shader_compiler.h"
|
||||
#include <SDL_syswm.h>
|
||||
#include <array>
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_dx11.h>
|
||||
#include <imgui_impl_sdl.h>
|
||||
Log_SetChannel(D3D11HostDisplay);
|
||||
|
||||
class D3D11HostDisplayTexture : public HostDisplayTexture
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
using ComPtr = Microsoft::WRL::ComPtr<T>;
|
||||
|
||||
D3D11HostDisplayTexture(ComPtr<ID3D11Texture2D> texture, ComPtr<ID3D11ShaderResourceView> srv, u32 width, u32 height,
|
||||
bool dynamic)
|
||||
: m_texture(std::move(texture)), m_srv(std::move(srv)), m_width(width), m_height(height), m_dynamic(dynamic)
|
||||
{
|
||||
}
|
||||
~D3D11HostDisplayTexture() override = default;
|
||||
|
||||
void* GetHandle() const override { return m_srv.Get(); }
|
||||
u32 GetWidth() const override { return m_width; }
|
||||
u32 GetHeight() const override { return m_height; }
|
||||
|
||||
ID3D11Texture2D* GetD3DTexture() const { return m_texture.Get(); }
|
||||
ID3D11ShaderResourceView* GetD3DSRV() const { return m_srv.Get(); }
|
||||
bool IsDynamic() const { return m_dynamic; }
|
||||
|
||||
static std::unique_ptr<D3D11HostDisplayTexture> Create(ID3D11Device* device, u32 width, u32 height, const void* data,
|
||||
u32 data_stride, bool dynamic)
|
||||
{
|
||||
const CD3D11_TEXTURE2D_DESC desc(DXGI_FORMAT_R8G8B8A8_UNORM, width, height, 1, 1, D3D11_BIND_SHADER_RESOURCE,
|
||||
dynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT,
|
||||
dynamic ? D3D11_CPU_ACCESS_WRITE : 0, 1, 0, 0);
|
||||
const D3D11_SUBRESOURCE_DATA srd{data, data_stride, data_stride * height};
|
||||
ComPtr<ID3D11Texture2D> texture;
|
||||
HRESULT hr = device->CreateTexture2D(&desc, data ? &srd : nullptr, texture.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
return {};
|
||||
|
||||
const CD3D11_SHADER_RESOURCE_VIEW_DESC srv_desc(D3D11_SRV_DIMENSION_TEXTURE2D, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 1, 0,
|
||||
1);
|
||||
ComPtr<ID3D11ShaderResourceView> srv;
|
||||
hr = device->CreateShaderResourceView(texture.Get(), &srv_desc, srv.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
return {};
|
||||
|
||||
return std::make_unique<D3D11HostDisplayTexture>(std::move(texture), std::move(srv), width, height, dynamic);
|
||||
}
|
||||
|
||||
private:
|
||||
ComPtr<ID3D11Texture2D> m_texture;
|
||||
ComPtr<ID3D11ShaderResourceView> m_srv;
|
||||
u32 m_width;
|
||||
u32 m_height;
|
||||
bool m_dynamic;
|
||||
};
|
||||
|
||||
D3D11HostDisplay::D3D11HostDisplay(SDL_Window* window) : m_window(window)
|
||||
{
|
||||
SDL_GetWindowSize(window, &m_window_width, &m_window_height);
|
||||
}
|
||||
|
||||
D3D11HostDisplay::~D3D11HostDisplay()
|
||||
{
|
||||
ImGui_ImplDX11_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
|
||||
if (m_window)
|
||||
SDL_DestroyWindow(m_window);
|
||||
}
|
||||
|
||||
HostDisplay::RenderAPI D3D11HostDisplay::GetRenderAPI() const
|
||||
{
|
||||
return HostDisplay::RenderAPI::D3D11;
|
||||
}
|
||||
|
||||
void* D3D11HostDisplay::GetHostRenderDevice() const
|
||||
{
|
||||
return m_device.Get();
|
||||
}
|
||||
|
||||
void* D3D11HostDisplay::GetHostRenderContext() const
|
||||
{
|
||||
return m_context.Get();
|
||||
}
|
||||
|
||||
std::unique_ptr<HostDisplayTexture> D3D11HostDisplay::CreateTexture(u32 width, u32 height, const void* data,
|
||||
u32 data_stride, bool dynamic)
|
||||
{
|
||||
return D3D11HostDisplayTexture::Create(m_device.Get(), width, height, data, data_stride, dynamic);
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::UpdateTexture(HostDisplayTexture* texture, u32 x, u32 y, u32 width, u32 height, const void* data,
|
||||
u32 data_stride)
|
||||
{
|
||||
D3D11HostDisplayTexture* d3d11_texture = static_cast<D3D11HostDisplayTexture*>(texture);
|
||||
if (!d3d11_texture->IsDynamic())
|
||||
{
|
||||
const CD3D11_BOX dst_box(x, y, 0, x + width, y + height, 1);
|
||||
m_context->UpdateSubresource(d3d11_texture->GetD3DTexture(), 0, &dst_box, data, data_stride, data_stride * height);
|
||||
}
|
||||
else
|
||||
{
|
||||
D3D11_MAPPED_SUBRESOURCE sr;
|
||||
HRESULT hr = m_context->Map(d3d11_texture->GetD3DTexture(), 0, D3D11_MAP_WRITE_DISCARD, 0, &sr);
|
||||
if (FAILED(hr))
|
||||
Panic("Failed to map dynamic host display texture");
|
||||
|
||||
char* dst_ptr = static_cast<char*>(sr.pData) + (y * sr.RowPitch) + (x * sizeof(u32));
|
||||
const char* src_ptr = static_cast<const char*>(data);
|
||||
if (sr.RowPitch == data_stride)
|
||||
{
|
||||
std::memcpy(dst_ptr, src_ptr, data_stride * height);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (u32 row = 0; row < height; row++)
|
||||
{
|
||||
std::memcpy(dst_ptr, src_ptr, width * sizeof(u32));
|
||||
src_ptr += data_stride;
|
||||
dst_ptr += sr.RowPitch;
|
||||
}
|
||||
}
|
||||
|
||||
m_context->Unmap(d3d11_texture->GetD3DTexture(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::SetDisplayTexture(void* texture, u32 offset_x, u32 offset_y, u32 width, u32 height,
|
||||
u32 texture_width, u32 texture_height, float aspect_ratio)
|
||||
{
|
||||
m_display_srv = static_cast<ID3D11ShaderResourceView*>(texture);
|
||||
m_display_offset_x = offset_x;
|
||||
m_display_offset_y = offset_y;
|
||||
m_display_width = width;
|
||||
m_display_height = height;
|
||||
m_display_texture_width = texture_width;
|
||||
m_display_texture_height = texture_height;
|
||||
m_display_aspect_ratio = aspect_ratio;
|
||||
m_display_texture_changed = true;
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::SetDisplayLinearFiltering(bool enabled)
|
||||
{
|
||||
m_display_linear_filtering = enabled;
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::SetVSync(bool enabled)
|
||||
{
|
||||
m_vsync = enabled;
|
||||
}
|
||||
|
||||
std::tuple<u32, u32> D3D11HostDisplay::GetWindowSize() const
|
||||
{
|
||||
return std::make_tuple(static_cast<u32>(m_window_width), static_cast<u32>(m_window_height));
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::WindowResized()
|
||||
{
|
||||
SDL_GetWindowSize(m_window, &m_window_width, &m_window_height);
|
||||
|
||||
m_swap_chain_rtv.Reset();
|
||||
|
||||
HRESULT hr = m_swap_chain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);
|
||||
if (FAILED(hr))
|
||||
Log_ErrorPrintf("ResizeBuffers() failed: 0x%08X", hr);
|
||||
|
||||
if (!CreateSwapChainRTV())
|
||||
Panic("Failed to recreate swap chain RTV after resize");
|
||||
}
|
||||
|
||||
bool D3D11HostDisplay::CreateD3DDevice()
|
||||
{
|
||||
const bool debug = false;
|
||||
|
||||
SDL_SysWMinfo syswm = {};
|
||||
if (!SDL_GetWindowWMInfo(m_window, &syswm))
|
||||
{
|
||||
Log_ErrorPrintf("SDL_GetWindowWMInfo failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
UINT create_flags = 0;
|
||||
if (debug)
|
||||
create_flags |= D3D11_CREATE_DEVICE_DEBUG;
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC swap_chain_desc = {};
|
||||
swap_chain_desc.BufferDesc.Width = m_window_width;
|
||||
swap_chain_desc.BufferDesc.Height = m_window_height;
|
||||
swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
swap_chain_desc.SampleDesc.Count = 1;
|
||||
swap_chain_desc.BufferCount = 3;
|
||||
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
swap_chain_desc.OutputWindow = syswm.info.win.window;
|
||||
swap_chain_desc.Windowed = TRUE;
|
||||
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
||||
|
||||
HRESULT hr = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, create_flags, nullptr, 0,
|
||||
D3D11_SDK_VERSION, &swap_chain_desc, m_swap_chain.GetAddressOf(),
|
||||
m_device.GetAddressOf(), nullptr, m_context.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("D3D11CreateDeviceAndSwapChain failed: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if 0
|
||||
ComPtr<ID3D11InfoQueue> info;
|
||||
hr = m_device.As(&info);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
info->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, TRUE);
|
||||
info->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_WARNING, TRUE);
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool D3D11HostDisplay::CreateSwapChainRTV()
|
||||
{
|
||||
ComPtr<ID3D11Texture2D> backbuffer;
|
||||
HRESULT hr = m_swap_chain->GetBuffer(0, IID_PPV_ARGS(backbuffer.GetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("GetBuffer for RTV failed: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D11_TEXTURE2D_DESC backbuffer_desc;
|
||||
backbuffer->GetDesc(&backbuffer_desc);
|
||||
|
||||
CD3D11_RENDER_TARGET_VIEW_DESC rtv_desc(D3D11_RTV_DIMENSION_TEXTURE2D, backbuffer_desc.Format, 0, 0,
|
||||
backbuffer_desc.ArraySize);
|
||||
hr = m_device->CreateRenderTargetView(backbuffer.Get(), &rtv_desc, m_swap_chain_rtv.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("CreateRenderTargetView for swap chain failed: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool D3D11HostDisplay::CreateD3DResources()
|
||||
{
|
||||
static constexpr char fullscreen_quad_vertex_shader[] = R"(
|
||||
void main(in uint vertex_id : SV_VertexID,
|
||||
out float2 v_tex0 : TEXCOORD0,
|
||||
out float4 o_pos : SV_Position)
|
||||
{
|
||||
v_tex0 = float2(float((vertex_id << 1) & 2u), float(vertex_id & 2u));
|
||||
o_pos = float4(v_tex0 * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);
|
||||
}
|
||||
)";
|
||||
|
||||
static constexpr char display_pixel_shader[] = R"(
|
||||
cbuffer UBOBlock : register(b0)
|
||||
{
|
||||
float4 u_src_rect;
|
||||
};
|
||||
|
||||
Texture2D samp0 : register(t0);
|
||||
SamplerState samp0_ss : register(s0);
|
||||
|
||||
void main(in float2 v_tex0 : TEXCOORD0,
|
||||
out float4 o_col0 : SV_Target)
|
||||
{
|
||||
float2 coords = u_src_rect.xy + v_tex0 * u_src_rect.zw;
|
||||
o_col0 = samp0.Sample(samp0_ss, coords);
|
||||
}
|
||||
)";
|
||||
|
||||
HRESULT hr;
|
||||
|
||||
m_display_vertex_shader =
|
||||
D3D11::ShaderCompiler::CompileAndCreateVertexShader(m_device.Get(), fullscreen_quad_vertex_shader, false);
|
||||
m_display_pixel_shader =
|
||||
D3D11::ShaderCompiler::CompileAndCreatePixelShader(m_device.Get(), display_pixel_shader, false);
|
||||
if (!m_display_vertex_shader || !m_display_pixel_shader)
|
||||
return false;
|
||||
|
||||
if (!m_display_uniform_buffer.Create(m_device.Get(), D3D11_BIND_CONSTANT_BUFFER, DISPLAY_UNIFORM_BUFFER_SIZE))
|
||||
return false;
|
||||
|
||||
CD3D11_RASTERIZER_DESC rasterizer_desc = CD3D11_RASTERIZER_DESC(CD3D11_DEFAULT());
|
||||
rasterizer_desc.CullMode = D3D11_CULL_NONE;
|
||||
hr = m_device->CreateRasterizerState(&rasterizer_desc, m_display_rasterizer_state.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
CD3D11_DEPTH_STENCIL_DESC depth_stencil_desc = CD3D11_DEPTH_STENCIL_DESC(CD3D11_DEFAULT());
|
||||
depth_stencil_desc.DepthEnable = FALSE;
|
||||
depth_stencil_desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
|
||||
hr = m_device->CreateDepthStencilState(&depth_stencil_desc, m_display_depth_stencil_state.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
CD3D11_BLEND_DESC blend_desc = CD3D11_BLEND_DESC(CD3D11_DEFAULT());
|
||||
hr = m_device->CreateBlendState(&blend_desc, m_display_blend_state.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
CD3D11_SAMPLER_DESC sampler_desc = CD3D11_SAMPLER_DESC(CD3D11_DEFAULT());
|
||||
sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
|
||||
hr = m_device->CreateSamplerState(&sampler_desc, m_point_sampler.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
sampler_desc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
|
||||
hr = m_device->CreateSamplerState(&sampler_desc, m_linear_sampler.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool D3D11HostDisplay::CreateImGuiContext()
|
||||
{
|
||||
if (!ImGui_ImplSDL2_InitForD3D(m_window) || !ImGui_ImplDX11_Init(m_device.Get(), m_context.Get()))
|
||||
return false;
|
||||
|
||||
ImGui_ImplDX11_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame(m_window);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<HostDisplay> D3D11HostDisplay::Create(SDL_Window* window)
|
||||
{
|
||||
std::unique_ptr<D3D11HostDisplay> display = std::make_unique<D3D11HostDisplay>(window);
|
||||
if (!display->CreateD3DDevice() || !display->CreateSwapChainRTV() || !display->CreateD3DResources() ||
|
||||
!display->CreateImGuiContext())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return display;
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::Render()
|
||||
{
|
||||
static constexpr std::array<float, 4> clear_color = {};
|
||||
m_context->ClearRenderTargetView(m_swap_chain_rtv.Get(), clear_color.data());
|
||||
m_context->OMSetRenderTargets(1, m_swap_chain_rtv.GetAddressOf(), nullptr);
|
||||
|
||||
RenderDisplay();
|
||||
|
||||
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
m_swap_chain->Present(BoolToUInt32(m_vsync), 0);
|
||||
|
||||
ImGui_ImplSDL2_NewFrame(m_window);
|
||||
ImGui_ImplDX11_NewFrame();
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::RenderDisplay()
|
||||
{
|
||||
if (!m_display_srv)
|
||||
return;
|
||||
|
||||
// - 20 for main menu padding
|
||||
auto [vp_left, vp_top, vp_width, vp_height] =
|
||||
CalculateDrawRect(m_window_width, std::max(m_window_height - 20, 1), m_display_aspect_ratio);
|
||||
vp_top += 20;
|
||||
|
||||
m_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
m_context->VSSetShader(m_display_vertex_shader.Get(), nullptr, 0);
|
||||
m_context->PSSetShader(m_display_pixel_shader.Get(), nullptr, 0);
|
||||
m_context->PSSetShaderResources(0, 1, &m_display_srv);
|
||||
m_context->PSSetSamplers(
|
||||
0, 1, m_display_linear_filtering ? m_linear_sampler.GetAddressOf() : m_point_sampler.GetAddressOf());
|
||||
|
||||
const float uniforms[4] = {static_cast<float>(m_display_offset_x) / static_cast<float>(m_display_texture_width),
|
||||
static_cast<float>(m_display_offset_y) / static_cast<float>(m_display_texture_height),
|
||||
static_cast<float>(m_display_width) / static_cast<float>(m_display_texture_width),
|
||||
static_cast<float>(m_display_height) / static_cast<float>(m_display_texture_height)};
|
||||
const auto map = m_display_uniform_buffer.Map(m_context.Get(), sizeof(uniforms), sizeof(uniforms));
|
||||
std::memcpy(map.pointer, uniforms, sizeof(uniforms));
|
||||
m_display_uniform_buffer.Unmap(m_context.Get(), sizeof(uniforms));
|
||||
m_context->PSSetConstantBuffers(0, 1, m_display_uniform_buffer.GetD3DBufferArray());
|
||||
|
||||
const CD3D11_VIEWPORT vp(static_cast<float>(vp_left), static_cast<float>(vp_top), static_cast<float>(vp_width),
|
||||
static_cast<float>(vp_height));
|
||||
m_context->RSSetViewports(1, &vp);
|
||||
m_context->RSSetState(m_display_rasterizer_state.Get());
|
||||
m_context->OMSetDepthStencilState(m_display_depth_stencil_state.Get(), 0);
|
||||
m_context->OMSetBlendState(m_display_blend_state.Get(), nullptr, 0xFFFFFFFFu);
|
||||
|
||||
m_context->Draw(3, 0);
|
||||
}
|
84
src/duckstation/d3d11_host_display.h
Normal file
84
src/duckstation/d3d11_host_display.h
Normal file
@ -0,0 +1,84 @@
|
||||
#pragma once
|
||||
#include "YBaseLib/Windows/WindowsHeaders.h"
|
||||
#include "common/d3d11/stream_buffer.h"
|
||||
#include "common/d3d11/texture.h"
|
||||
#include "core/host_display.h"
|
||||
#include <SDL.h>
|
||||
#include <d3d11.h>
|
||||
#include <memory>
|
||||
#include <wrl/client.h>
|
||||
|
||||
class D3D11HostDisplay final : public HostDisplay
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
using ComPtr = Microsoft::WRL::ComPtr<T>;
|
||||
|
||||
D3D11HostDisplay(SDL_Window* window);
|
||||
~D3D11HostDisplay();
|
||||
|
||||
static std::unique_ptr<HostDisplay> Create(SDL_Window* window);
|
||||
|
||||
RenderAPI GetRenderAPI() const override;
|
||||
void* GetHostRenderDevice() const override;
|
||||
void* GetHostRenderContext() const override;
|
||||
|
||||
std::unique_ptr<HostDisplayTexture> CreateTexture(u32 width, u32 height, const void* data, u32 data_stride,
|
||||
bool dynamic) override;
|
||||
void UpdateTexture(HostDisplayTexture* texture, u32 x, u32 y, u32 width, u32 height, const void* data,
|
||||
u32 data_stride) override;
|
||||
|
||||
void SetDisplayTexture(void* texture, u32 offset_x, u32 offset_y, u32 width, u32 height, u32 texture_width,
|
||||
u32 texture_height, float aspect_ratio) override;
|
||||
void SetDisplayLinearFiltering(bool enabled) override;
|
||||
|
||||
void SetVSync(bool enabled) override;
|
||||
|
||||
std::tuple<u32, u32> GetWindowSize() const override;
|
||||
void WindowResized() override;
|
||||
|
||||
private:
|
||||
static constexpr u32 DISPLAY_UNIFORM_BUFFER_SIZE = 16;
|
||||
|
||||
bool CreateD3DDevice();
|
||||
bool CreateD3DResources();
|
||||
bool CreateSwapChainRTV();
|
||||
bool CreateImGuiContext();
|
||||
|
||||
void Render();
|
||||
void RenderDisplay();
|
||||
|
||||
SDL_Window* m_window = nullptr;
|
||||
SDL_GLContext m_gl_context = nullptr;
|
||||
int m_window_width = 0;
|
||||
int m_window_height = 0;
|
||||
|
||||
ComPtr<ID3D11Device> m_device;
|
||||
ComPtr<ID3D11DeviceContext> m_context;
|
||||
ComPtr<IDXGISwapChain> m_swap_chain;
|
||||
ComPtr<ID3D11RenderTargetView> m_swap_chain_rtv;
|
||||
|
||||
ComPtr<ID3D11RasterizerState> m_display_rasterizer_state;
|
||||
ComPtr<ID3D11DepthStencilState> m_display_depth_stencil_state;
|
||||
ComPtr<ID3D11BlendState> m_display_blend_state;
|
||||
ComPtr<ID3D11VertexShader> m_display_vertex_shader;
|
||||
ComPtr<ID3D11PixelShader> m_display_pixel_shader;
|
||||
ComPtr<ID3D11SamplerState> m_point_sampler;
|
||||
ComPtr<ID3D11SamplerState> m_linear_sampler;
|
||||
|
||||
D3D11::Texture m_display_pixels_texture;
|
||||
D3D11::StreamBuffer m_display_uniform_buffer;
|
||||
|
||||
ID3D11ShaderResourceView* m_display_srv = nullptr;
|
||||
u32 m_display_offset_x = 0;
|
||||
u32 m_display_offset_y = 0;
|
||||
u32 m_display_width = 0;
|
||||
u32 m_display_height = 0;
|
||||
u32 m_display_texture_width = 0;
|
||||
u32 m_display_texture_height = 0;
|
||||
float m_display_aspect_ratio = 1.0f;
|
||||
|
||||
bool m_display_texture_changed = false;
|
||||
bool m_display_linear_filtering = false;
|
||||
bool m_vsync = false;
|
||||
};
|
@ -52,15 +52,19 @@
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="d3d11_host_display.cpp" />
|
||||
<ClCompile Include="opengl_host_display.cpp" />
|
||||
<ClCompile Include="icon.cpp" />
|
||||
<ClCompile Include="sdl_audio_stream.cpp" />
|
||||
<ClCompile Include="sdl_interface.cpp" />
|
||||
<ClCompile Include="sdl_host_interface.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="d3d11_host_display.h" />
|
||||
<ClInclude Include="opengl_host_display.h" />
|
||||
<ClInclude Include="icon.h" />
|
||||
<ClInclude Include="sdl_audio_stream.h" />
|
||||
<ClInclude Include="sdl_interface.h" />
|
||||
<ClInclude Include="sdl_host_interface.h" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{DAA8F93D-9C17-4DE2-BD0B-57891E0FF0D9}</ProjectGuid>
|
||||
@ -213,7 +217,7 @@
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>SDL2.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib32-debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
@ -235,7 +239,7 @@
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>SDL2.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib64-debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
@ -260,7 +264,7 @@
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>SDL2.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib32-debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
@ -285,7 +289,7 @@
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>SDL2.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib64-debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
@ -308,7 +312,7 @@
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalDependencies>SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>SDL2.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib32;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
@ -333,7 +337,7 @@
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalDependencies>SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>SDL2.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib32;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
@ -357,7 +361,7 @@
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalDependencies>SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>SDL2.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
@ -382,7 +386,7 @@
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalDependencies>SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>SDL2.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)dep\msvc\lib64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
|
@ -2,13 +2,17 @@
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="sdl_interface.cpp" />
|
||||
<ClCompile Include="sdl_audio_stream.cpp" />
|
||||
<ClCompile Include="icon.cpp" />
|
||||
<ClCompile Include="opengl_host_display.cpp" />
|
||||
<ClCompile Include="sdl_host_interface.cpp" />
|
||||
<ClCompile Include="d3d11_host_display.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="sdl_interface.h" />
|
||||
<ClInclude Include="icon.h" />
|
||||
<ClInclude Include="sdl_audio_stream.h" />
|
||||
<ClInclude Include="opengl_host_display.h" />
|
||||
<ClInclude Include="sdl_host_interface.h" />
|
||||
<ClInclude Include="d3d11_host_display.h" />
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -2,7 +2,7 @@
|
||||
#include "YBaseLib/Log.h"
|
||||
#include "YBaseLib/StringConverter.h"
|
||||
#include "core/system.h"
|
||||
#include "sdl_interface.h"
|
||||
#include "sdl_host_interface.h"
|
||||
#include <SDL.h>
|
||||
#include <cstdio>
|
||||
|
||||
@ -40,7 +40,7 @@ static int Run(int argc, char* argv[])
|
||||
#define CHECK_ARG_PARAM(str) (!std::strcmp(argv[i], str) && ((i + 1) < argc))
|
||||
|
||||
if (CHECK_ARG_PARAM("-state"))
|
||||
state_filename = SDLInterface::GetSaveStateFilename(std::strtoul(argv[++i], nullptr, 10));
|
||||
state_filename = SDLHostInterface::GetSaveStateFilename(std::strtoul(argv[++i], nullptr, 10));
|
||||
else if (CHECK_ARG_PARAM("-exp1"))
|
||||
exp1_filename = argv[++i];
|
||||
else
|
||||
@ -51,8 +51,8 @@ static int Run(int argc, char* argv[])
|
||||
}
|
||||
|
||||
// create display and host interface
|
||||
std::unique_ptr<SDLInterface> host_interface =
|
||||
SDLInterface::Create(filename, exp1_filename, state_filename.IsEmpty() ? nullptr : state_filename.GetCharArray());
|
||||
std::unique_ptr<SDLHostInterface> host_interface =
|
||||
SDLHostInterface::Create(filename, exp1_filename, state_filename.IsEmpty() ? nullptr : state_filename.GetCharArray());
|
||||
if (!host_interface)
|
||||
{
|
||||
Panic("Failed to create host interface");
|
||||
|
318
src/duckstation/opengl_host_display.cpp
Normal file
318
src/duckstation/opengl_host_display.cpp
Normal file
@ -0,0 +1,318 @@
|
||||
#include "opengl_host_display.h"
|
||||
#include "YBaseLib/Log.h"
|
||||
#include "icon.h"
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
#include <imgui_impl_sdl.h>
|
||||
Log_SetChannel(OpenGLHostDisplay);
|
||||
|
||||
class OpenGLHostDisplayTexture : public HostDisplayTexture
|
||||
{
|
||||
public:
|
||||
OpenGLHostDisplayTexture(GLuint id, u32 width, u32 height) : m_id(id), m_width(width), m_height(height) {}
|
||||
~OpenGLHostDisplayTexture() override { glDeleteTextures(1, &m_id); }
|
||||
|
||||
void* GetHandle() const override { return reinterpret_cast<void*>(static_cast<uintptr_t>(m_id)); }
|
||||
u32 GetWidth() const override { return m_width; }
|
||||
u32 GetHeight() const override { return m_height; }
|
||||
|
||||
GLuint GetGLID() const { return m_id; }
|
||||
|
||||
static std::unique_ptr<OpenGLHostDisplayTexture> Create(u32 width, u32 height, const void* initial_data,
|
||||
u32 initial_data_stride)
|
||||
{
|
||||
GLuint id;
|
||||
glGenTextures(1, &id);
|
||||
|
||||
GLint old_texture_binding = 0;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture_binding);
|
||||
|
||||
// TODO: Set pack width
|
||||
Assert(initial_data_stride == (width * sizeof(u32)));
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, initial_data);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
return std::make_unique<OpenGLHostDisplayTexture>(id, width, height);
|
||||
}
|
||||
|
||||
private:
|
||||
GLuint m_id;
|
||||
u32 m_width;
|
||||
u32 m_height;
|
||||
};
|
||||
|
||||
OpenGLHostDisplay::OpenGLHostDisplay(SDL_Window* window) : m_window(window)
|
||||
{
|
||||
SDL_GetWindowSize(window, &m_window_width, &m_window_height);
|
||||
}
|
||||
|
||||
OpenGLHostDisplay::~OpenGLHostDisplay()
|
||||
{
|
||||
if (m_gl_context)
|
||||
{
|
||||
if (m_display_vao != 0)
|
||||
glDeleteVertexArrays(1, &m_display_vao);
|
||||
if (m_display_linear_sampler != 0)
|
||||
glDeleteSamplers(1, &m_display_linear_sampler);
|
||||
if (m_display_nearest_sampler != 0)
|
||||
glDeleteSamplers(1, &m_display_nearest_sampler);
|
||||
|
||||
m_display_program.Destroy();
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
SDL_GL_MakeCurrent(nullptr, nullptr);
|
||||
SDL_GL_DeleteContext(m_gl_context);
|
||||
}
|
||||
|
||||
if (m_window)
|
||||
SDL_DestroyWindow(m_window);
|
||||
}
|
||||
|
||||
HostDisplay::RenderAPI OpenGLHostDisplay::GetRenderAPI() const
|
||||
{
|
||||
return HostDisplay::RenderAPI::OpenGL;
|
||||
}
|
||||
|
||||
void* OpenGLHostDisplay::GetHostRenderDevice() const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* OpenGLHostDisplay::GetHostRenderContext() const
|
||||
{
|
||||
return m_gl_context;
|
||||
}
|
||||
|
||||
std::unique_ptr<HostDisplayTexture> OpenGLHostDisplay::CreateTexture(u32 width, u32 height, const void* data,
|
||||
u32 data_stride, bool dynamic)
|
||||
{
|
||||
return OpenGLHostDisplayTexture::Create(width, height, data, data_stride);
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::UpdateTexture(HostDisplayTexture* texture, u32 x, u32 y, u32 width, u32 height,
|
||||
const void* data, u32 data_stride)
|
||||
{
|
||||
OpenGLHostDisplayTexture* tex = static_cast<OpenGLHostDisplayTexture*>(texture);
|
||||
Assert(data_stride == (width * sizeof(u32)));
|
||||
|
||||
GLint old_texture_binding = 0;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture_binding);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, tex->GetGLID());
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, old_texture_binding);
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::SetDisplayTexture(void* texture, u32 offset_x, u32 offset_y, u32 width, u32 height,
|
||||
u32 texture_width, u32 texture_height, float aspect_ratio)
|
||||
{
|
||||
m_display_texture_id = static_cast<GLuint>(reinterpret_cast<uintptr_t>(texture));
|
||||
m_display_offset_x = offset_x;
|
||||
m_display_offset_y = offset_y;
|
||||
m_display_width = width;
|
||||
m_display_height = height;
|
||||
m_display_texture_width = texture_width;
|
||||
m_display_texture_height = texture_height;
|
||||
m_display_aspect_ratio = aspect_ratio;
|
||||
m_display_texture_changed = true;
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::SetDisplayLinearFiltering(bool enabled)
|
||||
{
|
||||
m_display_linear_filtering = enabled;
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::SetVSync(bool enabled)
|
||||
{
|
||||
// Window framebuffer has to be bound to call SetSwapInterval.
|
||||
GLint current_fbo = 0;
|
||||
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
SDL_GL_SetSwapInterval(enabled ? 1 : 0);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, current_fbo);
|
||||
}
|
||||
|
||||
std::tuple<u32, u32> OpenGLHostDisplay::GetWindowSize() const
|
||||
{
|
||||
return std::make_tuple(static_cast<u32>(m_window_width), static_cast<u32>(m_window_height));
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::WindowResized()
|
||||
{
|
||||
SDL_GetWindowSize(m_window, &m_window_width, &m_window_height);
|
||||
}
|
||||
|
||||
static void APIENTRY GLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
|
||||
const GLchar* message, const void* userParam)
|
||||
{
|
||||
switch (severity)
|
||||
{
|
||||
case GL_DEBUG_SEVERITY_HIGH_KHR:
|
||||
Log_ErrorPrintf(message);
|
||||
break;
|
||||
case GL_DEBUG_SEVERITY_MEDIUM_KHR:
|
||||
Log_WarningPrint(message);
|
||||
break;
|
||||
case GL_DEBUG_SEVERITY_LOW_KHR:
|
||||
Log_InfoPrintf(message);
|
||||
break;
|
||||
case GL_DEBUG_SEVERITY_NOTIFICATION:
|
||||
// Log_DebugPrint(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenGLHostDisplay::CreateGLContext()
|
||||
{
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
m_gl_context = SDL_GL_CreateContext(m_window);
|
||||
if (!m_gl_context || SDL_GL_MakeCurrent(m_window, m_gl_context) != 0 || !gladLoadGL())
|
||||
{
|
||||
Panic("Failed to create GL context");
|
||||
return false;
|
||||
}
|
||||
|
||||
#if 1
|
||||
if (GLAD_GL_KHR_debug)
|
||||
{
|
||||
glad_glDebugMessageCallbackKHR(GLDebugCallback, nullptr);
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_GL_SetSwapInterval(0);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenGLHostDisplay::CreateImGuiContext()
|
||||
{
|
||||
if (!ImGui_ImplSDL2_InitForOpenGL(m_window, m_gl_context) || !ImGui_ImplOpenGL3_Init())
|
||||
return false;
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame(m_window);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenGLHostDisplay::CreateGLResources()
|
||||
{
|
||||
static constexpr char fullscreen_quad_vertex_shader[] = R"(
|
||||
#version 330 core
|
||||
|
||||
out vec2 v_tex0;
|
||||
|
||||
void main()
|
||||
{
|
||||
v_tex0 = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));
|
||||
gl_Position = vec4(v_tex0 * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f);
|
||||
gl_Position.y = -gl_Position.y;
|
||||
}
|
||||
)";
|
||||
|
||||
static constexpr char display_fragment_shader[] = R"(
|
||||
#version 330 core
|
||||
|
||||
uniform sampler2D samp0;
|
||||
uniform vec4 u_src_rect;
|
||||
|
||||
in vec2 v_tex0;
|
||||
out vec4 o_col0;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 coords = u_src_rect.xy + v_tex0 * u_src_rect.zw;
|
||||
o_col0 = texture(samp0, coords);
|
||||
}
|
||||
)";
|
||||
|
||||
if (!m_display_program.Compile(fullscreen_quad_vertex_shader, display_fragment_shader))
|
||||
return false;
|
||||
|
||||
m_display_program.BindFragData(0, "o_col0");
|
||||
if (!m_display_program.Link())
|
||||
return false;
|
||||
|
||||
m_display_program.Bind();
|
||||
m_display_program.RegisterUniform("u_src_rect");
|
||||
m_display_program.RegisterUniform("samp0");
|
||||
m_display_program.Uniform1i(1, 0);
|
||||
|
||||
glGenVertexArrays(1, &m_display_vao);
|
||||
|
||||
m_app_icon_texture =
|
||||
std::make_unique<GL::Texture>(APP_ICON_WIDTH, APP_ICON_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, APP_ICON_DATA, true);
|
||||
|
||||
// samplers
|
||||
glGenSamplers(1, &m_display_nearest_sampler);
|
||||
glSamplerParameteri(m_display_nearest_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glSamplerParameteri(m_display_nearest_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glGenSamplers(1, &m_display_linear_sampler);
|
||||
glSamplerParameteri(m_display_linear_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glSamplerParameteri(m_display_linear_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<HostDisplay> OpenGLHostDisplay::Create(SDL_Window* window)
|
||||
{
|
||||
std::unique_ptr<OpenGLHostDisplay> display = std::make_unique<OpenGLHostDisplay>(window);
|
||||
if (!display->CreateGLContext() || !display->CreateImGuiContext() || !display->CreateGLResources())
|
||||
return nullptr;
|
||||
|
||||
return display;
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::Render()
|
||||
{
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
RenderDisplay();
|
||||
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
SDL_GL_SwapWindow(m_window);
|
||||
|
||||
ImGui_ImplSDL2_NewFrame(m_window);
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
|
||||
GL::Program::ResetLastProgram();
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::RenderDisplay()
|
||||
{
|
||||
if (!m_display_texture_id)
|
||||
return;
|
||||
|
||||
// - 20 for main menu padding
|
||||
const auto [vp_left, vp_top, vp_width, vp_height] =
|
||||
CalculateDrawRect(m_window_width, std::max(m_window_height - 20, 1), m_display_aspect_ratio);
|
||||
|
||||
glViewport(vp_left, m_window_height - (20 + vp_top) - vp_height, vp_width, vp_height);
|
||||
glDisable(GL_BLEND);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
glDepthMask(GL_FALSE);
|
||||
m_display_program.Bind();
|
||||
m_display_program.Uniform4f(0, static_cast<float>(m_display_offset_x) / static_cast<float>(m_display_texture_width),
|
||||
static_cast<float>(m_display_offset_y) / static_cast<float>(m_display_texture_height),
|
||||
static_cast<float>(m_display_width) / static_cast<float>(m_display_texture_width),
|
||||
static_cast<float>(m_display_height) / static_cast<float>(m_display_texture_height));
|
||||
glBindTexture(GL_TEXTURE_2D, m_display_texture_id);
|
||||
glBindSampler(0, m_display_linear_filtering ? m_display_linear_sampler : m_display_nearest_sampler);
|
||||
glBindVertexArray(m_display_vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glBindSampler(0, 0);
|
||||
}
|
64
src/duckstation/opengl_host_display.h
Normal file
64
src/duckstation/opengl_host_display.h
Normal file
@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
#include "common/gl/program.h"
|
||||
#include "common/gl/texture.h"
|
||||
#include "core/host_display.h"
|
||||
#include <SDL.h>
|
||||
#include <memory>
|
||||
|
||||
class OpenGLHostDisplay final : public HostDisplay
|
||||
{
|
||||
public:
|
||||
OpenGLHostDisplay(SDL_Window* window);
|
||||
~OpenGLHostDisplay();
|
||||
|
||||
static std::unique_ptr<HostDisplay> Create(SDL_Window* window);
|
||||
|
||||
RenderAPI GetRenderAPI() const override;
|
||||
void* GetHostRenderDevice() const override;
|
||||
void* GetHostRenderContext() const override;
|
||||
|
||||
std::unique_ptr<HostDisplayTexture> CreateTexture(u32 width, u32 height, const void* data, u32 data_stride,
|
||||
bool dynamic) override;
|
||||
void UpdateTexture(HostDisplayTexture* texture, u32 x, u32 y, u32 width, u32 height, const void* data,
|
||||
u32 data_stride) override;
|
||||
|
||||
void SetDisplayTexture(void* texture, u32 offset_x, u32 offset_y, u32 width, u32 height, u32 texture_width,
|
||||
u32 texture_height, float aspect_ratio) override;
|
||||
void SetDisplayLinearFiltering(bool enabled) override;
|
||||
|
||||
void SetVSync(bool enabled) override;
|
||||
|
||||
std::tuple<u32, u32> GetWindowSize() const override;
|
||||
void WindowResized() override;
|
||||
|
||||
private:
|
||||
bool CreateGLContext();
|
||||
bool CreateImGuiContext();
|
||||
bool CreateGLResources();
|
||||
|
||||
void Render();
|
||||
void RenderDisplay();
|
||||
|
||||
SDL_Window* m_window = nullptr;
|
||||
SDL_GLContext m_gl_context = nullptr;
|
||||
int m_window_width = 0;
|
||||
int m_window_height = 0;
|
||||
|
||||
std::unique_ptr<GL::Texture> m_app_icon_texture = nullptr;
|
||||
|
||||
GL::Program m_display_program;
|
||||
GLuint m_display_vao = 0;
|
||||
GLuint m_display_texture_id = 0;
|
||||
u32 m_display_offset_x = 0;
|
||||
u32 m_display_offset_y = 0;
|
||||
u32 m_display_width = 0;
|
||||
u32 m_display_height = 0;
|
||||
u32 m_display_texture_width = 0;
|
||||
u32 m_display_texture_height = 0;
|
||||
float m_display_aspect_ratio = 1.0f;
|
||||
GLuint m_display_nearest_sampler = 0;
|
||||
GLuint m_display_linear_sampler = 0;
|
||||
|
||||
bool m_display_texture_changed = false;
|
||||
bool m_display_linear_filtering = false;
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
#include "sdl_interface.h"
|
||||
#include "sdl_host_interface.h"
|
||||
#include "YBaseLib/ByteStream.h"
|
||||
#include "YBaseLib/Error.h"
|
||||
#include "YBaseLib/Log.h"
|
||||
@ -6,62 +6,54 @@
|
||||
#include "core/digital_controller.h"
|
||||
#include "core/dma.h"
|
||||
#include "core/gpu.h"
|
||||
#include "core/host_display.h"
|
||||
#include "core/mdec.h"
|
||||
#include "core/memory_card.h"
|
||||
#include "core/spu.h"
|
||||
#include "core/system.h"
|
||||
#include "core/timers.h"
|
||||
#ifdef Y_PLATFORM_WINDOWS
|
||||
#include "d3d11_host_display.h"
|
||||
#endif
|
||||
#include "icon.h"
|
||||
#include "opengl_host_display.h"
|
||||
#include "sdl_audio_stream.h"
|
||||
#include <cinttypes>
|
||||
#include <glad.h>
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
#include <imgui_impl_sdl.h>
|
||||
#include <nfd.h>
|
||||
Log_SetChannel(SDLInterface);
|
||||
Log_SetChannel(SDLHostInterface);
|
||||
|
||||
static constexpr std::array<std::pair<Settings::GPURenderer, const char*>, 2> s_gpu_renderer_names = {
|
||||
{{Settings::GPURenderer::HardwareOpenGL, "Hardware (OpenGL)"}, {Settings::GPURenderer::Software, "Software"}}};
|
||||
static constexpr std::array<std::pair<Settings::GPURenderer, const char*>, 3> s_gpu_renderer_names = {
|
||||
{{Settings::GPURenderer::HardwareD3D11, "Hardware (Direct3D 11)"},
|
||||
{Settings::GPURenderer::HardwareOpenGL, "Hardware (OpenGL)"},
|
||||
{Settings::GPURenderer::Software, "Software"}}};
|
||||
|
||||
SDLInterface::SDLInterface() = default;
|
||||
SDLHostInterface::SDLHostInterface() = default;
|
||||
|
||||
SDLInterface::~SDLInterface()
|
||||
SDLHostInterface::~SDLHostInterface()
|
||||
{
|
||||
CloseGameControllers();
|
||||
|
||||
if (m_gl_context)
|
||||
{
|
||||
if (m_display_vao != 0)
|
||||
glDeleteVertexArrays(1, &m_display_vao);
|
||||
|
||||
m_display_program.Destroy();
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
SDL_GL_MakeCurrent(nullptr, nullptr);
|
||||
SDL_GL_DeleteContext(m_gl_context);
|
||||
}
|
||||
m_display.reset();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
if (m_window)
|
||||
SDL_DestroyWindow(m_window);
|
||||
}
|
||||
|
||||
bool SDLInterface::CreateSDLWindow()
|
||||
bool SDLHostInterface::CreateSDLWindow()
|
||||
{
|
||||
constexpr u32 DEFAULT_WINDOW_WIDTH = 900;
|
||||
constexpr u32 DEFAULT_WINDOW_HEIGHT = 700;
|
||||
|
||||
// Create window.
|
||||
constexpr u32 window_flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL;
|
||||
const u32 window_flags =
|
||||
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | (UseOpenGLRenderer() ? SDL_WINDOW_OPENGL : 0);
|
||||
|
||||
m_window = SDL_CreateWindow("DuckStation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, DEFAULT_WINDOW_WIDTH,
|
||||
DEFAULT_WINDOW_HEIGHT, window_flags);
|
||||
if (!m_window)
|
||||
{
|
||||
Panic("Failed to create window");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set window icon.
|
||||
SDL_Surface* icon_surface =
|
||||
@ -74,133 +66,38 @@ bool SDLInterface::CreateSDLWindow()
|
||||
SDL_FreeSurface(icon_surface);
|
||||
}
|
||||
|
||||
SDL_GetWindowSize(m_window, &m_window_width, &m_window_height);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void APIENTRY GLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
|
||||
const GLchar* message, const void* userParam)
|
||||
bool SDLHostInterface::CreateDisplay()
|
||||
{
|
||||
switch (severity)
|
||||
{
|
||||
case GL_DEBUG_SEVERITY_HIGH_KHR:
|
||||
Log_InfoPrint(message);
|
||||
break;
|
||||
case GL_DEBUG_SEVERITY_MEDIUM_KHR:
|
||||
Log_WarningPrint(message);
|
||||
break;
|
||||
case GL_DEBUG_SEVERITY_LOW_KHR:
|
||||
Log_InfoPrintf(message);
|
||||
break;
|
||||
case GL_DEBUG_SEVERITY_NOTIFICATION:
|
||||
// Log_DebugPrint(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool SDLInterface::CreateGLContext()
|
||||
{
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
m_gl_context = SDL_GL_CreateContext(m_window);
|
||||
if (!m_gl_context || SDL_GL_MakeCurrent(m_window, m_gl_context) != 0 || !gladLoadGL())
|
||||
{
|
||||
Panic("Failed to create GL context");
|
||||
return false;
|
||||
}
|
||||
|
||||
#if 0
|
||||
if (GLAD_GL_KHR_debug)
|
||||
{
|
||||
glad_glDebugMessageCallbackKHR(GLDebugCallback, nullptr);
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
}
|
||||
#ifdef Y_PLATFORM_WINDOWS
|
||||
m_display = UseOpenGLRenderer() ? OpenGLHostDisplay::Create(m_window) : D3D11HostDisplay::Create(m_window);
|
||||
#else
|
||||
m_display = OpenGLHostDisplay::Create(m_window);
|
||||
#endif
|
||||
|
||||
SDL_GL_SetSwapInterval(0);
|
||||
if (!m_display)
|
||||
return false;
|
||||
|
||||
m_app_icon_texture =
|
||||
m_display->CreateTexture(APP_ICON_WIDTH, APP_ICON_HEIGHT, APP_ICON_DATA, APP_ICON_WIDTH * sizeof(u32));
|
||||
if (!m_app_icon_texture)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SDLInterface::CreateImGuiContext()
|
||||
bool SDLHostInterface::CreateImGuiContext()
|
||||
{
|
||||
ImGui::CreateContext();
|
||||
ImGui::GetIO().IniFilename = nullptr;
|
||||
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad;
|
||||
ImGui::GetIO().BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
||||
|
||||
if (!ImGui_ImplSDL2_InitForOpenGL(m_window, m_gl_context) || !ImGui_ImplOpenGL3_Init())
|
||||
return false;
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame(m_window);
|
||||
ImGui::NewFrame();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SDLInterface::CreateGLResources()
|
||||
{
|
||||
static constexpr char fullscreen_quad_vertex_shader[] = R"(
|
||||
#version 330 core
|
||||
|
||||
out vec2 v_tex0;
|
||||
|
||||
void main()
|
||||
{
|
||||
v_tex0 = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));
|
||||
gl_Position = vec4(v_tex0 * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f);
|
||||
gl_Position.y = -gl_Position.y;
|
||||
}
|
||||
)";
|
||||
|
||||
static constexpr char display_fragment_shader[] = R"(
|
||||
#version 330 core
|
||||
|
||||
uniform sampler2D samp0;
|
||||
uniform vec4 u_src_rect;
|
||||
|
||||
in vec2 v_tex0;
|
||||
out vec4 o_col0;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 coords = u_src_rect.xy + v_tex0 * u_src_rect.zw;
|
||||
o_col0 = texture(samp0, coords);
|
||||
}
|
||||
)";
|
||||
|
||||
if (!m_display_program.Compile(fullscreen_quad_vertex_shader, display_fragment_shader))
|
||||
return false;
|
||||
|
||||
m_display_program.BindFragData(0, "o_col0");
|
||||
if (!m_display_program.Link())
|
||||
return false;
|
||||
|
||||
m_display_program.Bind();
|
||||
m_display_program.RegisterUniform("u_src_rect");
|
||||
m_display_program.RegisterUniform("samp0");
|
||||
m_display_program.Uniform1i(1, 0);
|
||||
|
||||
glGenVertexArrays(1, &m_display_vao);
|
||||
|
||||
m_app_icon_texture =
|
||||
std::make_unique<GL::Texture>(APP_ICON_WIDTH, APP_ICON_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, APP_ICON_DATA, true);
|
||||
|
||||
// samplers
|
||||
glGenSamplers(1, &m_display_nearest_sampler);
|
||||
glSamplerParameteri(m_display_nearest_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glSamplerParameteri(m_display_nearest_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glGenSamplers(1, &m_display_linear_sampler);
|
||||
glSamplerParameteri(m_display_linear_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glSamplerParameteri(m_display_linear_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SDLInterface::CreateAudioStream()
|
||||
bool SDLHostInterface::CreateAudioStream()
|
||||
{
|
||||
m_audio_stream = std::make_unique<SDLAudioStream>();
|
||||
if (!m_audio_stream->Reconfigure(44100, 2))
|
||||
@ -212,7 +109,7 @@ bool SDLInterface::CreateAudioStream()
|
||||
return true;
|
||||
}
|
||||
|
||||
void SDLInterface::UpdateAudioVisualSync()
|
||||
void SDLHostInterface::UpdateAudioVisualSync()
|
||||
{
|
||||
const bool speed_limiter_enabled = m_speed_limiter_enabled && !m_speed_limiter_temp_disabled;
|
||||
const bool audio_sync_enabled = speed_limiter_enabled;
|
||||
@ -221,16 +118,10 @@ void SDLInterface::UpdateAudioVisualSync()
|
||||
(speed_limiter_enabled && vsync_enabled) ? " and video" : "");
|
||||
|
||||
m_audio_stream->SetSync(false);
|
||||
|
||||
// Window framebuffer has to be bound to call SetSwapInterval.
|
||||
GLint current_fbo = 0;
|
||||
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
SDL_GL_SetSwapInterval(vsync_enabled ? 1 : 0);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, current_fbo);
|
||||
m_display->SetVSync(vsync_enabled);
|
||||
}
|
||||
|
||||
void SDLInterface::OpenGameControllers()
|
||||
void SDLHostInterface::OpenGameControllers()
|
||||
{
|
||||
for (int i = 0; i < SDL_NumJoysticks(); i++)
|
||||
{
|
||||
@ -247,14 +138,14 @@ void SDLInterface::OpenGameControllers()
|
||||
}
|
||||
}
|
||||
|
||||
void SDLInterface::CloseGameControllers()
|
||||
void SDLHostInterface::CloseGameControllers()
|
||||
{
|
||||
for (auto& it : m_sdl_controllers)
|
||||
SDL_GameControllerClose(it.second);
|
||||
m_sdl_controllers.clear();
|
||||
}
|
||||
|
||||
bool SDLInterface::InitializeSystem(const char* filename, const char* exp1_filename)
|
||||
bool SDLHostInterface::InitializeSystem(const char* filename, const char* exp1_filename)
|
||||
{
|
||||
if (!HostInterface::InitializeSystem(filename, exp1_filename))
|
||||
{
|
||||
@ -268,13 +159,13 @@ bool SDLInterface::InitializeSystem(const char* filename, const char* exp1_filen
|
||||
return true;
|
||||
}
|
||||
|
||||
void SDLInterface::ConnectDevices()
|
||||
void SDLHostInterface::ConnectDevices()
|
||||
{
|
||||
m_controller = DigitalController::Create();
|
||||
m_system->SetController(0, m_controller);
|
||||
}
|
||||
|
||||
void SDLInterface::ResetPerformanceCounters()
|
||||
void SDLHostInterface::ResetPerformanceCounters()
|
||||
{
|
||||
if (m_system)
|
||||
{
|
||||
@ -291,24 +182,22 @@ void SDLInterface::ResetPerformanceCounters()
|
||||
m_fps_timer.Reset();
|
||||
}
|
||||
|
||||
void SDLInterface::ShutdownSystem()
|
||||
void SDLHostInterface::ShutdownSystem()
|
||||
{
|
||||
m_system.reset();
|
||||
m_paused = false;
|
||||
m_display_texture = nullptr;
|
||||
UpdateAudioVisualSync();
|
||||
}
|
||||
|
||||
std::unique_ptr<SDLInterface> SDLInterface::Create(const char* filename /* = nullptr */,
|
||||
const char* exp1_filename /* = nullptr */,
|
||||
const char* save_state_filename /* = nullptr */)
|
||||
std::unique_ptr<SDLHostInterface> SDLHostInterface::Create(const char* filename /* = nullptr */,
|
||||
const char* exp1_filename /* = nullptr */,
|
||||
const char* save_state_filename /* = nullptr */)
|
||||
{
|
||||
std::unique_ptr<SDLInterface> intf = std::make_unique<SDLInterface>();
|
||||
if (!intf->CreateSDLWindow() || !intf->CreateGLContext() || !intf->CreateImGuiContext() ||
|
||||
!intf->CreateGLResources() || !intf->CreateAudioStream())
|
||||
{
|
||||
std::unique_ptr<SDLHostInterface> intf = std::make_unique<SDLHostInterface>();
|
||||
if (!intf->CreateSDLWindow() || !intf->CreateImGuiContext() || !intf->CreateDisplay() || !intf->CreateAudioStream())
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ImGui::NewFrame();
|
||||
|
||||
intf->OpenGameControllers();
|
||||
|
||||
@ -323,20 +212,26 @@ std::unique_ptr<SDLInterface> SDLInterface::Create(const char* filename /* = nul
|
||||
}
|
||||
|
||||
intf->UpdateAudioVisualSync();
|
||||
|
||||
return intf;
|
||||
}
|
||||
|
||||
TinyString SDLInterface::GetSaveStateFilename(u32 index)
|
||||
TinyString SDLHostInterface::GetSaveStateFilename(u32 index)
|
||||
{
|
||||
return TinyString::FromFormat("savestate_%u.bin", index);
|
||||
}
|
||||
|
||||
void SDLInterface::ReportMessage(const char* message)
|
||||
HostDisplay* SDLHostInterface::GetDisplay() const
|
||||
{
|
||||
return m_display.get();
|
||||
}
|
||||
|
||||
void SDLHostInterface::ReportMessage(const char* message)
|
||||
{
|
||||
AddOSDMessage(message, 3.0f);
|
||||
}
|
||||
|
||||
bool SDLInterface::IsWindowFullscreen() const
|
||||
bool SDLHostInterface::IsWindowFullscreen() const
|
||||
{
|
||||
return ((SDL_GetWindowFlags(m_window) & SDL_WINDOW_FULLSCREEN) != 0);
|
||||
}
|
||||
@ -491,7 +386,7 @@ static void HandleSDLControllerButtonEventForController(const SDL_Event* ev, Dig
|
||||
}
|
||||
}
|
||||
|
||||
void SDLInterface::HandleSDLEvent(const SDL_Event* event)
|
||||
void SDLHostInterface::HandleSDLEvent(const SDL_Event* event)
|
||||
{
|
||||
ImGui_ImplSDL2_ProcessEvent(event);
|
||||
|
||||
@ -500,10 +395,7 @@ void SDLInterface::HandleSDLEvent(const SDL_Event* event)
|
||||
case SDL_WINDOWEVENT:
|
||||
{
|
||||
if (event->window.event == SDL_WINDOWEVENT_RESIZED)
|
||||
{
|
||||
m_window_width = event->window.data1;
|
||||
m_window_height = event->window.data2;
|
||||
}
|
||||
m_display->WindowResized();
|
||||
}
|
||||
break;
|
||||
|
||||
@ -569,7 +461,7 @@ void SDLInterface::HandleSDLEvent(const SDL_Event* event)
|
||||
}
|
||||
}
|
||||
|
||||
void SDLInterface::HandleSDLKeyEvent(const SDL_Event* event)
|
||||
void SDLHostInterface::HandleSDLKeyEvent(const SDL_Event* event)
|
||||
{
|
||||
const bool repeat = event->key.repeat != 0;
|
||||
if (!repeat && m_controller && HandleSDLKeyEventForController(event, m_controller.get()))
|
||||
@ -652,92 +544,12 @@ void SDLInterface::HandleSDLKeyEvent(const SDL_Event* event)
|
||||
}
|
||||
}
|
||||
|
||||
void SDLInterface::ClearImGuiFocus()
|
||||
void SDLHostInterface::ClearImGuiFocus()
|
||||
{
|
||||
ImGui::SetWindowFocus(nullptr);
|
||||
}
|
||||
|
||||
void SDLInterface::Render()
|
||||
{
|
||||
DrawImGui();
|
||||
|
||||
if (m_system)
|
||||
m_system->GetGPU()->ResetGraphicsAPIState();
|
||||
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
RenderDisplay();
|
||||
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
SDL_GL_SwapWindow(m_window);
|
||||
|
||||
ImGui_ImplSDL2_NewFrame(m_window);
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
|
||||
ImGui::NewFrame();
|
||||
|
||||
GL::Program::ResetLastProgram();
|
||||
|
||||
if (m_system)
|
||||
m_system->GetGPU()->RestoreGraphicsAPIState();
|
||||
}
|
||||
|
||||
static std::tuple<int, int, int, int> CalculateDrawRect(int window_width, int window_height, float display_ratio)
|
||||
{
|
||||
const float window_ratio = float(window_width) / float(window_height);
|
||||
int left, top, width, height;
|
||||
if (window_ratio >= display_ratio)
|
||||
{
|
||||
width = static_cast<int>(float(window_height) * display_ratio);
|
||||
height = static_cast<int>(window_height);
|
||||
left = (window_width - width) / 2;
|
||||
top = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
width = static_cast<int>(window_width);
|
||||
height = static_cast<int>(float(window_width) / display_ratio);
|
||||
left = 0;
|
||||
top = (window_height - height) / 2;
|
||||
}
|
||||
|
||||
return std::tie(left, top, width, height);
|
||||
}
|
||||
|
||||
void SDLInterface::RenderDisplay()
|
||||
{
|
||||
if (!m_display_texture)
|
||||
return;
|
||||
|
||||
// - 20 for main menu padding
|
||||
const auto [vp_left, vp_top, vp_width, vp_height] =
|
||||
CalculateDrawRect(m_window_width, std::max(m_window_height - 20, 1), m_display_aspect_ratio);
|
||||
const bool linear_filter = m_system ? m_system->GetSettings().display_linear_filtering : false;
|
||||
|
||||
glViewport(vp_left, m_window_height - (20 + vp_top) - vp_height, vp_width, vp_height);
|
||||
glDisable(GL_BLEND);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
glDepthMask(GL_FALSE);
|
||||
m_display_program.Bind();
|
||||
m_display_program.Uniform4f(
|
||||
0, static_cast<float>(m_display_texture_offset_x) / static_cast<float>(m_display_texture->GetWidth()),
|
||||
static_cast<float>(m_display_texture_offset_y) / static_cast<float>(m_display_texture->GetHeight()),
|
||||
static_cast<float>(m_display_texture_width) / static_cast<float>(m_display_texture->GetWidth()),
|
||||
static_cast<float>(m_display_texture_height) / static_cast<float>(m_display_texture->GetHeight()));
|
||||
m_display_texture->Bind();
|
||||
glBindSampler(0, linear_filter ? m_display_linear_sampler : m_display_nearest_sampler);
|
||||
glBindVertexArray(m_display_vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glBindSampler(0, 0);
|
||||
}
|
||||
|
||||
void SDLInterface::DrawImGui()
|
||||
void SDLHostInterface::DrawImGui()
|
||||
{
|
||||
DrawMainMenuBar();
|
||||
|
||||
@ -754,7 +566,7 @@ void SDLInterface::DrawImGui()
|
||||
ImGui::Render();
|
||||
}
|
||||
|
||||
void SDLInterface::DrawMainMenuBar()
|
||||
void SDLHostInterface::DrawMainMenuBar()
|
||||
{
|
||||
if (!ImGui::BeginMainMenuBar())
|
||||
return;
|
||||
@ -936,7 +748,7 @@ void SDLInterface::DrawMainMenuBar()
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
||||
void SDLInterface::DrawPoweredOffWindow()
|
||||
void SDLHostInterface::DrawPoweredOffWindow()
|
||||
{
|
||||
constexpr int WINDOW_WIDTH = 400;
|
||||
constexpr int WINDOW_HEIGHT = 650;
|
||||
@ -956,8 +768,7 @@ void SDLInterface::DrawPoweredOffWindow()
|
||||
}
|
||||
|
||||
ImGui::SetCursorPosX((WINDOW_WIDTH - APP_ICON_WIDTH) / 2);
|
||||
ImGui::Image(reinterpret_cast<ImTextureID>(static_cast<std::uintptr_t>(m_app_icon_texture->GetGLId())),
|
||||
ImVec2(APP_ICON_WIDTH, APP_ICON_HEIGHT));
|
||||
ImGui::Image(m_app_icon_texture->GetHandle(), ImVec2(APP_ICON_WIDTH, APP_ICON_HEIGHT));
|
||||
ImGui::SetCursorPosY(APP_ICON_HEIGHT + 32);
|
||||
|
||||
static const ImVec2 button_size(static_cast<float>(BUTTON_WIDTH), static_cast<float>(BUTTON_HEIGHT));
|
||||
@ -1014,7 +825,7 @@ void SDLInterface::DrawPoweredOffWindow()
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
void SDLInterface::DrawAboutWindow()
|
||||
void SDLHostInterface::DrawAboutWindow()
|
||||
{
|
||||
ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x * 0.5f, ImGui::GetIO().DisplaySize.y * 0.5f),
|
||||
ImGuiCond_Always, ImVec2(0.5f, 0.5f));
|
||||
@ -1044,7 +855,7 @@ void SDLInterface::DrawAboutWindow()
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
void SDLInterface::DrawDebugMenu()
|
||||
void SDLHostInterface::DrawDebugMenu()
|
||||
{
|
||||
if (!ImGui::BeginMenu("Debug", m_system != nullptr))
|
||||
return;
|
||||
@ -1076,7 +887,7 @@ void SDLInterface::DrawDebugMenu()
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
void SDLInterface::DrawDebugWindows()
|
||||
void SDLHostInterface::DrawDebugWindows()
|
||||
{
|
||||
const Settings::DebugSettings& debug_settings = m_system->GetSettings().debugging;
|
||||
|
||||
@ -1094,7 +905,7 @@ void SDLInterface::DrawDebugWindows()
|
||||
m_system->GetMDEC()->DrawDebugStateWindow();
|
||||
}
|
||||
|
||||
void SDLInterface::AddOSDMessage(const char* message, float duration /*= 2.0f*/)
|
||||
void SDLHostInterface::AddOSDMessage(const char* message, float duration /*= 2.0f*/)
|
||||
{
|
||||
OSDMessage msg;
|
||||
msg.text = message;
|
||||
@ -1104,19 +915,7 @@ void SDLInterface::AddOSDMessage(const char* message, float duration /*= 2.0f*/)
|
||||
m_osd_messages.push_back(std::move(msg));
|
||||
}
|
||||
|
||||
void SDLInterface::SetDisplayTexture(GL::Texture* texture, u32 offset_x, u32 offset_y, u32 width, u32 height,
|
||||
float aspect_ratio)
|
||||
{
|
||||
m_display_texture = texture;
|
||||
m_display_texture_offset_x = offset_x;
|
||||
m_display_texture_offset_y = offset_y;
|
||||
m_display_texture_width = width;
|
||||
m_display_texture_height = height;
|
||||
m_display_aspect_ratio = aspect_ratio;
|
||||
m_display_texture_changed = true;
|
||||
}
|
||||
|
||||
void SDLInterface::DrawOSDMessages()
|
||||
void SDLHostInterface::DrawOSDMessages()
|
||||
{
|
||||
constexpr ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs |
|
||||
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
|
||||
@ -1157,21 +956,21 @@ void SDLInterface::DrawOSDMessages()
|
||||
}
|
||||
}
|
||||
|
||||
void SDLInterface::DoReset()
|
||||
void SDLHostInterface::DoReset()
|
||||
{
|
||||
m_system->Reset();
|
||||
ResetPerformanceCounters();
|
||||
AddOSDMessage("System reset.");
|
||||
}
|
||||
|
||||
void SDLInterface::DoPowerOff()
|
||||
void SDLHostInterface::DoPowerOff()
|
||||
{
|
||||
Assert(m_system);
|
||||
ShutdownSystem();
|
||||
AddOSDMessage("System powered off.");
|
||||
}
|
||||
|
||||
void SDLInterface::DoResume()
|
||||
void SDLHostInterface::DoResume()
|
||||
{
|
||||
Assert(!m_system);
|
||||
if (!InitializeSystem())
|
||||
@ -1189,7 +988,7 @@ void SDLInterface::DoResume()
|
||||
ClearImGuiFocus();
|
||||
}
|
||||
|
||||
void SDLInterface::DoStartDisc()
|
||||
void SDLHostInterface::DoStartDisc()
|
||||
{
|
||||
Assert(!m_system);
|
||||
|
||||
@ -1205,7 +1004,7 @@ void SDLInterface::DoStartDisc()
|
||||
ClearImGuiFocus();
|
||||
}
|
||||
|
||||
void SDLInterface::DoStartBIOS()
|
||||
void SDLHostInterface::DoStartBIOS()
|
||||
{
|
||||
Assert(!m_system);
|
||||
|
||||
@ -1217,7 +1016,7 @@ void SDLInterface::DoStartBIOS()
|
||||
ClearImGuiFocus();
|
||||
}
|
||||
|
||||
void SDLInterface::DoChangeDisc()
|
||||
void SDLHostInterface::DoChangeDisc()
|
||||
{
|
||||
Assert(m_system);
|
||||
|
||||
@ -1234,7 +1033,7 @@ void SDLInterface::DoChangeDisc()
|
||||
ClearImGuiFocus();
|
||||
}
|
||||
|
||||
void SDLInterface::DoLoadState(u32 index)
|
||||
void SDLHostInterface::DoLoadState(u32 index)
|
||||
{
|
||||
if (!HasSystem() && !InitializeSystem(nullptr, nullptr))
|
||||
return;
|
||||
@ -1244,14 +1043,14 @@ void SDLInterface::DoLoadState(u32 index)
|
||||
ClearImGuiFocus();
|
||||
}
|
||||
|
||||
void SDLInterface::DoSaveState(u32 index)
|
||||
void SDLHostInterface::DoSaveState(u32 index)
|
||||
{
|
||||
Assert(m_system);
|
||||
SaveState(GetSaveStateFilename(index));
|
||||
ClearImGuiFocus();
|
||||
}
|
||||
|
||||
void SDLInterface::DoTogglePause()
|
||||
void SDLHostInterface::DoTogglePause()
|
||||
{
|
||||
if (!m_system)
|
||||
return;
|
||||
@ -1261,7 +1060,7 @@ void SDLInterface::DoTogglePause()
|
||||
m_fps_timer.Reset();
|
||||
}
|
||||
|
||||
void SDLInterface::DoFrameStep()
|
||||
void SDLHostInterface::DoFrameStep()
|
||||
{
|
||||
if (!m_system)
|
||||
return;
|
||||
@ -1270,7 +1069,7 @@ void SDLInterface::DoFrameStep()
|
||||
m_paused = false;
|
||||
}
|
||||
|
||||
void SDLInterface::DoToggleSoftwareRendering()
|
||||
void SDLHostInterface::DoToggleSoftwareRendering()
|
||||
{
|
||||
if (!m_system)
|
||||
return;
|
||||
@ -1283,14 +1082,16 @@ void SDLInterface::DoToggleSoftwareRendering()
|
||||
}
|
||||
else
|
||||
{
|
||||
settings.gpu_renderer = Settings::GPURenderer::HardwareOpenGL;
|
||||
settings.gpu_renderer = m_display->GetRenderAPI() == HostDisplay::RenderAPI::D3D11 ?
|
||||
Settings::GPURenderer::HardwareD3D11 :
|
||||
Settings::GPURenderer::HardwareOpenGL;
|
||||
AddOSDMessage("Switched to hardware GPU renderer.");
|
||||
}
|
||||
|
||||
m_system->RecreateGPU();
|
||||
}
|
||||
|
||||
void SDLInterface::DoModifyInternalResolution(s32 increment)
|
||||
void SDLHostInterface::DoModifyInternalResolution(s32 increment)
|
||||
{
|
||||
if (!m_system)
|
||||
return;
|
||||
@ -1310,7 +1111,7 @@ void SDLInterface::DoModifyInternalResolution(s32 increment)
|
||||
GPU::VRAM_HEIGHT * settings.gpu_resolution_scale));
|
||||
}
|
||||
|
||||
void SDLInterface::Run()
|
||||
void SDLHostInterface::Run()
|
||||
{
|
||||
m_audio_stream->PauseOutput(false);
|
||||
|
||||
@ -1335,7 +1136,21 @@ void SDLInterface::Run()
|
||||
}
|
||||
}
|
||||
|
||||
Render();
|
||||
// rendering
|
||||
{
|
||||
DrawImGui();
|
||||
|
||||
if (m_system)
|
||||
m_system->GetGPU()->ResetGraphicsAPIState();
|
||||
|
||||
ImGui::Render();
|
||||
m_display->Render();
|
||||
|
||||
ImGui::NewFrame();
|
||||
|
||||
if (m_system)
|
||||
m_system->GetGPU()->RestoreGraphicsAPIState();
|
||||
}
|
||||
|
||||
if (m_system)
|
||||
{
|
@ -4,6 +4,7 @@
|
||||
#include "common/gl/program.h"
|
||||
#include "common/gl/texture.h"
|
||||
#include "core/host_interface.h"
|
||||
#include "core/host_display.h"
|
||||
#include <SDL.h>
|
||||
#include <array>
|
||||
#include <deque>
|
||||
@ -16,19 +17,18 @@ class DigitalController;
|
||||
class MemoryCard;
|
||||
class AudioStream;
|
||||
|
||||
class SDLInterface : public HostInterface
|
||||
class SDLHostInterface : public HostInterface
|
||||
{
|
||||
public:
|
||||
SDLInterface();
|
||||
~SDLInterface();
|
||||
SDLHostInterface();
|
||||
~SDLHostInterface();
|
||||
|
||||
static std::unique_ptr<SDLInterface> Create(const char* filename = nullptr, const char* exp1_filename = nullptr,
|
||||
static std::unique_ptr<SDLHostInterface> Create(const char* filename = nullptr, const char* exp1_filename = nullptr,
|
||||
const char* save_state_filename = nullptr);
|
||||
|
||||
static TinyString GetSaveStateFilename(u32 index);
|
||||
|
||||
void SetDisplayTexture(GL::Texture* texture, u32 offset_x, u32 offset_y, u32 width, u32 height,
|
||||
float aspect_ratio) override;
|
||||
HostDisplay* GetDisplay() const override;
|
||||
|
||||
void ReportMessage(const char* message) override;
|
||||
|
||||
@ -50,10 +50,15 @@ private:
|
||||
|
||||
bool HasSystem() const { return static_cast<bool>(m_system); }
|
||||
|
||||
#ifdef Y_PLATFORM_WINDOWS
|
||||
bool UseOpenGLRenderer() const { return m_settings.gpu_renderer == Settings::GPURenderer::HardwareOpenGL; }
|
||||
#else
|
||||
bool UseOpenGLRenderer() const { return true; }
|
||||
#endif
|
||||
|
||||
bool CreateSDLWindow();
|
||||
bool CreateGLContext();
|
||||
bool CreateDisplay();
|
||||
bool CreateImGuiContext();
|
||||
bool CreateGLResources();
|
||||
bool CreateAudioStream();
|
||||
void UpdateAudioVisualSync();
|
||||
|
||||
@ -85,8 +90,6 @@ private:
|
||||
void HandleSDLKeyEvent(const SDL_Event* event);
|
||||
void ClearImGuiFocus();
|
||||
|
||||
void Render();
|
||||
void RenderDisplay();
|
||||
void DrawMainMenuBar();
|
||||
void DrawPoweredOffWindow();
|
||||
void DrawAboutWindow();
|
||||
@ -95,23 +98,8 @@ private:
|
||||
void DrawDebugWindows();
|
||||
|
||||
SDL_Window* m_window = nullptr;
|
||||
SDL_GLContext m_gl_context = nullptr;
|
||||
int m_window_width = 0;
|
||||
int m_window_height = 0;
|
||||
|
||||
std::unique_ptr<GL::Texture> m_app_icon_texture = nullptr;
|
||||
|
||||
GL::Program m_display_program;
|
||||
GLuint m_display_vao = 0;
|
||||
GL::Texture* m_display_texture = nullptr;
|
||||
u32 m_display_texture_offset_x = 0;
|
||||
u32 m_display_texture_offset_y = 0;
|
||||
u32 m_display_texture_width = 0;
|
||||
u32 m_display_texture_height = 0;
|
||||
float m_display_aspect_ratio = 1.0f;
|
||||
bool m_display_texture_changed = false;
|
||||
GLuint m_display_nearest_sampler = false;
|
||||
GLuint m_display_linear_sampler = false;
|
||||
std::unique_ptr<HostDisplay> m_display;
|
||||
std::unique_ptr<HostDisplayTexture> m_app_icon_texture;
|
||||
|
||||
std::deque<OSDMessage> m_osd_messages;
|
||||
std::mutex m_osd_messages_lock;
|
Reference in New Issue
Block a user