From 5e75a66f82b828595c65b0a51bdce5e83f6e7374 Mon Sep 17 00:00:00 2001
From: Kojin <kojin@protonmail.com>
Date: Fri, 14 Dec 2018 10:45:42 -0500
Subject: [PATCH] gsdx-d3d11: Port RenderOsd function from opengl to support
 osd on d3d11

---
 plugins/GSdx/Renderers/DX11/GSDevice11.cpp | 44 ++++++++++++++++++++++
 plugins/GSdx/Renderers/DX11/GSDevice11.h   |  6 ++-
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/plugins/GSdx/Renderers/DX11/GSDevice11.cpp b/plugins/GSdx/Renderers/DX11/GSDevice11.cpp
index 191ff29e5c..d0d8623b2f 100644
--- a/plugins/GSdx/Renderers/DX11/GSDevice11.cpp
+++ b/plugins/GSdx/Renderers/DX11/GSDevice11.cpp
@@ -411,6 +411,9 @@ bool GSDevice11::Create(const std::shared_ptr<GSWnd> &wnd)
 		SetExclusive(!theApp.GetConfigB("windowed"));
 	}
 
+	GSVector2i tex_font = m_osd.get_texture_font_size();
+	m_font = CreateSurface(GSTexture::Texture, tex_font.x, tex_font.y, false, DXGI_FORMAT_R8_UNORM);
+
 	return true;
 }
 
@@ -808,6 +811,47 @@ void GSDevice11::StretchRect(GSTexture* sTex, const GSVector4& sRect, GSTexture*
 	PSSetShaderResources(NULL, NULL);
 }
 
+void GSDevice11::RenderOsd(GSTexture* dt)
+{
+	BeginScene();
+
+	// om
+	OMSetDepthStencilState(m_convert.dss, 0);
+	OMSetBlendState(m_merge.bs, 0);
+	OMSetRenderTargets(dt, NULL);
+
+	if(m_osd.m_texture_dirty) {
+		m_osd.upload_texture_atlas(m_font);
+	}
+
+	// ps
+	PSSetShaderResource(0, m_font);
+	PSSetSamplerState(m_convert.pt, NULL);
+	PSSetShader(m_convert.ps[ShaderConvert_OSD], NULL);
+
+	// ia
+	IASetInputLayout(m_convert.il);
+	IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+
+	// Note scaling could also be done in shader (require gl3/dx10)
+	size_t count = m_osd.Size();
+	void* dst = NULL;
+
+	IAMapVertexBuffer(&dst, sizeof(GSVertexPT1), count);
+	count = m_osd.GeneratePrimitives((GSVertexPT1*)dst, count);
+	IAUnmapVertexBuffer();
+
+	// vs
+	VSSetShader(m_convert.vs, NULL);
+
+	// gs
+	GSSetShader(NULL, NULL);
+
+	DrawPrimitive();
+
+	EndScene();
+}
+
 void GSDevice11::DoMerge(GSTexture* sTex[3], GSVector4* sRect, GSTexture* dTex, GSVector4* dRect, const GSRegPMODE& PMODE, const GSRegEXTBUF& EXTBUF, const GSVector4& c)
 {
 	bool slbg = PMODE.SLBG;
diff --git a/plugins/GSdx/Renderers/DX11/GSDevice11.h b/plugins/GSdx/Renderers/DX11/GSDevice11.h
index 3e5db5d95a..55fb0aedec 100644
--- a/plugins/GSdx/Renderers/DX11/GSDevice11.h
+++ b/plugins/GSdx/Renderers/DX11/GSDevice11.h
@@ -39,7 +39,7 @@ class GSDevice11 : public GSDeviceDX
 	void DoFXAA(GSTexture* sTex, GSTexture* dTex);
 	void DoShadeBoost(GSTexture* sTex, GSTexture* dTex);
 	void DoExternalFX(GSTexture* sTex, GSTexture* dTex);
-
+	void RenderOsd(GSTexture* dt);
 	void InitExternalFX();
 	void InitFXAA(); // Bug workaround! Stack corruption? Heap corruption? No idea
 	
@@ -94,7 +94,7 @@ public: // TODO
 	{
 		CComPtr<ID3D11InputLayout> il;
 		CComPtr<ID3D11VertexShader> vs;
-		CComPtr<ID3D11PixelShader> ps[18];
+		CComPtr<ID3D11PixelShader> ps[ShaderConvert_Count];
 		CComPtr<ID3D11SamplerState> ln;
 		CComPtr<ID3D11SamplerState> pt;
 		CComPtr<ID3D11DepthStencilState> dss;
@@ -159,6 +159,8 @@ public: // TODO
 	GSConstantBuffer m_gs_cb_cache;
 	PSConstantBuffer m_ps_cb_cache;
 
+	GSTexture* m_font;
+
 	bool CreateTextureFX();
 
 public: