2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
28 #define GLINFO_LOCATION (*gl_info)
30 /** Prints the GLSL info log which will contain error messages if they exist */
31 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
33 int infologLength = 0;
36 GL_EXTCALL(glGetObjectParameterivARB(obj,
37 GL_OBJECT_INFO_LOG_LENGTH_ARB,
40 /* A size of 1 is just a null-terminated string, so the log should be bigger than
41 * that if there are errors. */
42 if (infologLength > 1)
44 infoLog = (char *)HeapAlloc(GetProcessHeap(), 0, infologLength);
45 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
46 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
47 HeapFree(GetProcessHeap(), 0, infoLog);
52 * Loads (pixel shader) samplers
54 void shader_glsl_load_psamplers(
55 WineD3D_GL_Info *gl_info,
56 IWineD3DStateBlock* iface) {
58 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
59 GLhandleARB programId = stateBlock->glsl_program->programId;
62 char sampler_name[20];
64 for (i=0; i< GL_LIMITS(samplers); ++i) {
65 if (stateBlock->textures[i] != NULL) {
66 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
67 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
69 TRACE_(d3d_shader)("Loading %s for texture %d\n", sampler_name, i);
70 GL_EXTCALL(glUniform1iARB(name_loc, i));
71 checkGLcall("glUniform1iARB");
78 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
79 * When constant_list == NULL, it will load all the constants.
81 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
82 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
83 struct list *constant_list) {
84 local_constant* lconst;
89 if (TRACE_ON(d3d_shader)) {
90 for (i = 0; i < max_constants; ++i) {
91 tmp_loc = constant_locations[i];
93 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
94 constants[i * 4 + 0], constants[i * 4 + 1],
95 constants[i * 4 + 2], constants[i * 4 + 3]);
99 for (i = 0; i < max_constants; ++i) {
100 tmp_loc = constant_locations[i];
102 /* We found this uniform name in the program - go ahead and send the data */
103 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
106 checkGLcall("glUniform4fvARB()");
108 constant_entry *constant;
109 if (TRACE_ON(d3d_shader)) {
110 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
112 tmp_loc = constant_locations[i];
114 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
115 constants[i * 4 + 0], constants[i * 4 + 1],
116 constants[i * 4 + 2], constants[i * 4 + 3]);
120 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
122 tmp_loc = constant_locations[i];
124 /* We found this uniform name in the program - go ahead and send the data */
125 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
128 checkGLcall("glUniform4fvARB()");
131 /* Load immediate constants */
132 if (TRACE_ON(d3d_shader)) {
133 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
134 tmp_loc = constant_locations[lconst->idx];
136 GLfloat* values = (GLfloat*)lconst->value;
137 TRACE("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
138 values[0], values[1], values[2], values[3]);
142 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
143 tmp_loc = constant_locations[lconst->idx];
145 /* We found this uniform name in the program - go ahead and send the data */
146 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
149 checkGLcall("glUniform4fvARB()");
153 * Loads integer constants (aka uniforms) into the currently set GLSL program.
154 * When @constants_set == NULL, it will load all the constants.
156 void shader_glsl_load_constantsI(
157 IWineD3DBaseShaderImpl* This,
158 WineD3D_GL_Info *gl_info,
159 GLhandleARB programId,
160 unsigned max_constants,
162 BOOL* constants_set) {
167 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
168 const char* prefix = is_pshader? "PI":"VI";
171 for (i=0; i<max_constants; ++i) {
172 if (NULL == constants_set || constants_set[i]) {
174 TRACE("Loading constants %i: %i, %i, %i, %i\n",
175 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
177 /* TODO: Benchmark and see if it would be beneficial to store the
178 * locations of the constants to avoid looking up each time */
179 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
180 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
182 /* We found this uniform name in the program - go ahead and send the data */
183 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
184 checkGLcall("glUniform4ivARB");
189 /* Load immediate constants */
190 ptr = list_head(&This->baseShader.constantsI);
192 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
193 unsigned int idx = lconst->idx;
194 GLint* values = (GLint*) lconst->value;
196 TRACE("Loading local constants %i: %i, %i, %i, %i\n", idx,
197 values[0], values[1], values[2], values[3]);
199 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
200 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
202 /* We found this uniform name in the program - go ahead and send the data */
203 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
204 checkGLcall("glUniform4ivARB");
206 ptr = list_next(&This->baseShader.constantsI, ptr);
211 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
212 * When @constants_set == NULL, it will load all the constants.
214 void shader_glsl_load_constantsB(
215 IWineD3DBaseShaderImpl* This,
216 WineD3D_GL_Info *gl_info,
217 GLhandleARB programId,
218 unsigned max_constants,
220 BOOL* constants_set) {
225 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
226 const char* prefix = is_pshader? "PB":"VB";
229 for (i=0; i<max_constants; ++i) {
230 if (NULL == constants_set || constants_set[i]) {
232 TRACE("Loading constants %i: %i;\n", i, constants[i*4]);
234 /* TODO: Benchmark and see if it would be beneficial to store the
235 * locations of the constants to avoid looking up each time */
236 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
237 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
239 /* We found this uniform name in the program - go ahead and send the data */
240 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
241 checkGLcall("glUniform1ivARB");
246 /* Load immediate constants */
247 ptr = list_head(&This->baseShader.constantsB);
249 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
250 unsigned int idx = lconst->idx;
251 GLint* values = (GLint*) lconst->value;
253 TRACE("Loading local constants %i: %i\n", idx, values[0]);
255 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
256 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
258 /* We found this uniform name in the program - go ahead and send the data */
259 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
260 checkGLcall("glUniform1ivARB");
262 ptr = list_next(&This->baseShader.constantsB, ptr);
269 * Loads the app-supplied constants into the currently set GLSL program.
271 void shader_glsl_load_constants(
272 IWineD3DDevice* device,
274 char useVertexShader) {
276 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
277 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
278 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*) deviceImpl->wineD3D)->gl_info;
280 GLhandleARB *constant_locations;
281 struct list *constant_list;
282 GLhandleARB programId;
284 if (!stateBlock->glsl_program) {
285 /* No GLSL program set - nothing to do. */
288 programId = stateBlock->glsl_program->programId;
290 if (useVertexShader) {
291 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
292 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) vshader;
295 IWineD3DVertexDeclarationImpl* vertexDeclaration =
296 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
298 constant_locations = stateBlock->glsl_program->vuniformF_locations;
299 constant_list = &stateBlock->set_vconstantsF;
301 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
302 /* Load DirectX 8 float constants/uniforms for vertex shader */
303 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
304 vertexDeclaration->constants, constant_locations, NULL);
307 /* Load DirectX 9 float constants/uniforms for vertex shader */
308 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
309 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
311 /* Load DirectX 9 integer constants/uniforms for vertex shader */
312 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
313 stateBlock->vertexShaderConstantI,
314 stateBlock->set.vertexShaderConstantsI);
316 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
317 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
318 stateBlock->vertexShaderConstantB,
319 stateBlock->set.vertexShaderConstantsB);
321 /* Upload the position fixup params */
322 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
323 checkGLcall("glGetUniformLocationARB");
324 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
325 checkGLcall("glUniform4fvARB");
328 if (usePixelShader) {
330 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
332 constant_locations = stateBlock->glsl_program->puniformF_locations;
333 constant_list = &stateBlock->set_pconstantsF;
335 /* Load pixel shader samplers */
336 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
338 /* Load DirectX 9 float constants/uniforms for pixel shader */
339 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
340 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
342 /* Load DirectX 9 integer constants/uniforms for pixel shader */
343 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
344 stateBlock->pixelShaderConstantI,
345 stateBlock->set.pixelShaderConstantsI);
347 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
348 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
349 stateBlock->pixelShaderConstantB,
350 stateBlock->set.pixelShaderConstantsB);
354 /** Generate the variable & register declarations for the GLSL output target */
355 void shader_generate_glsl_declarations(
356 IWineD3DBaseShader *iface,
357 shader_reg_maps* reg_maps,
358 SHADER_BUFFER* buffer,
359 WineD3D_GL_Info* gl_info) {
361 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
364 /* There are some minor differences between pixel and vertex shaders */
365 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
366 char prefix = pshader ? 'P' : 'V';
368 /* Prototype the subroutines */
369 for (i = 0; i < This->baseShader.limits.label; i++) {
370 if (reg_maps->labels[i])
371 shader_addline(buffer, "void subroutine%lu();\n", i);
374 /* Declare the constants (aka uniforms) */
375 if (This->baseShader.limits.constant_float > 0) {
376 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
377 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
378 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
381 if (This->baseShader.limits.constant_int > 0)
382 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
384 if (This->baseShader.limits.constant_bool > 0)
385 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
388 shader_addline(buffer, "uniform vec4 posFixup;\n");
390 /* Declare texture samplers */
391 for (i = 0; i < This->baseShader.limits.sampler; i++) {
392 if (reg_maps->samplers[i]) {
394 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
398 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
401 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
403 case WINED3DSTT_CUBE:
404 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
406 case WINED3DSTT_VOLUME:
407 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
410 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
411 FIXME("Unrecognized sampler type: %#x\n", stype);
417 /* Declare address variables */
418 for (i = 0; i < This->baseShader.limits.address; i++) {
419 if (reg_maps->address[i])
420 shader_addline(buffer, "ivec4 A%d;\n", i);
423 /* Declare texture coordinate temporaries and initialize them */
424 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
425 if (reg_maps->texcoord[i])
426 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
429 /* Declare input register temporaries */
430 for (i=0; i < This->baseShader.limits.packed_input; i++) {
431 if (reg_maps->packed_input[i])
432 shader_addline(buffer, "vec4 IN%lu;\n", i);
435 /* Declare output register temporaries */
436 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
437 if (reg_maps->packed_output[i])
438 shader_addline(buffer, "vec4 OUT%lu;\n", i);
441 /* Declare temporary variables */
442 for(i = 0; i < This->baseShader.limits.temporary; i++) {
443 if (reg_maps->temporary[i])
444 shader_addline(buffer, "vec4 R%lu;\n", i);
447 /* Declare attributes */
448 for (i = 0; i < This->baseShader.limits.attributes; i++) {
449 if (reg_maps->attributes[i])
450 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
453 /* Declare loop register aL */
454 if (reg_maps->loop) {
455 shader_addline(buffer, "int aL;\n");
456 shader_addline(buffer, "int tmpInt;\n");
459 /* Temporary variables for matrix operations */
460 shader_addline(buffer, "vec4 tmp0;\n");
461 shader_addline(buffer, "vec4 tmp1;\n");
463 /* Start the main program */
464 shader_addline(buffer, "void main() {\n");
467 /*****************************************************************************
468 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
470 * For more information, see http://wiki.winehq.org/DirectX-Shaders
471 ****************************************************************************/
474 static void shader_glsl_add_param(
475 SHADER_OPCODE_ARG* arg,
477 const DWORD addr_token,
483 /** Used for opcode modifiers - They multiply the result by the specified amount */
484 static const char* shift_glsl_tab[] = {
486 "2.0 * ", /* 1 (x2) */
487 "4.0 * ", /* 2 (x4) */
488 "8.0 * ", /* 3 (x8) */
489 "16.0 * ", /* 4 (x16) */
490 "32.0 * ", /* 5 (x32) */
497 "0.0625 * ", /* 12 (d16) */
498 "0.125 * ", /* 13 (d8) */
499 "0.25 * ", /* 14 (d4) */
500 "0.5 * " /* 15 (d2) */
503 /** Print the beginning of the generated GLSL string. example: "reg_name.xyzw = vec4("
504 * Will also change the reg_mask if necessary (not all register types are equal in DX vs GL) */
505 static void shader_glsl_add_dst(DWORD param, const char* reg_name, char* reg_mask, char* outStr) {
507 int shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
510 if ((shader_get_regtype(param) == WINED3DSPR_RASTOUT)
511 && ((param & WINED3DSP_REGNUM_MASK) != 0)) {
512 /* gl_FogFragCoord or glPointSize - both floats */
513 strcpy(cast, "float");
514 strcpy(reg_mask, "");
516 } else if (reg_name[0] == 'A') {
517 /* Address register for vertex shaders (ivec4) */
518 strcpy(cast, "ivec4");
521 /* Everything else should be a 4 component float vector */
522 strcpy(cast, "vec4");
525 sprintf(outStr, "%s%s = %s%s(", reg_name, reg_mask, shift_glsl_tab[shift], cast);
528 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
529 static void shader_glsl_gen_modifier (
532 const char *in_regswizzle,
537 if (instr == WINED3DSIO_TEXKILL)
540 switch (instr & WINED3DSP_SRCMOD_MASK) {
541 case WINED3DSPSM_NONE:
542 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
544 case WINED3DSPSM_NEG:
545 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
547 case WINED3DSPSM_NOT:
548 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
550 case WINED3DSPSM_BIAS:
551 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
553 case WINED3DSPSM_BIASNEG:
554 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
556 case WINED3DSPSM_SIGN:
557 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
559 case WINED3DSPSM_SIGNNEG:
560 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
562 case WINED3DSPSM_COMP:
563 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
566 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
568 case WINED3DSPSM_X2NEG:
569 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
571 case WINED3DSPSM_DZ: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
572 sprintf(out_str, "vec4(%s.r / %s.b, %s.g / %s.b, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
575 sprintf(out_str, "vec4(%s.r / %s.a, %s.g / %s.a, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
577 case WINED3DSPSM_ABS:
578 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
580 case WINED3DSPSM_ABSNEG:
581 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
584 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
585 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
589 /** Writes the GLSL variable name that corresponds to the register that the
590 * DX opcode parameter is trying to access */
591 static void shader_glsl_get_register_name(
593 const DWORD addr_token,
596 SHADER_OPCODE_ARG* arg) {
598 /* oPos, oFog and oPts in D3D */
599 const char* hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
601 DWORD reg = param & WINED3DSP_REGNUM_MASK;
602 DWORD regtype = shader_get_regtype(param);
603 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
604 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
605 WineD3D_GL_Info* gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
607 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
613 case WINED3DSPR_TEMP:
614 sprintf(tmpStr, "R%u", reg);
616 case WINED3DSPR_INPUT:
618 /* Pixel shaders >= 3.0 */
619 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
620 sprintf(tmpStr, "IN%u", reg);
623 strcpy(tmpStr, "gl_Color");
625 strcpy(tmpStr, "gl_SecondaryColor");
628 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
630 sprintf(tmpStr, "attrib%u", reg);
633 case WINED3DSPR_CONST:
635 const char* prefix = pshader? "PC":"VC";
637 /* Relative addressing */
638 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
640 /* Relative addressing on shaders 2.0+ have a relative address token,
641 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
642 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
643 char relStr[100], relReg[50], relMask[6];
644 shader_glsl_add_param(arg, addr_token, 0, TRUE, relReg, relMask, relStr);
645 sprintf(tmpStr, "%s[%s + %u]", prefix, relStr, reg);
647 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
650 sprintf(tmpStr, "%s[%u]", prefix, reg);
654 case WINED3DSPR_CONSTINT:
656 sprintf(tmpStr, "PI[%u]", reg);
658 sprintf(tmpStr, "VI[%u]", reg);
660 case WINED3DSPR_CONSTBOOL:
662 sprintf(tmpStr, "PB[%u]", reg);
664 sprintf(tmpStr, "VB[%u]", reg);
666 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
668 sprintf(tmpStr, "T%u", reg);
670 sprintf(tmpStr, "A%u", reg);
673 case WINED3DSPR_LOOP:
674 sprintf(tmpStr, "aL");
676 case WINED3DSPR_SAMPLER:
678 sprintf(tmpStr, "Psampler%u", reg);
680 sprintf(tmpStr, "Vsampler%u", reg);
682 case WINED3DSPR_COLOROUT:
683 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
684 sprintf(tmpStr, "gl_FragData[%u]", reg);
686 /* TODO: See GL_ARB_draw_buffers */
687 FIXME("Unsupported write to render target %u\n", reg);
689 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
691 WARN("This OpenGL implementation doesn't support writing to multiple render targets!\n");
693 sprintf(tmpStr, "gl_FragColor");
696 case WINED3DSPR_RASTOUT:
697 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
699 case WINED3DSPR_DEPTHOUT:
700 sprintf(tmpStr, "gl_FragDepth");
702 case WINED3DSPR_ATTROUT:
704 sprintf(tmpStr, "gl_FrontColor");
706 sprintf(tmpStr, "gl_FrontSecondaryColor");
709 case WINED3DSPR_TEXCRDOUT:
710 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
711 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
712 sprintf(tmpStr, "OUT%u", reg);
714 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
717 FIXME("Unhandled register name Type(%d)\n", regtype);
718 sprintf(tmpStr, "unrecognized_register");
722 strcat(regstr, tmpStr);
725 /* Writes the GLSL writemask for the destination register */
726 static void shader_glsl_get_output_register_swizzle(
731 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
732 strcat(write_mask, ".");
733 if (param & WINED3DSP_WRITEMASK_0) strcat(write_mask, "x");
734 if (param & WINED3DSP_WRITEMASK_1) strcat(write_mask, "y");
735 if (param & WINED3DSP_WRITEMASK_2) strcat(write_mask, "z");
736 if (param & WINED3DSP_WRITEMASK_3) strcat(write_mask, "w");
740 static void shader_glsl_get_input_register_swizzle(
745 const char swizzle_reg_chars_color_fix[] = "zyxw";
746 const char swizzle_reg_chars[] = "xyzw";
747 const char* swizzle_regs = NULL;
750 DWORD swizzle = (param & WINED3DVS_SWIZZLE_MASK) >> WINED3DVS_SWIZZLE_SHIFT;
751 DWORD swizzle_x = swizzle & 0x03;
752 DWORD swizzle_y = (swizzle >> 2) & 0x03;
753 DWORD swizzle_z = (swizzle >> 4) & 0x03;
754 DWORD swizzle_w = (swizzle >> 6) & 0x03;
757 swizzle_regs = swizzle_reg_chars_color_fix;
759 swizzle_regs = swizzle_reg_chars;
763 * swizzle bits fields:
766 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) == swizzle) {
768 sprintf(reg_mask, ".%c%c%c%c",
769 swizzle_regs[swizzle_x],
770 swizzle_regs[swizzle_y],
771 swizzle_regs[swizzle_z],
772 swizzle_regs[swizzle_w]);
776 if (swizzle_x == swizzle_y &&
777 swizzle_x == swizzle_z &&
778 swizzle_x == swizzle_w)
780 sprintf(reg_mask, ".%c", swizzle_regs[swizzle_x]);
782 sprintf(reg_mask, ".%c%c%c%c",
783 swizzle_regs[swizzle_x],
784 swizzle_regs[swizzle_y],
785 swizzle_regs[swizzle_z],
786 swizzle_regs[swizzle_w]);
790 /** From a given parameter token, generate the corresponding GLSL string.
791 * Also, return the actual register name and swizzle in case the
792 * caller needs this information as well. */
793 static void shader_glsl_add_param(
794 SHADER_OPCODE_ARG* arg,
796 const DWORD addr_token,
802 BOOL is_color = FALSE;
803 reg_mask[0] = reg_name[0] = out_str[0] = 0;
805 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
808 shader_glsl_get_input_register_swizzle(param, is_color, reg_mask);
809 shader_glsl_gen_modifier(param, reg_name, reg_mask, out_str);
811 shader_glsl_get_output_register_swizzle(param, reg_mask);
812 sprintf(out_str, "%s%s", reg_name, reg_mask);
816 /** Process GLSL instruction modifiers */
817 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
819 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
821 if (arg->opcode->dst_token && mask != 0) {
826 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
828 if (mask & WINED3DSPDM_SATURATE) {
829 /* _SAT means to clamp the value of the register to between 0 and 1 */
830 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
832 if (mask & WINED3DSPDM_MSAMPCENTROID) {
833 FIXME("_centroid modifier not handled\n");
835 if (mask & WINED3DSPDM_PARTIALPRECISION) {
836 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
841 static inline const char* shader_get_comp_op(
842 const DWORD opcode) {
844 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
846 case COMPARISON_GT: return ">";
847 case COMPARISON_EQ: return "==";
848 case COMPARISON_GE: return ">=";
849 case COMPARISON_LT: return "<";
850 case COMPARISON_NE: return "!=";
851 case COMPARISON_LE: return "<=";
853 FIXME("Unrecognized comparison value: %u\n", op);
858 static void shader_glsl_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
859 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
860 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
861 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
862 const char sampler_prefix = shader_is_pshader_version(This->baseShader.hex_version) ? 'P' : 'V';
863 SHADER_BUFFER* buffer = arg->buffer;
865 if(deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
866 /* Note that there's no such thing as a projected cube texture. */
867 switch(sampler_type) {
869 shader_addline(buffer, "%s = texture2DProj(%cshader%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
871 case WINED3DSTT_VOLUME:
872 shader_addline(buffer, "%s = texture3DProj(%cshader%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
875 shader_addline(buffer, "%s = unrecognized_stype(%cshader%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
876 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
880 switch(sampler_type) {
882 shader_addline(buffer, "%s = texture2D(%csampler%u, %s.xy);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
884 case WINED3DSTT_CUBE:
885 shader_addline(buffer, "%s = textureCube(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
887 case WINED3DSTT_VOLUME:
888 shader_addline(buffer, "%s = texture3D(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
891 shader_addline(buffer, "%s = unrecognized_stype(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
892 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
899 /*****************************************************************************
901 * Begin processing individual instruction opcodes
903 ****************************************************************************/
905 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
906 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
908 CONST SHADER_OPCODE* curOpcode = arg->opcode;
909 SHADER_BUFFER* buffer = arg->buffer;
911 char dst_reg[50], src0_reg[50], src1_reg[50];
912 char dst_mask[6], src0_mask[6], src1_mask[6];
913 char dst_str[100], src0_str[100], src1_str[100];
915 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
916 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
917 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
918 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
919 strcat(tmpLine, "vec4(");
920 strcat(tmpLine, src0_str);
921 strcat(tmpLine, ")");
923 /* Determine the GLSL operator to use based on the opcode */
924 switch (curOpcode->opcode) {
925 case WINED3DSIO_MUL: strcat(tmpLine, " * "); break;
926 case WINED3DSIO_ADD: strcat(tmpLine, " + "); break;
927 case WINED3DSIO_SUB: strcat(tmpLine, " - "); break;
929 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
932 shader_addline(buffer, "%svec4(%s))%s;\n", tmpLine, src1_str, dst_mask);
935 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
936 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
938 SHADER_BUFFER* buffer = arg->buffer;
940 char dst_str[100], src0_str[100];
941 char dst_reg[50], src0_reg[50];
942 char dst_mask[6], src0_mask[6];
944 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
945 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
946 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
947 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
950 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
951 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
953 CONST SHADER_OPCODE* curOpcode = arg->opcode;
954 SHADER_BUFFER* buffer = arg->buffer;
956 char dst_str[100], src0_str[100], src1_str[100];
957 char dst_reg[50], src0_reg[50], src1_reg[50];
958 char dst_mask[6], src0_mask[6], src1_mask[6];
961 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
962 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
963 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
965 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpDest);
967 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
968 if (curOpcode->opcode == WINED3DSIO_DP4)
969 strcpy(cast, "vec4(");
971 strcpy(cast, "vec3(");
973 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
974 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
977 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
978 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
980 CONST SHADER_OPCODE* curOpcode = arg->opcode;
981 SHADER_BUFFER* buffer = arg->buffer;
983 char dst_str[100], src_str[100];
984 char dst_reg[50], src_reg[50];
985 char dst_mask[6], src_mask[6];
988 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
990 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
992 /* Determine the GLSL function to use based on the opcode */
993 /* TODO: Possibly make this a table for faster lookups */
994 switch (curOpcode->opcode) {
995 case WINED3DSIO_MIN: strcat(tmpLine, "min"); break;
996 case WINED3DSIO_MAX: strcat(tmpLine, "max"); break;
997 case WINED3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
998 case WINED3DSIO_ABS: strcat(tmpLine, "abs"); break;
999 case WINED3DSIO_FRC: strcat(tmpLine, "fract"); break;
1000 case WINED3DSIO_POW: strcat(tmpLine, "pow"); break;
1001 case WINED3DSIO_CRS: strcat(tmpLine, "cross"); break;
1002 case WINED3DSIO_NRM: strcat(tmpLine, "normalize"); break;
1003 case WINED3DSIO_LOGP:
1004 case WINED3DSIO_LOG: strcat(tmpLine, "log2"); break;
1005 case WINED3DSIO_EXP: strcat(tmpLine, "exp2"); break;
1006 case WINED3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
1007 case WINED3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
1008 case WINED3DSIO_SGN: strcat(tmpLine, "sign"); break;
1010 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1014 strcat(tmpLine, "(");
1016 if (curOpcode->num_params > 0) {
1017 strcat(tmpLine, "vec4(");
1018 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1019 strcat(tmpLine, src_str);
1020 strcat(tmpLine, ")");
1021 for (i = 2; i < curOpcode->num_params; ++i) {
1022 strcat(tmpLine, ", vec4(");
1023 shader_glsl_add_param(arg, arg->src[i-1], arg->src_addr[i-1], TRUE, src_reg, src_mask, src_str);
1024 strcat(tmpLine, src_str);
1025 strcat(tmpLine, ")");
1028 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
1032 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1033 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1034 * dst.x = 2^(floor(src))
1035 * dst.y = src - floor(src)
1036 * dst.z = 2^src (partial precision is allowed, but optional)
1038 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1039 * dst = 2^src; (partial precision is allowed, but optional)
1041 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1044 char dst_str[100], src_str[100];
1045 char dst_reg[50], src_reg[50];
1046 char dst_mask[6], src_mask[6];
1047 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1048 DWORD hex_version = This->baseShader.hex_version;
1050 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1051 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1052 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1054 if (hex_version < WINED3DPS_VERSION(2,0)) {
1055 shader_addline(arg->buffer, "tmp0.x = vec4(exp2(floor(%s))).x;\n", src_str);
1056 shader_addline(arg->buffer, "tmp0.y = vec4(%s - floor(%s)).y;\n", src_str, src_str);
1057 shader_addline(arg->buffer, "tmp0.z = vec4(exp2(%s)).x;\n", src_str);
1058 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1059 shader_addline(arg->buffer, "%svec4(tmp0))%s;\n", tmpLine, dst_mask);
1061 shader_addline(arg->buffer, "%svec4(exp2(%s)))%s;\n", tmpLine, src_str, dst_mask);
1065 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1066 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1069 char dst_str[100], src_str[100];
1070 char dst_reg[50], src_reg[50];
1071 char dst_mask[6], src_mask[6];
1073 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1074 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1075 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1076 strcat(tmpLine, "1.0 / ");
1077 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
1080 /** Process signed comparison opcodes in GLSL. */
1081 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1084 char dst_str[100], src0_str[100], src1_str[100];
1085 char dst_reg[50], src0_reg[50], src1_reg[50];
1086 char dst_mask[6], src0_mask[6], src1_mask[6];
1088 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1089 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1090 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1092 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
1093 if (strlen(src0_mask) != 2) {
1094 shader_glsl_map2gl(arg);
1098 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1100 switch (arg->opcode->opcode) {
1101 case WINED3DSIO_SLT: strcpy(compareStr, "<"); break;
1102 case WINED3DSIO_SGE: strcpy(compareStr, ">="); break;
1104 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1106 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
1107 tmpLine, src0_str, compareStr, src1_str, dst_mask);
1111 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
1112 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1115 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1116 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1117 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1119 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1120 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1121 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1122 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1124 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1125 shader_addline(arg->buffer, "%smix(vec4(%s), vec4(%s), vec4(lessThan(vec4(%s), vec4(0.0)))))%s;\n",
1126 tmpLine, src1_str, src2_str, src0_str, dst_mask);
1129 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
1130 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1133 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1134 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1135 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1137 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1138 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1139 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1140 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1141 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1142 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
1143 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1146 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1147 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1150 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1151 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1152 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1154 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1155 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1156 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1157 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1158 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1160 shader_addline(arg->buffer, "%s(vec4(%s) * vec4(%s)) + vec4(%s))%s;\n",
1161 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1164 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1165 Vertex shaders to GLSL codes */
1166 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1168 int nComponents = 0;
1169 SHADER_OPCODE_ARG tmpArg;
1171 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1173 /* Set constants for the temporary argument */
1174 tmpArg.shader = arg->shader;
1175 tmpArg.buffer = arg->buffer;
1176 tmpArg.src[0] = arg->src[0];
1177 tmpArg.src_addr[0] = arg->src_addr[0];
1178 tmpArg.src_addr[1] = arg->src_addr[1];
1179 tmpArg.reg_maps = arg->reg_maps;
1181 switch(arg->opcode->opcode) {
1182 case WINED3DSIO_M4x4:
1184 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1186 case WINED3DSIO_M4x3:
1188 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1190 case WINED3DSIO_M3x4:
1192 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1194 case WINED3DSIO_M3x3:
1196 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1198 case WINED3DSIO_M3x2:
1200 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1206 for (i = 0; i < nComponents; i++) {
1207 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1208 tmpArg.src[1] = arg->src[1]+i;
1209 shader_glsl_dot(&tmpArg);
1214 The LRP instruction performs a component-wise linear interpolation
1215 between the second and third operands using the first operand as the
1216 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
1218 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1221 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1222 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1223 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1225 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1226 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1227 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1228 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1230 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1232 shader_addline(arg->buffer, "%s%s + %s * (%s - %s))%s;\n",
1233 tmpLine, src2_str, src0_str, src1_str, src2_str, dst_mask);
1236 /** Process the WINED3DSIO_LIT instruction in GLSL:
1237 * dst.x = dst.w = 1.0
1238 * dst.y = (src0.x > 0) ? src0.x
1239 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1240 * where src.w is clamped at +- 128
1242 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1244 char dst_str[100], src0_str[100];
1245 char dst_reg[50], src0_reg[50];
1246 char dst_mask[6], src0_mask[6];
1248 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1249 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1251 shader_addline(arg->buffer,
1252 "%s = vec4(1.0, (%s.x > 0.0 ? %s.x : 0.0), (%s.x > 0.0 ? ((%s.y > 0.0) ? pow(%s.y, clamp(%s.w, -128.0, 128.0)) : 0.0) : 0.0), 1.0)%s;\n",
1253 dst_str, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, dst_mask);
1256 /** Process the WINED3DSIO_DST instruction in GLSL:
1258 * dst.y = src0.x * src0.y
1262 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1264 char dst_str[100], src0_str[100], src1_str[100];
1265 char dst_reg[50], src0_reg[50], src1_reg[50];
1266 char dst_mask[6], src0_mask[6], src1_mask[6];
1268 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1269 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1270 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1272 shader_addline(arg->buffer, "%s = vec4(1.0, %s.x * %s.y, %s.z, %s.w)%s;\n",
1273 dst_str, src0_reg, src1_reg, src0_reg, src1_reg, dst_mask);
1276 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1277 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1278 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1280 * dst.x = cos(src0.?)
1281 * dst.y = sin(src0.?)
1285 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1287 char dst_str[100], src0_str[100];
1288 char dst_reg[50], src0_reg[50];
1289 char dst_mask[6], src0_mask[6];
1291 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1292 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1294 shader_addline(arg->buffer, "%s = vec4(cos(%s), sin(%s), %s.z, %s.w)%s;\n",
1295 dst_str, src0_str, src0_str, dst_reg, dst_reg, dst_mask);
1298 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1299 * Start a for() loop where src0.y is the initial value of aL,
1300 * increment aL by src0.z for a total of src0.x iterations.
1301 * Need to use a temporary variable for this operation.
1303 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1309 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1311 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1312 src1_reg, src1_reg, src1_reg);
1315 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1316 shader_addline(arg->buffer, "}\n");
1319 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1325 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1326 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s.x; tmpInt++) {\n", src0_reg);
1329 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1335 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1336 shader_addline(arg->buffer, "if (%s) {\n", src0_str);
1339 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1341 char src0_str[100], src1_str[100];
1342 char src0_reg[50], src1_reg[50];
1343 char src0_mask[6], src1_mask[6];
1345 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1346 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1348 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1349 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1352 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1353 shader_addline(arg->buffer, "} else {\n");
1356 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1357 shader_addline(arg->buffer, "break;\n");
1360 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1362 char src0_str[100], src1_str[100];
1363 char src0_reg[50], src1_reg[50];
1364 char src0_mask[6], src1_mask[6];
1366 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1367 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1369 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1370 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1373 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1375 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1376 shader_addline(arg->buffer, "}\n");
1377 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1380 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1381 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1382 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1385 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1391 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1392 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1393 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_str, snum);
1396 /*********************************************
1397 * Pixel Shader Specific Code begins here
1398 ********************************************/
1399 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1400 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1402 DWORD hex_version = This->baseShader.hex_version;
1404 char dst_str[100], dst_reg[50], dst_mask[6];
1405 char coord_str[100], coord_reg[50], coord_mask[6];
1407 /* All versions have a destination register */
1408 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1410 /* 1.0-1.3: Use destination register as coordinate source.
1411 1.4+: Use provided coordinate source register. */
1412 if (hex_version < WINED3DPS_VERSION(1,4))
1413 strcpy(coord_reg, dst_reg);
1415 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, coord_reg, coord_mask, coord_str);
1417 /* 1.0-1.4: Use destination register as sampler source.
1418 * 2.0+: Use provided sampler source. */
1419 if (hex_version < WINED3DPS_VERSION(2,0)) {
1420 shader_glsl_sample(arg, arg->dst & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1422 shader_glsl_sample(arg, arg->src[1] & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1427 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1429 /* FIXME: Make this work for more than just 2D textures */
1431 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1432 SHADER_BUFFER* buffer = arg->buffer;
1433 DWORD hex_version = This->baseShader.hex_version;
1440 shader_glsl_add_param(arg, arg->dst, 0, FALSE, tmpReg, tmpMask, tmpStr);
1442 if (hex_version != WINED3DPS_VERSION(1,4)) {
1443 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1444 shader_addline(buffer, "%s = clamp(gl_TexCoord[%u], 0.0, 1.0);\n", tmpReg, reg);
1446 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1447 shader_addline(buffer, "%s = gl_TexCoord[%u]%s;\n", tmpStr, reg2, tmpMask);
1451 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1452 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1453 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1454 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1456 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1457 char src0_str[100], dst_str[100];
1458 char src0_name[50], dst_name[50];
1459 char src0_mask[6], dst_mask[6];
1461 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1462 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1464 shader_addline(arg->buffer, "tmp0.x = dot(vec3(gl_TexCoord[%u]), vec3(%s));\n", dstreg, src0_str);
1465 shader_addline(arg->buffer, "%s = vec4(texture1D(Psampler%u, tmp0.x))%s;\n", dst_str, dstreg, dst_mask);
1468 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1469 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1470 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1472 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1473 char src0_str[100], dst_str[100];
1474 char src0_name[50], dst_name[50];
1475 char src0_mask[6], dst_mask[6];
1477 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1478 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1480 shader_addline(arg->buffer, "%s = vec4(dot(vec3(T%u), vec3(%s)))%s;\n",
1481 dst_str, dstreg, src0_str, dst_mask);
1484 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1485 * Calculate the depth as dst.x / dst.y */
1486 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1492 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1494 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_reg, dst_reg);
1497 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1498 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1499 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1500 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1502 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1504 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1505 char src0_str[100], dst_str[100];
1506 char src0_name[50], dst_name[50];
1507 char src0_mask[6], dst_mask[6];
1509 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1510 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1512 shader_addline(arg->buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", dstreg, src0_str);
1513 shader_addline(arg->buffer, "gl_FragDepth = vec4((tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y)%s;\n", dst_str, dst_name);
1516 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1517 * Calculate the 1st of a 2-row matrix multiplication. */
1518 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1520 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1521 SHADER_BUFFER* buffer = arg->buffer;
1526 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1527 shader_addline(buffer, "tmp0.x = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1530 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1531 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1532 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1534 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1535 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1536 SHADER_BUFFER* buffer = arg->buffer;
1537 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1542 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1543 shader_addline(buffer, "tmp0.%c = dot(vec3(T%u), vec3(%s));\n", 'x' + current_state->current_row, reg, src0_str);
1544 current_state->texcoord_w[current_state->current_row++] = reg;
1547 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1549 /* FIXME: Make this work for more than just 2D textures */
1551 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1552 SHADER_BUFFER* buffer = arg->buffer;
1557 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1558 shader_addline(buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1559 shader_addline(buffer, "T%u = texture2D(Psampler%u, tmp0.st);\n", reg, reg);
1562 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1563 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1564 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1569 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1570 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1571 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1573 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1574 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1576 /* Sample the texture using the calculated coordinates */
1577 sprintf(dst_str, "T%u", reg);
1578 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1579 current_state->current_row = 0;
1582 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1583 * Perform the 3rd row of a 3x3 matrix multiply */
1584 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
1589 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1590 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1591 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1593 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1595 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1596 shader_addline(arg->buffer, "T%u = vec4(tmp0.x, tmp0.y, tmp0.z, 1.0);\n", reg);
1597 current_state->current_row = 0;
1600 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1601 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1602 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1604 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1605 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1607 char src0_str[100], src0_name[50], src0_mask[6];
1608 char src1_str[100], src1_name[50], src1_mask[6];
1609 SHADER_BUFFER* buffer = arg->buffer;
1610 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1611 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1614 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1615 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1616 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1618 strcpy(dimensions, "");
1619 FIXME("Unrecognized sampler type: %#x\n", stype);
1623 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1624 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_name, src1_mask, src1_str);
1626 /* Perform the last matrix multiply operation */
1627 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1629 /* Calculate reflection vector */
1630 shader_addline(buffer, "tmp0.xyz = reflect(-vec3(%s), vec3(tmp0));\n", src1_str);
1632 /* Sample the texture */
1633 shader_addline(buffer, "T%u = texture%s(Psampler%u, tmp0.%s);\n",
1634 reg, dimensions, reg, (stype == WINED3DSTT_2D) ? "xy" : "xyz");
1635 current_state->current_row = 0;
1638 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1639 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1640 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1642 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1643 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1644 SHADER_BUFFER* buffer = arg->buffer;
1645 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1646 char src0_str[100], src0_name[50], src0_mask[6];
1648 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1650 /* Perform the last matrix multiply operation */
1651 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1653 /* Construct the eye-ray vector from w coordinates */
1654 shader_addline(buffer, "tmp1.x = gl_TexCoord[%u].w;\n", current_state->texcoord_w[0]);
1655 shader_addline(buffer, "tmp1.y = gl_TexCoord[%u].w;\n", current_state->texcoord_w[1]);
1656 shader_addline(buffer, "tmp1.z = gl_TexCoord[%u].w;\n", reg);
1658 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
1659 shader_addline(buffer, "tmp0.x = dot(vec3(tmp0), vec3(tmp1));\n");
1660 shader_addline(buffer, "tmp0 = tmp0.w * tmp0;\n");
1661 shader_addline(buffer, "tmp0 = (2.0 * tmp0) - tmp1;\n");
1664 * We don't really know if a Cube or a Volume texture is being sampled, but since Cube textures
1665 * are used more commonly, we'll default to that.
1666 * We probably need to push back the pixel shader generation code until drawPrimitive() for
1667 * shader versions < 2.0, since that's the only time we can guarantee that we're sampling
1668 * the correct type of texture because we can lookup what textures are bound at that point.
1670 shader_addline(buffer, "T%u = textureCube(Psampler%u, tmp0.xyz);\n", reg, reg);
1671 current_state->current_row = 0;
1674 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1675 * Apply a fake bump map transform.
1676 * FIXME: Should apply the BUMPMAPENV matrix. For now, just sample the texture */
1677 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1679 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1680 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1682 FIXME("Not applying the BUMPMAPENV matrix for pixel shader instruction texbem.\n");
1683 shader_addline(arg->buffer, "T%u = texture2D(Psampler%u, gl_TexCoord[%u].xy + T%u.xy);\n",
1684 reg1, reg1, reg1, reg2);
1687 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1688 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1689 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1692 char dst_str[100], src0_str[100];
1693 char dst_reg[50], src0_reg[50];
1694 char dst_mask[6], src0_mask[6];
1695 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1697 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1698 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1700 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1701 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1702 tmpLine, src0_regnum, dst_reg, dst_mask);
1705 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1706 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1707 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1710 char dst_str[100], src0_str[100];
1711 char dst_reg[50], src0_reg[50];
1712 char dst_mask[6], src0_mask[6];
1713 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1715 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1716 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1718 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1719 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1720 tmpLine, src0_regnum, dst_reg, dst_mask);
1723 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
1724 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
1725 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1728 char dst_str[100], src0_str[100];
1729 char dst_reg[50], src0_reg[50];
1730 char dst_mask[6], src0_mask[6];
1732 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1733 DWORD stype = arg->reg_maps->samplers[src0_regnum] & WINED3DSP_TEXTURETYPE_MASK;
1735 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1736 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1737 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1739 strcpy(dimensions, "");
1740 FIXME("Unrecognized sampler type: %#x\n", stype);
1744 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1745 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1747 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1748 shader_addline(arg->buffer, "%stexture%s(Psampler%u, %s.%s))%s;\n",
1749 tmpLine, dimensions, src0_regnum, dst_reg, (stype == WINED3DSTT_2D) ? "xy" : "xyz", dst_mask);
1752 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
1753 * If any of the first 3 components are < 0, discard this pixel */
1754 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
1756 char dst_str[100], dst_name[50], dst_mask[6];
1758 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1759 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_name);
1762 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
1763 * dst = dot2(src0, src1) + src2 */
1764 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
1767 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1768 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1769 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1771 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1772 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1773 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1774 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1775 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1776 shader_addline(arg->buffer, "%sdot(vec2(%s), vec2(%s)) + %s)%s;\n",
1777 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1780 void pshader_glsl_input_pack(
1781 SHADER_BUFFER* buffer,
1782 semantic* semantics_in) {
1786 for (i = 0; i < MAX_REG_INPUT; i++) {
1788 DWORD usage_token = semantics_in[i].usage;
1789 DWORD register_token = semantics_in[i].reg;
1790 DWORD usage, usage_idx;
1794 if (!usage_token) continue;
1795 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1796 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1797 shader_glsl_get_output_register_swizzle(register_token, reg_mask);
1801 case D3DDECLUSAGE_COLOR:
1803 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
1804 i, reg_mask, reg_mask);
1805 else if (usage_idx == 1)
1806 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
1807 i, reg_mask, reg_mask);
1809 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
1810 i, reg_mask, reg_mask);
1813 case D3DDECLUSAGE_TEXCOORD:
1814 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
1815 i, reg_mask, usage_idx, reg_mask );
1818 case D3DDECLUSAGE_FOG:
1819 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
1820 i, reg_mask, reg_mask);
1824 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
1825 i, reg_mask, reg_mask);
1830 /*********************************************
1831 * Vertex Shader Specific Code begins here
1832 ********************************************/
1834 void vshader_glsl_output_unpack(
1835 SHADER_BUFFER* buffer,
1836 semantic* semantics_out) {
1840 for (i = 0; i < MAX_REG_OUTPUT; i++) {
1842 DWORD usage_token = semantics_out[i].usage;
1843 DWORD register_token = semantics_out[i].reg;
1844 DWORD usage, usage_idx;
1848 if (!usage_token) continue;
1850 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1851 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1852 shader_glsl_get_output_register_swizzle(register_token, reg_mask);
1856 case D3DDECLUSAGE_COLOR:
1858 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1859 else if (usage_idx == 1)
1860 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1862 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1865 case D3DDECLUSAGE_POSITION:
1866 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1869 case D3DDECLUSAGE_TEXCOORD:
1870 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
1871 usage_idx, reg_mask, i, reg_mask);
1874 case WINED3DSHADERDECLUSAGE_PSIZE:
1875 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
1878 case WINED3DSHADERDECLUSAGE_FOG:
1879 shader_addline(buffer, "gl_FogFragCoord%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1883 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1888 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
1889 GLhandleARB program_id;
1890 GLhandleARB vshader_id, pshader_id;
1891 const char *blt_vshader[] = {
1894 " gl_Position = gl_Vertex;\n"
1895 " gl_FrontColor = vec4(1.0);\n"
1896 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
1897 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
1901 const char *blt_pshader[] = {
1902 "uniform sampler2D sampler;\n"
1905 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
1909 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
1910 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
1911 GL_EXTCALL(glCompileShaderARB(vshader_id));
1913 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
1914 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
1915 GL_EXTCALL(glCompileShaderARB(pshader_id));
1917 program_id = GL_EXTCALL(glCreateProgramObjectARB());
1918 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
1919 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
1920 GL_EXTCALL(glLinkProgramARB(program_id));
1922 print_glsl_info_log(&GLINFO_LOCATION, program_id);
1927 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
1928 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1929 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1930 GLhandleARB program_id = 0;
1932 if (useVS || usePS) set_glsl_shader_program(iface);
1933 else This->stateBlock->glsl_program = NULL;
1935 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
1936 if (program_id) TRACE("Using GLSL program %u\n", program_id);
1937 GL_EXTCALL(glUseProgramObjectARB(program_id));
1938 checkGLcall("glUseProgramObjectARB");
1941 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
1942 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1943 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1944 static GLhandleARB program_id = 0;
1945 static GLhandleARB loc = -1;
1948 program_id = create_glsl_blt_shader(gl_info);
1949 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
1952 GL_EXTCALL(glUseProgramObjectARB(program_id));
1953 GL_EXTCALL(glUniform1iARB(loc, 0));
1956 static void shader_glsl_cleanup(BOOL usePS, BOOL useVS) {
1960 const shader_backend_t glsl_shader_backend = {
1961 &shader_glsl_select,
1962 &shader_glsl_select_depth_blt,
1963 &shader_glsl_load_constants,
1964 &shader_glsl_cleanup