Add initial libretro implementation

This commit is contained in:
Connor McLaughlin 2020-06-09 02:44:42 +10:00
parent 5dedd8707b
commit d7f083559e
18 changed files with 2245 additions and 1 deletions

View file

@ -51,6 +51,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "scmversion", "src\scmversio
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "discord-rpc", "dep\discord-rpc\discord-rpc.vcxproj", "{4266505B-DBAF-484B-AB31-B53B9C8235B3}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "duckstation-libretro", "src\duckstation-libretro\duckstation-libretro.vcxproj", "{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -431,6 +433,22 @@ Global
{4266505B-DBAF-484B-AB31-B53B9C8235B3}.ReleaseLTCG|x64.Build.0 = ReleaseLTCG|x64
{4266505B-DBAF-484B-AB31-B53B9C8235B3}.ReleaseLTCG|x86.ActiveCfg = ReleaseLTCG|Win32
{4266505B-DBAF-484B-AB31-B53B9C8235B3}.ReleaseLTCG|x86.Build.0 = ReleaseLTCG|Win32
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.Debug|x64.ActiveCfg = Debug|x64
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.Debug|x64.Build.0 = Debug|x64
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.Debug|x86.ActiveCfg = Debug|Win32
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.Debug|x86.Build.0 = Debug|Win32
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.DebugFast|x64.ActiveCfg = DebugFast|x64
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.DebugFast|x64.Build.0 = DebugFast|x64
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.DebugFast|x86.ActiveCfg = DebugFast|Win32
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.DebugFast|x86.Build.0 = DebugFast|Win32
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.Release|x64.ActiveCfg = Release|x64
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.Release|x64.Build.0 = Release|x64
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.Release|x86.ActiveCfg = Release|Win32
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.Release|x86.Build.0 = Release|Win32
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.ReleaseLTCG|x64.ActiveCfg = ReleaseLTCG|x64
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.ReleaseLTCG|x64.Build.0 = ReleaseLTCG|x64
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.ReleaseLTCG|x86.ActiveCfg = ReleaseLTCG|Win32
{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}.ReleaseLTCG|x86.Build.0 = ReleaseLTCG|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View file

@ -1,9 +1,12 @@
add_subdirectory(common)
add_subdirectory(common-tests)
add_subdirectory(core)
add_subdirectory(frontend-common)
add_subdirectory(scmversion)
if(ANDROID OR BUILD_SDL_FRONTEND OR BUILD_QT_FRONTEND)
add_subdirectory(frontend-common)
endif()
if(BUILD_SDL_FRONTEND)
add_subdirectory(duckstation-sdl)
endif()
@ -12,3 +15,7 @@ if(BUILD_QT_FRONTEND)
add_subdirectory(duckstation-qt)
endif()
if(BUILD_LIBRETRO_CORE)
add_subdirectory(duckstation-libretro)
endif()

View file

@ -0,0 +1,23 @@
add_library(duckstation-libretro SHARED
libretro_audio_stream.cpp
libretro_audio_stream.h
libretro_host_display.cpp
libretro_host_display.h
libretro_host_interface.cpp
libretro_host_interface.h
libretro_settings_interface.cpp
libretro_settings_interface.h
main.cpp
opengl_host_display.cpp
opengl_host_display.h
)
if(WIN32)
target_sources(duckstation-libretro PRIVATE
d3d11_host_display.cpp
d3d11_host_display.h
)
endif()
target_link_libraries(duckstation-libretro PRIVATE core common imgui glad scmversion libretro-common)

View file

@ -0,0 +1,283 @@
#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
}

View file

@ -0,0 +1,62 @@
#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;
};

View file

