DXGL r894 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r893‎ | r894 | r895 >
Date:17:17, 3 March 2019
Author:admin
Status:new
Tags:
Comment:
Support Blt from palette surface formats to RGB.
Fix default palettes in DXGL Test.
Modified paths:
  • /ddraw/ShaderGen2D.cpp (modified) (history)
  • /ddraw/ShaderGen3D.cpp (modified) (history)
  • /ddraw/glDirectDrawPalette.c (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/glTexture.cpp (modified) (history)
  • /ddraw/struct.h (modified) (history)
  • /dxglcfg/palette.cpp (modified) (history)
  • /dxglcfg/tests.cpp (modified) (history)

Diff [purge]

Index: ddraw/ShaderGen2D.cpp
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2013-2018 William Feely
 3+// Copyright (C) 2013-2019 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -66,12 +66,12 @@
6767 0x11: 4-bit palette
6868 0x12: 2-bit palette
6969 0x13: 1-bit palette
70 -0x14: 4-bit palette index
71 -0x15: 2-bit palette index
72 -0x16: 1-bit palette index
73 -0x20: (first entry for specific RGB formats)
74 -0x80: (first entry for specific YUV formats)
75 -0xC0: (first entry for compressed)
 70+0x14: 4-bit palette index (future)
 71+0x15: 2-bit palette index (future)
 72+0x16: 1-bit palette index (future)
 73+0x20: (first entry for specific RGB formats) (future)
 74+0x80: (first entry for specific YUV formats) (future)
 75+0xC0: (first entry for compressed) (future)
7676 */
7777
7878 static const char revheader[] =
@@ -99,6 +99,8 @@
100100 static const char unif_desttex[] = "uniform sampler2D desttex;\n";
101101 static const char unif_patterntex[] = "uniform sampler2D patterntex;\n";
102102 static const char unif_stenciltex[] = "uniform sampler2D stenciltex;\n";
 103+static const char unif_srcpal[] = "uniform sampler2D srcpal;\n";
 104+static const char unif_destpal[] = "uniform sampler2D destpal;\n";
103105 static const char unif_ckeysrc[] = "uniform ivec3 ckeysrc;\n";
104106 static const char unif_ckeydest[] = "uniform ivec3 ckeydest;\n";
105107 static const char unif_ckeysrchigh[] = "uniform ivec3 ckeysrchigh;\n";
@@ -118,6 +120,9 @@
119121 // Operations
120122 static const char op_src[] = "src = ivec4(texture2D(srctex,gl_TexCoord[0].st)*vec4(colorsizesrc)+.5);\n";
121123 static const char op_pixel[] = "pixel = ivec4(texture2D(srctex,gl_TexCoord[0].st)*vec4(colorsizedest)+.5);\n";
 124+static const char op_palpixel[] = "vec4 myindex = texture2D(srctex, gl_TexCoord[0].xy);\n\
 125+vec2 index = vec2(((myindex.x*(255.0/256.0))+(0.5/256.0)),0.5);\n\
 126+pixel = ivec4(texture2D(srcpal, index)*vec4(colorsizedest)+.5);\n";
122127 static const char op_color[] = "pixel = fillcolor;\n";
123128 static const char op_dest[] = "dest = ivec4(texture2D(desttex,gl_TexCoord[1].st)*vec4(colorsizedest)+.5);\n";
124129 static const char op_pattern[] = "patternst = vec2(mod(gl_FragCoord.x,float(patternsize.x))/float(patternsize.x),\n\
@@ -730,11 +735,14 @@
731736 tmp.ptr = NULL;
732737 BOOL intproc = FALSE;
733738 BOOL usedest = FALSE;
 739+ unsigned char srctype = (id >> 32) & 0xFF;
 740+ unsigned char srctype2;
 741+ unsigned char desttype = (id >> 40) & 0xFF;
734742 gen->genshaders2D[index].shader.vsrc.ptr = NULL;
735743 gen->genshaders2D[index].shader.fsrc.ptr = NULL;
736 - char idstring[22];
737 - _snprintf(idstring, 21, "%0.8I32X\n", id);
738 - idstring[21] = 0;
 744+ char idstring[30];
 745+ _snprintf(idstring, 29, "%0.16I64X\n", id);
 746+ idstring[29] = 0;
739747 // Create vertex shader
740748 // Header
741749 STRING *vsrc = &gen->genshaders2D[index].shader.vsrc;
@@ -859,6 +867,19 @@
860868 String_Append(fsrc, unif_ckeydest);
861869 if (id & 0x40000000) String_Append(fsrc, unif_ckeydesthigh);
862870 }
 871+ if (srctype == desttype) srctype2 = 0;
 872+ else srctype2 = srctype;
 873+ switch (srctype2)
 874+ {
 875+ default:
 876+ break;
 877+ case 0x10:
 878+ case 0x11:
 879+ case 0x12:
 880+ case 0x13:
 881+ String_Append(fsrc, unif_srcpal);
 882+ break;
 883+ }
