DXGL r451 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r450‎ | r451 | r452 >
Date:00:22, 13 June 2014
Author:admin
Status:new
Tags:
Comment:
Basic implementation of 2D Shader Generator for Blt() commands. Will be activated soon.
Modified paths:
  • /ddraw/ShaderGen2D.cpp (modified) (history)

Diff [purge]

Index: ddraw/ShaderGen2D.cpp
@@ -149,9 +149,7 @@
150150 static const char mainstart[] = "void main()\n{\n";
151151 static const char mainend[] = "} ";
152152 // Attributes
153 -static const char attr_srcxy[] = "attribute vec2 srcxy;\n";
154 -static const char attr_destxy[] = "attribute vec2 destxy;\n";
155 -static const char attr_patternxy[] = "attribute vec2 patternxy;\n";
 153+static const char attr_xy[] = "attribute vec2 xy;\n";
156154 static const char attr_rgb[] = "attribute vec3 rgb;\n";
157155 static const char attr_rgba[] = "attribute vec4 rgba;\n";
158156 static const char attr_srcst[] = "attribute vec2 srcst;\n";
@@ -159,21 +157,36 @@
160158 static const char attr_patternst[] = "attribute vec2 patternst;\n";
161159
162160 // Uniforms
163 -static const char var_srctex[] = "uniform sampler2d srctex;";
164 -static const char var_desttex[] = "uniform sampler2d desttex;";
165 -static const char var_patterntex[] = "uniform sampler2d patterntex;";
 161+static const char unif_view[] = "uniform vec4 view;\n";
 162+static const char unif_srctex[] = "uniform sampler2d srctex;\n";
 163+static const char unif_desttex[] = "uniform sampler2d desttex;\n";
 164+static const char unif_patterntex[] = "uniform sampler2d patterntex;\n";
 165+static const char unif_ckeysrc[] = "uniform ivec3 ckeysrc;\n";
 166+static const char unif_ckeydest[] = "uniform ivec3 ckeydest;\n";
166167
 168+
167169 // Variables
168 -static const char var_src[] = "ivec4 src;\n";
169170 static const char var_dest[] = "ivec4 dest;\n";
170171 static const char var_pattern[] = "ivec4 pattern;\n";
171 -static const char var_pixel[] = "vec4 pixel;\n";
 172+static const char var_pixel[] = "ivec4 pixel;\n";
172173
173174 // Operations
174 -static const char op_src[] = "src = ivec4(texture2D(src,gl_TexCoord[0].st)*255.5);\n";
 175+static const char op_src[] = "pixel = ivec4(texture2D(src,gl_TexCoord[0].st)*255.5);\n";
 176+static const char op_color[] = "pixel = ivec4(gl_FragColor*255.5);\n";
175177 static const char op_dest[] = "dest = ivec4(texture2D(dest,gl_TexCoord[1].st)*255.5);\n";
176178 static const char op_pattern[] = "pattern = ivec4(texture2D(pattern,gl_TexCoord[2].st)*255.5);\n";
177 -static const char op_destout[] = "gl_FragColor = vec4(dest)/255.5;\n";
 179+static const char op_destout[] = "gl_FragColor = vec4(pixel)/255.5;\n";
 180+static const char op_vertex[] = "vec4 xyzw = vec4(xy[0],xy[1],0,1);\n\
 181+mat4 proj = mat4(\n\
 182+vec4(2.0 / (view[1] - view[0]), 0, 0, 0),\n\
 183+vec4(0, 2.0 / (view[2] - view[3]), 0, 0),\n\
 184+vec4(0, 0, -2.0, 0),\n\
 185+vec4(-(view[1] + view[0]) / (view[1] - view[0]),\n\
 186+-(view[2] + view[3]) / (view[2] - view[3]), -1 , 1));\n\
 187+gl_Position = proj * xyzw;\n";
 188+static const char op_vertcolorrgb[] = "gl_FrontColor = vec4(rgb,1.0);\n";
 189+static const char op_texcoord0[] = "gl_TexCoord[0] = vec4(st,0.0,1.0);\n";
 190+static const char op_ckeysrc[] = "if(pixel == ckey) discard;\n";