@ -0,0 +1,392 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="DebugFast|Win32">
<Configuration>DebugFast</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="DebugFast|x64">
<Configuration>DebugFast</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="ReleaseLTCG|Win32">
<Configuration>ReleaseLTCG</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="ReleaseLTCG|x64">
<Configuration>ReleaseLTCG</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\dep\imgui\imgui.vcxproj">
<Project>{bb08260f-6fbc-46af-8924-090ee71360c6}</Project>
</ProjectReference>
<ProjectReference Include="..\common\common.vcxproj">
<Project>{ee054e08-3799-4a59-a422-18259c105ffd}</Project>
</ProjectReference>
<ProjectReference Include="..\core\core.vcxproj">
<Project>{868b98c8-65a1-494b-8346-250a73a48c0a}</Project>
</ProjectReference>
<ProjectReference Include="..\scmversion\scmversion.vcxproj">
<Project>{075ced82-6a20-46df-94c7-9624ac9ddbeb}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="d3d11_host_display.cpp" />
<ClCompile Include="libretro_audio_stream.cpp" />
<ClCompile Include="libretro_host_display.cpp" />
<ClCompile Include="libretro_host_interface.cpp" />
<ClCompile Include="libretro_settings_interface.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="opengl_host_display.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="d3d11_host_display.h" />
<ClInclude Include="libretro_audio_stream.h" />
<ClInclude Include="libretro_host_display.h" />
<ClInclude Include="libretro_host_interface.h" />
<ClInclude Include="libretro_settings_interface.h" />
<ClInclude Include="opengl_host_display.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{9D206548-DE8F-4D9D-A561-C7E5CD7A20DF}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>duckstation-libretro</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet>
<SpectreMitigation>false</SpectreMitigation>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet>
<SpectreMitigation>false</SpectreMitigation>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet>
<SpectreMitigation>false</SpectreMitigation>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet>
<SpectreMitigation>false</SpectreMitigation>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(Platform)\</OutDir>
<IntDir>$(SolutionDir)build\$(ProjectName)-$(Platform)-$(Configuration)\</IntDir>
<TargetName>$(ProjectName)-$(Platform)-$(Configuration)</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IntDir>$(SolutionDir)build\$(ProjectName)-$(Platform)-$(Configuration)\</IntDir>
<TargetName>$(ProjectName)-$(Platform)-$(Configuration)</TargetName>
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(Platform)\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(Platform)\</OutDir>
<IntDir>$(SolutionDir)build\$(ProjectName)-$(Platform)-$(Configuration)\</IntDir>
<TargetName>$(ProjectName)-$(Platform)-$(Configuration)</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|x64'">
<IntDir>$(SolutionDir)build\$(ProjectName)-$(Platform)-$(Configuration)\</IntDir>
<TargetName>$(ProjectName)-$(Platform)-$(Configuration)</TargetName>
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(Platform)\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)bin\$(Platform)\</OutDir>
<IntDir>$(SolutionDir)build\$(ProjectName)-$(Platform)-$(Configuration)\</IntDir>
<TargetName>$(ProjectName)-$(Platform)-$(Configuration)</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(Platform)\</OutDir>
<IntDir>$(SolutionDir)build\$(ProjectName)-$(Platform)-$(Configuration)\</IntDir>
<TargetName>$(ProjectName)-$(Platform)-$(Configuration)</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IntDir>$(SolutionDir)build\$(ProjectName)-$(Platform)-$(Configuration)\</IntDir>
<TargetName>$(ProjectName)-$(Platform)-$(Configuration)</TargetName>
<OutDir>$(SolutionDir)bin\$(Platform)\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|x64'">
<IntDir>$(SolutionDir)build\$(ProjectName)-$(Platform)-$(Configuration)\</IntDir>
<TargetName>$(ProjectName)-$(Platform)-$(Configuration)</TargetName>
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)bin\$(Platform)\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalIncludeDirectories>$(SolutionDir)dep\imgui\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libretro-common\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
<LanguageStandard>stdcpp17</LanguageStandard>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>d3d11.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalIncludeDirectories>$(SolutionDir)dep\imgui\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libretro-common\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
<LanguageStandard>stdcpp17</LanguageStandard>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>d3d11.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_ITERATOR_DEBUG_LEVEL=1;_CRT_SECURE_NO_WARNINGS;WIN32;_DEBUGFAST;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalIncludeDirectories>$(SolutionDir)dep\imgui\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libretro-common\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
<LanguageStandard>stdcpp17</LanguageStandard>
<SupportJustMyCode>false</SupportJustMyCode>
<InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>d3d11.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugFast|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_ITERATOR_DEBUG_LEVEL=1;_CRT_SECURE_NO_WARNINGS;WIN32;_DEBUGFAST;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalIncludeDirectories>$(SolutionDir)dep\imgui\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libretro-common\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
<LanguageStandard>stdcpp17</LanguageStandard>
<SupportJustMyCode>false</SupportJustMyCode>
<InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>d3d11.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)dep\imgui\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libretro-common\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<WholeProgramOptimization>false</WholeProgramOptimization>
<LanguageStandard>stdcpp17</LanguageStandard>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>d3d11.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|Win32'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)dep\imgui\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libretro-common\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<WholeProgramOptimization>true</WholeProgramOptimization>
<LanguageStandard>stdcpp17</LanguageStandard>
<OmitFramePointers>true</OmitFramePointers>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>d3d11.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)dep\imgui\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libretro-common\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<WholeProgramOptimization>false</WholeProgramOptimization>
<LanguageStandard>stdcpp17</LanguageStandard>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>d3d11.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseLTCG|x64'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)dep\imgui\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libretro-common\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<WholeProgramOptimization>true</WholeProgramOptimization>
<LanguageStandard>stdcpp17</LanguageStandard>
<OmitFramePointers>true</OmitFramePointers>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>d3d11.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="libretro_host_interface.cpp" />
<ClCompile Include="libretro_audio_stream.cpp" />
<ClCompile Include="opengl_host_display.cpp" />
<ClCompile Include="libretro_host_display.cpp" />
<ClCompile Include="d3d11_host_display.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="libretro_settings_interface.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="libretro_host_interface.h" />
<ClInclude Include="libretro_audio_stream.h" />
<ClInclude Include="opengl_host_display.h" />
<ClInclude Include="libretro_host_display.h" />
<ClInclude Include="d3d11_host_display.h" />
<ClInclude Include="libretro_settings_interface.h" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,23 @@
#include "libretro_audio_stream.h"
#include "libretro_host_interface.h"
LibretroAudioStream::LibretroAudioStream() = default;
LibretroAudioStream::~LibretroAudioStream() = default;
bool LibretroAudioStream::OpenDevice()
{
m_output_buffer.resize(m_buffer_size * m_channels);
return true;
}
void LibretroAudioStream::PauseDevice(bool paused) {}
void LibretroAudioStream::CloseDevice() {}
void LibretroAudioStream::FramesAvailable()
{
const u32 num_frames = GetSamplesAvailable();
ReadFrames(m_output_buffer.data(), num_frames, false);
g_retro_audio_sample_batch_callback(m_output_buffer.data(), num_frames);
}