863884
864885 // Variables
865886 String_Append(fsrc, var_pixel);
@@ -877,7 +898,22 @@
878899 String_Append(fsrc, mainstart);
879900 if (id & 0x10000000) String_Append(fsrc, op_clip);
880901 if (id & DDBLT_COLORFILL) String_Append(fsrc, op_color);
881 - else String_Append(fsrc, op_pixel);
 902+ else
 903+ {
 904+ switch (srctype2)
 905+ {
 906+ case 0:
 907+ default:
 908+ String_Append(fsrc, op_pixel);
 909+ break;
 910+ case 0x10:
 911+ case 0x11:
 912+ case 0x12:
 913+ case 0x13:
 914+ String_Append(fsrc, op_palpixel);
 915+ break;
 916+ }
 917+ }
882918 if (id & DDBLT_KEYSRC) String_Append(fsrc, op_src);
883919 if (usedest) String_Append(fsrc, op_dest);
884920 if (id & DDBLT_KEYSRC)
@@ -966,6 +1002,8 @@
9671003 gen->genshaders2D[index].shader.uniforms[10] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "colorsizesrc");
9681004 gen->genshaders2D[index].shader.uniforms[11] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "colorsizedest");
9691005 gen->genshaders2D[index].shader.uniforms[12] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "fillcolor");
970 -
 1006+ gen->genshaders2D[index].shader.uniforms[13] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "srcpal");
 1007+ gen->genshaders2D[index].shader.uniforms[14] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "destpal");
 1008+
9711009 gen->genshaders2D[index].id = id;
9721010 }
\ No newline at end of file
Index: ddraw/ShaderGen3D.cpp
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2012-2016 William Feely
 3+// Copyright (C) 2012-2019 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -149,7 +149,7 @@