178191
179192
180193 // Functions
@@ -743,7 +756,7 @@
744757 // Header
745758 STRING *vsrc = &gen->genshaders2D->shader.vsrc;
746759 String_Append(vsrc, revheader);
747 - if ((id >> 17) & 1)
 760+ if (id & DDBLT_ROP)
748761 {
749762 if (gen->ext->glver_major >= 3)
750763 {
@@ -761,5 +774,130 @@
762775 else String_Append(vsrc, version_110);
763776 String_Append(vsrc, idheader);
764777 String_Append(vsrc, idstring);
 778+
765779 // Attributes
 780+ String_Append(vsrc, attr_xy);
 781+ if (id & DDBLT_COLORFILL) String_Append(vsrc, attr_rgb);
 782+ else String_Append(vsrc, attr_srcst);
 783+
 784+ // Uniforms
 785+ String_Append(vsrc, unif_view);
 786+
 787+ // Main
 788+ String_Append(vsrc, mainstart);
 789+ String_Append(vsrc, op_vertex);
 790+ if (id & DDBLT_COLORFILL) String_Append(vsrc, op_vertcolorrgb);
 791+ else String_Append(vsrc, op_texcoord0);
 792+ String_Append(vsrc, mainend);
 793+#ifdef _DEBUG
 794+ OutputDebugStringA("2D blitter vertex shader:\n");
 795+ OutputDebugStringA(vsrc->ptr);
 796+ OutputDebugStringA("\nCompiling 2D blitter vertex shader:\n");
 797+#endif
 798+ gen->genshaders2D[index].shader.vs = gen->ext->glCreateShader(GL_VERTEX_SHADER);
 799+ GLint srclen = strlen(vsrc->ptr);
 800+ gen->ext->glShaderSource(gen->genshaders2D[index].shader.vs, 1, &vsrc->ptr, &srclen);
 801+ gen->ext->glCompileShader(gen->genshaders2D[index].shader.vs);
 802+ GLint result;
 803+ char *infolog = NULL;
 804+ gen->ext->glGetShaderiv(gen->genshaders2D[index].shader.vs, GL_COMPILE_STATUS, &result);
 805+#ifdef _DEBUG
 806+ GLint loglen;
 807+ if (!result)
 808+ {
 809+ gen->ext->glGetShaderiv(gen->genshaders2D[index].shader.vs, GL_INFO_LOG_LENGTH, &loglen);
 810+ infolog = (char*)malloc(loglen);
 811+ gen->ext->glGetShaderInfoLog(gen->genshaders2D[index].shader.vs, loglen, &result, infolog);
 812+ OutputDebugStringA("Compilation failed. Error messages:\n");
 813+ OutputDebugStringA(infolog);
 814+ free(infolog);
 815+ }
 816+#endif
 817+
 818+ // Create fragment shader
 819+ STRING *fsrc = &gen->genshaders2D->shader.fsrc;
 820+ String_Append(fsrc, revheader);
 821+ if (id & DDBLT_ROP)
 822+ {
 823+ if (gen->ext->glver_major >= 3)
 824+ {
 825+ String_Append(fsrc, version_130);
 826+ intproc = true;
 827+ }
 828+ else if (gen->ext->GLEXT_EXT_gpu_shader4)
 829+ {
 830+ String_Append(fsrc, version_110);
 831+ String_Append(fsrc, ext_shader4);
 832+ intproc = true;
 833+ }
 834+ else String_Append(fsrc, version_110);
 835+ }
 836+ else String_Append(fsrc, version_110);
 837+ String_Append(fsrc, idheader);
 838+ String_Append(fsrc, idstring);
 839+
 840+ // Uniforms
 841+ if (!(id & DDBLT_COLORFILL)) String_Append(fsrc, unif_srctex);
 842+ if (id & DDBLT_KEYSRC) String_Append(fsrc, unif_ckeysrc);
 843+
 844+ // Variables
 845+ String_Append(fsrc, var_pixel);
 846+
 847+ // Main
 848+ String_Append(fsrc, mainstart);
 849+ if (id & DDBLT_COLORFILL) String_Append(fsrc, op_color);
 850+ else String_Append(fsrc, op_src);
 851+ if (id & DDBLT_KEYSRC) String_Append(fsrc, op_ckeysrc);
 852+
 853+ String_Append(fsrc, op_destout);
 854+ String_Append(fsrc, mainend);
 855+#ifdef _DEBUG
 856+ OutputDebugStringA("2D blitter fragment shader:\n");
 857+ OutputDebugStringA(fsrc->ptr);
 858+ OutputDebugStringA("\nCompiling 2D blitter fragment shader:\n");
 859+#endif
 860+ gen->genshaders2D[index].shader.fs = gen->ext->glCreateShader(GL_FRAGMENT_SHADER);
 861+ srclen = strlen(fsrc->ptr);
 862+ gen->ext->glShaderSource(gen->genshaders2D[index].shader.fs, 1, &fsrc->ptr, &srclen);
 863+ gen->ext->glCompileShader(gen->genshaders2D[index].shader.fs);
 864+ gen->ext->glGetShaderiv(gen->genshaders2D[index].shader.fs, GL_COMPILE_STATUS, &result);
 865+#ifdef _DEBUG
 866+ if (!result)
 867+ {
 868+ gen->ext->glGetShaderiv(gen->genshaders2D[index].shader.fs, GL_INFO_LOG_LENGTH, &loglen);
 869+ infolog = (char*)malloc(loglen);
 870+ gen->ext->glGetShaderInfoLog(gen->genshaders2D[index].shader.fs, loglen, &result, infolog);
 871+ OutputDebugStringA("Compilation failed. Error messages:\n");
 872+ OutputDebugStringA(infolog);
 873+ free(infolog);
 874+ }
 875+#endif
 876+ gen->genshaders2D[index].shader.prog = gen->ext->glCreateProgram();
 877+ gen->ext->glAttachShader(gen->genshaders2D[index].shader.prog, gen->genshaders2D[index].shader.vs);
 878+ gen->ext->glAttachShader(gen->genshaders2D[index].shader.prog, gen->genshaders2D[index].shader.fs);
 879+ gen->ext->glLinkProgram(gen->genshaders2D[index].shader.prog);
 880+ gen->ext->glGetProgramiv(gen->genshaders2D[index].shader.prog, GL_LINK_STATUS, &result);
 881+#ifdef _DEBUG
 882+ if (!result)
 883+ {
 884+ gen->ext->glGetProgramiv(gen->genshaders2D[index].shader.prog, GL_INFO_LOG_LENGTH, &loglen);
 885+ infolog = (char*)malloc(loglen);
 886+ gen->ext->glGetProgramInfoLog(gen->genshaders2D[index].shader.prog, loglen, &result, infolog);
 887+ OutputDebugStringA("Program link failed. Error messages:\n");
 888+ OutputDebugStringA(infolog);
 889+ free(infolog);
 890+ }
 891+#endif
 892+ gen->genshaders2D[index].shader.attribs[0] = gen->ext->glGetAttribLocation(gen->genshaders2D[index].shader.prog, "xy");
 893+ gen->genshaders2D[index].shader.attribs[1] = gen->ext->glGetAttribLocation(gen->genshaders2D[index].shader.prog, "rgb");
 894+ gen->genshaders2D[index].shader.attribs[2] = gen->ext->glGetAttribLocation(gen->genshaders2D[index].shader.prog, "rgba");
 895+ gen->genshaders2D[index].shader.attribs[3] = gen->ext->glGetAttribLocation(gen->genshaders2D[index].shader.prog, "srcst");
 896+ gen->genshaders2D[index].shader.attribs[4] = gen->ext->glGetAttribLocation(gen->genshaders2D[index].shader.prog, "destst");
 897+ gen->genshaders2D[index].shader.attribs[5] = gen->ext->glGetAttribLocation(gen->genshaders2D[index].shader.prog, "patternst");
 898+ gen->genshaders2D[index].shader.uniforms[0] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "view");
 899+ gen->genshaders2D[index].shader.uniforms[1] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "srctex");
 900+ gen->genshaders2D[index].shader.uniforms[2] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "desttex");
 901+ gen->genshaders2D[index].shader.uniforms[3] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "patterntex");
 902+ gen->genshaders2D[index].shader.uniforms[4] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "ckeysrc");
 903+ gen->genshaders2D[index].shader.uniforms[5] = gen->ext->glGetUniformLocation(gen->genshaders2D[index].shader.prog, "ckeydest");
766904 }
\ No newline at end of file