View file

@ -0,0 +1,21 @@
#pragma once
#include "common/audio_stream.h"
#include <cstdint>
#include <vector>
class LibretroAudioStream final : public AudioStream
{
public:
LibretroAudioStream();
~LibretroAudioStream();
protected:
bool OpenDevice() override;
void PauseDevice(bool paused) override;
void CloseDevice() override;
void FramesAvailable() override;
private:
// TODO: Optimize this buffer away.
std::vector<SampleType> m_output_buffer;
};

View file

@ -0,0 +1,150 @@
#include "libretro_host_display.h"
#include "libretro_host_interface.h"
#include "common/assert.h"
#include "common/log.h"
#include "libretro.h"
#include <array>
#include <tuple>
Log_SetChannel(LibretroHostDisplay);
class LibretroDisplayTexture : public HostDisplayTexture
{
public:
LibretroDisplayTexture(u32 width, u32 height) : m_width(width), m_height(height), m_data(width * height) {}
~LibretroDisplayTexture() override = default;
void* GetHandle() const override { return const_cast<LibretroDisplayTexture*>(this); }
u32 GetWidth() const override { return m_width; }
u32 GetHeight() const override { return m_height; }
const u32* GetData() const { return m_data.data(); }
u32 GetDataPitch() const { return m_width * sizeof(u32); }
static void SwapAndCopy(void* dst, const void* src, u32 count)
{
// RGBA -> BGRX conversion
u8* dst_ptr = static_cast<u8*>(dst);
const u8* src_ptr = static_cast<const u8*>(src);
for (u32 i = 0; i < count; i++)
{
u32 sval;
std::memcpy(&sval, src_ptr, sizeof(sval));
src_ptr += sizeof(sval);
const u32 dval = (sval & 0xFF00FF00u) | ((sval & 0xFF) << 16) | ((sval >> 16) & 0xFFu);
std::memcpy(dst_ptr, &dval, sizeof(dval));
dst_ptr += sizeof(dval);
}
}
void Read(u32 x, u32 y, u32 width, u32 height, void* data, u32 data_stride) const
{
u8* data_ptr = static_cast<u8*>(data);
const u32* in_ptr = m_data.data() + y * m_width + x;
for (u32 i = 0; i < height; i++)
{
SwapAndCopy(data_ptr, in_ptr, width);
data_ptr += data_stride;
in_ptr += m_width;
}
}
void Write(u32 x, u32 y, u32 width, u32 height, const void* data, u32 data_stride)
{
const u8* data_ptr = static_cast<const u8*>(data);
u32* out_ptr = m_data.data() + y * m_width + x;
for (u32 i = 0; i < height; i++)
{
SwapAndCopy(out_ptr, data_ptr, width);
data_ptr += data_stride;
out_ptr += m_width;
}
}
static std::unique_ptr<LibretroDisplayTexture> Create(u32 width, u32 height, const void* initial_data,
u32 initial_data_stride)
{
std::unique_ptr<LibretroDisplayTexture> tex = std::make_unique<LibretroDisplayTexture>(width, height);
if (initial_data)
tex->Write(0, 0, width, height, initial_data, initial_data_stride);
return tex;
}
private:
u32 m_width;
u32 m_height;
std::vector<u32> m_data;
};
LibretroHostDisplay::LibretroHostDisplay()
{
// switch to a 32-bit buffer
retro_pixel_format pf = RETRO_PIXEL_FORMAT_XRGB8888;
if (!g_retro_environment_callback(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &pf))
Log_ErrorPrint("Failed to set pixel format to XRGB8888");
}
LibretroHostDisplay::~LibretroHostDisplay() = default;
HostDisplay::RenderAPI LibretroHostDisplay::GetRenderAPI() const
{
return RenderAPI::None;
}
void* LibretroHostDisplay::GetRenderDevice() const
{
return nullptr;
}
void* LibretroHostDisplay::GetRenderContext() const
{
return nullptr;
}
void LibretroHostDisplay::WindowResized(s32 new_window_width, s32 new_window_height) {}
std::unique_ptr<HostDisplayTexture> LibretroHostDisplay::CreateTexture(u32 width, u32 height, const void* data,
u32 data_stride, bool dynamic)
{
return LibretroDisplayTexture::Create(width, height, data, data_stride);
}
void LibretroHostDisplay::UpdateTexture(HostDisplayTexture* texture, u32 x, u32 y, u32 width, u32 height,
const void* data, u32 data_stride)
{
static_cast<LibretroDisplayTexture*>(texture)->Write(x, y, width, height, data, data_stride);
}
bool LibretroHostDisplay::DownloadTexture(const void* texture_handle, u32 x, u32 y, u32 width, u32 height,
void* out_data, u32 out_data_stride)
{
static_cast<const LibretroDisplayTexture*>(texture_handle)->Read(x, y, width, height, out_data, out_data_stride);
return true;
}
void LibretroHostDisplay::SetVSync(bool enabled) {}
void LibretroHostDisplay::Render()
{
if (m_display_texture_view_width != m_last_display_width || m_display_texture_view_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_texture_view_width;
m_last_display_height = m_display_texture_view_height;
}
// TODO: padding...
if (m_display_texture_handle)
{
const LibretroDisplayTexture* tex = static_cast<const LibretroDisplayTexture*>(m_display_texture_handle);
g_retro_video_refresh_callback(tex->GetData() + m_display_texture_view_y * tex->GetWidth() + m_display_texture_view_x, m_display_texture_view_width, m_display_texture_view_height , tex->GetDataPitch());
}
}