150150 */
151151 int __cdecl compshader2D(const GenShader2D *elem1, const GenShader2D *elem2)
152152 {
153 - return elem1->id - elem2->id;
 153+ return memcmp(&elem1->id, &elem2->id, sizeof(__int64));
154154 }
155155
156156 /**
@@ -200,7 +200,7 @@
201201 This->current_shadertype = 1;
202202 GenShader2D key2d;
203203 GenShader2D *shader2d;
204 - key2d.id = id & 0xFFFFFFFF;
 204+ key2d.id = id;
205205 shader2d = (GenShader2D*)bsearch(&key2d, gen2d->genshaders2D, gen2d->genindex, sizeof(GenShader2D),
206206 (int(__cdecl *) (const void *, const void *))compshader2D);
207207 if (!shader2d)
@@ -216,7 +216,7 @@
217217 String_Free(&gen2d->genshaders2D[gen2d->genindex].shader.fsrc);
218218 ZeroMemory(&gen2d->genshaders2D[gen2d->genindex], sizeof(GenShader2D));
219219 }
220 - ShaderGen2D_CreateShader2D(gen2d, gen2d->genindex, id & 0xFFFFFFFF);
 220+ ShaderGen2D_CreateShader2D(gen2d, gen2d->genindex, id);
221221 gen2d->genindex++;
222222 if (gen2d->genindex >= gen2d->maxshaders)
223223 {
Index: ddraw/glDirectDrawPalette.c
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2011-2016 William Feely
 3+// Copyright (C) 2011-2019 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -235,13 +235,16 @@
236236 memcpy(((char *)This->palette)+(dwStartingEntry*entrysize),lpEntries,dwCount*entrysize);
237237 if (!(This->flags & DDPCAPS_ALLOW256))
238238 {
239 - memcpy(&This->palette[0], DefaultPalette, 4);
240 - memcpy(&This->palette[255], DefaultPalette + 1020, 4);
 239+ if (This->flags & DDPCAPS_8BIT)
 240+ {
 241+ memcpy(&This->palette[0], DefaultPalette, 4);
 242+ memcpy(&This->palette[255], DefaultPalette + 1020, 4);
 243+ }
241244 }
242245 if (This->texture)
243246 {
244247 glTexture_Lock(This->texture, 0, NULL, &ddsd, 0, FALSE);
245 - memcpy(ddsd.lpSurface, This->palette, 1024);
 248+ memcpy(ddsd.lpSurface, This->palette, 4 * This->palsize);
246249 glTexture_Unlock(This->texture, 0, NULL, FALSE);
247250 }
248251 if ((This->flags & DDPCAPS_PRIMARYSURFACE) && (This->surface))
@@ -287,7 +290,25 @@
288291 glDirectDrawPalette *newpal;
289292 TRACE_ENTER(3,9,dwFlags,14,lpDDColorArray,14,lplpDDPalette);
290293 if (!IsWritablePointer(lplpDDPalette, sizeof(LPDIRECTDRAWPALETTE), FALSE)) TRACE_RET(HRESULT, 23, DDERR_INVALIDPARAMS);
291 - if (lpDDColorArray && !IsReadablePointer(lpDDColorArray,256*sizeof(PALETTEENTRY))) TRACE_RET(HRESULT, 23, DDERR_INVALIDPARAMS);
 294+ if (lpDDColorArray)
 295+ {
 296+ if (dwFlags & DDPCAPS_8BIT)
 297+ {
 298+ if (!IsReadablePointer(lpDDColorArray, 256 * sizeof(PALETTEENTRY))) TRACE_RET(HRESULT, 23, DDERR_INVALIDPARAMS);
 299+ }
 300+ if (dwFlags & DDPCAPS_4BIT)
 301+ {
 302+ if (!IsReadablePointer(lpDDColorArray, 16 * sizeof(PALETTEENTRY))) TRACE_RET(HRESULT, 23, DDERR_INVALIDPARAMS);
 303+ }
 304+ if (dwFlags & DDPCAPS_2BIT)
 305+ {
 306+ if (!IsReadablePointer(lpDDColorArray, 4 * sizeof(PALETTEENTRY))) TRACE_RET(HRESULT, 23, DDERR_INVALIDPARAMS);
 307+ }
 308+ if (dwFlags & DDPCAPS_1BIT)
 309+ {
 310+ if (!IsReadablePointer(lpDDColorArray, 2 * sizeof(PALETTEENTRY))) TRACE_RET(HRESULT, 23, DDERR_INVALIDPARAMS);
 311+ }
 312+ }
292313 if (dwFlags & 0xFFFFF000) TRACE_RET(HRESULT, 23, DDERR_INVALIDPARAMS);
293314 if ((dwFlags & DDPCAPS_8BIT) && (dwFlags & DDPCAPS_8BITENTRIES)) TRACE_RET(HRESULT, 23, DDERR_INVALIDPARAMS);
294315 if (((dwFlags & DDPCAPS_1BIT) || (dwFlags & DDPCAPS_2BIT) || (dwFlags & DDPCAPS_4BIT)) && (dwFlags & DDPCAPS_ALLOW256))
@@ -309,15 +330,24 @@
310331 }
311332 else
312333 {
313 - if(newpal->flags & DDPCAPS_1BIT)
314 - if(newpal->flags & DDPCAPS_8BITENTRIES) memcpy(newpal->palette,lpDDColorArray,2);
315 - else memcpy(newpal->palette,lpDDColorArray,2*sizeof(PALETTEENTRY));
316 - else if(newpal->flags & DDPCAPS_2BIT)
317 - if(newpal->flags & DDPCAPS_8BITENTRIES) memcpy(newpal->palette,lpDDColorArray,4);
318 - else memcpy(newpal->palette,lpDDColorArray,4*sizeof(PALETTEENTRY));
319 - else if(newpal->flags & DDPCAPS_4BIT)
320 - if(newpal->flags & DDPCAPS_8BITENTRIES) memcpy(newpal->palette,lpDDColorArray,16);
321 - else memcpy(newpal->palette,lpDDColorArray,16*sizeof(PALETTEENTRY));
 334+ if (newpal->flags & DDPCAPS_1BIT)
 335+ {
 336+ if (newpal->flags & DDPCAPS_8BITENTRIES) memcpy(newpal->palette, lpDDColorArray, 2);
 337+ else memcpy(newpal->palette, lpDDColorArray, 2 * sizeof(PALETTEENTRY));
 338+ newpal->palsize = 2;
 339+ }
 340+ else if (newpal->flags & DDPCAPS_2BIT)
 341+ {
 342+ if (newpal->flags & DDPCAPS_8BITENTRIES) memcpy(newpal->palette, lpDDColorArray, 4);
 343+ else memcpy(newpal->palette, lpDDColorArray, 4 * sizeof(PALETTEENTRY));
 344+ newpal->palsize = 4;
 345+ }
 346+ else if (newpal->flags & DDPCAPS_4BIT)
 347+ {
 348+ if (newpal->flags & DDPCAPS_8BITENTRIES) memcpy(newpal->palette, lpDDColorArray, 16);
 349+ else memcpy(newpal->palette, lpDDColorArray, 16 * sizeof(PALETTEENTRY));
 350+ newpal->palsize = 16;
 351+ }
322352 else
323353 {
324354 memcpy(newpal->palette, lpDDColorArray, 256 * sizeof(PALETTEENTRY));
@@ -326,6 +356,7 @@
327357 memcpy(&newpal->palette[0], DefaultPalette, 4);
328358 memcpy(&newpal->palette[255], DefaultPalette + 1020, 4);
329359 }
 360+ newpal->palsize = 256;
330361 }
331362 }
332363 if(lplpDDPalette) *lplpDDPalette = (LPDIRECTDRAWPALETTE)newpal;
@@ -338,8 +369,10 @@
339370 DDSURFACEDESC2 ddsd;
340371 ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
341372 memcpy(&ddsd, &ddsd256pal, sizeof(DDSURFACEDESC2));
342 - glTexture_Create(&ddsd256pal, &This->texture, renderer, 256, 1, FALSE, FALSE);
 373+ ddsd.dwWidth = This->palsize;
 374+ ddsd.lPitch = This->palsize * 4;
 375+ glTexture_Create(&ddsd, &This->texture, renderer, This->palsize, 1, FALSE, FALSE);
343376 glTexture_Lock(This->texture, 0, NULL, &ddsd, 0, FALSE);
344 - memcpy(ddsd.lpSurface, This->palette, 1024);
 377+ memcpy(ddsd.lpSurface, This->palette, (This->palsize * 4));
345378 glTexture_Unlock(This->texture, 0, NULL, FALSE);
346379 }
\ No newline at end of file
Index: ddraw/glRenderer.cpp
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2012-2018 William Feely
 3+// Copyright (C) 2012-2019 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -3379,6 +3379,7 @@
33803380 if (rop_texture_usage[(cmd->bltfx.dwROP >> 16) & 0xFF] & 4) usepattern = TRUE;
33813381 }
33823382 else shaderid = cmd->flags & 0xF2FAADFF;
 3383+ if (cmd->src) shaderid |= ((long long)cmd->src->blttype << 32);
33833384 //TODO: Add src/dest texture types
33843385 if (cmd->flags & DDBLT_KEYDEST) usedest = TRUE;
33853386 if (IsAlphaCKey())
@@ -3516,6 +3517,22 @@
35173518 glUtil_EnableArray(This->util, shader->shader.attribs[5], TRUE);
35183519 This->ext->glVertexAttribPointer(shader->shader.attribs[5], 2, GL_FLOAT, GL_FALSE, sizeof(BltVertex), &This->bltvertices[0].stencils);
35193520 }
 3521+ switch ((shaderid >> 32) & 0xFF)
 3522+ {
 3523+ case 0x10:
 3524+ case 0x11:
 3525+ case 0x12:
 3526+ case 0x13: // Use palette
 3527+ if (cmd->src->palette)
 3528+ {
 3529+ if (cmd->src->palette->levels[0].dirty & 1) glTexture__Upload(cmd->src->palette, 0);
 3530+ glUtil_SetTexture(This->util, 12, cmd->src->palette);
 3531+ This->ext->glUniform1i(shader->shader.uniforms[13], 12);
 3532+ }
 3533+ break;
 3534+ default:
 3535+ break;
 3536+ }
35203537 if (cmd->src)
35213538 {
35223539 glUtil_SetTexture(This->util, 8, cmd->src);
Index: ddraw/glTexture.cpp
@@ -814,6 +814,7 @@
815815 This->convfunctionupload = 11;
816816 This->convfunctiondownload = 14;
817817 This->internalsize = 1; // Store in R8/LUMINANCE8 texture
 818+ This->blttype = 0x13;
818819 if (This->renderer->ext->glver_major >= 3)
819820 {
820821 This->internalformats[0] = GL_R8;
@@ -840,6 +841,7 @@
841842 This->convfunctionupload = 12;
842843 This->convfunctiondownload = 15;
843844 This->internalsize = 1; // Store in R8/LUMINANCE8 texture
 845+ This->blttype = 0x12;
844846 if (This->renderer->ext->glver_major >= 3)
845847 {
846848 This->internalformats[0] = GL_R8;
@@ -866,6 +868,7 @@
867869 This->convfunctionupload = 13;
868870 This->convfunctiondownload = 16;
869871 This->internalsize = 1; // Store in R8/LUMINANCE8 texture
 872+ This->blttype = 0x11;
870873 if (This->renderer->ext->glver_major >= 3)
871874 {
872875 This->internalformats[0] = GL_R8;
@@ -888,6 +891,7 @@
889892 This->colorbits[3] = 0;
890893 break;
891894 case 3: // 8-bit palette
 895+ This->blttype = 0x10;
892896 if (This->renderer->ext->glver_major >= 3)
893897 {
894898 This->internalformats[0] = GL_R8;
Index: ddraw/struct.h
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2015-2016 William Feely
 3+// Copyright (C) 2015-2019 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -315,6 +315,7 @@
316316 int convfunctionupload;
317317 int convfunctiondownload;
318318 int internalsize;
 319+ unsigned char blttype;
319320 struct glTexture *palette;
320321 struct glTexture *stencil;
321322 struct glTexture *dummycolor;
@@ -426,6 +427,7 @@
427428 IUnknown *creator;
428429 LPDIRECTDRAWSURFACE7 surface;
429430 DXGLTimer *timer;
 431+ DWORD palsize;
430432 } glDirectDrawPalette;
431433
432434 // Function pointer table for glDirectDrawPalette
Index: dxglcfg/palette.cpp
@@ -23,19 +23,19 @@
2424 }; // Just black and white
2525
2626 const unsigned char DefaultPalette2[16] = {
27 - 0x00,0x00,0x00,0x00,0xff,0xff,0x55,0x00,
 27+ 0x00,0x00,0x00,0x00,0x55,0xff,0xff,0x00,
2828 0xff,0x55,0xff,0x00,0xff,0xff,0xff,0x00
2929 }; // Based on IBM PC CGA Palette 1 with intensity bit set
3030
3131 const unsigned char DefaultPalette4[64] = {
32 - 0x00,0x00,0x00,0x00,0xaa,0x00,0x00,0x00,
33 - 0x00,0xaa,0x00,0x00,0xaa,0xaa,0x00,0x00,
34 - 0x00,0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,
35 - 0x00,0x55,0xaa,0x00,0xaa,0xaa,0xaa,0x00,
36 - 0x55,0x55,0x55,0x00,0xff,0x55,0x55,0x00,
37 - 0x55,0xff,0x55,0x00,0xff,0xff,0x55,0x00,
38 - 0x55,0x55,0xff,0x00,0x55,0xff,0x55,0x00,
39 - 0x55,0xff,0xff,0x00,0xff,0xff,0xff,0x00
 32+ 0x00,0x00,0x00,0x00,0x00,0x00,0xaa,0x00,
 33+ 0x00,0xaa,0x00,0x00,0x00,0xaa,0xaa,0x00,
 34+ 0xaa,0x00,0x00,0x00,0xaa,0x00,0xaa,0x00,
 35+ 0xaa,0x55,0x00,0x00,0xaa,0xaa,0xaa,0x00,
 36+ 0x55,0x55,0x55,0x00,0x55,0x55,0xff,0x00,
 37+ 0x55,0xff,0x55,0x00,0x55,0xff,0xff,0x00,
 38+ 0xff,0x55,0x55,0x00,0xff,0x55,0xff,0x00,
 39+ 0xff,0xff,0x55,0x00,0xff,0xff,0xff,0x00
4040 }; // Based on IBM PC CGA/EGA/VGA default 16 color palette
4141
4242 const unsigned char DefaultPalette8[1024] = {
Index: dxglcfg/tests.cpp
@@ -2477,6 +2477,27 @@
24782478 }
24792479 else
24802480 {
 2481+ switch (PaletteType(ddsd.ddpfPixelFormat.dwFlags))
 2482+ {
 2483+ case 0:
 2484+ break;
 2485+ case 1:
 2486+ ddinterface->CreatePalette(DDPCAPS_1BIT, (LPPALETTEENTRY)&DefaultPalette1, &sprites[1].palette, NULL);
 2487+ sprites[1].surface->SetPalette(sprites[1].palette);
 2488+ break;
 2489+ case 2:
 2490+ ddinterface->CreatePalette(DDPCAPS_2BIT, (LPPALETTEENTRY)&DefaultPalette2, &sprites[1].palette, NULL);
 2491+ sprites[1].surface->SetPalette(sprites[1].palette);
 2492+ break;
 2493+ case 4:
 2494+ ddinterface->CreatePalette(DDPCAPS_4BIT, (LPPALETTEENTRY)&DefaultPalette4, &sprites[1].palette, NULL);
 2495+ sprites[1].surface->SetPalette(sprites[1].palette);
 2496+ break;
 2497+ case 8:
 2498+ ddinterface->CreatePalette(DDPCAPS_8BIT, (LPPALETTEENTRY)&DefaultPalette8, &sprites[1].palette, NULL);
 2499+ sprites[1].surface->SetPalette(sprites[1].palette);
 2500+ break;
 2501+ }
24812502 error = sprites[1].surface->Blt(NULL, sprites[0].surface, NULL, DDBLT_WAIT, NULL);
24822503 if (error)
24832504 {