mirror of https://github.com/stenzek/duckstation
libretro: Additional work
- Reliable resolution switching. - Hook up logging. - Memory cards and controller type settings. - Save state support. - Direct3D support.pull/578/head
parent
2a38090e7a
commit
861b98ed3b
@ -1,283 +0,0 @@
|
||||
#include "d3d11_host_display.h"
|
||||
#include "common/assert.h"
|
||||
#include "common/d3d11/shader_compiler.h"
|
||||
#include "common/log.h"
|
||||
#include "frontend-common/display_ps.hlsl.h"
|
||||
#include "frontend-common/display_vs.hlsl.h"
|
||||
#include "libretro_host_interface.h"
|
||||
#include <array>
|
||||
Log_SetChannel(D3D11HostDisplay);
|
||||
|
||||
#define HAVE_D3D11
|
||||
#include "libretro_d3d.h"
|
||||
|
||||
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(ComPtr<ID3D11Device> device, ComPtr<ID3D11DeviceContext> context)
|
||||
: m_device(std::move(device)), m_context(std::move(context))
|
||||
{
|
||||
}
|
||||
|
||||
D3D11HostDisplay::~D3D11HostDisplay() = default;
|
||||
|
||||
HostDisplay::RenderAPI D3D11HostDisplay::GetRenderAPI() const
|
||||
{
|
||||
return HostDisplay::RenderAPI::D3D11;
|
||||
}
|
||||
|
||||
void* D3D11HostDisplay::GetRenderDevice() const
|
||||
{
|
||||
return m_device.Get();
|
||||
}
|
||||
|
||||
void* D3D11HostDisplay::GetRenderContext() 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);
|
||||
}
|
||||
}
|
||||
|
||||
bool D3D11HostDisplay::DownloadTexture(const void* texture_handle, u32 x, u32 y, u32 width, u32 height, void* out_data,
|
||||
u32 out_data_stride)
|
||||
{
|
||||
ID3D11ShaderResourceView* srv =
|
||||
const_cast<ID3D11ShaderResourceView*>(static_cast<const ID3D11ShaderResourceView*>(texture_handle));
|
||||
ID3D11Resource* srv_resource;
|
||||
D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc;
|
||||
srv->GetResource(&srv_resource);
|
||||
srv->GetDesc(&srv_desc);
|
||||
|
||||
if (!m_readback_staging_texture.EnsureSize(m_context.Get(), width, height, srv_desc.Format, false))
|
||||
return false;
|
||||
|
||||
m_readback_staging_texture.CopyFromTexture(m_context.Get(), srv_resource, 0, x, y, 0, 0, width, height);
|
||||
return m_readback_staging_texture.ReadPixels<u32>(m_context.Get(), 0, 0, width, height, out_data_stride / sizeof(u32),
|
||||
static_cast<u32*>(out_data));
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::SetVSync(bool enabled) {}
|
||||
|
||||
bool D3D11HostDisplay::CreateD3DResources()
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
m_display_vertex_shader =
|
||||
D3D11::ShaderCompiler::CreateVertexShader(m_device.Get(), s_display_vs_bytecode, sizeof(s_display_vs_bytecode));
|
||||
m_display_pixel_shader =
|
||||
D3D11::ShaderCompiler::CreatePixelShader(m_device.Get(), s_display_ps_bytecode, sizeof(s_display_ps_bytecode));
|
||||
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::RequestHardwareRendererContext(retro_hw_render_callback* cb)
|
||||
{
|
||||
cb->cache_context = true;
|
||||
cb->bottom_left_origin = false;
|
||||
cb->context_type = RETRO_HW_CONTEXT_DIRECT3D;
|
||||
cb->version_major = 11;
|
||||
cb->version_minor = 0;
|
||||
|
||||
return g_retro_environment_callback(RETRO_ENVIRONMENT_SET_HW_RENDER, cb);
|
||||
}
|
||||
|
||||
std::unique_ptr<HostDisplay> D3D11HostDisplay::Create(bool debug_device)
|
||||
{
|
||||
retro_hw_render_interface_d3d11 ri = {};
|
||||
ri.interface_type = RETRO_HW_RENDER_INTERFACE_D3D11;
|
||||
ri.interface_version = RETRO_HW_RENDER_INTERFACE_D3D11_VERSION;
|
||||
|
||||
if (!g_retro_environment_callback(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, &ri))
|
||||
{
|
||||
Log_ErrorPrint("Failed to get HW render interface");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ComPtr<ID3D11Device> device(ri.device);
|
||||
ComPtr<ID3D11DeviceContext> context(ri.context);
|
||||
|
||||
std::unique_ptr<D3D11HostDisplay> display = std::make_unique<D3D11HostDisplay>(std::move(device), std::move(context));
|
||||
if (!display->CreateD3DResources())
|
||||
return nullptr;
|
||||
|
||||
return display;
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::Render()
|
||||
{
|
||||
#if 0
|
||||
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();
|
||||
|
||||
if (!m_vsync && m_allow_tearing_supported)
|
||||
m_swap_chain->Present(0, DXGI_PRESENT_ALLOW_TEARING);
|
||||
else
|
||||
m_swap_chain->Present(BoolToUInt32(m_vsync), 0);
|
||||
|
||||
ImGui::NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame(m_window);
|
||||
ImGui_ImplDX11_NewFrame();
|
||||
#endif
|
||||
}
|
||||
|
||||
void D3D11HostDisplay::RenderDisplay()
|
||||
{
|
||||
#if 0
|
||||
if (!m_display_texture_handle)
|
||||
return;
|
||||
|
||||
const auto [vp_left, vp_top, vp_width, vp_height] =
|
||||
CalculateDrawRect(m_window_width, m_window_height, m_display_top_margin);
|
||||
|
||||
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, reinterpret_cast<ID3D11ShaderResourceView**>(&m_display_texture_handle));
|
||||
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_texture_view_x) / static_cast<float>(m_display_texture_width),
|
||||
static_cast<float>(m_display_texture_view_y) / static_cast<float>(m_display_texture_height),
|
||||
(static_cast<float>(m_display_texture_view_width) - 0.5f) / static_cast<float>(m_display_texture_width),
|
||||
(static_cast<float>(m_display_texture_view_height) - 0.5f) / 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->VSSetConstantBuffers(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);
|
||||
#endif
|
||||
}
|
||||
@ -1,62 +0,0 @@
|
||||
#pragma once
|
||||
#include "common/d3d11/staging_texture.h"
|
||||
#include "common/d3d11/stream_buffer.h"
|
||||
#include "common/d3d11/texture.h"
|
||||
#include "common/windows_headers.h"
|
||||
#include "core/host_display.h"
|
||||
#include "libretro.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(ComPtr<ID3D11Device> device, ComPtr<ID3D11DeviceContext> context);
|
||||
~D3D11HostDisplay();
|
||||
|
||||
static bool RequestHardwareRendererContext(retro_hw_render_callback* cb);
|
||||
|
||||
static std::unique_ptr<HostDisplay> Create(bool debug_device);
|
||||
|
||||
RenderAPI GetRenderAPI() const override;
|
||||
void* GetRenderDevice() const override;
|
||||
void* GetRenderContext() 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;
|
||||
bool DownloadTexture(const void* texture_handle, u32 x, u32 y, u32 width, u32 height, void* out_data,
|
||||
u32 out_data_stride) override;
|
||||
|
||||
void SetVSync(bool enabled) override;
|
||||
|
||||
private:
|
||||
static constexpr u32 DISPLAY_UNIFORM_BUFFER_SIZE = 16;
|
||||
|
||||
bool CreateD3DResources();
|
||||
|
||||
void Render() override;
|
||||
void RenderDisplay();
|
||||
|
||||
ComPtr<IDXGIFactory> m_dxgi_factory;
|
||||
|
||||
ComPtr<ID3D11Device> m_device;
|
||||
ComPtr<ID3D11DeviceContext> m_context;
|
||||
|
||||
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;
|
||||
D3D11::AutoStagingTexture m_readback_staging_texture;
|
||||
};
|
||||
@ -0,0 +1,116 @@
|
||||
#include "libretro_d3d11_host_display.h"
|
||||
#include "common/align.h"
|
||||
#include "common/assert.h"
|
||||
#include "common/d3d11/shader_compiler.h"
|
||||
#include "common/log.h"
|
||||
#include "libretro_host_interface.h"
|
||||
Log_SetChannel(D3D11HostDisplay);
|
||||
|
||||
#define HAVE_D3D11
|
||||
#include "libretro_d3d.h"
|
||||
|
||||
LibretroD3D11HostDisplay::LibretroD3D11HostDisplay() = default;
|
||||
|
||||
LibretroD3D11HostDisplay::~LibretroD3D11HostDisplay() = default;
|
||||
|
||||
void LibretroD3D11HostDisplay::SetVSync(bool enabled)
|
||||
{
|
||||
// The libretro frontend controls this.
|
||||
Log_DevPrintf("Ignoring SetVSync(%u)", BoolToUInt32(enabled));
|
||||
}
|
||||
|
||||
bool LibretroD3D11HostDisplay::RequestHardwareRendererContext(retro_hw_render_callback* cb)
|
||||
{
|
||||
cb->cache_context = true;
|
||||
cb->bottom_left_origin = false;
|
||||
cb->context_type = RETRO_HW_CONTEXT_DIRECT3D;
|
||||
cb->version_major = 11;
|
||||
cb->version_minor = 0;
|
||||
|
||||
return g_retro_environment_callback(RETRO_ENVIRONMENT_SET_HW_RENDER, cb);
|
||||
}
|
||||
|
||||
bool LibretroD3D11HostDisplay::CreateRenderDevice(const WindowInfo& wi, std::string_view adapter_name,
|
||||
bool debug_device)
|
||||
{
|
||||
retro_hw_render_interface* ri = nullptr;
|
||||
if (!g_retro_environment_callback(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, &ri))
|
||||
{
|
||||
Log_ErrorPrint("Failed to get HW render interface");
|
||||
return false;
|
||||
}
|
||||
else if (ri->interface_type != RETRO_HW_RENDER_INTERFACE_D3D11 ||
|
||||
ri->interface_version != RETRO_HW_RENDER_INTERFACE_D3D11_VERSION)
|
||||
{
|
||||
Log_ErrorPrint("Unexpected HW interface - type %u version %u", static_cast<unsigned>(ri->interface_type),
|
||||
static_cast<unsigned>(ri->interface_version));
|
||||
return false;
|
||||
}
|
||||
|
||||
const retro_hw_render_interface_d3d11* d3d11_ri = reinterpret_cast<const retro_hw_render_interface_d3d11*>(ri);
|
||||
if (!d3d11_ri->device || !d3d11_ri->context)
|
||||
{
|
||||
Log_ErrorPrintf("Missing D3D device or context");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_device = d3d11_ri->device;
|
||||
m_context = d3d11_ri->context;
|
||||
return CreateResources();
|
||||
}
|
||||
|
||||
void LibretroD3D11HostDisplay::DestroyRenderDevice()
|
||||
{
|
||||
DestroyResources();
|
||||
m_framebuffer.Destroy();
|
||||
m_context.Reset();
|
||||
m_device.Reset();
|
||||
}
|
||||
|
||||
void LibretroD3D11HostDisplay::ResizeRenderWindow(s32 new_window_width, s32 new_window_height)
|
||||
{
|
||||
m_window_info.surface_width = static_cast<u32>(new_window_width);
|
||||
m_window_info.surface_height = static_cast<u32>(new_window_height);
|
||||
}
|
||||
|
||||
bool LibretroD3D11HostDisplay::Render()
|
||||
{
|
||||
// TODO: Skip framebuffer when offset is (0,0).
|
||||
if (!CheckFramebufferSize(m_display_texture_width, m_display_texture_height))
|
||||
return false;
|
||||
|
||||
// Ensure we're not currently bound.
|
||||
ID3D11ShaderResourceView* null_srv = nullptr;
|
||||
m_context->PSSetShaderResources(0, 1, &null_srv);
|
||||
m_context->OMSetRenderTargets(1u, m_framebuffer.GetD3DRTVArray(), nullptr);
|
||||
|
||||
if (HasDisplayTexture())
|
||||
{
|
||||
RenderDisplay(0, 0, m_display_texture_width, m_display_texture_height, m_display_texture_handle,
|
||||
m_display_texture_width, m_display_texture_height, m_display_texture_view_x, m_display_texture_view_y,
|
||||
m_display_texture_view_width, m_display_texture_view_height, m_display_linear_filtering);
|
||||
}
|
||||
|
||||
if (HasSoftwareCursor())
|
||||
{
|
||||
const auto [left, top, width, height] = CalculateSoftwareCursorDrawRect();
|
||||
RenderSoftwareCursor(left, top, width, height, m_cursor_texture.get());
|
||||
}
|
||||
|
||||
// NOTE: libretro frontend expects the data bound to PS SRV slot 0.
|
||||
m_context->OMSetRenderTargets(0, nullptr, nullptr);
|
||||
m_context->PSSetShaderResources(0, 1, m_framebuffer.GetD3DSRVArray());
|
||||
g_retro_video_refresh_callback(RETRO_HW_FRAME_BUFFER_VALID, m_display_texture_width, m_display_texture_height, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LibretroD3D11HostDisplay::CheckFramebufferSize(u32 width, u32 height)
|
||||
{
|
||||
if (m_framebuffer.GetWidth() >= width && m_framebuffer.GetHeight() >= height)
|
||||
return true;
|
||||
|
||||
const u32 rounded_width = Common::AlignUpPow2(width, 1024);
|
||||
const u32 rounded_height = Common::AlignUpPow2(height, 512);
|
||||
return m_framebuffer.Create(m_device.Get(), rounded_width, rounded_height, DXGI_FORMAT_R8G8B8A8_UNORM,
|
||||
D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET);
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include "common/d3d11/texture.h"
|
||||
#include "frontend-common/d3d11_host_display.h"
|
||||
#include "libretro.h"
|
||||
|
||||
class LibretroD3D11HostDisplay final : public FrontendCommon::D3D11HostDisplay
|
||||
{
|
||||
public:
|
||||
LibretroD3D11HostDisplay();
|
||||
~LibretroD3D11HostDisplay();
|
||||
|
||||
static bool RequestHardwareRendererContext(retro_hw_render_callback* cb);
|
||||
|
||||
bool CreateRenderDevice(const WindowInfo& wi, std::string_view adapter_name, bool debug_device) override;
|
||||
void DestroyRenderDevice();
|
||||
|
||||
void ResizeRenderWindow(s32 new_window_width, s32 new_window_height) override;
|
||||
|
||||
void SetVSync(bool enabled) override;
|
||||
|
||||
bool Render() override;
|
||||
|
||||
private:
|
||||
bool CheckFramebufferSize(u32 width, u32 height);
|
||||
|
||||
D3D11::Texture m_framebuffer;
|
||||
};
|
||||
@ -0,0 +1,143 @@
|
||||
#include "libretro_opengl_host_display.h"
|
||||
#include "common/assert.h"
|
||||
#include "common/log.h"
|
||||
#include "core/gpu.h"
|
||||
#include "libretro.h"
|
||||
#include "libretro_host_interface.h"
|
||||
#include <array>
|
||||
#include <tuple>
|
||||
Log_SetChannel(LibretroOpenGLHostDisplay);
|
||||
|
||||
LibretroOpenGLHostDisplay::LibretroOpenGLHostDisplay() = default;
|
||||
|
||||
LibretroOpenGLHostDisplay::~LibretroOpenGLHostDisplay() = default;
|
||||
|
||||
HostDisplay::RenderAPI LibretroOpenGLHostDisplay::GetRenderAPI() const
|
||||
{
|
||||
return m_is_gles ? HostDisplay::RenderAPI::OpenGLES : HostDisplay::RenderAPI::OpenGL;
|
||||
}
|
||||
|
||||
void LibretroOpenGLHostDisplay::SetVSync(bool enabled)
|
||||
{
|
||||
// The libretro frontend controls this.
|
||||
Log_DevPrintf("Ignoring SetVSync(%u)", BoolToUInt32(enabled));
|
||||
}
|
||||
|
||||
bool LibretroOpenGLHostDisplay::RequestHardwareRendererContext(retro_hw_render_callback* cb)
|
||||
{
|
||||
// Prefer a desktop OpenGL context where possible. If we can't get this, try OpenGL ES.
|
||||
static constexpr std::array<std::tuple<u32, u32>, 11> desktop_versions_to_try = {
|
||||
{/*{4, 6}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0}, {3, 3}, {3, 2}, */ {3, 1}, {3, 0}}};
|
||||
static constexpr std::array<std::tuple<u32, u32>, 4> es_versions_to_try = {{{3, 2}, {3, 1}, {3, 0}}};
|
||||
|
||||
cb->cache_context = true;
|
||||
cb->bottom_left_origin = true;
|
||||
|
||||
for (const auto& [major, minor] : desktop_versions_to_try)
|
||||
{
|
||||
if (major > 3 || (major == 3 && minor >= 2))
|
||||
{
|
||||
cb->context_type = RETRO_HW_CONTEXT_OPENGL_CORE;
|
||||
cb->version_major = major;
|
||||
cb->version_minor = minor;
|
||||
}
|
||||
else
|
||||
{
|
||||
cb->context_type = RETRO_HW_CONTEXT_OPENGL;
|
||||
cb->version_major = 0;
|
||||
cb->version_minor = 0;
|
||||
}
|
||||
|
||||
if (g_retro_environment_callback(RETRO_ENVIRONMENT_SET_HW_RENDER, cb))
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const auto& [major, minor] : es_versions_to_try)
|
||||
{
|
||||
if (major >= 3 && minor > 0)
|
||||
{
|
||||
cb->context_type = RETRO_HW_CONTEXT_OPENGLES_VERSION;
|
||||
cb->version_major = major;
|
||||
cb->version_minor = minor;
|
||||
}
|
||||
else
|
||||
{
|
||||
cb->context_type = RETRO_HW_CONTEXT_OPENGLES3;
|
||||
cb->version_major = 0;
|
||||
cb->version_minor = 0;
|
||||
}
|
||||
|
||||
if (g_retro_environment_callback(RETRO_ENVIRONMENT_SET_HW_RENDER, cb))
|
||||
return true;
|
||||
}
|
||||
|
||||
Log_ErrorPrint("Failed to set any GL HW renderer");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool LibretroOpenGLHostDisplay::CreateRenderDevice(const WindowInfo& wi, std::string_view adapter_name,
|
||||
bool debug_device)
|
||||
{
|
||||
Assert(wi.type == WindowInfo::Type::Libretro);
|
||||
|
||||
// gross - but can't do much because of the GLADloadproc below.
|
||||
static retro_hw_render_callback* cb;
|
||||
cb = static_cast<retro_hw_render_callback*>(wi.display_connection);
|
||||
|
||||
m_window_info = wi;
|
||||
m_is_gles = (cb->context_type == RETRO_HW_CONTEXT_OPENGLES3 || cb->context_type == RETRO_HW_CONTEXT_OPENGLES_VERSION);
|
||||
|
||||
const GLADloadproc get_proc_address = [](const char* sym) -> void* {
|
||||
return reinterpret_cast<void*>(cb->get_proc_address(sym));
|
||||
};
|
||||
|
||||
// Load GLAD.
|
||||
const auto load_result = m_is_gles ? gladLoadGLES2Loader(get_proc_address) : gladLoadGLLoader(get_proc_address);
|
||||
if (!load_result)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to load GL functions");
|
||||
return false;
|
||||
}
|
||||
|
||||
return CreateResources();
|
||||
}
|
||||
|
||||
void LibretroOpenGLHostDisplay::DestroyRenderDevice()
|
||||
{
|
||||
DestroyResources();
|
||||
}
|
||||
|
||||
void LibretroOpenGLHostDisplay::ResizeRenderWindow(s32 new_window_width, s32 new_window_height)
|
||||
{
|
||||
m_window_info.surface_width = static_cast<u32>(new_window_width);
|
||||
m_window_info.surface_height = static_cast<u32>(new_window_height);
|
||||
}
|
||||
|
||||
bool LibretroOpenGLHostDisplay::Render()
|
||||
{
|
||||
const GLuint fbo = static_cast<GLuint>(
|
||||
static_cast<retro_hw_render_callback*>(m_window_info.display_connection)->get_current_framebuffer());
|
||||
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
if (HasDisplayTexture())
|
||||
{
|
||||
RenderDisplay(0, 0, m_display_texture_width, m_display_texture_height, m_display_texture_handle,
|
||||
m_display_texture_width, m_display_texture_height, m_display_texture_view_x, m_display_texture_view_y,
|
||||
m_display_texture_view_width, m_display_texture_view_height, m_display_linear_filtering);
|
||||
}
|
||||
|
||||
if (HasSoftwareCursor())
|
||||
{
|
||||
const auto [left, top, width, height] = CalculateSoftwareCursorDrawRect();
|
||||
RenderSoftwareCursor(left, top, width, height, m_cursor_texture.get());
|
||||
}
|
||||
|
||||
g_retro_video_refresh_callback(RETRO_HW_FRAME_BUFFER_VALID, m_display_texture_width, m_display_texture_height, 0);
|
||||
|
||||
GL::Program::ResetLastProgram();
|
||||
return true;
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include "common/gl/program.h"
|
||||
#include "common/gl/texture.h"
|
||||
#include "core/host_display.h"
|
||||
#include "frontend-common/opengl_host_display.h"
|
||||
#include "libretro.h"
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class LibretroOpenGLHostDisplay final : public FrontendCommon::OpenGLHostDisplay
|
||||
{
|
||||
public:
|
||||
LibretroOpenGLHostDisplay();
|
||||
~LibretroOpenGLHostDisplay();
|
||||
|
||||
static bool RequestHardwareRendererContext(retro_hw_render_callback* cb);
|
||||
|
||||
RenderAPI GetRenderAPI() const override;
|
||||
|
||||
bool CreateRenderDevice(const WindowInfo& wi, std::string_view adapter_name, bool debug_device) override;
|
||||
void DestroyRenderDevice();
|
||||
|
||||
void ResizeRenderWindow(s32 new_window_width, s32 new_window_height) override;
|
||||
|
||||
void SetVSync(bool enabled) override;
|
||||
|
||||
bool Render() override;
|
||||
|
||||
private:
|
||||
bool m_is_gles = false;
|
||||
};
|
||||
@ -1,385 +0,0 @@
|
||||
#include "opengl_host_display.h"
|
||||
#include "common/assert.h"
|
||||
#include "common/log.h"
|
||||
#include "libretro.h"
|
||||
#include "libretro_host_interface.h"
|
||||
#include <array>
|
||||
#include <tuple>
|
||||
Log_SetChannel(OpenGLHostDisplay);
|
||||
|
||||
class OpenGLDisplayWidgetTexture : public HostDisplayTexture
|
||||
{
|
||||
public:
|
||||
OpenGLDisplayWidgetTexture(GLuint id, u32 width, u32 height) : m_id(id), m_width(width), m_height(height) {}
|
||||
~OpenGLDisplayWidgetTexture() 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<OpenGLDisplayWidgetTexture> 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 || 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);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
return std::make_unique<OpenGLDisplayWidgetTexture>(id, width, height);
|
||||
}
|
||||
|
||||
private:
|
||||
GLuint m_id;
|
||||
u32 m_width;
|
||||
u32 m_height;
|
||||
};
|
||||
|
||||
OpenGLHostDisplay::OpenGLHostDisplay(bool is_gles) : m_is_gles(is_gles) {}
|
||||
|
||||
OpenGLHostDisplay::~OpenGLHostDisplay()
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
HostDisplay::RenderAPI OpenGLHostDisplay::GetRenderAPI() const
|
||||
{
|
||||
return m_is_gles ? HostDisplay::RenderAPI::OpenGLES : HostDisplay::RenderAPI::OpenGL;
|
||||
}
|
||||
|
||||
void* OpenGLHostDisplay::GetRenderDevice() const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* OpenGLHostDisplay::GetRenderContext() const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<HostDisplayTexture> OpenGLHostDisplay::CreateTexture(u32 width, u32 height, const void* data,
|
||||
u32 data_stride, bool dynamic)
|
||||
{
|
||||
return OpenGLDisplayWidgetTexture::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)
|
||||
{
|
||||
OpenGLDisplayWidgetTexture* tex = static_cast<OpenGLDisplayWidgetTexture*>(texture);
|
||||
Assert((data_stride % sizeof(u32)) == 0);
|
||||
|
||||
GLint old_texture_binding = 0, old_alignment = 0, old_row_length = 0;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture_binding);
|
||||
glGetIntegerv(GL_UNPACK_ALIGNMENT, &old_alignment);
|
||||
glGetIntegerv(GL_UNPACK_ROW_LENGTH, &old_row_length);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, tex->GetGLID());
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||||
glPixelStorei(GL_UNPACK_ROW_LENGTH, data_stride / sizeof(u32));
|
||||
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, old_alignment);
|
||||
glPixelStorei(GL_UNPACK_ROW_LENGTH, old_row_length);
|
||||
glBindTexture(GL_TEXTURE_2D, old_texture_binding);
|
||||
}
|
||||
|
||||
bool OpenGLHostDisplay::DownloadTexture(const void* texture_handle, u32 x, u32 y, u32 width, u32 height, void* out_data,
|
||||
u32 out_data_stride)
|
||||
{
|
||||
GLint old_alignment = 0, old_row_length = 0;
|
||||
glGetIntegerv(GL_PACK_ALIGNMENT, &old_alignment);
|
||||
glGetIntegerv(GL_PACK_ROW_LENGTH, &old_row_length);
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, sizeof(u32));
|
||||
glPixelStorei(GL_PACK_ROW_LENGTH, out_data_stride / sizeof(u32));
|
||||
|
||||
const GLuint texture = static_cast<GLuint>(reinterpret_cast<uintptr_t>(texture_handle));
|
||||
GL::Texture::GetTextureSubImage(texture, 0, x, y, 0, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
height * out_data_stride, out_data);
|
||||
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, old_alignment);
|
||||
glPixelStorei(GL_PACK_ROW_LENGTH, old_row_length);
|
||||
return true;
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::SetVSync(bool enabled)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
const char* OpenGLHostDisplay::GetGLSLVersionString() const
|
||||
{
|
||||
if (m_is_gles)
|
||||
{
|
||||
if (GLAD_GL_ES_VERSION_3_0)
|
||||
return "#version 300 es";
|
||||
else
|
||||
return "#version 100";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (GLAD_GL_VERSION_3_3)
|
||||
return "#version 330";
|
||||
else
|
||||
return "#version 130";
|
||||
}
|
||||
}
|
||||
|
||||
std::string OpenGLHostDisplay::GetGLSLVersionHeader() const
|
||||
{
|
||||
std::string header = GetGLSLVersionString();
|
||||
header += "\n\n";
|
||||
if (m_is_gles)
|
||||
{
|
||||
header += "precision highp float;\n";
|
||||
header += "precision highp int;\n\n";
|
||||
}
|
||||
|
||||
return header;
|
||||
}
|
||||
|
||||
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::RequestHardwareRendererContext(retro_hw_render_callback* cb)
|
||||
{
|
||||
// Prefer a desktop OpenGL context where possible. If we can't get this, try OpenGL ES.
|
||||
static constexpr std::array<std::tuple<u32, u32>, 11> desktop_versions_to_try = {
|
||||
{/*{4, 6}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0}, {3, 3}, {3, 2}, */ {3, 1}, {3, 0}}};
|
||||
static constexpr std::array<std::tuple<u32, u32>, 4> es_versions_to_try = {{{3, 2}, {3, 1}, {3, 0}}};
|
||||
|
||||
cb->cache_context = true;
|
||||
cb->bottom_left_origin = true;
|
||||
|
||||
for (const auto& [major, minor] : desktop_versions_to_try)
|
||||
{
|
||||
if (major > 3 || (major == 3 && minor >= 2))
|
||||
{
|
||||
cb->context_type = RETRO_HW_CONTEXT_OPENGL_CORE;
|
||||
cb->version_major = major;
|
||||
cb->version_minor = minor;
|
||||
}
|
||||
else
|
||||
{
|
||||
cb->context_type = RETRO_HW_CONTEXT_OPENGL;
|
||||
cb->version_major = 0;
|
||||
cb->version_minor = 0;
|
||||
}
|
||||
|
||||
if (g_retro_environment_callback(RETRO_ENVIRONMENT_SET_HW_RENDER, cb))
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const auto& [major, minor] : es_versions_to_try)
|
||||
{
|
||||
if (major >= 3 && minor > 0)
|
||||
{
|
||||
cb->context_type = RETRO_HW_CONTEXT_OPENGLES_VERSION;
|
||||
cb->version_major = major;
|
||||
cb->version_minor = minor;
|
||||
}
|
||||
else
|
||||
{
|
||||
cb->context_type = RETRO_HW_CONTEXT_OPENGLES3;
|
||||
cb->version_major = 0;
|
||||
cb->version_minor = 0;
|
||||
}
|
||||
|
||||
if (g_retro_environment_callback(RETRO_ENVIRONMENT_SET_HW_RENDER, cb))
|
||||
return true;
|
||||
}
|
||||
|
||||
Log_ErrorPrint("Failed to set any GL HW renderer");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<HostDisplay> OpenGLHostDisplay::Create(bool debug_device)
|
||||
{
|
||||
const retro_hw_context_type context_type = g_libretro_host_interface.GetHWRenderCallback().context_type;
|
||||
const GLADloadproc get_proc_address = [](const char* sym) -> void* {
|
||||
return reinterpret_cast<void*>(g_libretro_host_interface.GetHWRenderCallback().get_proc_address(sym));
|
||||
};
|
||||
const bool is_gles =
|
||||
(context_type == RETRO_HW_CONTEXT_OPENGLES3 || context_type == RETRO_HW_CONTEXT_OPENGLES_VERSION);
|
||||
|
||||
// Load GLAD.
|
||||
const auto load_result = is_gles ? gladLoadGLES2Loader(get_proc_address) : gladLoadGLLoader(get_proc_address);
|
||||
if (!load_result)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to load GL functions");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#if 0
|
||||
// Disabled until we can turn it off as well
|
||||
if (debug_device && GLAD_GL_KHR_debug)
|
||||
{
|
||||
glad_glDebugMessageCallbackKHR(GLDebugCallback, nullptr);
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
}
|
||||
#endif
|
||||
|
||||
std::unique_ptr<OpenGLHostDisplay> display = std::make_unique<OpenGLHostDisplay>(is_gles);
|
||||
if (!display->CreateGLResources())
|
||||
{
|
||||
Log_ErrorPrint("Failed to create GL resources");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return display;
|
||||
}
|
||||
|
||||
bool OpenGLHostDisplay::CreateGLResources()
|
||||
{
|
||||
static constexpr char fullscreen_quad_vertex_shader[] = R"(
|
||||
uniform vec4 u_src_rect;
|
||||
out vec2 v_tex0;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 pos = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));
|
||||
v_tex0 = u_src_rect.xy + pos * u_src_rect.zw;
|
||||
gl_Position = vec4(pos * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f);
|
||||
}
|
||||
)";
|
||||
|
||||
static constexpr char display_fragment_shader[] = R"(
|
||||
uniform sampler2D samp0;
|
||||
|
||||
in vec2 v_tex0;
|
||||
out vec4 o_col0;
|
||||
|
||||
void main()
|
||||
{
|
||||
o_col0 = texture(samp0, v_tex0);
|
||||
}
|
||||
)";
|
||||
|
||||
if (!m_display_program.Compile(GetGLSLVersionHeader() + fullscreen_quad_vertex_shader, {},
|
||||
GetGLSLVersionHeader() + display_fragment_shader))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to compile display shaders");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_is_gles)
|
||||
m_display_program.BindFragData(0, "o_col0");
|
||||
|
||||
if (!m_display_program.Link())
|
||||
{
|
||||
Log_ErrorPrintf("Failed to link display program");
|
||||
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);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::Render()
|
||||
{
|
||||
if (m_display_width != m_last_display_width || m_display_height != m_last_display_height)
|
||||
{
|
||||
retro_game_geometry geom = {};
|
||||
geom.base_width = m_display_width;
|
||||
geom.base_height = m_display_height;
|
||||
geom.aspect_ratio = m_display_pixel_aspect_ratio;
|
||||
|
||||
if (!g_retro_environment_callback(RETRO_ENVIRONMENT_SET_GEOMETRY, &geom))
|
||||
Log_WarningPrint("RETRO_ENVIRONMENT_SET_GEOMETRY failed");
|
||||
|
||||
m_last_display_width = m_display_width;
|
||||
m_last_display_height = m_display_height;
|
||||
}
|
||||
|
||||
const GLuint fbo = static_cast<GLuint>(g_libretro_host_interface.GetHWRenderCallback().get_current_framebuffer());
|
||||
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
RenderDisplay();
|
||||
|
||||
g_retro_video_refresh_callback(RETRO_HW_FRAME_BUFFER_VALID, m_display_width, m_display_height, 0);
|
||||
|
||||
GL::Program::ResetLastProgram();
|
||||
}
|
||||
|
||||
void OpenGLHostDisplay::RenderDisplay()
|
||||
{
|
||||
if (!m_display_texture_handle)
|
||||
return;
|
||||
|
||||
const auto [vp_left, vp_top, vp_width, vp_height] =
|
||||
CalculateDrawRect(m_display_width, m_display_height, m_display_top_margin);
|
||||
|
||||
glViewport(vp_left, m_display_height - 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_view_x) / static_cast<float>(m_display_texture_width),
|
||||
static_cast<float>(m_display_texture_view_y) / static_cast<float>(m_display_texture_height),
|
||||
(static_cast<float>(m_display_texture_view_width) - 0.5f) / static_cast<float>(m_display_texture_width),
|
||||
(static_cast<float>(m_display_texture_view_height) + 0.5f) / static_cast<float>(m_display_texture_height));
|
||||
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(reinterpret_cast<uintptr_t>(m_display_texture_handle)));
|
||||
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);
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
#pragma once
|
||||
#include "common/gl/program.h"
|
||||
#include "common/gl/texture.h"
|
||||
#include "core/host_display.h"
|
||||
#include "libretro.h"
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class OpenGLHostDisplay final : public HostDisplay
|
||||
{
|
||||
public:
|
||||
OpenGLHostDisplay(bool is_gles);
|
||||
~OpenGLHostDisplay();
|
||||
|
||||
static bool RequestHardwareRendererContext(retro_hw_render_callback* cb);
|
||||
|
||||
static std::unique_ptr<HostDisplay> Create(bool debug_device);
|
||||
|
||||
RenderAPI GetRenderAPI() const override;
|
||||
void* GetRenderDevice() const override;
|
||||
void* GetRenderContext() 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;
|
||||
bool DownloadTexture(const void* texture_handle, u32 x, u32 y, u32 width, u32 height, void* out_data,
|
||||
u32 out_data_stride) override;
|
||||
|
||||
void SetVSync(bool enabled) override;
|
||||
|
||||
void Render() override;
|
||||
|
||||
private:
|
||||
const char* GetGLSLVersionString() const;
|
||||
std::string GetGLSLVersionHeader() const;
|
||||
|
||||
bool GetGLContext(bool debug_device);
|
||||
bool CreateGLResources();
|
||||
|
||||
void RenderDisplay();
|
||||
|
||||
GL::Program m_display_program;
|
||||
GLuint m_display_vao = 0;
|
||||
GLuint m_display_nearest_sampler = 0;
|
||||
GLuint m_display_linear_sampler = 0;
|
||||
|
||||
s32 m_last_display_width = -1;
|
||||
s32 m_last_display_height = -1;
|
||||
|
||||
retro_hw_render_callback m_render_callback = {};
|
||||
bool m_is_gles = false;
|
||||
};
|
||||
Loading…
Reference in New Issue