View file

@ -0,0 +1,30 @@
#pragma once
#include "core/host_display.h"
#include <memory>
class LibretroHostDisplay final : public HostDisplay
{
public:
LibretroHostDisplay();
~LibretroHostDisplay();
RenderAPI GetRenderAPI() const override;
void* GetRenderDevice() const override;
void* GetRenderContext() const override;
void WindowResized(s32 new_window_width, s32 new_window_height) 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:
s32 m_last_display_width = -1;
s32 m_last_display_height = -1;
};

View file

@ -0,0 +1,415 @@
#include "libretro_host_interface.h"
#include "common/assert.h"
#include "common/file_system.h"
#include "common/log.h"
#include "common/string_util.h"
#include "core/analog_controller.h"
#include "core/digital_controller.h"
#include "core/gpu.h"
#include "core/system.h"
#include "libretro_audio_stream.h"
#include "libretro_host_display.h"
#include "libretro_settings_interface.h"
#include "opengl_host_display.h"
#include <array>
#include <cstring>
#include <tuple>
#include <utility>
#include <vector>
Log_SetChannel(LibretroHostInterface);
#ifdef WIN32
#include "d3d11_host_display.h"
#endif
//////////////////////////////////////////////////////////////////////////
// TODO:
// - Fix up D3D11
// - Save states
// - Expose the rest of the options
// - Memory card and controller settings
// - Better paths for memory cards/BIOS
// - Fix up aspect ratio
//////////////////////////////////////////////////////////////////////////
LibretroHostInterface g_libretro_host_interface;
retro_environment_t g_retro_environment_callback;
retro_video_refresh_t g_retro_video_refresh_callback;
retro_audio_sample_t g_retro_audio_sample_callback;
retro_audio_sample_batch_t g_retro_audio_sample_batch_callback;
retro_input_poll_t g_retro_input_poll_callback;
retro_input_state_t g_retro_input_state_callback;
LibretroHostInterface::LibretroHostInterface() = default;
LibretroHostInterface::~LibretroHostInterface() = default;
bool LibretroHostInterface::Initialize()
{
if (!HostInterface::Initialize())
return false;
LoadSettings();
return true;
}
void LibretroHostInterface::Shutdown()
{
HostInterface::Shutdown();
}
void LibretroHostInterface::ReportError(const char* message)
{
Log_ErrorPrint(message);
}
void LibretroHostInterface::ReportMessage(const char* message)
{
Log_InfoPrint(message);
}
bool LibretroHostInterface::ConfirmMessage(const char* message)
{
Log_InfoPrintf("Confirm: %s", message);
return false;
}
void LibretroHostInterface::retro_get_system_av_info(struct retro_system_av_info* info)
{
Assert(m_system);
std::memset(info, 0, sizeof(*info));
info->geometry.aspect_ratio = 4.0f / 3.0f;
if (!m_system->IsPALRegion())
{
info->geometry.base_width = 640;
info->geometry.base_height = 480;
}
else
{
info->geometry.base_width = 720;
info->geometry.base_height = 576;
}
info->geometry.max_width = 1024;
info->geometry.max_height = 512;
info->timing.fps = m_system->GetThrottleFrequency();
info->timing.sample_rate = static_cast<double>(AUDIO_SAMPLE_RATE);
}
bool LibretroHostInterface::retro_load_game(const struct retro_game_info* game)
{
SystemBootParameters bp;
bp.filename = game->path;
if (!BootSystem(bp))
return false;
RequestHardwareRendererContext();
return true;
}
void LibretroHostInterface::retro_run_frame()
{
Assert(m_system);
UpdateControllers();
m_system->GetGPU()->RestoreGraphicsAPIState();
m_system->RunFrame();
m_system->GetGPU()->ResetGraphicsAPIState();
m_display->Render();
}
unsigned LibretroHostInterface::retro_get_region()
{
return m_system->IsPALRegion() ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
}
bool LibretroHostInterface::AcquireHostDisplay()
{
// start in software mode, switch to hardware later
m_display = new LibretroHostDisplay();
return true;
}
void LibretroHostInterface::ReleaseHostDisplay()
{
delete m_display;
m_display = nullptr;
}
std::unique_ptr<AudioStream> LibretroHostInterface::CreateAudioStream(AudioBackend backend)
{
return std::make_unique<LibretroAudioStream>();
}
static std::array<retro_core_option_definition, 14> s_option_definitions = {{
{"Console.Region",
"Console Region",
"Determines which region/hardware to emulate. Auto-Detect will use the region of the disc inserted.",
{{"Auto", "Auto-Detect"},
{"NTSC-J", "NTSC-J (Japan)"},
{"NTSC-U", "NTSC-U (US)"},
{"PAL", "PAL (Europe, Australia)"}},
"Auto"},
{"BIOS.FastBoot",
"Fast Boot",
"Skips the BIOS shell/intro, booting directly into the game. Usually safe to enable, but some games break.",
{{"true", "Enabled"}, {"false", "Disabled"}},
"false"},
{"CDROM.RegionCheck",
"CD-ROM Region Check",
"Prevents discs from incorrect regions being read by the emulator. Usually safe to disable.",
{{"true", "Enabled"}, {"false", "Disabled"}},
"false"},
{"CDROM.ReadThread",
"CD-ROM Read Thread",
"Reads CD-ROM sectors ahead asynchronously, reducing the risk of frame time spikes.",
{{"true", "Enabled"}, {"false", "Disabled"}},
"true"},
{"CPU.ExecutionMode",
"CPU Execution Mode",
"Which mode to use for CPU emulation. Recompiler provides the best performance.",
{{"Interpreter", "Interpreter"}, {"CachedIntepreter", "Cached Interpreter"}, {"Recompiler", "Recompiler"}},
"Recompiler"},
{"GPU.Renderer",
"GPU Renderer",
"Which renderer to use to emulate the GPU",
{
#ifdef WIN32
{"D3D11", "Hardware (D3D11)"},
#endif
{"OpenGL", "Hardware (OpenGL)"},
{"Software", "Software"}},
"OpenGL"},
{"GPU.ResolutionScale",
"Rendering Resolution Scale",
"Scales internal rendering resolution by the specified multiplier. Larger values are slower. Some games require "
"1x rendering resolution or they will have rendering issues.",
{{"1", "1x (1024x512)"},
{"2", "2x (2048x1024)"},
{"3", "3x (3072x1536)"},
{"4", "4x (4096x2048)"},
{"5", "5x (5120x2160)"},
{"6", "6x (6144x3072)"},
{"7", "7x (7168x3584)"},
{"8", "8x (8192x4096)"}},
"1"},
{"GPU.TrueColor",
"True Color Rendering",
"Disables dithering and uses the full 8 bits per channel of color information. May break rendering in some games.",
{{"true", "Enabled"}, {"false", "Disabled"}},
"false"},
{"GPU.ScaledDithering",
"Scaled Dithering",
"Scales the dithering pattern with the internal rendering resolution, making it less noticeable. Usually safe to "
"enable.",
{{"true", "Enabled"}, {"false", "Disabled"}},
"true"},
{"GPU.DisableInterlacing",
"Disable Interlacing",
"Disables interlaced rendering and display in the GPU. Some games can render in 480p this way, but others will "
"break.",
{{"true", "Enabled"}, {"false", "Disabled"}},
"false"},
{"GPU.ForceNTSCTimings",
"Force NTSC Timings",
"Forces PAL games to run at NTSC timings, i.e. 60hz. Some PAL games will run at their \"normal\" speeds, while "
"others will break.",
{{"true", "Enabled"}, {"false", "Disabled"}},
"false"},
{"Display.CropMode",
"Crop Mode",
"Changes how much of the image is cropped. Some games display garbage in the overscan area which is typically "
"hidden.",
{{"None", "None"}, {"Overscan", "Only Overscan Area"}, {"Borders", "All Borders"}},
"Overscan"},
{"Display.PixelAspectRatio",
"Pixel Aspect Ratio",
"Determines how the pixels in VRAM area are displayed on the screen.",
{{"4:3", "4:3"}, {"16:9", "16:9"}, {"1:1", "1:1"}},
"4:3"},
{},
}};
bool LibretroHostInterface::SetCoreOptions()
{
unsigned options_version = 0;
if (g_retro_environment_callback(RETRO_ENVIRONMENT_GET_CORE_OPTIONS_VERSION, &options_version) &&
options_version >= 1)
{
return g_retro_environment_callback(RETRO_ENVIRONMENT_SET_CORE_OPTIONS, &s_option_definitions);
}
// use legacy options struct, which sucks. do we need to?
return false;
}
void LibretroHostInterface::LoadSettings()
{
LibretroSettingsInterface si;
m_settings.Load(si);
// Overrides
m_settings.log_level = LOGLEVEL_DEV;
m_settings.log_to_console = true;
// start in software, switch later
m_settings.gpu_renderer = GPURenderer::Software;
// Assume BIOS files are located in system directory.
const char* system_directory = nullptr;
if (!g_retro_environment_callback(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_directory) || !system_directory)
system_directory = "bios";
m_settings.bios_path =
StringUtil::StdStringFromFormat("%s%cscph1001.bin", system_directory, FS_OSPATH_SEPERATOR_CHARACTER);
// TODOs - expose via config
m_settings.controller_types[0] = ControllerType::DigitalController;
m_settings.controller_types[1] = ControllerType::None;
m_settings.memory_card_types[0] = MemoryCardType::None;
m_settings.memory_card_types[1] = MemoryCardType::None;
}
void LibretroHostInterface::UpdateSettings()
{
Settings old_settings(std::move(m_settings));
LoadSettings();
CheckForSettingsChanges(old_settings);
}
void LibretroHostInterface::UpdateControllers()
{
g_retro_input_poll_callback();
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
{
switch (m_settings.controller_types[i])
{
case ControllerType::None:
break;
case ControllerType::DigitalController:
UpdateControllersDigitalController(i);
break;
default:
Log_ErrorPrintf("Unhandled controller type '%s'",
Settings::GetControllerTypeDisplayName(m_settings.controller_types[i]));
break;
}
}
}
void LibretroHostInterface::UpdateControllersDigitalController(u32 index)
{
DigitalController* controller = static_cast<DigitalController*>(m_system->GetController(index));
DebugAssert(controller);
static constexpr std::array<std::pair<DigitalController::Button, u32>, 14> mapping = {
{{DigitalController::Button::Left, RETRO_DEVICE_ID_JOYPAD_LEFT},
{DigitalController::Button::Right, RETRO_DEVICE_ID_JOYPAD_RIGHT},
{DigitalController::Button::Up, RETRO_DEVICE_ID_JOYPAD_UP},
{DigitalController::Button::Down, RETRO_DEVICE_ID_JOYPAD_DOWN},
{DigitalController::Button::Circle, RETRO_DEVICE_ID_JOYPAD_A},
{DigitalController::Button::Cross, RETRO_DEVICE_ID_JOYPAD_B},
{DigitalController::Button::Triangle, RETRO_DEVICE_ID_JOYPAD_X},
{DigitalController::Button::Square, RETRO_DEVICE_ID_JOYPAD_Y},
{DigitalController::Button::Start, RETRO_DEVICE_ID_JOYPAD_START},
{DigitalController::Button::Select, RETRO_DEVICE_ID_JOYPAD_SELECT},
{DigitalController::Button::L1, RETRO_DEVICE_ID_JOYPAD_L},
{DigitalController::Button::L2, RETRO_DEVICE_ID_JOYPAD_L2},
{DigitalController::Button::R1, RETRO_DEVICE_ID_JOYPAD_R},
{DigitalController::Button::R2, RETRO_DEVICE_ID_JOYPAD_R2}}};
for (const auto& it : mapping)
{
const int16_t state = g_retro_input_state_callback(index, RETRO_DEVICE_JOYPAD, 0, it.second);
controller->SetButtonState(it.first, state != 0);
}
}
bool LibretroHostInterface::RequestHardwareRendererContext()
{
GPURenderer renderer = Settings::DEFAULT_GPU_RENDERER;
retro_variable renderer_variable{"GPU.Renderer", "OpenGL"};
if (g_retro_environment_callback(RETRO_ENVIRONMENT_GET_VARIABLE, &renderer_variable) && renderer_variable.value)
renderer = Settings::ParseRendererName(renderer_variable.value).value_or(Settings::DEFAULT_GPU_RENDERER);
if (renderer == GPURenderer::Software)
return true;
m_hw_render_callback = {};
m_hw_render_callback.context_reset = HardwareRendererContextReset;
m_hw_render_callback.context_destroy = HardwareRendererContextDestroy;
#ifdef WIN32
if (renderer == GPURenderer::HardwareD3D11 && false)
return D3D11HostDisplay::RequestHardwareRendererContext(&m_hw_render_callback);
#endif
if (renderer == GPURenderer::HardwareOpenGL)
return OpenGLHostDisplay::RequestHardwareRendererContext(&m_hw_render_callback);
return false;
}
void LibretroHostInterface::HardwareRendererContextReset()
{
Log_InfoPrintf("Hardware context reset, type = %u",
static_cast<unsigned>(g_libretro_host_interface.m_hw_render_callback.context_type));
std::unique_ptr<HostDisplay> new_display = nullptr;
GPURenderer new_renderer = GPURenderer::Software;
switch (g_libretro_host_interface.m_hw_render_callback.context_type)
{
case RETRO_HW_CONTEXT_OPENGL:
case RETRO_HW_CONTEXT_OPENGL_CORE:
case RETRO_HW_CONTEXT_OPENGLES3:
case RETRO_HW_CONTEXT_OPENGLES_VERSION:
new_display = OpenGLHostDisplay::Create(g_libretro_host_interface.m_settings.gpu_use_debug_device);
new_renderer = GPURenderer::HardwareOpenGL;
break;
#ifdef WIN32
case RETRO_HW_CONTEXT_DIRECT3D:
new_display = D3D11HostDisplay::Create(g_libretro_host_interface.m_settings.gpu_use_debug_device);
new_renderer = GPURenderer::HardwareD3D11;
break;
#endif
default:
break;
}
if (!new_display)
{
Log_ErrorPrintf("Failed to create hardware host display");
return;
}
HostDisplay* old_display = g_libretro_host_interface.m_display;
g_libretro_host_interface.m_display = new_display.release();
g_libretro_host_interface.m_settings.gpu_renderer = new_renderer;
g_libretro_host_interface.m_system->RecreateGPU(new_renderer);
delete old_display;
}
void LibretroHostInterface::HardwareRendererContextDestroy()
{
// switch back to software
HostDisplay* old_display = g_libretro_host_interface.m_display;
g_libretro_host_interface.m_display = new LibretroHostDisplay();
g_libretro_host_interface.m_settings.gpu_renderer = GPURenderer::Software;
g_libretro_host_interface.m_system->RecreateGPU(GPURenderer::Software);
delete old_display;
}

