DXGL r911 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r910‎ | r911 | r912 >
Date:00:26, 11 April 2019
Author:admin
Status:new
Tags:
Comment:
Refactor YUV conversion to use ffmpeg-opengl GLSL algorithm.
Don't apply YUV-to-RGB conversion if target is YUV.
Use separate Blt type for Y-only surfaces, separate from Luminance.
Modified paths:
  • /ThirdParty.txt (modified) (history)
  • /ddraw/ShaderGen2D.cpp (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/glTexture.cpp (modified) (history)

Diff [purge]

Index: ThirdParty.txt
@@ -220,5 +220,37 @@
221221
222222 ------------------------------------------------------------------------------
223223
 224+Contains some GLSL code derived from Nervous Systems ffmpeg-opengl YUV feature,
 225+original code from:
 226+https://github.com/nervous-systems/ffmpeg-opengl/blob/feature/yuv/vf_genericshader.c
 227+
 228+Released under the Unlicense:
 229+This is free and unencumbered software released into the public domain.
 230+
 231+Anyone is free to copy, modify, publish, use, compile, sell, or
 232+distribute this software, either in source code form or as a compiled
 233+binary, for any purpose, commercial or non-commercial, and by any
 234+means.
 235+
 236+In jurisdictions that recognize copyright laws, the author or authors
 237+of this software dedicate any and all copyright interest in the
 238+software to the public domain. We make this dedication for the benefit
 239+of the public at large and to the detriment of our heirs and
 240+successors. We intend this dedication to be an overt act of
 241+relinquishment in perpetuity of all present and future rights to this
 242+software under copyright law.
 243+
 244+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 245+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 246+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 247+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 248+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 249+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 250+OTHER DEALINGS IN THE SOFTWARE.
 251+
 252+For more information, please refer to <http://unlicense.org/>
 253+
 254+------------------------------------------------------------------------------
 255+
224256 Contains code contributed from the following:
225257 Syahmi Azhar (mouse cursor hacks)
\ No newline at end of file
Index: ddraw/ShaderGen2D.cpp
@@ -14,6 +14,14 @@
1515 // You should have received a copy of the GNU Lesser General Public
1616 // License along with this library; if not, write to the Free Software
1717 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 18+
 19+// Contains some GLSL code derived from Nervous Systems ffmpeg-opengl YUV feature,
 20+// original code from:
 21+// https://github.com/nervous-systems/ffmpeg-opengl/blob/feature/yuv/vf_genericshader.c
 22+// Original code was released under the Unlicense and thus dedicated to the public
 23+// domain, as evidenced by the following repository license:
 24+// https://github.com/nervous-systems/ffmpeg-opengl/blob/feature/yuv/LICENSE
 25+
1826 #include "common.h"
1927 #include "string.h"
2028 #include "glExtensions.h"
@@ -60,8 +68,9 @@
6169 AND the dwFlags by 0xF2FAADFF before packing ROP index bits
6270
6371 Texture types:
64 -0x00: Classic DXGL processing
 72+0x00: Standard RGBA
6573 0x01: Luminance-only (write to red)
 74+0x02: Luminance-only (Calculate Y on Blt)
6675 0x10: 8-bit palette
6776 0x11: 4-bit palette
6877 0x12: 2-bit palette
@@ -92,6 +101,13 @@
93102 static const char linefeed[] = "\n";
94103 static const char mainstart[] = "void main()\n{\n";
95104 static const char mainend[] = "} ";
 105+
 106+// Constants
 107+static const char const_bt601_coeff[] =
 108+"const mat3 bt601_coeff = mat3(1.164,1.164,1.164,0.0,-0.392,2.017,1.596,-0.813,0.0);\n\
 109+const vec3 yuv_offsets = vec3(-0.0625, -0.5, -0.5);\n";
 110+
 111+
96112 // Attributes
97113 static const char attr_xy[] = "attribute vec2 xy;\n";
98114 static const char attr_rgb[] = "attribute vec3 rgb;\n";
@@ -171,15 +187,7 @@
172188 static const char func_yuvatorgba[] =
173189 "vec4 yuvatorgba(vec4 yuva)\n\
174190 {\n\
175 - vec4 rgba;\n\
176 - yuva.r = 1.1643 * (yuva.r - 0.0625);\n\
177 - yuva.g = yuva.g - 0.5;\n\
178 - yuva.b = yuva.b - 0.5;\n\
179 - rgba.r = yuva.r + (1.5958 * yuva.b);\n\
180 - rgba.g = yuva.r - (0.39173 * yuva.g) - (0.8129 * yuva.b);\n\
181 - rgba.b = yuva.r + (2.017 * yuva.g);\n\
182 - rgba.a = yuva.a;\n\
183 - return rgba;\n\
 191+ return vec4(vec3(bt601_coeff * (yuva.rgb + yuv_offsets)),yuva.a);\n\
184192 }\n\n";
185193 static const char func_readrgbg[] = "";
186194 static const char func_readgrgb[] = "";
@@ -877,6 +885,21 @@
878886 String_Append(fsrc, idheader);
879887 String_Append(fsrc, idstring);
880888
 889+ // Constants
 890+ switch (srctype)
 891+ {
 892+ case 0:
 893+ default:
 894+ break;
 895+ case 0x80:
 896+ case 0x81:
 897+ case 0x82:
 898+ case 0x83:
 899+ if ((desttype >= 0x80) && (desttype <= 0x83)) break;
 900+ String_Append(fsrc, const_bt601_coeff);
 901+ break;
 902+ }
 903+
881904 // Uniforms
882905 if (id & DDBLT_COLORFILL) String_Append(fsrc, unif_fillcolor);
883906 else
@@ -979,6 +1002,7 @@
9801003 String_Append(fsrc, op_pixel);
9811004 break;
9821005 case 0x01:
 1006+ case 0x02:
9831007 String_Append(fsrc, op_lumpixel);
9841008 break;
9851009 case 0x10:
@@ -1028,10 +1052,12 @@
10291053 String_Append(fsrc, op_destoutdestblend);
10301054 else
10311055 {
1032 - switch (srctype2)
 1056+ switch (srctype)
10331057 {
10341058 case 0x83:
1035 - String_Append(fsrc, op_destoutyuvrgb);
 1059+ if ((desttype >= 0x80) && (desttype <= 0x83))
 1060+ String_Append(fsrc, op_destout);
 1061+ else String_Append(fsrc, op_destoutyuvrgb);
10361062 break;
10371063 default:
10381064 String_Append(fsrc, op_destout);
Index: ddraw/glRenderer.cpp
@@ -3380,7 +3380,7 @@
33813381 }
33823382 else shaderid = cmd->flags & 0xF2FAADFF;
33833383 if (cmd->src) shaderid |= ((long long)cmd->src->blttype << 32);
3384 - //TODO: Add src/dest texture types
 3384+ if (cmd->dest) shaderid |= ((long long)cmd->dest->blttype << 40);
33853385 if (cmd->flags & DDBLT_KEYDEST) usedest = TRUE;
33863386 if (IsAlphaCKey())
33873387 {
Index: ddraw/glTexture.cpp
@@ -1196,11 +1196,37 @@
11971197 This->colorbits[3] = 8;
11981198 This->packsize = 1;
11991199 break;
 1200+ case DXGLPIXELFORMAT_LUM8: // 8-bit Luminance
 1201+ This->blttype = 0x01;
 1202+ if (This->renderer->ext->glver_major >= 3 && !(This->levels[0].ddsd.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
 1203+ {
 1204+ This->internalformats[0] = GL_R8;
 1205+ This->format = GL_RED;
 1206+ }
 1207+ else
 1208+ {
 1209+ This->internalformats[0] = GL_LUMINANCE8;
 1210+ This->format = GL_LUMINANCE;
 1211+ }
 1212+ This->internalformats[1] = GL_RGB8;
 1213+ This->internalformats[2] = GL_RGBA8;
 1214+ This->type = GL_UNSIGNED_BYTE;
 1215+ if (!This->target) This->target = GL_TEXTURE_2D;
 1216+ This->colororder = 5;
 1217+ This->colorsizes[0] = 255;
 1218+ This->colorsizes[1] = 255;
 1219+ This->colorsizes[2] = 255;
 1220+ This->colorsizes[3] = 255;
 1221+ This->colorbits[0] = 8;
 1222+ This->colorbits[1] = 0;
 1223+ This->colorbits[2] = 0;
 1224+ This->colorbits[3] = 0;
 1225+ This->packsize = 1;
 1226+ break;
12001227 case DXGLPIXELFORMAT_FOURCC_Y8: // 8-bit Y-only
12011228 case DXGLPIXELFORMAT_FOURCC_Y800:
12021229 case DXGLPIXELFORMAT_FOURCC_GREY:
1203 - case DXGLPIXELFORMAT_LUM8: // 8-bit Luminance
1204 - This->blttype = 0x01;
 1230+ This->blttype = 0x02;
12051231 if (This->renderer->ext->glver_major >= 3 && !(This->levels[0].ddsd.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
12061232 {
12071233 This->internalformats[0] = GL_R8;
@@ -1379,7 +1405,7 @@
13801406 This->packsize = 1;
13811407 break;
13821408 case DXGLPIXELFORMAT_FOURCC_Y16:
1383 - This->blttype = 0x01;
 1409+ This->blttype = 0x02;
13841410 if (This->renderer->ext->glver_major >= 3 && !(This->levels[0].ddsd.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
13851411 {
13861412 This->internalformats[0] = GL_R16;