[vlc-commits] d3d_shaders: merge the 2 constant buffers into one

Steve Lhomme git at videolan.org
Wed Feb 10 11:07:03 UTC 2021


vlc | branch: master | Steve Lhomme <robux4 at ycbcr.xyz> | Tue Feb  9 08:01:25 2021 +0100| [8d29562f724990fb55020b66969af4232840911c] | committer: Steve Lhomme

d3d_shaders: merge the 2 constant buffers into one

In D3D12 the proper alignment is 256 bytes so we just use that. Uploading 25 bytes instead
of 8/200 will not have a significant impact.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=8d29562f724990fb55020b66969af4232840911c
---

 modules/video_output/win32/d3d11_quad.c    | 33 ++++++------------------------
 modules/video_output/win32/d3d11_quad.h    |  3 +--
 modules/video_output/win32/d3d11_shaders.h |  3 +--
 modules/video_output/win32/d3d_shaders.c   | 27 +++++++++++-------------
 modules/video_output/win32/d3d_shaders.h   | 13 ++++--------
 5 files changed, 24 insertions(+), 55 deletions(-)

diff --git a/modules/video_output/win32/d3d11_quad.c b/modules/video_output/win32/d3d11_quad.c
index 40cc1af11d..8633290c60 100644
--- a/modules/video_output/win32/d3d11_quad.c
+++ b/modules/video_output/win32/d3d11_quad.c
@@ -60,7 +60,7 @@ void D3D11_RenderQuad(d3d11_device_t *d3d_dev, d3d11_quad_t *quad, d3d11_vertex_
         ID3D11DeviceContext_PSSetSamplers(d3d_dev->d3dcontext, 0, 2, quad->SamplerStates);
 
     /* pixel shader */
-    ID3D11DeviceContext_PSSetConstantBuffers(d3d_dev->d3dcontext, 0, ARRAY_SIZE(quad->pPixelShaderConstants), quad->pPixelShaderConstants);
+    ID3D11DeviceContext_PSSetConstantBuffers(d3d_dev->d3dcontext, 0, 1, &quad->pPixelShaderConstants);
     assert(quad->resourceCount <= DXGI_MAX_SHADER_VIEW);
 
     ID3D11DeviceContext_PSSetShaderResources(d3d_dev->d3dcontext, 0, quad->resourceCount, resourceView);
@@ -137,15 +137,10 @@ fail:
 
 void D3D11_ReleaseQuad(d3d11_quad_t *quad)
 {
-    if (quad->pPixelShaderConstants[PS_CONST_LUMI_BOUNDS])
+    if (quad->pPixelShaderConstants)
     {
-        ID3D11Buffer_Release(quad->pPixelShaderConstants[PS_CONST_LUMI_BOUNDS]);
-        quad->pPixelShaderConstants[PS_CONST_LUMI_BOUNDS] = NULL;
-    }
-    if (quad->pPixelShaderConstants[PS_CONST_COLORSPACE])
-    {
-        ID3D11Buffer_Release(quad->pPixelShaderConstants[PS_CONST_COLORSPACE]);
-        quad->pPixelShaderConstants[PS_CONST_COLORSPACE] = NULL;
+        ID3D11Buffer_Release(quad->pPixelShaderConstants);
+        quad->pPixelShaderConstants = NULL;
     }
     if (quad->pVertexBuffer)
     {
@@ -216,10 +211,7 @@ static bool ShaderUpdateConstants(vlc_object_t *o, d3d11_device_t *d3d_dev, d3d1
     switch (type)
     {
         case PS_CONST_LUMI_BOUNDS:
-            res = (ID3D11Resource *)quad->pPixelShaderConstants[PS_CONST_LUMI_BOUNDS];
-            break;
-        case PS_CONST_COLORSPACE:
-            res = (ID3D11Resource *)quad->pPixelShaderConstants[PS_CONST_COLORSPACE];
+            res = (ID3D11Resource *)quad->pPixelShaderConstants;
             break;
         case VS_CONST_VIEWPOINT:
             res = (ID3D11Resource *)quad->viewpointShaderConstant;
@@ -239,9 +231,6 @@ static bool ShaderUpdateConstants(vlc_object_t *o, d3d11_device_t *d3d_dev, d3d1
         case PS_CONST_LUMI_BOUNDS:
             memcpy(mappedResource.pData, new_buf, sizeof(PS_CONSTANT_BUFFER));
             break;
-        case PS_CONST_COLORSPACE:
-            memcpy(mappedResource.pData, new_buf, sizeof(PS_COLOR_TRANSFORM));
-            break;
         case VS_CONST_VIEWPOINT:
             memcpy(mappedResource.pData, new_buf, sizeof(VS_PROJECTION_CONST));
             break;
@@ -286,7 +275,6 @@ int D3D11_AllocateQuad(vlc_object_t *o, d3d11_device_t *d3d_dev,
                        video_projection_mode_t projection, d3d11_quad_t *quad)
 {
     quad->generic.vertexConstants = &quad->vConstants;
-    quad->generic.colorsConstants = &quad->cConstants;
     quad->generic.shaderConstants = &quad->pConstants;
 
     HRESULT hr;
@@ -297,20 +285,12 @@ int D3D11_AllocateQuad(vlc_object_t *o, d3d11_device_t *d3d_dev,
         .BindFlags = D3D11_BIND_CONSTANT_BUFFER,
         .CPUAccessFlags = D3D11_CPU_ACCESS_WRITE,
     };
-    hr = ID3D11Device_CreateBuffer(d3d_dev->d3ddevice, &constantDesc, NULL, &quad->pPixelShaderConstants[PS_CONST_LUMI_BOUNDS]);
+    hr = ID3D11Device_CreateBuffer(d3d_dev->d3ddevice, &constantDesc, NULL, &quad->pPixelShaderConstants);
     if(FAILED(hr)) {
         msg_Err(o, "Could not create the pixel shader constant buffer. (hr=0x%lX)", hr);
         goto error;
     }
 
-    static_assert((sizeof(PS_COLOR_TRANSFORM)%16)==0,"Constant buffers require 16-byte alignment");
-    constantDesc.ByteWidth = sizeof(PS_COLOR_TRANSFORM);
-    hr = ID3D11Device_CreateBuffer(d3d_dev->d3ddevice, &constantDesc, NULL, &quad->pPixelShaderConstants[PS_CONST_COLORSPACE]);
-    if(FAILED(hr)) {
-        msg_Err(o, "Could not create the pixel shader colorspace buffer. (hr=0x%lX)", hr);
-        goto error;
-    }
-
     if (projection == PROJECTION_MODE_EQUIRECTANGULAR || projection == PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD)
     {
         static_assert((sizeof(VS_PROJECTION_CONST)%16)==0,"Constant buffers require 16-byte alignment");
@@ -339,7 +319,6 @@ int D3D11_SetupQuad(vlc_object_t *o, d3d11_device_t *d3d_dev, const video_format
     D3D_SetupQuad(o, fmt, &quad->generic, displayFormat);
 
     ShaderUpdateConstants(o, d3d_dev, quad, PS_CONST_LUMI_BOUNDS, quad->generic.shaderConstants);
-    ShaderUpdateConstants(o, d3d_dev, quad, PS_CONST_COLORSPACE, quad->generic.colorsConstants);
 
     for (size_t i=0; i<ARRAY_SIZE(quad->cropViewport); i++)
     {
diff --git a/modules/video_output/win32/d3d11_quad.h b/modules/video_output/win32/d3d11_quad.h
index b25ded57ad..01ffca82fd 100644
--- a/modules/video_output/win32/d3d11_quad.h
+++ b/modules/video_output/win32/d3d11_quad.h
@@ -27,8 +27,7 @@
 #include "d3d11_shaders.h"
 
 #define PS_CONST_LUMI_BOUNDS 0
-#define PS_CONST_COLORSPACE  1
-#define VS_CONST_VIEWPOINT   2
+#define VS_CONST_VIEWPOINT   1
 
 typedef bool (*d3d11_select_plane_t)(void *opaque, size_t plane_index);
 
diff --git a/modules/video_output/win32/d3d11_shaders.h b/modules/video_output/win32/d3d11_shaders.h
index f076d949aa..e0a716a2a6 100644
--- a/modules/video_output/win32/d3d11_shaders.h
+++ b/modules/video_output/win32/d3d11_shaders.h
@@ -42,14 +42,13 @@ typedef struct
     ID3D11Buffer              *pVertexBuffer;
     ID3D11Buffer              *pIndexBuffer;
     ID3D11Buffer              *viewpointShaderConstant;
-    ID3D11Buffer              *pPixelShaderConstants[2];
+    ID3D11Buffer              *pPixelShaderConstants;
     UINT                       PSConstantsCount;
     ID3D11PixelShader         *d3dpixelShader[DXGI_MAX_RENDER_TARGET];
     ID3D11SamplerState        *SamplerStates[2];
     D3D11_VIEWPORT            cropViewport[DXGI_MAX_RENDER_TARGET];
 
     PS_CONSTANT_BUFFER        pConstants;
-    PS_COLOR_TRANSFORM        cConstants;
     VS_PROJECTION_CONST       vConstants;
 } d3d11_quad_t;
 
diff --git a/modules/video_output/win32/d3d_shaders.c b/modules/video_output/win32/d3d_shaders.c
index be4674a1e5..0830528d9b 100644
--- a/modules/video_output/win32/d3d_shaders.c
+++ b/modules/video_output/win32/d3d_shaders.c
@@ -51,17 +51,14 @@ const float ST2084_c3 = (2392.0 / 4096.0) * 32.0;\n"
 static const char* globPixelShaderDefault = "\
   cbuffer PS_CONSTANT_BUFFER : register(b0)\n\
   {\n\
+    float4x4 WhitePoint;\n\
+    float4x4 Colorspace;\n\
+    float4x4 Primaries;\n\
     float Opacity;\n\
     float LuminanceScale;\n\
     float BoundaryX;\n\
     float BoundaryY;\n\
   };\n\
-  cbuffer PS_COLOR_TRANSFORM : register(b1)\n\
-  {\n\
-    float4x4 WhitePoint;\n\
-    float4x4 Colorspace;\n\
-    float4x4 Primaries;\n\
-  };\n\
   Texture2D%s shaderTexture[4];\n\
   SamplerState normalSampler : register(s0);\n\
   SamplerState borderSampler : register(s1);\n\
@@ -1057,7 +1054,7 @@ void D3D_SetupQuad(vlc_object_t *o, const video_format_t *fmt, d3d_quad_t *quad,
                        0.f,              0.f,              0.f, 1.f,
     };
 
-    memcpy(quad->colorsConstants->WhitePoint, IDENTITY_4X4, sizeof(quad->colorsConstants->WhitePoint));
+    memcpy(quad->shaderConstants->WhitePoint, IDENTITY_4X4, sizeof(quad->shaderConstants->WhitePoint));
 
     const FLOAT *ppColorspace;
     if (RGB_src_shader == DxgiIsRGBFormat(displayFormat->pixelFormat))
@@ -1067,9 +1064,9 @@ void D3D_SetupQuad(vlc_object_t *o, const video_format_t *fmt, d3d_quad_t *quad,
     else if (RGB_src_shader)
     {
         ppColorspace = COLORSPACE_FULL_RGBA_TO_BT601_YUV;
-        quad->colorsConstants->WhitePoint[0*4 + 3] = -itu_black_level;
-        quad->colorsConstants->WhitePoint[1*4 + 3] = itu_achromacy;
-        quad->colorsConstants->WhitePoint[2*4 + 3] = itu_achromacy;
+        quad->shaderConstants->WhitePoint[0*4 + 3] = -itu_black_level;
+        quad->shaderConstants->WhitePoint[1*4 + 3] = itu_achromacy;
+        quad->shaderConstants->WhitePoint[2*4 + 3] = itu_achromacy;
     }
     else
     {
@@ -1096,16 +1093,16 @@ void D3D_SetupQuad(vlc_object_t *o, const video_format_t *fmt, d3d_quad_t *quad,
                 break;
         }
         /* all matrices work in studio range and output in full range */
-        quad->colorsConstants->WhitePoint[0*4 + 3] = -itu_black_level;
-        quad->colorsConstants->WhitePoint[1*4 + 3] = -itu_achromacy;
-        quad->colorsConstants->WhitePoint[2*4 + 3] = -itu_achromacy;
+        quad->shaderConstants->WhitePoint[0*4 + 3] = -itu_black_level;
+        quad->shaderConstants->WhitePoint[1*4 + 3] = -itu_achromacy;
+        quad->shaderConstants->WhitePoint[2*4 + 3] = -itu_achromacy;
     }
 
-    memcpy(quad->colorsConstants->Colorspace, ppColorspace, sizeof(quad->colorsConstants->Colorspace));
+    memcpy(quad->shaderConstants->Colorspace, ppColorspace, sizeof(quad->shaderConstants->Colorspace));
 
     if (fmt->primaries != displayFormat->primaries)
     {
-        GetPrimariesTransform(quad->colorsConstants->Primaries, fmt->primaries,
+        GetPrimariesTransform(quad->shaderConstants->Primaries, fmt->primaries,
                               displayFormat->primaries);
     }
 }
diff --git a/modules/video_output/win32/d3d_shaders.h b/modules/video_output/win32/d3d_shaders.h
index 99cf19704c..c660476346 100644
--- a/modules/video_output/win32/d3d_shaders.h
+++ b/modules/video_output/win32/d3d_shaders.h
@@ -45,20 +45,16 @@ typedef struct {
 
 /* structures passed to the pixel shader */
 typedef struct {
+    FLOAT WhitePoint[4*4];
+    FLOAT Colorspace[4*4];
+    FLOAT Primaries[4*4];
     FLOAT Opacity;
     FLOAT LuminanceScale;
     FLOAT BoundaryX;
     FLOAT BoundaryY;
-    FLOAT padding[60]; // 256 bytes alignment
+    FLOAT padding[12]; // 256 bytes alignment
 } PS_CONSTANT_BUFFER;
 
-typedef struct {
-    FLOAT WhitePoint[4*4];
-    FLOAT Colorspace[4*4];
-    FLOAT Primaries[4*4];
-    FLOAT padding[16]; // 256 bytes alignment
-} PS_COLOR_TRANSFORM;
-
 typedef struct {
     FLOAT View[4*4];
     FLOAT Zoom[4*4];
@@ -100,7 +96,6 @@ typedef struct
     unsigned int              i_height;
 
     PS_CONSTANT_BUFFER        *shaderConstants;
-    PS_COLOR_TRANSFORM        *colorsConstants;
     VS_PROJECTION_CONST       *vertexConstants;
 
 } d3d_quad_t;



More information about the vlc-commits mailing list