View file

@ -0,0 +1,57 @@
#pragma once
#include "core/host_interface.h"
#include "core/system.h"
#include "libretro.h"
class LibretroHostInterface : public HostInterface
{
public:
LibretroHostInterface();
~LibretroHostInterface() override;
static bool SetCoreOptions();
bool Initialize() override;
void Shutdown() override;
void ReportError(const char* message) override;
void ReportMessage(const char* message) override;
bool ConfirmMessage(const char* message) override;
const retro_hw_render_callback& GetHWRenderCallback() const { return m_hw_render_callback; }
// Called by frontend
void retro_get_system_av_info(struct retro_system_av_info* info);
bool retro_load_game(const struct retro_game_info* game);
void retro_run_frame();
unsigned retro_get_region();
protected:
bool AcquireHostDisplay() override;
void ReleaseHostDisplay() override;
std::unique_ptr<AudioStream> CreateAudioStream(AudioBackend backend) override;
private:
void LoadSettings();
void UpdateSettings();
void UpdateControllers();
void UpdateControllersDigitalController(u32 index);
// Hardware renderer setup.
bool RequestHardwareRendererContext();
static void HardwareRendererContextReset();
static void HardwareRendererContextDestroy();
retro_hw_render_callback m_hw_render_callback = {};
};
extern LibretroHostInterface g_libretro_host_interface;
// libretro callbacks
extern retro_environment_t g_retro_environment_callback;
extern retro_video_refresh_t g_retro_video_refresh_callback;
extern retro_audio_sample_t g_retro_audio_sample_callback;
extern retro_audio_sample_batch_t g_retro_audio_sample_batch_callback;
extern retro_input_poll_t g_retro_input_poll_callback;
extern retro_input_state_t g_retro_input_state_callback;

