Skip to content

Commit

Permalink
video routing fixes
Browse files Browse the repository at this point in the history
dx11: terminate video routing in DX11Renderer::Term. Don't use
DX11Context.
dx11: avoid using local static vars
gl: don't send rendered frame when full FB is on
gl: send rendered framebuffer
gl: terminate video routing in termGLCommon. Don't use GLGraphicsContext

gl: avoid crash on quick exit during test automation
  • Loading branch information
flyinghead committed Jan 5, 2024
1 parent c0c6f20 commit 032cc17
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
52 changes: 26 additions & 26 deletions core/rend/dx11/dx11_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include <memory>

void os_VideoRoutingTermDX();

const D3D11_INPUT_ELEMENT_DESC MainLayout[]
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, (UINT)offsetof(Vertex, x), D3D11_INPUT_PER_VERTEX_DATA, 0 },
Expand Down Expand Up @@ -173,6 +175,9 @@ bool DX11Renderer::Init()
void DX11Renderer::Term()
{
NOTICE_LOG(RENDERER, "DX11 renderer terminating");
#ifdef VIDEO_ROUTING
os_VideoRoutingTermDX();
#endif
n2Helper.term();
vtxConstants.reset();
pxlConstants.reset();
Expand Down Expand Up @@ -1342,36 +1347,22 @@ void DX11Renderer::renderVideoRouting()

ID3D11RenderTargetView* pRenderTargetView = theDX11Context.getRenderTarget().get();

// Backbuffer texture would be different after reszing, fetching new address everytime
// Backbuffer texture would be different after resizing, fetching new address everytime
ID3D11Resource* pResource = nullptr;
pRenderTargetView->GetResource(&pResource);
ID3D11Texture2D* backBufferTexture = nullptr;
pResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&backBufferTexture);

if (config::VideoRoutingScale)
{
static int targetWidth, targetHeight, vrStagingWidth, vrStagingHeight;
static D3D11_VIEWPORT scaledViewPort{};

auto updateScaledTexture = [this]() -> void {
targetWidth = config::VideoRoutingVRes * settings.display.width / settings.display.height;
targetHeight = config::VideoRoutingVRes;

vrScaledTexture.reset();
vrScaledRenderTarget.reset();
createTexAndRenderTarget(vrScaledTexture, vrScaledRenderTarget, targetWidth, targetHeight);

scaledViewPort.Width = targetWidth;
scaledViewPort.Height = targetHeight;
scaledViewPort.MinDepth = 0.f;
scaledViewPort.MaxDepth = 1.f;
};

D3D11_TEXTURE2D_DESC bbDesc = {};
backBufferTexture->GetDesc(&bbDesc);
D3D11_TEXTURE2D_DESC vrsDesc = {};
if (vrStagingTexture)
vrStagingTexture->GetDesc(&vrsDesc);

// Window resized
if (bbDesc.Width != vrStagingWidth || bbDesc.Height != vrStagingHeight)
// Window resized?
if (!vrStagingTexture || bbDesc.Width != vrsDesc.Width || bbDesc.Height != vrsDesc.Height)
{
vrStagingTexture.reset();
vrStagingTextureSRV.reset();
Expand All @@ -1387,15 +1378,25 @@ void DX11Renderer::renderVideoRouting()
viewDesc.Texture2D.MipLevels = 1;

device->CreateShaderResourceView(vrStagingTexture.get(), &viewDesc, &vrStagingTextureSRV.get());

updateScaledTexture();
}

// Scale down value changed
if (targetHeight != config::VideoRoutingVRes)
// Scale down value changed?
D3D11_TEXTURE2D_DESC vrscDesc = {};
if (vrScaledTexture)
vrScaledTexture->GetDesc(&vrscDesc);
int targetWidth = config::VideoRoutingVRes * settings.display.width / settings.display.height;
if (!vrScaledTexture || (int)vrscDesc.Height != config::VideoRoutingVRes)
{
updateScaledTexture();

vrScaledTexture.reset();
vrScaledRenderTarget.reset();
createTexAndRenderTarget(vrScaledTexture, vrScaledRenderTarget, targetWidth, config::VideoRoutingVRes);
}
D3D11_VIEWPORT scaledViewPort{};
scaledViewPort.Width = targetWidth;
scaledViewPort.Height = config::VideoRoutingVRes;
scaledViewPort.MinDepth = 0.f;
scaledViewPort.MaxDepth = 1.f;

deviceContext->OMSetRenderTargets(1, &vrScaledRenderTarget.get(), nullptr);
deviceContext->RSSetViewports(1, &scaledViewPort);
Expand All @@ -1414,7 +1415,6 @@ void DX11Renderer::renderVideoRouting()
}
else
{
extern void os_VideoRoutingTermDX();
os_VideoRoutingTermDX();
}
#endif
Expand Down
4 changes: 0 additions & 4 deletions core/rend/dx11/dx11context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ void DX11Context::term()
FreeLibrary(d3dcompilerHandle);
d3dcompilerHandle = NULL;
}
#ifdef VIDEO_ROUTING
extern void os_VideoRoutingTermDX();
os_VideoRoutingTermDX();
#endif
}

void DX11Context::Present()
Expand Down
5 changes: 3 additions & 2 deletions core/rend/gl4/gles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,8 @@ struct OpenGL4Renderer : OpenGLRenderer
{
DrawOSD(false);
frameRendered = true;
renderVideoRouting();
}
renderVideoRouting();
restoreCurrentFramebuffer();

return true;
Expand Down Expand Up @@ -781,7 +781,8 @@ static void resize(int w, int h)

bool OpenGL4Renderer::renderFrame(int width, int height)
{
initVideoRoutingFrameBuffer();
if (!config::EmulateFramebuffer)
initVideoRoutingFrameBuffer();

const bool is_rtt = pvrrc.isRTT;

Expand Down
2 changes: 2 additions & 0 deletions core/rend/gles/gldraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ void DrawStrips()

void OpenGLRenderer::RenderFramebuffer(const FramebufferInfo& info)
{
initVideoRoutingFrameBuffer();
glReadFramebuffer(info);
saveCurrentFramebuffer();
getVideoShift(gl.ofbo.shiftX, gl.ofbo.shiftY);
Expand Down Expand Up @@ -663,6 +664,7 @@ void OpenGLRenderer::RenderFramebuffer(const FramebufferInfo& info)

DrawOSD(false);
frameRendered = true;
renderVideoRouting();
restoreCurrentFramebuffer();
}

Expand Down
12 changes: 9 additions & 3 deletions core/rend/gles/gles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ void main()
}
)";

void os_VideoRoutingTermGL();
static void gl_free_osd_resources();

GLCache glcache;
Expand Down Expand Up @@ -420,6 +421,8 @@ void do_swap_automation()
dump_screenshot(img, framebuffer->getWidth(), framebuffer->getHeight());
delete[] img;
dc_exit();
void sdl_window_destroy();
sdl_window_destroy(); // avoid crash
flycast_term();
exit(0);
}
Expand All @@ -443,6 +446,9 @@ static void gl_delete_shaders()

void termGLCommon()
{
#ifdef VIDEO_ROUTING
os_VideoRoutingTermGL();
#endif
termQuad();

// palette, fog
Expand Down Expand Up @@ -1170,7 +1176,8 @@ static void upload_vertex_indices()

bool OpenGLRenderer::renderFrame(int width, int height)
{
initVideoRoutingFrameBuffer();
if (!config::EmulateFramebuffer)
initVideoRoutingFrameBuffer();

bool is_rtt = pvrrc.isRTT;

Expand Down Expand Up @@ -1448,9 +1455,9 @@ bool OpenGLRenderer::Render()
{
DrawOSD(false);
frameRendered = true;
renderVideoRouting();
}

renderVideoRouting();
restoreCurrentFramebuffer();

return true;
Expand All @@ -1474,7 +1481,6 @@ void OpenGLRenderer::renderVideoRouting()
}
else
{
extern void os_VideoRoutingTermGL();
os_VideoRoutingTermGL();
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions core/wsi/gl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ void GLGraphicsContext::preTerm()
#ifndef LIBRETRO
imguiDriver.reset();
#endif
#ifdef VIDEO_ROUTING
extern void os_VideoRoutingTermGL();
os_VideoRoutingTermGL();
#endif

instance = nullptr;
}

Expand Down

0 comments on commit 032cc17

Please sign in to comment.