From a61afbde5288d7291105c6168bf1d980a3eb02ef Mon Sep 17 00:00:00 2001 From: OV2 Date: Wed, 27 Mar 2013 15:55:59 +0100 Subject: [PATCH 01/10] win32: add overlay to d3d9 --- gfx/d3d9/d3d9.cpp | 242 +++++++++++++++++++++++++++++++++++++++++++++- gfx/d3d9/d3d9.hpp | 27 ++++++ 2 files changed, 267 insertions(+), 2 deletions(-) diff --git a/gfx/d3d9/d3d9.cpp b/gfx/d3d9/d3d9.cpp index 0ef2495c3c..f5d3dd7215 100644 --- a/gfx/d3d9/d3d9.cpp +++ b/gfx/d3d9/d3d9.cpp @@ -394,6 +394,8 @@ D3DVideo::D3DVideo(const video_info_t *info) : { gfx_set_dwm(); + std::memset(&overlay,0,sizeof(overlay)); + std::memset(&windowClass, 0, sizeof(windowClass)); windowClass.cbSize = sizeof(windowClass); windowClass.style = CS_HREDRAW | CS_VREDRAW; @@ -449,7 +451,11 @@ D3DVideo::D3DVideo(const video_info_t *info) : driver.video_display = 0; driver.video_window = (uintptr_t)hWnd; - show_cursor(!info->fullscreen); + show_cursor(!info->fullscreen +#ifdef HAVE_OVERLAY + || overlay.overlay_enabled +#endif + ); Callback::quit = false; ShowWindow(hWnd, SW_RESTORE); @@ -485,6 +491,12 @@ void D3DVideo::deinit() D3DVideo::~D3DVideo() { deinit(); +#ifdef HAVE_OVERLAY + if(overlay.tex) + overlay.tex->Release(); + if(overlay.vert_buf) + overlay.vert_buf->Release(); +#endif if (dev) dev->Release(); if (g_pD3D) @@ -558,6 +570,10 @@ bool D3DVideo::frame(const void *frame, dev->EndScene(); } + if(overlay.overlay_enabled) { + overlay_render(); + } + RARCH_PERFORMANCE_STOP(d3d_frame); if (dev->Present(nullptr, nullptr, nullptr, nullptr) != D3D_OK) @@ -1157,6 +1173,175 @@ void D3DVideo::resize(unsigned new_width, unsigned new_height) } } +#ifdef HAVE_OVERLAY +bool D3DVideo::overlay_load(const uint32_t *image, unsigned width, unsigned height) +{ + if(overlay.tex) + overlay.tex->Release(); + if (FAILED(dev->CreateTexture(width, height, 1, + 0, + D3DFMT_A8R8G8B8, + D3DPOOL_MANAGED, + &overlay.tex, nullptr))) + { + RARCH_ERR("[D3D9]: Failed to create overlay texture\n"); + return false; + } + + D3DLOCKED_RECT d3dlr; + if (SUCCEEDED(overlay.tex->LockRect(0, &d3dlr, nullptr, D3DLOCK_NOSYSLOCK))) + { + std::memcpy(d3dlr.pBits, image, height * d3dlr.Pitch); + overlay.tex->UnlockRect(0); + } + + overlay_tex_geom(0, 0, 1, 1); // Default. Stretch to whole screen. + overlay_vertex_geom(0, 0, 1, 1); + + return true; +} + +void D3DVideo::overlay_tex_geom(float x, float y, float w, float h) +{ + overlay.tex_coords.x = x; + overlay.tex_coords.y = y; + overlay.tex_coords.w = w; + overlay.tex_coords.h = h; +} + +void D3DVideo::overlay_vertex_geom(float x, float y, float w, float h) +{ + y = 1.0f - y; + h = -h; + overlay.vert_coords.x = x; + overlay.vert_coords.y = y; + overlay.vert_coords.w = w; + overlay.vert_coords.h = h; +} + +void D3DVideo::overlay_enable(bool state) +{ + overlay.overlay_enabled = state; + show_cursor(state); +} + +void D3DVideo::overlay_full_screen(bool enable) +{ + overlay.overlay_fullscreen = enable; +} + +void D3DVideo::overlay_set_alpha(float mod) +{ + overlay.overlay_alpha_mod = mod; +} + +void D3DVideo::overlay_render() +{ + if(!overlay.vert_buf) { + dev->CreateVertexBuffer( + 4 * sizeof(Vertex), + dev->GetSoftwareVertexProcessing() ? D3DUSAGE_SOFTWAREPROCESSING : 0, + 0, + D3DPOOL_MANAGED, + &overlay.vert_buf, + nullptr); + } + + Vertex vert[4]; + for (unsigned i = 0; i < 4; i++) + vert[i].z = 0.5f; + + float overlay_width = final_viewport.Width; + float overlay_height = final_viewport.Height; + + vert[0].x = overlay.vert_coords.x * overlay_width; + vert[1].x = (overlay.vert_coords.x + overlay.vert_coords.w) * overlay_width; + vert[2].x = overlay.vert_coords.x * overlay_width; + vert[3].x = (overlay.vert_coords.x + overlay.vert_coords.w) * overlay_width; + vert[0].y = overlay.vert_coords.y * overlay_height; + vert[1].y = overlay.vert_coords.y * overlay_height; + vert[2].y = (overlay.vert_coords.y + overlay.vert_coords.h) * overlay_height; + vert[3].y = (overlay.vert_coords.y + overlay.vert_coords.h) * overlay_height; + + vert[0].u = overlay.tex_coords.x; + vert[1].u = overlay.tex_coords.x + overlay.tex_coords.w; + vert[2].u = overlay.tex_coords.x; + vert[3].u = overlay.tex_coords.x + overlay.tex_coords.w; + vert[0].v = overlay.tex_coords.y; + vert[1].v = overlay.tex_coords.y; + vert[2].v = overlay.tex_coords.y + overlay.tex_coords.h; + vert[3].v = overlay.tex_coords.y + overlay.tex_coords.h; + + // unnecessary, but just in case + vert[0].lut_u = 0.0f; + vert[1].lut_u = 1.0f; + vert[2].lut_u = 0.0f; + vert[3].lut_u = 1.0f; + vert[0].lut_v = 0.0f; + vert[1].lut_v = 0.0f; + vert[2].lut_v = 1.0f; + vert[3].lut_v = 1.0f; + + // Align texels and vertices. + for (unsigned i = 0; i < 4; i++) + { + vert[i].x -= 0.5f; + vert[i].y += 0.5f; + } + + void *verts; + overlay.vert_buf->Lock(0, sizeof(vert), &verts, 0); + std::memcpy(verts, vert, sizeof(vert)); + overlay.vert_buf->Unlock(); + + dev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); + dev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); + dev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); + + dev->SetStreamSource(0, overlay.vert_buf, 0, sizeof(Vertex)); + dev->SetStreamSource(1, overlay.vert_buf, 0, sizeof(Vertex)); + dev->SetStreamSource(2, overlay.vert_buf, 0, sizeof(Vertex)); + + if(overlay.overlay_fullscreen) + { + // set viewport to full window + D3DVIEWPORT9 vp_full; + vp_full.X = 0; + vp_full.Y = 0; + vp_full.Width = screen_width; + vp_full.Height = screen_height; + vp_full.MinZ = 0.0f; + vp_full.MaxZ = 1.0f; + dev->SetViewport(&vp_full); + + // clear new area + D3DRECT clear_rects[2]; + clear_rects[0].y2 = clear_rects[1].y2 = vp_full.Height; + clear_rects[0].y1 = clear_rects[1].y1 = 0; + clear_rects[0].x1 = 0; + clear_rects[0].x2 = final_viewport.X; + clear_rects[1].x1 = final_viewport.X + final_viewport.Width; + clear_rects[1].x2 = vp_full.Width; + + dev->Clear(2, clear_rects, D3DCLEAR_TARGET, 0, 1, 0); + } + + dev->SetTexture(0, overlay.tex); + dev->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); + dev->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER); + dev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); + dev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); + if (SUCCEEDED(dev->BeginScene())) + { + dev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); + dev->EndScene(); + } + dev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE); + dev->SetViewport(&final_viewport); +} + +#endif + static void *d3d9_init(const video_info_t *info, const input_driver_t **input, void **input_data) { @@ -1242,6 +1427,57 @@ static bool d3d9_set_shader(void *data, enum rarch_shader_type type, const char return reinterpret_cast(data)->set_shader(path); } +#ifdef HAVE_OVERLAY +static bool d3d9_overlay_load(void *data, const uint32_t *image, unsigned width, unsigned height) +{ + return reinterpret_cast(data)->overlay_load(image, width, height); +} + +static void d3d9_overlay_tex_geom(void *data, + float x, float y, + float w, float h) +{ + return reinterpret_cast(data)->overlay_tex_geom(x, y, w, h); +} + +static void d3d9_overlay_vertex_geom(void *data, + float x, float y, + float w, float h) +{ + return reinterpret_cast(data)->overlay_vertex_geom(x, y, w, h); +} + +static void d3d9_overlay_enable(void *data, bool state) +{ + return reinterpret_cast(data)->overlay_enable(state); +} + +static void d3d9_overlay_full_screen(void *data, bool enable) +{ + return reinterpret_cast(data)->overlay_full_screen(enable); +} + +static void d3d9_overlay_set_alpha(void *data, float mod) +{ + return reinterpret_cast(data)->overlay_set_alpha(mod); +} + +static const video_overlay_interface_t d3d9_overlay_interface = { + d3d9_overlay_enable, + d3d9_overlay_load, + d3d9_overlay_tex_geom, + d3d9_overlay_vertex_geom, + d3d9_overlay_full_screen, + d3d9_overlay_set_alpha, +}; + +static void d3d9_get_overlay_interface(void *data, const video_overlay_interface_t **iface) +{ + (void)data; + *iface = &d3d9_overlay_interface; +} +#endif + const video_driver_t video_d3d9 = { d3d9_init, d3d9_frame, @@ -1259,5 +1495,7 @@ const video_driver_t video_d3d9 = { d3d9_set_rotation, d3d9_viewport_info, d3d9_read_viewport, +#ifdef HAVE_OVERLAY + d3d9_get_overlay_interface, +#endif }; - diff --git a/gfx/d3d9/d3d9.hpp b/gfx/d3d9/d3d9.hpp index 8e7d274fad..2c219d42b9 100644 --- a/gfx/d3d9/d3d9.hpp +++ b/gfx/d3d9/d3d9.hpp @@ -52,6 +52,16 @@ class D3DVideo void resize(unsigned new_width, unsigned new_height); bool set_shader(const std::string &path); +#ifdef HAVE_OVERLAY + bool overlay_load(const uint32_t *image, unsigned width, unsigned height); + void overlay_tex_geom(float x, float y, float w, float h); + void overlay_vertex_geom(float x, float y, float w, float h); + void overlay_enable(bool state); + void overlay_full_screen(bool enable); + void overlay_set_alpha(float mod); + void overlay_render(); +#endif + private: WNDCLASSEX windowClass; @@ -102,6 +112,23 @@ class D3DVideo uint32_t font_color; void update_title(); + +#ifdef HAVE_OVERLAY + struct + { + struct Coords + { + float x, y, w, h; + }; + Coords tex_coords; + Coords vert_coords; + bool overlay_enabled; + bool overlay_fullscreen; + float overlay_alpha_mod; + IDirect3DTexture9 *tex; + IDirect3DVertexBuffer9 *vert_buf; + } overlay; +#endif }; #endif From b782ec2d33f802705c27230d2131acf5b15e07ad Mon Sep 17 00:00:00 2001 From: OV2 Date: Wed, 27 Mar 2013 18:19:36 +0100 Subject: [PATCH 02/10] win32: specify vertex declaration for overlay --- gfx/d3d9/d3d9.cpp | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/gfx/d3d9/d3d9.cpp b/gfx/d3d9/d3d9.cpp index f5d3dd7215..4529e3cbb7 100644 --- a/gfx/d3d9/d3d9.cpp +++ b/gfx/d3d9/d3d9.cpp @@ -1236,10 +1236,16 @@ void D3DVideo::overlay_set_alpha(float mod) } void D3DVideo::overlay_render() -{ +{ + struct overlay_vertex + { + float x,y, z; + float u,v; + } vert[4]; + if(!overlay.vert_buf) { dev->CreateVertexBuffer( - 4 * sizeof(Vertex), + sizeof(vert), dev->GetSoftwareVertexProcessing() ? D3DUSAGE_SOFTWAREPROCESSING : 0, 0, D3DPOOL_MANAGED, @@ -1247,7 +1253,6 @@ void D3DVideo::overlay_render() nullptr); } - Vertex vert[4]; for (unsigned i = 0; i < 4; i++) vert[i].z = 0.5f; @@ -1272,16 +1277,6 @@ void D3DVideo::overlay_render() vert[2].v = overlay.tex_coords.y + overlay.tex_coords.h; vert[3].v = overlay.tex_coords.y + overlay.tex_coords.h; - // unnecessary, but just in case - vert[0].lut_u = 0.0f; - vert[1].lut_u = 1.0f; - vert[2].lut_u = 0.0f; - vert[3].lut_u = 1.0f; - vert[0].lut_v = 0.0f; - vert[1].lut_v = 0.0f; - vert[2].lut_v = 1.0f; - vert[3].lut_v = 1.0f; - // Align texels and vertices. for (unsigned i = 0; i < 4; i++) { @@ -1298,9 +1293,17 @@ void D3DVideo::overlay_render() dev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); dev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); - dev->SetStreamSource(0, overlay.vert_buf, 0, sizeof(Vertex)); - dev->SetStreamSource(1, overlay.vert_buf, 0, sizeof(Vertex)); - dev->SetStreamSource(2, overlay.vert_buf, 0, sizeof(Vertex)); + D3DVERTEXELEMENT9 vElems[4] = { + {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, + {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, + D3DDECL_END() + }; + + IDirect3DVertexDeclaration9 * vertex_decl; + dev->CreateVertexDeclaration(vElems, &vertex_decl); + dev->SetVertexDeclaration(vertex_decl); + + dev->SetStreamSource(0, overlay.vert_buf, 0, sizeof(overlay_vertex)); if(overlay.overlay_fullscreen) { @@ -1338,6 +1341,7 @@ void D3DVideo::overlay_render() } dev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE); dev->SetViewport(&final_viewport); + vertex_decl->Release(); } #endif From 66b4b45f5cdc0ac52458daeded5113f4a0858a46 Mon Sep 17 00:00:00 2001 From: OV2 Date: Thu, 28 Mar 2013 14:29:04 +0100 Subject: [PATCH 03/10] win32: add opacity shader to overlay --- gfx/d3d9/d3d9.cpp | 58 +++++++++++++++++++++++++++++++++++++++++------ gfx/d3d9/d3d9.hpp | 2 ++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/gfx/d3d9/d3d9.cpp b/gfx/d3d9/d3d9.cpp index 4529e3cbb7..aaaa421e11 100644 --- a/gfx/d3d9/d3d9.cpp +++ b/gfx/d3d9/d3d9.cpp @@ -496,6 +496,10 @@ D3DVideo::~D3DVideo() overlay.tex->Release(); if(overlay.vert_buf) overlay.vert_buf->Release(); + if(overlay.opacity_shader) + overlay.opacity_shader->Release(); + if(overlay.opacity_shader_table) + overlay.opacity_shader_table->Release(); #endif if (dev) dev->Release(); @@ -1235,12 +1239,21 @@ void D3DVideo::overlay_set_alpha(float mod) overlay.overlay_alpha_mod = mod; } +static const char *opacity_fragment = + "uniform float opacity;\n" + "float4 main_fragment(uniform sampler2D samp, float2 tex : TEXCOORD0) : COLOR\n" + "{\n" + " float4 col = tex2D(samp, tex);\n" + " col.a *= opacity;\n" + " return col;\n" + "}"; + void D3DVideo::overlay_render() { struct overlay_vertex { - float x,y, z; - float u,v; + float x, y, z; + float u, v; } vert[4]; if(!overlay.vert_buf) { @@ -1289,19 +1302,21 @@ void D3DVideo::overlay_render() std::memcpy(verts, vert, sizeof(vert)); overlay.vert_buf->Unlock(); + // enable alpha dev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); dev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); dev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); - D3DVERTEXELEMENT9 vElems[4] = { + // set vertex decl for overlay + D3DVERTEXELEMENT9 vElems[4] = { {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, D3DDECL_END() }; - IDirect3DVertexDeclaration9 * vertex_decl; dev->CreateVertexDeclaration(vElems, &vertex_decl); dev->SetVertexDeclaration(vertex_decl); + vertex_decl->Release(); dev->SetStreamSource(0, overlay.vert_buf, 0, sizeof(overlay_vertex)); @@ -1328,7 +1343,33 @@ void D3DVideo::overlay_render() dev->Clear(2, clear_rects, D3DCLEAR_TARGET, 0, 1, 0); } - + + // custom pixel shader for opacity + IDirect3DPixelShader9 *prev_pixel_shader = NULL; + if(overlay.overlay_alpha_mod != 1.0f) + { + if(!overlay.opacity_shader) + { + LPD3DXBUFFER code; + D3DXCompileShader(opacity_fragment, // source + strlen(opacity_fragment), // len + NULL, // macros + NULL, // includes + "main_fragment", // main function + "ps_2_0", // shader profile + 0, // flags + &code, // compiled operations + NULL, // errors + &overlay.opacity_shader_table); // constants + dev->CreatePixelShader((DWORD*)code->GetBufferPointer(), &overlay.opacity_shader); + code->Release(); + } + overlay.opacity_shader_table->SetFloat(dev, "opacity", overlay.overlay_alpha_mod); + dev->GetPixelShader(&prev_pixel_shader); + dev->SetPixelShader(overlay.opacity_shader); + } + + // render overlay dev->SetTexture(0, overlay.tex); dev->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); dev->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER); @@ -1339,9 +1380,12 @@ void D3DVideo::overlay_render() dev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); dev->EndScene(); } - dev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE); + + //restore previous state + dev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); dev->SetViewport(&final_viewport); - vertex_decl->Release(); + if(prev_pixel_shader) + dev->SetPixelShader(prev_pixel_shader); } #endif diff --git a/gfx/d3d9/d3d9.hpp b/gfx/d3d9/d3d9.hpp index 2c219d42b9..26b5065a1c 100644 --- a/gfx/d3d9/d3d9.hpp +++ b/gfx/d3d9/d3d9.hpp @@ -127,6 +127,8 @@ class D3DVideo float overlay_alpha_mod; IDirect3DTexture9 *tex; IDirect3DVertexBuffer9 *vert_buf; + IDirect3DPixelShader9 *opacity_shader; + ID3DXConstantTable *opacity_shader_table; } overlay; #endif }; From 15f6026c865266db98dea286c281e6009652c088 Mon Sep 17 00:00:00 2001 From: OV2 Date: Thu, 28 Mar 2013 14:55:42 +0100 Subject: [PATCH 04/10] win32: fix compile without HAVE_OVERLAY --- gfx/d3d9/d3d9.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gfx/d3d9/d3d9.cpp b/gfx/d3d9/d3d9.cpp index aaaa421e11..d570e7c953 100644 --- a/gfx/d3d9/d3d9.cpp +++ b/gfx/d3d9/d3d9.cpp @@ -394,7 +394,9 @@ D3DVideo::D3DVideo(const video_info_t *info) : { gfx_set_dwm(); +#ifdef HAVE_OVERLAY std::memset(&overlay,0,sizeof(overlay)); +#endif std::memset(&windowClass, 0, sizeof(windowClass)); windowClass.cbSize = sizeof(windowClass); @@ -574,9 +576,11 @@ bool D3DVideo::frame(const void *frame, dev->EndScene(); } +#ifdef HAVE_OVERLAY if(overlay.overlay_enabled) { overlay_render(); } +#endif RARCH_PERFORMANCE_STOP(d3d_frame); From 2b51d48b2bcddbf98d5c7284414e73765f6f566a Mon Sep 17 00:00:00 2001 From: OV2 Date: Thu, 28 Mar 2013 15:05:12 +0100 Subject: [PATCH 05/10] win32: adjust makefile --- Makefile.win | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Makefile.win b/Makefile.win index 14468dc86d..f113d4f978 100644 --- a/Makefile.win +++ b/Makefile.win @@ -155,8 +155,14 @@ endif ifeq ($(HAVE_ZLIB), 1) OBJ += gfx/rpng/rpng.o file_extract.o - LIBS += -lz - DEFINES += -DHAVE_ZLIB_DEFLATE + DEFINES += -DHAVE_ZLIB + ifeq ($(WANT_MINIZ), 1) + OBJ += deps/miniz/miniz.o + DEFINES += -DWANT_MINIZ + else + LIBS += -lz + DEFINES += -DHAVE_ZLIB_DEFLATE + endif endif ifeq ($(HAVE_LIBXML2), 1) @@ -268,7 +274,9 @@ else endif clean: - rm -f *.o + rm -f *.o + rm -f deps/miniz/*.o + rm -f frontend/*.o rm -f frontend/menu/*.o rm -f frontend/menu/utils/*.o rm -f audio/*.o @@ -283,6 +291,7 @@ clean: rm -f gfx/math/*.o rm -f gfx/fonts/*.o rm -f gfx/py_state/*.o + rm -f gfx/rpng/*.o rm -f record/*.o rm -f input/*.o rm -f $(TARGET) From f250cc14ab8966faab101cda86301d3c8341d42f Mon Sep 17 00:00:00 2001 From: OV2 Date: Thu, 28 Mar 2013 15:33:15 +0100 Subject: [PATCH 06/10] style --- gfx/d3d9/d3d9.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gfx/d3d9/d3d9.cpp b/gfx/d3d9/d3d9.cpp index d570e7c953..1a63f18ffd 100644 --- a/gfx/d3d9/d3d9.cpp +++ b/gfx/d3d9/d3d9.cpp @@ -577,7 +577,8 @@ bool D3DVideo::frame(const void *frame, } #ifdef HAVE_OVERLAY - if(overlay.overlay_enabled) { + if(overlay.overlay_enabled) + { overlay_render(); } #endif @@ -1260,7 +1261,8 @@ void D3DVideo::overlay_render() float u, v; } vert[4]; - if(!overlay.vert_buf) { + if(!overlay.vert_buf) + { dev->CreateVertexBuffer( sizeof(vert), dev->GetSoftwareVertexProcessing() ? D3DUSAGE_SOFTWAREPROCESSING : 0, From a00858a6bec06013e159c8fb0858609c9e47dbb9 Mon Sep 17 00:00:00 2001 From: OV2 Date: Fri, 29 Mar 2013 11:46:56 +0100 Subject: [PATCH 07/10] win32: use color in d3d stock shader, pass valid color values --- gfx/d3d9/render_chain.cpp | 37 +++++++++++++++++++++++++------------ gfx/d3d9/render_chain.hpp | 1 + 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/gfx/d3d9/render_chain.cpp b/gfx/d3d9/render_chain.cpp index d8bd7aef71..79f7df8d60 100644 --- a/gfx/d3d9/render_chain.cpp +++ b/gfx/d3d9/render_chain.cpp @@ -24,17 +24,27 @@ namespace Global { static const char *stock_program = - "void main_vertex(out float4 oPos : POSITION, float4 pos : POSITION,\n" - " out float2 oTex : TEXCOORD0, float2 tex : TEXCOORD0,\n" - " uniform float4x4 modelViewProj)\n" - "{\n" - " oPos = mul(modelViewProj, pos);\n" - " oTex = tex;\n" - "}\n" - - "float4 main_fragment(uniform sampler2D samp, float2 tex : TEXCOORD0) : COLOR\n" - "{\n" - " return tex2D(samp, tex);\n" + "void main_vertex" + "(" + " float4 position : POSITION," + " float2 texCoord : TEXCOORD0," + " float4 color : COLOR," + "" + " uniform float4x4 modelViewProj," + "" + " out float4 oPosition : POSITION," + " out float2 otexCoord : TEXCOORD0," + " out float4 oColor : COLOR" + ")" + "{" + " oPosition = mul(modelViewProj, position);" + " otexCoord = texCoord;" + " oColor = color;" + "}" + "" + "float4 main_fragment(in float4 color : COLOR, float2 tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0) : COLOR" + "{" + " return color * tex2D(s0, tex);" "}"; } @@ -382,7 +392,10 @@ void RenderChain::set_vertices(Pass &pass, float _v = static_cast(height) / info.tex_h; Vertex vert[4]; for (unsigned i = 0; i < 4; i++) + { vert[i].z = 0.5f; + vert[i].r = vert[i].g = vert[i].b = vert[i].a = 1.0f; + } vert[0].x = 0.0f; vert[1].x = out_width; @@ -909,7 +922,7 @@ void RenderChain::init_fvf(Pass &pass) static const D3DVERTEXELEMENT9 position_decl = DECL_FVF_POSITION(0); static const D3DVERTEXELEMENT9 tex_coord0 = DECL_FVF_TEXCOORD(1, 3, 0); static const D3DVERTEXELEMENT9 tex_coord1 = DECL_FVF_TEXCOORD(2, 5, 1); - static const D3DVERTEXELEMENT9 color = DECL_FVF_COLOR(3, 3, 0); + static const D3DVERTEXELEMENT9 color = DECL_FVF_COLOR(3, 7, 0); D3DVERTEXELEMENT9 decl[MAXD3DDECLLENGTH] = {{0}}; if (cgD3D9GetVertexDeclaration(pass.vPrg, decl) == CG_FALSE) diff --git a/gfx/d3d9/render_chain.hpp b/gfx/d3d9/render_chain.hpp index 51231a126d..5ad9e7cc7a 100644 --- a/gfx/d3d9/render_chain.hpp +++ b/gfx/d3d9/render_chain.hpp @@ -27,6 +27,7 @@ struct Vertex float x, y, z; float u, v; float lut_u, lut_v; + float r, g, b, a; }; struct LinkInfo From 34b2a3b21006e7673eb938f6a4862935abc85827 Mon Sep 17 00:00:00 2001 From: OV2 Date: Fri, 29 Mar 2013 11:48:33 +0100 Subject: [PATCH 08/10] win32: use stock shader and color for overlay opacity --- gfx/d3d9/d3d9.cpp | 37 ++++++------------------------------- gfx/d3d9/d3d9.hpp | 2 -- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/gfx/d3d9/d3d9.cpp b/gfx/d3d9/d3d9.cpp index 1a63f18ffd..d6a434ded1 100644 --- a/gfx/d3d9/d3d9.cpp +++ b/gfx/d3d9/d3d9.cpp @@ -498,10 +498,6 @@ D3DVideo::~D3DVideo() overlay.tex->Release(); if(overlay.vert_buf) overlay.vert_buf->Release(); - if(overlay.opacity_shader) - overlay.opacity_shader->Release(); - if(overlay.opacity_shader_table) - overlay.opacity_shader_table->Release(); #endif if (dev) dev->Release(); @@ -1259,6 +1255,7 @@ void D3DVideo::overlay_render() { float x, y, z; float u, v; + float r, g, b, a; } vert[4]; if(!overlay.vert_buf) @@ -1273,7 +1270,11 @@ void D3DVideo::overlay_render() } for (unsigned i = 0; i < 4; i++) + { vert[i].z = 0.5f; + vert[i].r = vert[i].g = vert[i].b = 1.0f; + vert[i].a = overlay.overlay_alpha_mod; + } float overlay_width = final_viewport.Width; float overlay_height = final_viewport.Height; @@ -1317,6 +1318,7 @@ void D3DVideo::overlay_render() D3DVERTEXELEMENT9 vElems[4] = { {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, + {0, 20, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0}, D3DDECL_END() }; IDirect3DVertexDeclaration9 * vertex_decl; @@ -1350,31 +1352,6 @@ void D3DVideo::overlay_render() dev->Clear(2, clear_rects, D3DCLEAR_TARGET, 0, 1, 0); } - // custom pixel shader for opacity - IDirect3DPixelShader9 *prev_pixel_shader = NULL; - if(overlay.overlay_alpha_mod != 1.0f) - { - if(!overlay.opacity_shader) - { - LPD3DXBUFFER code; - D3DXCompileShader(opacity_fragment, // source - strlen(opacity_fragment), // len - NULL, // macros - NULL, // includes - "main_fragment", // main function - "ps_2_0", // shader profile - 0, // flags - &code, // compiled operations - NULL, // errors - &overlay.opacity_shader_table); // constants - dev->CreatePixelShader((DWORD*)code->GetBufferPointer(), &overlay.opacity_shader); - code->Release(); - } - overlay.opacity_shader_table->SetFloat(dev, "opacity", overlay.overlay_alpha_mod); - dev->GetPixelShader(&prev_pixel_shader); - dev->SetPixelShader(overlay.opacity_shader); - } - // render overlay dev->SetTexture(0, overlay.tex); dev->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); @@ -1390,8 +1367,6 @@ void D3DVideo::overlay_render() //restore previous state dev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); dev->SetViewport(&final_viewport); - if(prev_pixel_shader) - dev->SetPixelShader(prev_pixel_shader); } #endif diff --git a/gfx/d3d9/d3d9.hpp b/gfx/d3d9/d3d9.hpp index 26b5065a1c..2c219d42b9 100644 --- a/gfx/d3d9/d3d9.hpp +++ b/gfx/d3d9/d3d9.hpp @@ -127,8 +127,6 @@ class D3DVideo float overlay_alpha_mod; IDirect3DTexture9 *tex; IDirect3DVertexBuffer9 *vert_buf; - IDirect3DPixelShader9 *opacity_shader; - ID3DXConstantTable *opacity_shader_table; } overlay; #endif }; From 29fa71b664bf629dbbe623162a1b9f426a306fd1 Mon Sep 17 00:00:00 2001 From: OV2 Date: Fri, 29 Mar 2013 11:50:05 +0100 Subject: [PATCH 09/10] style --- gfx/d3d9/d3d9.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gfx/d3d9/d3d9.cpp b/gfx/d3d9/d3d9.cpp index d6a434ded1..574155552e 100644 --- a/gfx/d3d9/d3d9.cpp +++ b/gfx/d3d9/d3d9.cpp @@ -494,9 +494,9 @@ D3DVideo::~D3DVideo() { deinit(); #ifdef HAVE_OVERLAY - if(overlay.tex) + if (overlay.tex) overlay.tex->Release(); - if(overlay.vert_buf) + if (overlay.vert_buf) overlay.vert_buf->Release(); #endif if (dev) @@ -573,7 +573,7 @@ bool D3DVideo::frame(const void *frame, } #ifdef HAVE_OVERLAY - if(overlay.overlay_enabled) + if (overlay.overlay_enabled) { overlay_render(); } @@ -1181,7 +1181,7 @@ void D3DVideo::resize(unsigned new_width, unsigned new_height) #ifdef HAVE_OVERLAY bool D3DVideo::overlay_load(const uint32_t *image, unsigned width, unsigned height) { - if(overlay.tex) + if (overlay.tex) overlay.tex->Release(); if (FAILED(dev->CreateTexture(width, height, 1, 0, @@ -1258,7 +1258,7 @@ void D3DVideo::overlay_render() float r, g, b, a; } vert[4]; - if(!overlay.vert_buf) + if (!overlay.vert_buf) { dev->CreateVertexBuffer( sizeof(vert), @@ -1328,7 +1328,7 @@ void D3DVideo::overlay_render() dev->SetStreamSource(0, overlay.vert_buf, 0, sizeof(overlay_vertex)); - if(overlay.overlay_fullscreen) + if (overlay.overlay_fullscreen) { // set viewport to full window D3DVIEWPORT9 vp_full; From 88d457baab96241f80dc623ef957a045b070580d Mon Sep 17 00:00:00 2001 From: OV2 Date: Fri, 29 Mar 2013 15:36:48 +0100 Subject: [PATCH 10/10] win32: style, remove left over opacity shader --- gfx/d3d9/d3d9.cpp | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/gfx/d3d9/d3d9.cpp b/gfx/d3d9/d3d9.cpp index 574155552e..7744977998 100644 --- a/gfx/d3d9/d3d9.cpp +++ b/gfx/d3d9/d3d9.cpp @@ -395,7 +395,7 @@ D3DVideo::D3DVideo(const video_info_t *info) : gfx_set_dwm(); #ifdef HAVE_OVERLAY - std::memset(&overlay,0,sizeof(overlay)); + std::memset(&overlay, 0, sizeof(overlay)); #endif std::memset(&windowClass, 0, sizeof(windowClass)); @@ -574,9 +574,7 @@ bool D3DVideo::frame(const void *frame, #ifdef HAVE_OVERLAY if (overlay.overlay_enabled) - { overlay_render(); - } #endif RARCH_PERFORMANCE_STOP(d3d_frame); @@ -1240,15 +1238,6 @@ void D3DVideo::overlay_set_alpha(float mod) overlay.overlay_alpha_mod = mod; } -static const char *opacity_fragment = - "uniform float opacity;\n" - "float4 main_fragment(uniform sampler2D samp, float2 tex : TEXCOORD0) : COLOR\n" - "{\n" - " float4 col = tex2D(samp, tex);\n" - " col.a *= opacity;\n" - " return col;\n" - "}"; - void D3DVideo::overlay_render() { struct overlay_vertex