View file

@ -0,0 +1,116 @@
#include "libretro_settings_interface.h"
#include "common/log.h"
#include "common/string_util.h"
#include "libretro_host_interface.h"
#include <type_traits>
Log_SetChannel(LibretroSettingsInterface);
template<typename T, typename DefaultValueType>
static T GetVariable(const char* section, const char* key, DefaultValueType default_value)
{
TinyString full_key;
full_key.Format("%s.%s", section, key);
retro_variable rv = {full_key.GetCharArray(), nullptr};
if (!g_retro_environment_callback(RETRO_ENVIRONMENT_GET_VARIABLE, &rv) || !rv.value)
return T(default_value);
if constexpr (std::is_same_v<T, std::string>)
{
return T(rv.value);
}
else if constexpr (std::is_same_v<T, bool>)
{
return (StringUtil::Strcasecmp(rv.value, "true") == 0 || StringUtil::Strcasecmp(rv.value, "1") == 0);
}
else if constexpr (std::is_same_v<T, float>)
{
return std::strtof(rv.value, nullptr);
}
else
{
std::optional<T> parsed = StringUtil::FromChars<T>(rv.value);
if (!parsed.has_value())
return T(default_value);
return parsed.value();
}
}
void LibretroSettingsInterface::Clear()
{
Log_WarningPrintf("Clear not implemented");
}
int LibretroSettingsInterface::GetIntValue(const char* section, const char* key, int default_value /*= 0*/)
{
return GetVariable<int>(section, key, default_value);
}
float LibretroSettingsInterface::GetFloatValue(const char* section, const char* key, float default_value /*= 0.0f*/)
{
return GetVariable<float>(section, key, default_value);
}
bool LibretroSettingsInterface::GetBoolValue(const char* section, const char* key, bool default_value /*= false*/)
{
return GetVariable<bool>(section, key, default_value);
}
std::string LibretroSettingsInterface::GetStringValue(const char* section, const char* key,
const char* default_value /*= ""*/)
{
return GetVariable<std::string>(section, key, default_value);
}
void LibretroSettingsInterface::SetIntValue(const char* section, const char* key, int value)
{
Log_ErrorPrintf("SetIntValue(\"%s\", \"%s\", %d) not implemented", section, key, value);
}
void LibretroSettingsInterface::SetFloatValue(const char* section, const char* key, float value)
{
Log_ErrorPrintf("SetFloatValue(\"%s\", \"%s\", %f) not implemented", section, key, value);
}
void LibretroSettingsInterface::SetBoolValue(const char* section, const char* key, bool value)
{
Log_ErrorPrintf("SetBoolValue(\"%s\", \"%s\", %u) not implemented", section, key, static_cast<unsigned>(value));
}
void LibretroSettingsInterface::SetStringValue(const char* section, const char* key, const char* value)
{
Log_ErrorPrintf("SetStringValue(\"%s\", \"%s\", \"%s\") not implemented", section, key, value);
}
std::vector<std::string> LibretroSettingsInterface::GetStringList(const char* section, const char* key)
{
std::string value = GetVariable<std::string>(section, key, "");
if (value.empty())
return {};
return std::vector<std::string>({std::move(value)});
}
void LibretroSettingsInterface::SetStringList(const char* section, const char* key,
const std::vector<std::string_view>& items)
{
Log_ErrorPrintf("SetStringList(\"%s\", \"%s\") not implemented", section, key);
}
bool LibretroSettingsInterface::RemoveFromStringList(const char* section, const char* key, const char* item)
{
Log_ErrorPrintf("RemoveFromStringList(\"%s\", \"%s\", \"%s\") not implemented", section, key, item);
return false;
}
bool LibretroSettingsInterface::AddToStringList(const char* section, const char* key, const char* item)
{
Log_ErrorPrintf("AddToStringList(\"%s\", \"%s\", \"%s\") not implemented", section, key, item);
return false;
}
void LibretroSettingsInterface::DeleteValue(const char* section, const char* key)
{
Log_ErrorPrintf("DeleteValue(\"%s\", \"%s\") not implemented", section, key);
}

View file

@ -0,0 +1,25 @@
#pragma once
#include "core/settings.h"
class LibretroSettingsInterface : public SettingsInterface
{
public:
void Clear() override;
int GetIntValue(const char* section, const char* key, int default_value = 0) override;
float GetFloatValue(const char* section, const char* key, float default_value = 0.0f) override;
bool GetBoolValue(const char* section, const char* key, bool default_value = false) override;
std::string GetStringValue(const char* section, const char* key, const char* default_value = "") override;
void SetIntValue(const char* section, const char* key, int value) override;
void SetFloatValue(const char* section, const char* key, float value) override;
void SetBoolValue(const char* section, const char* key, bool value) override;
void SetStringValue(const char* section, const char* key, const char* value) override;
std::vector<std::string> GetStringList(const char* section, const char* key) override;
void SetStringList(const char* section, const char* key, const std::vector<std::string_view>& items) override;
bool RemoveFromStringList(const char* section, const char* key, const char* item) override;
bool AddToStringList(const char* section, const char* key, const char* item) override;
void DeleteValue(const char* section, const char* key) override;
};

View file

@ -0,0 +1,164 @@
#include "common/assert.h"
#include "common/log.h"
#include "libretro_host_interface.h"
#include "scmversion/scmversion.h"
Log_SetChannel(Main);
RETRO_API unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
RETRO_API void retro_init(void)
{
Log::SetConsoleOutputParams(true);
Log::SetDebugOutputParams(true);
Log_InfoPrintf("retro_init()");
if (!g_libretro_host_interface.Initialize())
Panic("Host interface initialization failed");
}
RETRO_API void retro_deinit(void)
{
Log_InfoPrintf("retro_deinit()");
g_libretro_host_interface.Shutdown();
}
RETRO_API void retro_get_system_info(struct retro_system_info* info)
{
std::memset(info, 0, sizeof(*info));
#if defined(_DEBUGFAST)
info->library_name = "DuckStation DebugFast";
#elif defined(_DEBUG)
info->library_name = "DuckStation Debug";
#else
info->library_name = "DuckStation";
#endif
info->library_version = g_scm_tag_str;
info->valid_extensions = "exe|cue|bin|chd|psf";
info->need_fullpath = true;
info->block_extract = false;
}
RETRO_API void retro_get_system_av_info(struct retro_system_av_info* info)
{
g_libretro_host_interface.retro_get_system_av_info(info);
}
RETRO_API void retro_set_controller_port_device(unsigned port, unsigned device)
{
Log_ErrorPrintf("retro_set_controller_port_device(%u, %u)", port, device);
}
RETRO_API void retro_reset(void)
{
Log_InfoPrint("retro_reset()");
g_libretro_host_interface.ResetSystem();
}
RETRO_API void retro_run(void)
{
g_libretro_host_interface.retro_run_frame();
}
RETRO_API size_t retro_serialize_size(void)
{
Log_ErrorPrintf("retro_serialize_size()");
return 0;
}
RETRO_API bool retro_serialize(void* data, size_t size)
{
Log_ErrorPrintf("retro_serialize()");
return false;
}
RETRO_API bool retro_unserialize(const void* data, size_t size)
{
Log_ErrorPrintf("retro_unserialize()");
return false;
}
RETRO_API void retro_cheat_reset(void)
{
Log_ErrorPrintf("retro_cheat_reset()");
}
RETRO_API void retro_cheat_set(unsigned index, bool enabled, const char* code)
{
Log_ErrorPrintf("retro_cheat_set(%u, %u, %s)", index, enabled, code);
}
RETRO_API bool retro_load_game(const struct retro_game_info* game)
{
Log_InfoPrintf("retro_load_game(%s)", game->path);
return g_libretro_host_interface.retro_load_game(game);
}
RETRO_API bool retro_load_game_special(unsigned game_type, const struct retro_game_info* info, size_t num_info)
{
Log_ErrorPrintf("retro_load_game_special()");
return false;
}
RETRO_API void retro_unload_game(void)
{
Log_ErrorPrintf("retro_unload_game()");
g_libretro_host_interface.DestroySystem();
}
RETRO_API unsigned retro_get_region(void)
{
return g_libretro_host_interface.retro_get_region();
}
RETRO_API void* retro_get_memory_data(unsigned id)
{
return nullptr;
}
RETRO_API size_t retro_get_memory_size(unsigned id)
{
return 0;
}
RETRO_API void retro_set_environment(retro_environment_t f)
{
static bool core_options_set = false;
g_retro_environment_callback = f;
if (!core_options_set)
{
core_options_set = true;
if (!g_libretro_host_interface.SetCoreOptions())
Log_WarningPrintf("Failed to set core options, settings will not be changeable.");
}
}
RETRO_API void retro_set_video_refresh(retro_video_refresh_t f)
{
g_retro_video_refresh_callback = f;
}
RETRO_API void retro_set_audio_sample(retro_audio_sample_t f)
{
g_retro_audio_sample_callback = f;
}
RETRO_API void retro_set_audio_sample_batch(retro_audio_sample_batch_t f)
{
g_retro_audio_sample_batch_callback = f;
}
RETRO_API void retro_set_input_poll(retro_input_poll_t f)
{
g_retro_input_poll_callback = f;
}
RETRO_API void retro_set_input_state(retro_input_state_t f)
{
g_retro_input_state_callback = f;
}

View file

@ -0,0 +1,385 @@
#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);
}

View file

@ -0,0 +1,53 @@
#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;
};