wined3d: Pass the correct mask to shader_glsl_add_src_param() in pshader_glsl_texdp3t...
[wine/wine64.git] / dlls / wined3d / glsl_shader.c
blob789d84659c9beae92f0bd3e6310d084b94e7a3e1
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 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
23 * D3D shader asm has swizzles on source parameters, and write masks for
24 * destination parameters. GLSL uses swizzles for both. The result of this is
25 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
26 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
27 * mask for the destination parameter into account.
30 #include "config.h"
31 #include <stdio.h>
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 #define GLINFO_LOCATION (*gl_info)
38 typedef struct {
39 const char *name;
40 DWORD coord_mask;
41 } glsl_sample_function_t;
43 /** Prints the GLSL info log which will contain error messages if they exist */
44 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
46 int infologLength = 0;
47 char *infoLog;
49 GL_EXTCALL(glGetObjectParameterivARB(obj,
50 GL_OBJECT_INFO_LOG_LENGTH_ARB,
51 &infologLength));
53 /* A size of 1 is just a null-terminated string, so the log should be bigger than
54 * that if there are errors. */
55 if (infologLength > 1)
57 infoLog = (char *)HeapAlloc(GetProcessHeap(), 0, infologLength);
58 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
59 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
60 HeapFree(GetProcessHeap(), 0, infoLog);
64 /**
65 * Loads (pixel shader) samplers
67 void shader_glsl_load_psamplers(
68 WineD3D_GL_Info *gl_info,
69 IWineD3DStateBlock* iface) {
71 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
72 GLhandleARB programId = stateBlock->glsl_program->programId;
73 GLhandleARB name_loc;
74 int i;
75 char sampler_name[20];
77 for (i=0; i< GL_LIMITS(samplers); ++i) {
78 if (stateBlock->textures[i] != NULL) {
79 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
80 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
81 if (name_loc != -1) {
82 TRACE("Loading %s for texture %d\n", sampler_name, i);
83 GL_EXTCALL(glUniform1iARB(name_loc, i));
84 checkGLcall("glUniform1iARB");
90 /**
91 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
92 * When constant_list == NULL, it will load all the constants.
94 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
95 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
96 struct list *constant_list) {
97 local_constant* lconst;
98 GLhandleARB tmp_loc;
99 int i;
101 if (!constant_list) {
102 if (TRACE_ON(d3d_shader)) {
103 for (i = 0; i < max_constants; ++i) {
104 tmp_loc = constant_locations[i];
105 if (tmp_loc != -1) {
106 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
107 constants[i * 4 + 0], constants[i * 4 + 1],
108 constants[i * 4 + 2], constants[i * 4 + 3]);
112 for (i = 0; i < max_constants; ++i) {
113 tmp_loc = constant_locations[i];
114 if (tmp_loc != -1) {
115 /* We found this uniform name in the program - go ahead and send the data */
116 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
119 checkGLcall("glUniform4fvARB()");
120 } else {
121 constant_entry *constant;
122 if (TRACE_ON(d3d_shader)) {
123 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
124 i = constant->idx;
125 tmp_loc = constant_locations[i];
126 if (tmp_loc != -1) {
127 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
128 constants[i * 4 + 0], constants[i * 4 + 1],
129 constants[i * 4 + 2], constants[i * 4 + 3]);
133 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
134 i = constant->idx;
135 tmp_loc = constant_locations[i];
136 if (tmp_loc != -1) {
137 /* We found this uniform name in the program - go ahead and send the data */
138 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
141 checkGLcall("glUniform4fvARB()");
144 /* Load immediate constants */
145 if (TRACE_ON(d3d_shader)) {
146 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
147 tmp_loc = constant_locations[lconst->idx];
148 if (tmp_loc != -1) {
149 GLfloat* values = (GLfloat*)lconst->value;
150 TRACE("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
151 values[0], values[1], values[2], values[3]);
155 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
156 tmp_loc = constant_locations[lconst->idx];
157 if (tmp_loc != -1) {
158 /* We found this uniform name in the program - go ahead and send the data */
159 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
162 checkGLcall("glUniform4fvARB()");
165 /**
166 * Loads integer constants (aka uniforms) into the currently set GLSL program.
167 * When @constants_set == NULL, it will load all the constants.
169 void shader_glsl_load_constantsI(
170 IWineD3DBaseShaderImpl* This,
171 WineD3D_GL_Info *gl_info,
172 GLhandleARB programId,
173 unsigned max_constants,
174 int* constants,
175 BOOL* constants_set) {
177 GLhandleARB tmp_loc;
178 int i;
179 char tmp_name[8];
180 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
181 const char* prefix = is_pshader? "PI":"VI";
182 struct list* ptr;
184 for (i=0; i<max_constants; ++i) {
185 if (NULL == constants_set || constants_set[i]) {
187 TRACE("Loading constants %i: %i, %i, %i, %i\n",
188 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
190 /* TODO: Benchmark and see if it would be beneficial to store the
191 * locations of the constants to avoid looking up each time */
192 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
193 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
194 if (tmp_loc != -1) {
195 /* We found this uniform name in the program - go ahead and send the data */
196 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
197 checkGLcall("glUniform4ivARB");
202 /* Load immediate constants */
203 ptr = list_head(&This->baseShader.constantsI);
204 while (ptr) {
205 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
206 unsigned int idx = lconst->idx;
207 GLint* values = (GLint*) lconst->value;
209 TRACE("Loading local constants %i: %i, %i, %i, %i\n", idx,
210 values[0], values[1], values[2], values[3]);
212 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
213 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
214 if (tmp_loc != -1) {
215 /* We found this uniform name in the program - go ahead and send the data */
216 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
217 checkGLcall("glUniform4ivARB");
219 ptr = list_next(&This->baseShader.constantsI, ptr);
223 /**
224 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
225 * When @constants_set == NULL, it will load all the constants.
227 void shader_glsl_load_constantsB(
228 IWineD3DBaseShaderImpl* This,
229 WineD3D_GL_Info *gl_info,
230 GLhandleARB programId,
231 unsigned max_constants,
232 BOOL* constants,
233 BOOL* constants_set) {
235 GLhandleARB tmp_loc;
236 int i;
237 char tmp_name[8];
238 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
239 const char* prefix = is_pshader? "PB":"VB";
240 struct list* ptr;
242 for (i=0; i<max_constants; ++i) {
243 if (NULL == constants_set || constants_set[i]) {
245 TRACE("Loading constants %i: %i;\n", i, constants[i*4]);
247 /* TODO: Benchmark and see if it would be beneficial to store the
248 * locations of the constants to avoid looking up each time */
249 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
250 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
251 if (tmp_loc != -1) {
252 /* We found this uniform name in the program - go ahead and send the data */
253 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
254 checkGLcall("glUniform1ivARB");
259 /* Load immediate constants */
260 ptr = list_head(&This->baseShader.constantsB);
261 while (ptr) {
262 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
263 unsigned int idx = lconst->idx;
264 GLint* values = (GLint*) lconst->value;
266 TRACE("Loading local constants %i: %i\n", idx, values[0]);
268 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
269 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
270 if (tmp_loc != -1) {
271 /* We found this uniform name in the program - go ahead and send the data */
272 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
273 checkGLcall("glUniform1ivARB");
275 ptr = list_next(&This->baseShader.constantsB, ptr);
282 * Loads the app-supplied constants into the currently set GLSL program.
284 void shader_glsl_load_constants(
285 IWineD3DDevice* device,
286 char usePixelShader,
287 char useVertexShader) {
289 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
290 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
291 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*) deviceImpl->wineD3D)->gl_info;
293 GLhandleARB *constant_locations;
294 struct list *constant_list;
295 GLhandleARB programId;
297 if (!stateBlock->glsl_program) {
298 /* No GLSL program set - nothing to do. */
299 return;
301 programId = stateBlock->glsl_program->programId;
303 if (useVertexShader) {
304 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
305 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) vshader;
306 GLint pos;
308 IWineD3DVertexDeclarationImpl* vertexDeclaration =
309 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
311 constant_locations = stateBlock->glsl_program->vuniformF_locations;
312 constant_list = &stateBlock->set_vconstantsF;
314 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
315 /* Load DirectX 8 float constants/uniforms for vertex shader */
316 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
317 vertexDeclaration->constants, constant_locations, NULL);
320 /* Load DirectX 9 float constants/uniforms for vertex shader */
321 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
322 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
324 /* Load DirectX 9 integer constants/uniforms for vertex shader */
325 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
326 stateBlock->vertexShaderConstantI,
327 stateBlock->set.vertexShaderConstantsI);
329 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
330 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
331 stateBlock->vertexShaderConstantB,
332 stateBlock->set.vertexShaderConstantsB);
334 /* Upload the position fixup params */
335 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
336 checkGLcall("glGetUniformLocationARB");
337 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
338 checkGLcall("glUniform4fvARB");
341 if (usePixelShader) {
343 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
345 constant_locations = stateBlock->glsl_program->puniformF_locations;
346 constant_list = &stateBlock->set_pconstantsF;
348 /* Load pixel shader samplers */
349 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
351 /* Load DirectX 9 float constants/uniforms for pixel shader */
352 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
353 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
355 /* Load DirectX 9 integer constants/uniforms for pixel shader */
356 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
357 stateBlock->pixelShaderConstantI,
358 stateBlock->set.pixelShaderConstantsI);
360 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
361 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
362 stateBlock->pixelShaderConstantB,
363 stateBlock->set.pixelShaderConstantsB);
367 /** Generate the variable & register declarations for the GLSL output target */
368 void shader_generate_glsl_declarations(
369 IWineD3DBaseShader *iface,
370 shader_reg_maps* reg_maps,
371 SHADER_BUFFER* buffer,
372 WineD3D_GL_Info* gl_info) {
374 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
375 int i;
377 /* There are some minor differences between pixel and vertex shaders */
378 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
379 char prefix = pshader ? 'P' : 'V';
381 /* Prototype the subroutines */
382 for (i = 0; i < This->baseShader.limits.label; i++) {
383 if (reg_maps->labels[i])
384 shader_addline(buffer, "void subroutine%lu();\n", i);
387 /* Declare the constants (aka uniforms) */
388 if (This->baseShader.limits.constant_float > 0) {
389 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
390 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
391 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
394 if (This->baseShader.limits.constant_int > 0)
395 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
397 if (This->baseShader.limits.constant_bool > 0)
398 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
400 if(!pshader)
401 shader_addline(buffer, "uniform vec4 posFixup;\n");
403 /* Declare texture samplers */
404 for (i = 0; i < This->baseShader.limits.sampler; i++) {
405 if (reg_maps->samplers[i]) {
407 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
408 switch (stype) {
410 case WINED3DSTT_1D:
411 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
412 break;
413 case WINED3DSTT_2D:
414 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
415 break;
416 case WINED3DSTT_CUBE:
417 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
418 break;
419 case WINED3DSTT_VOLUME:
420 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
421 break;
422 default:
423 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
424 FIXME("Unrecognized sampler type: %#x\n", stype);
425 break;
430 /* Declare address variables */
431 for (i = 0; i < This->baseShader.limits.address; i++) {
432 if (reg_maps->address[i])
433 shader_addline(buffer, "ivec4 A%d;\n", i);
436 /* Declare texture coordinate temporaries and initialize them */
437 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
438 if (reg_maps->texcoord[i])
439 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
442 /* Declare input register temporaries */
443 for (i=0; i < This->baseShader.limits.packed_input; i++) {
444 if (reg_maps->packed_input[i])
445 shader_addline(buffer, "vec4 IN%lu;\n", i);
448 /* Declare output register temporaries */
449 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
450 if (reg_maps->packed_output[i])
451 shader_addline(buffer, "vec4 OUT%lu;\n", i);
454 /* Declare temporary variables */
455 for(i = 0; i < This->baseShader.limits.temporary; i++) {
456 if (reg_maps->temporary[i])
457 shader_addline(buffer, "vec4 R%lu;\n", i);
460 /* Declare attributes */
461 for (i = 0; i < This->baseShader.limits.attributes; i++) {
462 if (reg_maps->attributes[i])
463 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
466 /* Declare loop register aL */
467 if (reg_maps->loop) {
468 shader_addline(buffer, "int aL;\n");
469 shader_addline(buffer, "int tmpInt;\n");
472 /* Temporary variables for matrix operations */
473 shader_addline(buffer, "vec4 tmp0;\n");
474 shader_addline(buffer, "vec4 tmp1;\n");
476 /* Start the main program */
477 shader_addline(buffer, "void main() {\n");
480 /*****************************************************************************
481 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
483 * For more information, see http://wiki.winehq.org/DirectX-Shaders
484 ****************************************************************************/
486 /* Prototypes */
487 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
488 const DWORD addr_token, DWORD mask, char *reg_name, char *swizzle, char *out_str);
490 /* FIXME: This should be removed */
491 static void shader_glsl_add_src_param_old(SHADER_OPCODE_ARG* arg, const DWORD param,
492 const DWORD addr_token, char *reg_name, char *swizzle, char *out_str) {
493 shader_glsl_add_src_param(arg, param, addr_token, 0, reg_name, swizzle, out_str);
496 /** Used for opcode modifiers - They multiply the result by the specified amount */
497 static const char * const shift_glsl_tab[] = {
498 "", /* 0 (none) */
499 "2.0 * ", /* 1 (x2) */
500 "4.0 * ", /* 2 (x4) */
501 "8.0 * ", /* 3 (x8) */
502 "16.0 * ", /* 4 (x16) */
503 "32.0 * ", /* 5 (x32) */
504 "", /* 6 (x64) */
505 "", /* 7 (x128) */
506 "", /* 8 (d256) */
507 "", /* 9 (d128) */
508 "", /* 10 (d64) */
509 "", /* 11 (d32) */
510 "0.0625 * ", /* 12 (d16) */
511 "0.125 * ", /* 13 (d8) */
512 "0.25 * ", /* 14 (d4) */
513 "0.5 * " /* 15 (d2) */
516 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
517 static void shader_glsl_gen_modifier (
518 const DWORD instr,
519 const char *in_reg,
520 const char *in_regswizzle,
521 char *out_str) {
523 out_str[0] = 0;
525 if (instr == WINED3DSIO_TEXKILL)
526 return;
528 switch (instr & WINED3DSP_SRCMOD_MASK) {
529 case WINED3DSPSM_NONE:
530 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
531 break;
532 case WINED3DSPSM_NEG:
533 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
534 break;
535 case WINED3DSPSM_NOT:
536 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
537 break;
538 case WINED3DSPSM_BIAS:
539 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
540 break;
541 case WINED3DSPSM_BIASNEG:
542 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
543 break;
544 case WINED3DSPSM_SIGN:
545 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
546 break;
547 case WINED3DSPSM_SIGNNEG:
548 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
549 break;
550 case WINED3DSPSM_COMP:
551 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
552 break;
553 case WINED3DSPSM_X2:
554 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
555 break;
556 case WINED3DSPSM_X2NEG:
557 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
558 break;
559 case WINED3DSPSM_DZ: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
560 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);
561 break;
562 case WINED3DSPSM_DW:
563 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);
564 break;
565 case WINED3DSPSM_ABS:
566 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
567 break;
568 case WINED3DSPSM_ABSNEG:
569 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
570 break;
571 default:
572 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
573 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
577 /** Writes the GLSL variable name that corresponds to the register that the
578 * DX opcode parameter is trying to access */
579 static void shader_glsl_get_register_name(
580 const DWORD param,
581 const DWORD addr_token,
582 char* regstr,
583 BOOL* is_color,
584 SHADER_OPCODE_ARG* arg) {
586 /* oPos, oFog and oPts in D3D */
587 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
589 DWORD reg = param & WINED3DSP_REGNUM_MASK;
590 DWORD regtype = shader_get_regtype(param);
591 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
592 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
593 WineD3D_GL_Info* gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
595 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
596 char tmpStr[50];
598 *is_color = FALSE;
600 switch (regtype) {
601 case WINED3DSPR_TEMP:
602 sprintf(tmpStr, "R%u", reg);
603 break;
604 case WINED3DSPR_INPUT:
605 if (pshader) {
606 /* Pixel shaders >= 3.0 */
607 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
608 sprintf(tmpStr, "IN%u", reg);
609 else {
610 if (reg==0)
611 strcpy(tmpStr, "gl_Color");
612 else
613 strcpy(tmpStr, "gl_SecondaryColor");
615 } else {
616 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
617 *is_color = TRUE;
618 sprintf(tmpStr, "attrib%u", reg);
620 break;
621 case WINED3DSPR_CONST:
623 const char* prefix = pshader? "PC":"VC";
625 /* Relative addressing */
626 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
628 /* Relative addressing on shaders 2.0+ have a relative address token,
629 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
630 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
631 char relStr[100], relReg[50], relMask[6];
632 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, relReg, relMask, relStr);
633 sprintf(tmpStr, "%s[%s + %u]", prefix, relStr, reg);
634 } else
635 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
637 } else
638 sprintf(tmpStr, "%s[%u]", prefix, reg);
640 break;
642 case WINED3DSPR_CONSTINT:
643 if (pshader)
644 sprintf(tmpStr, "PI[%u]", reg);
645 else
646 sprintf(tmpStr, "VI[%u]", reg);
647 break;
648 case WINED3DSPR_CONSTBOOL:
649 if (pshader)
650 sprintf(tmpStr, "PB[%u]", reg);
651 else
652 sprintf(tmpStr, "VB[%u]", reg);
653 break;
654 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
655 if (pshader) {
656 sprintf(tmpStr, "T%u", reg);
657 } else {
658 sprintf(tmpStr, "A%u", reg);
660 break;
661 case WINED3DSPR_LOOP:
662 sprintf(tmpStr, "aL");
663 break;
664 case WINED3DSPR_SAMPLER:
665 if (pshader)
666 sprintf(tmpStr, "Psampler%u", reg);
667 else
668 sprintf(tmpStr, "Vsampler%u", reg);
669 break;
670 case WINED3DSPR_COLOROUT:
671 if (reg >= GL_LIMITS(buffers)) {
672 WARN("Write to render target %u, only %d supported\n", reg, 4);
674 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
675 sprintf(tmpStr, "gl_FragData[%u]", reg);
676 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
677 sprintf(tmpStr, "gl_FragColor");
679 break;
680 case WINED3DSPR_RASTOUT:
681 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
682 break;
683 case WINED3DSPR_DEPTHOUT:
684 sprintf(tmpStr, "gl_FragDepth");
685 break;
686 case WINED3DSPR_ATTROUT:
687 if (reg == 0) {
688 sprintf(tmpStr, "gl_FrontColor");
689 } else {
690 sprintf(tmpStr, "gl_FrontSecondaryColor");
692 break;
693 case WINED3DSPR_TEXCRDOUT:
694 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
695 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
696 sprintf(tmpStr, "OUT%u", reg);
697 else
698 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
699 break;
700 default:
701 FIXME("Unhandled register name Type(%d)\n", regtype);
702 sprintf(tmpStr, "unrecognized_register");
703 break;
706 strcat(regstr, tmpStr);
709 /* Get the GLSL write mask for the destination register */
710 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
711 char *ptr = write_mask;
712 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
714 /* gl_FogFragCoord and glPointSize are floats, fixup the write mask. */
715 if ((shader_get_regtype(param) == WINED3DSPR_RASTOUT) && ((param & WINED3DSP_REGNUM_MASK) != 0)) {
716 mask = WINED3DSP_WRITEMASK_0;
717 } else {
718 if (mask != WINED3DSP_WRITEMASK_ALL) {
719 *ptr++ = '.';
720 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
721 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
722 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
723 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
727 *ptr = '\0';
729 return mask;
732 static size_t shader_glsl_get_write_mask_size(DWORD write_mask) {
733 size_t size = 0;
735 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
736 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
737 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
738 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
740 return size;
743 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
744 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
745 * but addressed as "rgba". To fix this we need to swap the register's x
746 * and z components. */
747 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
748 char *ptr = swizzle_str;
750 /* swizzle bits fields: wwzzyyxx */
751 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
752 DWORD swizzle_x = swizzle & 0x03;
753 DWORD swizzle_y = (swizzle >> 2) & 0x03;
754 DWORD swizzle_z = (swizzle >> 4) & 0x03;
755 DWORD swizzle_w = (swizzle >> 6) & 0x03;
757 /* FIXME: This is here to keep shader_glsl_add_src_param_old happy.
758 * As soon as that is removed, this needs to go as well. */
759 if (!mask) {
760 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
761 * generate a swizzle string. Unless we need to our own swizzling. */
762 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
763 *ptr++ = '.';
764 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
765 *ptr++ = swizzle_chars[swizzle_x];
766 } else {
767 *ptr++ = swizzle_chars[swizzle_x];
768 *ptr++ = swizzle_chars[swizzle_y];
769 *ptr++ = swizzle_chars[swizzle_z];
770 *ptr++ = swizzle_chars[swizzle_w];
773 } else {
774 *ptr++ = '.';
775 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle_x];
776 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[swizzle_y];
777 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[swizzle_z];
778 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[swizzle_w];
781 *ptr = '\0';
784 /* From a given parameter token, generate the corresponding GLSL string.
785 * Also, return the actual register name and swizzle in case the
786 * caller needs this information as well. */
787 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
788 const DWORD addr_token, DWORD mask, char *reg_name, char *swizzle, char *out_str) {
789 BOOL is_color = FALSE;
790 swizzle[0] = reg_name[0] = out_str[0] = 0;
792 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
794 shader_glsl_get_swizzle(param, is_color, mask, swizzle);
795 shader_glsl_gen_modifier(param, reg_name, swizzle, out_str);
798 /* From a given parameter token, generate the corresponding GLSL string.
799 * Also, return the actual register name and swizzle in case the
800 * caller needs this information as well. */
801 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
802 const DWORD addr_token, char *reg_name, char *write_mask, char *out_str) {
803 BOOL is_color = FALSE;
804 DWORD mask;
805 write_mask[0] = reg_name[0] = out_str[0] = 0;
807 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
809 mask = shader_glsl_get_write_mask(param, write_mask);
810 sprintf(out_str, "%s%s", reg_name, write_mask);
812 return mask;
815 /* Append the destination part of the instruction to the buffer, return the effective write mask */
816 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
817 char reg_name[50], write_mask[6], reg_str[100];
818 DWORD mask;
819 int shift;
821 shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
822 mask = shader_glsl_add_dst_param(arg, arg->dst, arg->dst_addr, reg_name, write_mask, reg_str);
823 shader_addline(buffer, "%s = %s(", reg_str, shift_glsl_tab[shift]);
825 return mask;
828 /** Process GLSL instruction modifiers */
829 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
831 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
833 if (arg->opcode->dst_token && mask != 0) {
834 char dst_reg[50];
835 char dst_mask[6];
836 char dst_str[100];
838 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
840 if (mask & WINED3DSPDM_SATURATE) {
841 /* _SAT means to clamp the value of the register to between 0 and 1 */
842 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
844 if (mask & WINED3DSPDM_MSAMPCENTROID) {
845 FIXME("_centroid modifier not handled\n");
847 if (mask & WINED3DSPDM_PARTIALPRECISION) {
848 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
853 static inline const char* shader_get_comp_op(
854 const DWORD opcode) {
856 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
857 switch (op) {
858 case COMPARISON_GT: return ">";
859 case COMPARISON_EQ: return "==";
860 case COMPARISON_GE: return ">=";
861 case COMPARISON_LT: return "<";
862 case COMPARISON_NE: return "!=";
863 case COMPARISON_LE: return "<=";
864 default:
865 FIXME("Unrecognized comparison value: %u\n", op);
866 return "(\?\?)";
870 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, glsl_sample_function_t *sample_function) {
871 /* Note that there's no such thing as a projected cube texture. */
872 switch(sampler_type) {
873 case WINED3DSTT_1D:
874 sample_function->name = projected ? "texture1DProj" : "texture1D";
875 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
876 break;
877 case WINED3DSTT_2D:
878 sample_function->name = projected ? "texture2DProj" : "texture2D";
879 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
880 break;
881 case WINED3DSTT_CUBE:
882 sample_function->name = "textureCube";
883 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
884 break;
885 case WINED3DSTT_VOLUME:
886 sample_function->name = projected ? "texture3DProj" : "texture3D";
887 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
888 break;
889 default:
890 sample_function->name = "";
891 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
892 break;
896 static void shader_glsl_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
897 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
898 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
899 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
900 const char sampler_prefix = shader_is_pshader_version(This->baseShader.hex_version) ? 'P' : 'V';
901 SHADER_BUFFER* buffer = arg->buffer;
902 glsl_sample_function_t sample_function;
904 if(deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
905 shader_glsl_get_sample_function(sampler_type, TRUE, &sample_function);
906 shader_addline(buffer, "%s = %s(%csampler%u, %s);\n", dst_str, sample_function.name, sampler_prefix, sampler_idx, coord_reg);
907 } else {
908 char coord_swizzle[6];
910 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
911 shader_glsl_get_write_mask(sample_function.coord_mask, coord_swizzle);
913 shader_addline(buffer, "%s = %s(%csampler%u, %s%s);\n", dst_str, sample_function.name, sampler_prefix, sampler_idx, coord_reg, coord_swizzle);
918 /*****************************************************************************
920 * Begin processing individual instruction opcodes
922 ****************************************************************************/
924 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
925 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
926 CONST SHADER_OPCODE* curOpcode = arg->opcode;
927 SHADER_BUFFER* buffer = arg->buffer;
928 char src0_reg[50], src1_reg[50];
929 char src0_mask[6], src1_mask[6];
930 char src0_str[100], src1_str[100];
931 DWORD write_mask;
932 char op;
934 /* Determine the GLSL operator to use based on the opcode */
935 switch (curOpcode->opcode) {
936 case WINED3DSIO_MUL: op = '*'; break;
937 case WINED3DSIO_ADD: op = '+'; break;
938 case WINED3DSIO_SUB: op = '-'; break;
939 default:
940 op = ' ';
941 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
942 break;
945 write_mask = shader_glsl_append_dst(buffer, arg);
946 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
947 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, src1_reg, src1_mask, src1_str);
948 shader_addline(buffer, "%s %c %s);\n", src0_str, op, src1_str);
951 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
952 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
953 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
954 SHADER_BUFFER* buffer = arg->buffer;
955 char src0_str[100];
956 char src0_reg[50];
957 char src0_mask[6];
958 DWORD write_mask;
960 write_mask = shader_glsl_append_dst(buffer, arg);
961 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
963 /* In vs_1_1 WINED3DSIO_MOV can write to the adress register. In later
964 * shader versions WINED3DSIO_MOVA is used for this. */
965 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
966 !shader_is_pshader_version(shader->baseShader.hex_version) &&
967 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR) ||
968 arg->opcode->opcode == WINED3DSIO_MOVA) {
969 /* We need to *round* to the nearest int here. */
970 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
971 if (mask_size > 1) {
972 shader_addline(buffer, "ivec%d(floor(%s + vec%d(0.5))));\n", mask_size, src0_str, mask_size);
973 } else {
974 shader_addline(buffer, "int(floor(%s + 0.5)));\n", src0_str);
976 } else {
977 shader_addline(buffer, "%s);\n", src0_str);
981 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
982 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
983 CONST SHADER_OPCODE* curOpcode = arg->opcode;
984 SHADER_BUFFER* buffer = arg->buffer;
985 char src0_str[100], src1_str[100];
986 char src0_reg[50], src1_reg[50];
987 char src0_mask[6], src1_mask[6];
988 DWORD dst_write_mask, src_write_mask;
989 size_t dst_size = 0;
991 dst_write_mask = shader_glsl_append_dst(buffer, arg);
992 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
994 /* dp3 works on vec3, dp4 on vec4 */
995 if (curOpcode->opcode == WINED3DSIO_DP4) {
996 src_write_mask = WINED3DSP_WRITEMASK_ALL;
997 } else {
998 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1001 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, src0_reg, src0_mask, src0_str);
1002 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, src1_reg, src1_mask, src1_str);
1004 if (dst_size > 1) {
1005 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_str, src1_str);
1006 } else {
1007 shader_addline(buffer, "dot(%s, %s));\n", src0_str, src1_str);
1011 /* Note that this instruction has some restrictions. The destination write mask
1012 * can't contain the w component, and the source swizzles have to be .xyzw */
1013 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
1014 char src0_reg[50], src0_mask[6], src0_str[100];
1015 char src1_reg[50], src1_mask[6], src1_str[100];
1016 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1017 char dst_mask[6];
1019 shader_glsl_get_write_mask(arg->dst, dst_mask);
1020 shader_glsl_append_dst(arg->buffer, arg);
1021 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, src0_reg, src0_mask, src0_str);
1022 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, src1_reg, src1_mask, src1_str);
1023 shader_addline(arg->buffer, "cross(%s, %s).%s);\n", src0_str, src1_str, dst_mask);
1026 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1027 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1028 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1029 SHADER_BUFFER* buffer = arg->buffer;
1030 const char *instruction;
1031 char arguments[256];
1032 char src_str[100];
1033 char src_reg[50];
1034 char src_mask[6];
1035 DWORD write_mask;
1036 unsigned i;
1038 /* Determine the GLSL function to use based on the opcode */
1039 /* TODO: Possibly make this a table for faster lookups */
1040 switch (curOpcode->opcode) {
1041 case WINED3DSIO_MIN: instruction = "min"; break;
1042 case WINED3DSIO_MAX: instruction = "max"; break;
1043 case WINED3DSIO_RSQ: instruction = "inversesqrt"; break;
1044 case WINED3DSIO_ABS: instruction = "abs"; break;
1045 case WINED3DSIO_FRC: instruction = "fract"; break;
1046 case WINED3DSIO_POW: instruction = "pow"; break;
1047 case WINED3DSIO_NRM: instruction = "normalize"; break;
1048 case WINED3DSIO_LOGP:
1049 case WINED3DSIO_LOG: instruction = "log2"; break;
1050 case WINED3DSIO_EXP: instruction = "exp2"; break;
1051 case WINED3DSIO_SGN: instruction = "sign"; break;
1052 default: instruction = "";
1053 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1054 break;
1057 write_mask = shader_glsl_append_dst(buffer, arg);
1059 arguments[0] = '\0';
1060 if (curOpcode->num_params > 0) {
1061 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src_reg, src_mask, src_str);
1062 strcat(arguments, src_str);
1063 for (i = 2; i < curOpcode->num_params; ++i) {
1064 strcat(arguments, ", ");
1065 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, src_reg, src_mask, src_str);
1066 strcat(arguments, src_str);
1070 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1073 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1074 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1075 * dst.x = 2^(floor(src))
1076 * dst.y = src - floor(src)
1077 * dst.z = 2^src (partial precision is allowed, but optional)
1078 * dst.w = 1.0;
1079 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1080 * dst = 2^src; (partial precision is allowed, but optional)
1082 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1083 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1084 char src_str[100];
1085 char src_reg[50];
1086 char src_mask[6];
1088 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, src_reg, src_mask, src_str);
1090 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1091 char dst_mask[6];
1093 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_str);
1094 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_str, src_str);
1095 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_str);
1096 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1098 shader_glsl_append_dst(arg->buffer, arg);
1099 shader_glsl_get_write_mask(arg->dst, dst_mask);
1100 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1101 } else {
1102 DWORD write_mask;
1103 size_t mask_size;
1105 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1106 mask_size = shader_glsl_get_write_mask_size(write_mask);
1108 if (mask_size > 1) {
1109 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_str);
1110 } else {
1111 shader_addline(arg->buffer, "exp2(%s));\n", src_str);
1116 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1117 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1118 char src_str[100];
1119 char src_reg[50];
1120 char src_mask[6];
1121 DWORD write_mask;
1122 size_t mask_size;
1124 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1125 mask_size = shader_glsl_get_write_mask_size(write_mask);
1126 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, src_reg, src_mask, src_str);
1128 if (mask_size > 1) {
1129 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_str);
1130 } else {
1131 shader_addline(arg->buffer, "1.0 / %s);\n", src_str);
1135 /** Process signed comparison opcodes in GLSL. */
1136 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1137 char src0_str[100], src1_str[100];
1138 char src0_reg[50], src1_reg[50];
1139 char src0_mask[6], src1_mask[6];
1140 DWORD write_mask;
1141 size_t mask_size;
1143 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1144 mask_size = shader_glsl_get_write_mask_size(write_mask);
1145 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
1146 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, src1_reg, src1_mask, src1_str);
1148 if (mask_size > 1) {
1149 const char *compare;
1151 switch(arg->opcode->opcode) {
1152 case WINED3DSIO_SLT: compare = "lessThan"; break;
1153 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1154 default: compare = "";
1155 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1158 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare, src0_str, src1_str);
1159 } else {
1160 const char *compare;
1162 switch(arg->opcode->opcode) {
1163 case WINED3DSIO_SLT: compare = "<"; break;
1164 case WINED3DSIO_SGE: compare = ">="; break;
1165 default: compare = "";
1166 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1169 shader_addline(arg->buffer, "(%s %s %s) ? 1.0 : 0.0);\n", src0_str, compare, src1_str);
1173 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1174 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1175 char src0_str[100], src1_str[100], src2_str[100];
1176 char src0_reg[50], src1_reg[50], src2_reg[50];
1177 char src0_mask[6], src1_mask[6], src2_mask[6];
1178 DWORD write_mask;
1179 size_t mask_size;
1181 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1182 mask_size = shader_glsl_get_write_mask_size(write_mask);
1184 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
1185 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, src1_reg, src1_mask, src1_str);
1186 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, src2_reg, src2_mask, src2_str);
1188 if (mask_size > 1) {
1189 shader_addline(arg->buffer, "mix(%s, %s, vec%d(lessThan(%s, vec%d(0.0)))));\n", src1_str, src2_str, mask_size, src0_str, mask_size);
1190 } else {
1191 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n", src0_str, src1_str, src2_str);
1195 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
1196 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1197 * the compare is done per component of src0. */
1198 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1199 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1200 char src0_str[100], src1_str[100], src2_str[100];
1201 char src0_reg[50], src1_reg[50], src2_reg[50];
1202 char src0_mask[6], src1_mask[6], src2_mask[6];
1203 DWORD write_mask;
1204 size_t mask_size;
1206 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1208 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1209 mask_size = 1;
1210 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, src0_reg, src0_mask, src0_str);
1211 } else {
1212 mask_size = shader_glsl_get_write_mask_size(write_mask);
1213 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
1216 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, src1_reg, src1_mask, src1_str);
1217 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, src2_reg, src2_mask, src2_str);
1219 if (mask_size > 1) {
1220 shader_addline(arg->buffer, "mix(%s, %s, vec%d(lessThan(%s, vec%d(0.5)))));\n", src2_str, src1_str, mask_size, src0_str, mask_size);
1221 } else {
1222 shader_addline(arg->buffer, "%s < 0.5 ? %s : %s);\n", src0_str, src1_str, src2_str);
1226 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1227 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1228 char src0_str[100], src1_str[100], src2_str[100];
1229 char src0_reg[50], src1_reg[50], src2_reg[50];
1230 char src0_mask[6], src1_mask[6], src2_mask[6];
1231 DWORD write_mask;
1233 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1234 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
1235 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, src1_reg, src1_mask, src1_str);
1236 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, src2_reg, src2_mask, src2_str);
1237 shader_addline(arg->buffer, "(%s * %s) + %s);\n", src0_str, src1_str, src2_str);
1240 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1241 Vertex shaders to GLSL codes */
1242 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1243 int i;
1244 int nComponents = 0;
1245 SHADER_OPCODE_ARG tmpArg;
1247 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1249 /* Set constants for the temporary argument */
1250 tmpArg.shader = arg->shader;
1251 tmpArg.buffer = arg->buffer;
1252 tmpArg.src[0] = arg->src[0];
1253 tmpArg.src_addr[0] = arg->src_addr[0];
1254 tmpArg.src_addr[1] = arg->src_addr[1];
1255 tmpArg.reg_maps = arg->reg_maps;
1257 switch(arg->opcode->opcode) {
1258 case WINED3DSIO_M4x4:
1259 nComponents = 4;
1260 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1261 break;
1262 case WINED3DSIO_M4x3:
1263 nComponents = 3;
1264 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1265 break;
1266 case WINED3DSIO_M3x4:
1267 nComponents = 4;
1268 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1269 break;
1270 case WINED3DSIO_M3x3:
1271 nComponents = 3;
1272 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1273 break;
1274 case WINED3DSIO_M3x2:
1275 nComponents = 2;
1276 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1277 break;
1278 default:
1279 break;
1282 for (i = 0; i < nComponents; i++) {
1283 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1284 tmpArg.src[1] = arg->src[1]+i;
1285 shader_glsl_dot(&tmpArg);
1290 The LRP instruction performs a component-wise linear interpolation
1291 between the second and third operands using the first operand as the
1292 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1293 This is equivalent to mix(src2, src1, src0);
1295 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1296 char src0_str[100], src1_str[100], src2_str[100];
1297 char src0_reg[50], src1_reg[50], src2_reg[50];
1298 char src0_mask[6], src1_mask[6], src2_mask[6];
1299 DWORD write_mask;
1301 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1303 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
1304 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, src1_reg, src1_mask, src1_str);
1305 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, src2_reg, src2_mask, src2_str);
1307 shader_addline(arg->buffer, "mix(%s, %s, %s));\n", src2_str, src1_str, src0_str);
1310 /** Process the WINED3DSIO_LIT instruction in GLSL:
1311 * dst.x = dst.w = 1.0
1312 * dst.y = (src0.x > 0) ? src0.x
1313 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1314 * where src.w is clamped at +- 128
1316 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1317 char src0_str[100];
1318 char src0_reg[50];
1319 char src0_mask[6];
1320 char src1_str[100];
1321 char src1_reg[50];
1322 char src1_mask[6];
1323 char src3_str[100];
1324 char src3_reg[50];
1325 char src3_mask[6];
1326 char dst_mask[6];
1328 shader_glsl_append_dst(arg->buffer, arg);
1329 shader_glsl_get_write_mask(arg->dst, dst_mask);
1331 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, src0_reg, src0_mask, src0_str);
1332 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, src1_reg, src1_mask, src1_str);
1333 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, src3_reg, src3_mask, src3_str);
1335 shader_addline(arg->buffer, "vec4(1.0, (%s > 0.0 ? %s : 0.0), (%s > 0.0 ? ((%s > 0.0) ? pow(%s, clamp(%s, -128.0, 128.0)) : 0.0) : 0.0), 1.0)%s);\n",
1336 src0_str, src0_str, src0_str, src1_str, src1_str, src3_str, dst_mask);
1339 /** Process the WINED3DSIO_DST instruction in GLSL:
1340 * dst.x = 1.0
1341 * dst.y = src0.x * src0.y
1342 * dst.z = src0.z
1343 * dst.w = src1.w
1345 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1346 char src0y_str[100], src0z_str[100], src1y_str[100], src1w_str[100];
1347 char src0y_reg[50], src0z_reg[50], src1y_reg[50], src1w_reg[50];
1348 char dst_mask[6], src0y_mask[6], src0z_mask[6], src1y_mask[6], src1w_mask[6];
1350 shader_glsl_append_dst(arg->buffer, arg);
1351 shader_glsl_get_write_mask(arg->dst, dst_mask);
1353 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, src0y_reg, src0y_mask, src0y_str);
1354 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, src0z_reg, src0z_mask, src0z_str);
1355 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, src1y_reg, src1y_mask, src1y_str);
1356 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, src1w_reg, src1w_mask, src1w_str);
1358 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1359 src0y_str, src1y_str, src0z_str, src1w_str, dst_mask);
1362 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1363 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1364 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1366 * dst.x = cos(src0.?)
1367 * dst.y = sin(src0.?)
1368 * dst.z = dst.z
1369 * dst.w = dst.w
1371 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1372 char src0_str[100];
1373 char src0_reg[50];
1374 char src0_mask[6];
1375 DWORD write_mask;
1377 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1378 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, src0_reg, src0_mask, src0_str);
1380 switch (write_mask) {
1381 case WINED3DSP_WRITEMASK_0:
1382 shader_addline(arg->buffer, "cos(%s));\n", src0_str);
1383 break;
1385 case WINED3DSP_WRITEMASK_1:
1386 shader_addline(arg->buffer, "sin(%s));\n", src0_str);
1387 break;
1389 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1390 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_str, src0_str);
1391 break;
1393 default:
1394 ERR("Write mask should be .x, .y or .xy\n");
1395 break;
1399 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1400 * Start a for() loop where src0.y is the initial value of aL,
1401 * increment aL by src0.z for a total of src0.x iterations.
1402 * Need to use a temporary variable for this operation.
1404 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1406 char src1_str[100];
1407 char src1_reg[50];
1408 char src1_mask[6];
1410 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1412 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1413 src1_reg, src1_reg, src1_reg);
1416 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1417 shader_addline(arg->buffer, "}\n");
1420 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1422 char src0_str[100];
1423 char src0_reg[50];
1424 char src0_mask[6];
1426 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1427 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s.x; tmpInt++) {\n", src0_reg);
1430 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1432 char src0_str[100];
1433 char src0_reg[50];
1434 char src0_mask[6];
1436 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1437 shader_addline(arg->buffer, "if (%s) {\n", src0_str);
1440 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1442 char src0_str[100], src1_str[100];
1443 char src0_reg[50], src1_reg[50];
1444 char src0_mask[6], src1_mask[6];
1446 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1447 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1449 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1450 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1453 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1454 shader_addline(arg->buffer, "} else {\n");
1457 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1458 shader_addline(arg->buffer, "break;\n");
1461 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1463 char src0_str[100], src1_str[100];
1464 char src0_reg[50], src1_reg[50];
1465 char src0_mask[6], src1_mask[6];
1467 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1468 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1470 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1471 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1474 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1476 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1477 shader_addline(arg->buffer, "}\n");
1478 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1481 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1482 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1483 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1486 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1488 char src1_str[100];
1489 char src1_reg[50];
1490 char src1_mask[6];
1492 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1493 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1494 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_str, snum);
1497 /*********************************************
1498 * Pixel Shader Specific Code begins here
1499 ********************************************/
1500 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1501 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1503 DWORD hex_version = This->baseShader.hex_version;
1505 char dst_str[100], dst_reg[50], dst_mask[6];
1506 char coord_str[100], coord_reg[50], coord_mask[6];
1508 /* All versions have a destination register */
1509 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1511 /* 1.0-1.3: Use destination register as coordinate source.
1512 1.4+: Use provided coordinate source register. */
1513 if (hex_version < WINED3DPS_VERSION(1,4))
1514 strcpy(coord_reg, dst_reg);
1515 else
1516 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], coord_reg, coord_mask, coord_str);
1518 /* 1.0-1.4: Use destination register as sampler source.
1519 * 2.0+: Use provided sampler source. */
1520 if (hex_version < WINED3DPS_VERSION(2,0)) {
1521 shader_glsl_sample(arg, arg->dst & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1522 } else {
1523 shader_glsl_sample(arg, arg->src[1] & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1528 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1530 /* FIXME: Make this work for more than just 2D textures */
1532 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1533 SHADER_BUFFER* buffer = arg->buffer;
1534 DWORD hex_version = This->baseShader.hex_version;
1536 char tmpStr[100];
1537 char tmpReg[50];
1538 char tmpMask[6];
1539 tmpReg[0] = 0;
1541 shader_glsl_add_dst_param(arg, arg->dst, 0, tmpReg, tmpMask, tmpStr);
1543 if (hex_version != WINED3DPS_VERSION(1,4)) {
1544 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1545 shader_addline(buffer, "%s = clamp(gl_TexCoord[%u], 0.0, 1.0);\n", tmpReg, reg);
1546 } else {
1547 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1548 shader_addline(buffer, "%s = gl_TexCoord[%u]%s;\n", tmpStr, reg2, tmpMask);
1552 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1553 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1554 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1555 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1556 char src0_str[100];
1557 char src0_name[50];
1558 char src0_mask[6];
1559 char dst_mask[6];
1560 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1561 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1563 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, src0_name, src0_mask, src0_str);
1565 shader_glsl_append_dst(arg->buffer, arg);
1566 shader_glsl_get_write_mask(arg->dst, dst_mask);
1567 shader_addline(arg->buffer, "texture1D(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
1568 sampler_idx, sampler_idx, src0_str, dst_mask);
1571 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1572 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1573 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1575 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1576 char src0_str[100], dst_str[100];
1577 char src0_name[50], dst_name[50];
1578 char src0_mask[6], dst_mask[6];
1580 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1581 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1583 shader_addline(arg->buffer, "%s = vec4(dot(vec3(T%u), vec3(%s)))%s;\n",
1584 dst_str, dstreg, src0_str, dst_mask);
1587 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1588 * Calculate the depth as dst.x / dst.y */
1589 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1591 char dst_str[100];
1592 char dst_reg[50];
1593 char dst_mask[6];
1595 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1597 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_reg, dst_reg);
1600 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1601 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1602 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1603 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1605 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1607 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1608 char src0_str[100], dst_str[100];
1609 char src0_name[50], dst_name[50];
1610 char src0_mask[6], dst_mask[6];
1612 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1613 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1615 shader_addline(arg->buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", dstreg, src0_str);
1616 shader_addline(arg->buffer, "gl_FragDepth = vec4((tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y)%s;\n", dst_str, dst_name);
1619 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1620 * Calculate the 1st of a 2-row matrix multiplication. */
1621 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1623 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1624 SHADER_BUFFER* buffer = arg->buffer;
1625 char src0_str[100];
1626 char src0_name[50];
1627 char src0_mask[6];
1629 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1630 shader_addline(buffer, "tmp0.x = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1633 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1634 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1635 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1637 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1638 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1639 SHADER_BUFFER* buffer = arg->buffer;
1640 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1641 char src0_str[100];
1642 char src0_name[50];
1643 char src0_mask[6];
1645 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1646 shader_addline(buffer, "tmp0.%c = dot(vec3(T%u), vec3(%s));\n", 'x' + current_state->current_row, reg, src0_str);
1647 current_state->texcoord_w[current_state->current_row++] = reg;
1650 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1651 char dst_str[8];
1652 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1653 SHADER_BUFFER* buffer = arg->buffer;
1654 char src0_str[100];
1655 char src0_name[50];
1656 char src0_mask[6];
1658 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1659 shader_addline(buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1661 /* Sample the texture using the calculated coordinates */
1662 sprintf(dst_str, "T%u", reg);
1663 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1666 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1667 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1668 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1669 char dst_str[8];
1670 char src0_str[100];
1671 char src0_name[50];
1672 char src0_mask[6];
1673 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1674 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1675 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1677 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1678 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1680 /* Sample the texture using the calculated coordinates */
1681 sprintf(dst_str, "T%u", reg);
1682 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1683 current_state->current_row = 0;
1686 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1687 * Perform the 3rd row of a 3x3 matrix multiply */
1688 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
1690 char src0_str[100];
1691 char src0_name[50];
1692 char src0_mask[6];
1693 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1694 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1695 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1697 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1699 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1700 shader_addline(arg->buffer, "T%u = vec4(tmp0.x, tmp0.y, tmp0.z, 1.0);\n", reg);
1701 current_state->current_row = 0;
1704 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1705 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1706 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1708 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1709 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1710 char dimensions[5];
1711 char dst_str[8];
1712 char src0_str[100], src0_name[50], src0_mask[6];
1713 char src1_str[100], src1_name[50], src1_mask[6];
1714 SHADER_BUFFER* buffer = arg->buffer;
1715 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1716 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1718 switch (stype) {
1719 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1720 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1721 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1722 default:
1723 strcpy(dimensions, "");
1724 FIXME("Unrecognized sampler type: %#x\n", stype);
1725 break;
1728 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1729 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_name, src1_mask, src1_str);
1731 /* Perform the last matrix multiply operation */
1732 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1734 /* Calculate reflection vector */
1735 shader_addline(buffer, "tmp0.xyz = reflect(-vec3(%s), vec3(tmp0));\n", src1_str);
1737 /* Sample the texture */
1738 sprintf(dst_str, "T%u", reg);
1739 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1740 current_state->current_row = 0;
1743 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1744 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1745 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1747 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1748 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1749 SHADER_BUFFER* buffer = arg->buffer;
1750 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1751 char dst_str[8];
1752 char src0_str[100], src0_name[50], src0_mask[6];
1754 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1756 /* Perform the last matrix multiply operation */
1757 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1759 /* Construct the eye-ray vector from w coordinates */
1760 shader_addline(buffer, "tmp1.x = gl_TexCoord[%u].w;\n", current_state->texcoord_w[0]);
1761 shader_addline(buffer, "tmp1.y = gl_TexCoord[%u].w;\n", current_state->texcoord_w[1]);
1762 shader_addline(buffer, "tmp1.z = gl_TexCoord[%u].w;\n", reg);
1764 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
1765 shader_addline(buffer, "tmp0.x = dot(vec3(tmp0), vec3(tmp1));\n");
1766 shader_addline(buffer, "tmp0 = tmp0.w * tmp0;\n");
1767 shader_addline(buffer, "tmp0 = (2.0 * tmp0) - tmp1;\n");
1769 /* Sample the texture using the calculated coordinates */
1770 sprintf(dst_str, "T%u", reg);
1771 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1772 current_state->current_row = 0;
1775 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1776 * Apply a fake bump map transform.
1777 * FIXME: Should apply the BUMPMAPENV matrix. For now, just sample the texture */
1778 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1780 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1781 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1783 FIXME("Not applying the BUMPMAPENV matrix for pixel shader instruction texbem.\n");
1784 shader_addline(arg->buffer, "T%u = texture2D(Psampler%u, gl_TexCoord[%u].xy + T%u.xy);\n",
1785 reg1, reg1, reg1, reg2);
1788 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1789 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1790 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1791 char src0_str[100];
1792 char src0_reg[50];
1793 char src0_mask[6];
1794 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1795 char dst_mask[6];
1797 shader_glsl_append_dst(arg->buffer, arg);
1798 shader_glsl_get_write_mask(arg->dst, dst_mask);
1799 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, src0_reg, src0_mask, src0_str);
1801 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_reg, dst_mask);
1804 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1805 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1806 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1807 char src0_str[100];
1808 char src0_reg[50];
1809 char src0_mask[6];
1810 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1811 char dst_mask[6];
1813 shader_glsl_append_dst(arg->buffer, arg);
1814 shader_glsl_get_write_mask(arg->dst, dst_mask);
1815 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, src0_reg, src0_mask, src0_str);
1817 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_reg, dst_mask);
1820 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
1821 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
1822 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1823 char src0_str[100];
1824 char src0_reg[50];
1825 char src0_mask[6];
1826 char dst_mask[6];
1827 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1828 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1829 glsl_sample_function_t sample_function;
1831 shader_glsl_append_dst(arg->buffer, arg);
1832 shader_glsl_get_write_mask(arg->dst, dst_mask);
1833 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1834 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, src0_reg, src0_mask, src0_str);
1836 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_str, dst_mask);
1839 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
1840 * If any of the first 3 components are < 0, discard this pixel */
1841 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
1843 char dst_str[100], dst_name[50], dst_mask[6];
1845 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1846 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_name);
1849 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
1850 * dst = dot2(src0, src1) + src2 */
1851 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
1852 char src0_str[100], src1_str[100], src2_str[100];
1853 char src0_reg[50], src1_reg[50], src2_reg[50];
1854 char src0_mask[6], src1_mask[6], src2_mask[6];
1855 DWORD write_mask;
1856 size_t mask_size;
1858 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1859 mask_size = shader_glsl_get_write_mask_size(write_mask);
1861 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, src0_reg, src0_mask, src0_str);
1862 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, src1_reg, src1_mask, src1_str);
1863 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, src2_reg, src2_mask, src2_str);
1865 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_str, src1_str, src2_str);
1868 void pshader_glsl_input_pack(
1869 SHADER_BUFFER* buffer,
1870 semantic* semantics_in) {
1872 unsigned int i;
1874 for (i = 0; i < MAX_REG_INPUT; i++) {
1876 DWORD usage_token = semantics_in[i].usage;
1877 DWORD register_token = semantics_in[i].reg;
1878 DWORD usage, usage_idx;
1879 char reg_mask[6];
1881 /* Uninitialized */
1882 if (!usage_token) continue;
1883 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1884 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1885 shader_glsl_get_write_mask(register_token, reg_mask);
1887 switch(usage) {
1889 case D3DDECLUSAGE_COLOR:
1890 if (usage_idx == 0)
1891 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
1892 i, reg_mask, reg_mask);
1893 else if (usage_idx == 1)
1894 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
1895 i, reg_mask, reg_mask);
1896 else
1897 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
1898 i, reg_mask, reg_mask);
1899 break;
1901 case D3DDECLUSAGE_TEXCOORD:
1902 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
1903 i, reg_mask, usage_idx, reg_mask );
1904 break;
1906 case D3DDECLUSAGE_FOG:
1907 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
1908 i, reg_mask, reg_mask);
1909 break;
1911 default:
1912 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
1913 i, reg_mask, reg_mask);
1918 /*********************************************
1919 * Vertex Shader Specific Code begins here
1920 ********************************************/
1922 void vshader_glsl_output_unpack(
1923 SHADER_BUFFER* buffer,
1924 semantic* semantics_out) {
1926 unsigned int i;
1928 for (i = 0; i < MAX_REG_OUTPUT; i++) {
1930 DWORD usage_token = semantics_out[i].usage;
1931 DWORD register_token = semantics_out[i].reg;
1932 DWORD usage, usage_idx;
1933 char reg_mask[6];
1935 /* Uninitialized */
1936 if (!usage_token) continue;
1938 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1939 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1940 shader_glsl_get_write_mask(register_token, reg_mask);
1942 switch(usage) {
1944 case D3DDECLUSAGE_COLOR:
1945 if (usage_idx == 0)
1946 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1947 else if (usage_idx == 1)
1948 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1949 else
1950 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1951 break;
1953 case D3DDECLUSAGE_POSITION:
1954 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1955 break;
1957 case D3DDECLUSAGE_TEXCOORD:
1958 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
1959 usage_idx, reg_mask, i, reg_mask);
1960 break;
1962 case WINED3DSHADERDECLUSAGE_PSIZE:
1963 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
1964 break;
1966 case WINED3DSHADERDECLUSAGE_FOG:
1967 shader_addline(buffer, "gl_FogFragCoord%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1968 break;
1970 default:
1971 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1976 /** Attach a GLSL pixel or vertex shader object to the shader program */
1977 static void attach_glsl_shader(IWineD3DDevice *iface, IWineD3DBaseShader* shader) {
1978 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1979 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1980 GLhandleARB shaderObj = ((IWineD3DBaseShaderImpl*)shader)->baseShader.prgId;
1981 if (This->stateBlock->glsl_program && shaderObj != 0) {
1982 TRACE("Attaching GLSL shader object %u to program %u\n", shaderObj, This->stateBlock->glsl_program->programId);
1983 GL_EXTCALL(glAttachObjectARB(This->stateBlock->glsl_program->programId, shaderObj));
1984 checkGLcall("glAttachObjectARB");
1988 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
1989 * It sets the programId on the current StateBlock (because it should be called
1990 * inside of the DrawPrimitive() part of the render loop).
1992 * If a program for the given combination does not exist, create one, and store
1993 * the program in the list. If it creates a program, it will link the given
1994 * objects, too.
1996 * We keep the shader programs around on a list because linking
1997 * shader objects together is an expensive operation. It's much
1998 * faster to loop through a list of pre-compiled & linked programs
1999 * each time that the application sets a new pixel or vertex shader
2000 * than it is to re-link them together at that time.
2002 * The list will be deleted in IWineD3DDevice::Release().
2004 static void set_glsl_shader_program(IWineD3DDevice *iface) {
2005 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2006 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2007 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
2008 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
2009 struct glsl_shader_prog_link *curLink = NULL;
2010 struct glsl_shader_prog_link *newLink = NULL;
2011 struct list *ptr = NULL;
2012 GLhandleARB programId = 0;
2013 int i;
2014 char glsl_name[8];
2016 ptr = list_head( &This->glsl_shader_progs );
2017 while (ptr) {
2018 /* At least one program exists - see if it matches our ps/vs combination */
2019 curLink = LIST_ENTRY( ptr, struct glsl_shader_prog_link, entry );
2020 if (vshader == curLink->vertexShader && pshader == curLink->pixelShader) {
2021 /* Existing Program found, use it */
2022 TRACE("Found existing program (%u) for this vertex/pixel shader combination\n",
2023 curLink->programId);
2024 This->stateBlock->glsl_program = curLink;
2025 return;
2027 /* This isn't the entry we need - try the next one */
2028 ptr = list_next( &This->glsl_shader_progs, ptr );
2031 /* If we get to this point, then no matching program exists, so we create one */
2032 programId = GL_EXTCALL(glCreateProgramObjectARB());
2033 TRACE("Created new GLSL shader program %u\n", programId);
2035 /* Allocate a new link for the list of programs */
2036 newLink = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
2037 newLink->programId = programId;
2038 This->stateBlock->glsl_program = newLink;
2040 /* Attach GLSL vshader */
2041 if (NULL != vshader && This->vs_selected_mode == SHADER_GLSL) {
2042 int i;
2043 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
2044 char tmp_name[10];
2046 TRACE("Attaching vertex shader to GLSL program\n");
2047 attach_glsl_shader(iface, (IWineD3DBaseShader*)vshader);
2049 /* Bind vertex attributes to a corresponding index number to match
2050 * the same index numbers as ARB_vertex_programs (makes loading
2051 * vertex attributes simpler). With this method, we can use the
2052 * exact same code to load the attributes later for both ARB and
2053 * GLSL shaders.
2055 * We have to do this here because we need to know the Program ID
2056 * in order to make the bindings work, and it has to be done prior
2057 * to linking the GLSL program. */
2058 for (i = 0; i < max_attribs; ++i) {
2059 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
2060 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
2062 checkGLcall("glBindAttribLocationARB");
2063 newLink->vertexShader = vshader;
2066 /* Attach GLSL pshader */
2067 if (NULL != pshader && This->ps_selected_mode == SHADER_GLSL) {
2068 TRACE("Attaching pixel shader to GLSL program\n");
2069 attach_glsl_shader(iface, (IWineD3DBaseShader*)pshader);
2070 newLink->pixelShader = pshader;
2073 /* Link the program */
2074 TRACE("Linking GLSL shader program %u\n", programId);
2075 GL_EXTCALL(glLinkProgramARB(programId));
2076 print_glsl_info_log(&GLINFO_LOCATION, programId);
2077 list_add_head( &This->glsl_shader_progs, &newLink->entry);
2079 newLink->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
2080 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
2081 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
2082 newLink->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2084 newLink->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
2085 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
2086 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
2087 newLink->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2090 return;
2093 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
2094 GLhandleARB program_id;
2095 GLhandleARB vshader_id, pshader_id;
2096 const char *blt_vshader[] = {
2097 "void main(void)\n"
2098 "{\n"
2099 " gl_Position = gl_Vertex;\n"
2100 " gl_FrontColor = vec4(1.0);\n"
2101 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2102 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2103 "}\n"
2106 const char *blt_pshader[] = {
2107 "uniform sampler2D sampler;\n"
2108 "void main(void)\n"
2109 "{\n"
2110 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2111 "}\n"
2114 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2115 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2116 GL_EXTCALL(glCompileShaderARB(vshader_id));
2118 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2119 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2120 GL_EXTCALL(glCompileShaderARB(pshader_id));
2122 program_id = GL_EXTCALL(glCreateProgramObjectARB());
2123 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2124 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2125 GL_EXTCALL(glLinkProgramARB(program_id));
2127 print_glsl_info_log(&GLINFO_LOCATION, program_id);
2129 return program_id;
2132 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2133 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2134 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2135 GLhandleARB program_id = 0;
2137 if (useVS || usePS) set_glsl_shader_program(iface);
2138 else This->stateBlock->glsl_program = NULL;
2140 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2141 if (program_id) TRACE("Using GLSL program %u\n", program_id);
2142 GL_EXTCALL(glUseProgramObjectARB(program_id));
2143 checkGLcall("glUseProgramObjectARB");
2146 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2147 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2148 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2149 static GLhandleARB program_id = 0;
2150 static GLhandleARB loc = -1;
2152 if (!program_id) {
2153 program_id = create_glsl_blt_shader(gl_info);
2154 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2157 GL_EXTCALL(glUseProgramObjectARB(program_id));
2158 GL_EXTCALL(glUniform1iARB(loc, 0));
2161 static void shader_glsl_cleanup(BOOL usePS, BOOL useVS) {
2162 /* Nothing to do */
2165 const shader_backend_t glsl_shader_backend = {
2166 &shader_glsl_select,
2167 &shader_glsl_select_depth_blt,
2168 &shader_glsl_load_constants,
2169 &shader_glsl_cleanup