wined3d: Store the gl information in a per adapter structure and initialize it only...
[wine.git] / dlls / wined3d / glsl_shader.c
blob49998418183baaa29813319e9ba9651c17188beb
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);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
37 #define GLINFO_LOCATION (*gl_info)
39 typedef struct {
40 char reg_name[50];
41 char mask_str[6];
42 } glsl_dst_param_t;
44 typedef struct {
45 char reg_name[50];
46 char param_str[100];
47 } glsl_src_param_t;
49 typedef struct {
50 const char *name;
51 DWORD coord_mask;
52 } glsl_sample_function_t;
54 /** Prints the GLSL info log which will contain error messages if they exist */
55 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
57 int infologLength = 0;
58 char *infoLog;
60 GL_EXTCALL(glGetObjectParameterivARB(obj,
61 GL_OBJECT_INFO_LOG_LENGTH_ARB,
62 &infologLength));
64 /* A size of 1 is just a null-terminated string, so the log should be bigger than
65 * that if there are errors. */
66 if (infologLength > 1)
68 infoLog = (char *)HeapAlloc(GetProcessHeap(), 0, infologLength);
69 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
70 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
71 HeapFree(GetProcessHeap(), 0, infoLog);
75 /**
76 * Loads (pixel shader) samplers
78 static void shader_glsl_load_psamplers(
79 WineD3D_GL_Info *gl_info,
80 IWineD3DStateBlock* iface) {
82 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
83 GLhandleARB programId = stateBlock->glsl_program->programId;
84 GLhandleARB name_loc;
85 int i;
86 char sampler_name[20];
88 for (i=0; i< GL_LIMITS(samplers); ++i) {
89 if (stateBlock->textures[i] != NULL) {
90 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
91 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
92 if (name_loc != -1) {
93 TRACE("Loading %s for texture %d\n", sampler_name, i);
94 GL_EXTCALL(glUniform1iARB(name_loc, i));
95 checkGLcall("glUniform1iARB");
101 /**
102 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
103 * When constant_list == NULL, it will load all the constants.
105 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
106 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
107 struct list *constant_list) {
108 constants_entry *constant;
109 local_constant* lconst;
110 GLhandleARB tmp_loc;
111 DWORD i, j;
112 DWORD *idx;
114 if (TRACE_ON(d3d_shader)) {
115 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
116 idx = constant->idx;
117 j = constant->count;
118 while (j--) {
119 i = *idx++;
120 tmp_loc = constant_locations[i];
121 if (tmp_loc != -1) {
122 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
123 constants[i * 4 + 0], constants[i * 4 + 1],
124 constants[i * 4 + 2], constants[i * 4 + 3]);
129 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
130 idx = constant->idx;
131 j = constant->count;
132 while (j--) {
133 i = *idx++;
134 tmp_loc = constant_locations[i];
135 if (tmp_loc != -1) {
136 /* We found this uniform name in the program - go ahead and send the data */
137 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
141 checkGLcall("glUniform4fvARB()");
143 /* Load immediate constants */
144 if (TRACE_ON(d3d_shader)) {
145 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
146 tmp_loc = constant_locations[lconst->idx];
147 if (tmp_loc != -1) {
148 GLfloat* values = (GLfloat*)lconst->value;
149 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
150 values[0], values[1], values[2], values[3]);
154 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
155 tmp_loc = constant_locations[lconst->idx];
156 if (tmp_loc != -1) {
157 /* We found this uniform name in the program - go ahead and send the data */
158 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
161 checkGLcall("glUniform4fvARB()");
164 /**
165 * Loads integer constants (aka uniforms) into the currently set GLSL program.
166 * When @constants_set == NULL, it will load all the constants.
168 static void shader_glsl_load_constantsI(
169 IWineD3DBaseShaderImpl* This,
170 WineD3D_GL_Info *gl_info,
171 GLhandleARB programId,
172 unsigned max_constants,
173 int* constants,
174 BOOL* constants_set) {
176 GLhandleARB tmp_loc;
177 int i;
178 char tmp_name[8];
179 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
180 const char* prefix = is_pshader? "PI":"VI";
181 struct list* ptr;
183 for (i=0; i<max_constants; ++i) {
184 if (NULL == constants_set || constants_set[i]) {
186 TRACE_(d3d_constants)("Loading constants %i: %i, %i, %i, %i\n",
187 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
189 /* TODO: Benchmark and see if it would be beneficial to store the
190 * locations of the constants to avoid looking up each time */
191 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
192 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
193 if (tmp_loc != -1) {
194 /* We found this uniform name in the program - go ahead and send the data */
195 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
196 checkGLcall("glUniform4ivARB");
201 /* Load immediate constants */
202 ptr = list_head(&This->baseShader.constantsI);
203 while (ptr) {
204 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
205 unsigned int idx = lconst->idx;
206 GLint* values = (GLint*) lconst->value;
208 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
209 values[0], values[1], values[2], values[3]);
211 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
212 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
213 if (tmp_loc != -1) {
214 /* We found this uniform name in the program - go ahead and send the data */
215 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
216 checkGLcall("glUniform4ivARB");
218 ptr = list_next(&This->baseShader.constantsI, ptr);
222 /**
223 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
224 * When @constants_set == NULL, it will load all the constants.
226 static void shader_glsl_load_constantsB(
227 IWineD3DBaseShaderImpl* This,
228 WineD3D_GL_Info *gl_info,
229 GLhandleARB programId,
230 unsigned max_constants,
231 BOOL* constants,
232 BOOL* constants_set) {
234 GLhandleARB tmp_loc;
235 int i;
236 char tmp_name[8];
237 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
238 const char* prefix = is_pshader? "PB":"VB";
239 struct list* ptr;
241 for (i=0; i<max_constants; ++i) {
242 if (NULL == constants_set || constants_set[i]) {
244 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i*4]);
246 /* TODO: Benchmark and see if it would be beneficial to store the
247 * locations of the constants to avoid looking up each time */
248 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
249 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
250 if (tmp_loc != -1) {
251 /* We found this uniform name in the program - go ahead and send the data */
252 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
253 checkGLcall("glUniform1ivARB");
258 /* Load immediate constants */
259 ptr = list_head(&This->baseShader.constantsB);
260 while (ptr) {
261 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
262 unsigned int idx = lconst->idx;
263 GLint* values = (GLint*) lconst->value;
265 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
267 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
268 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
269 if (tmp_loc != -1) {
270 /* We found this uniform name in the program - go ahead and send the data */
271 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
272 checkGLcall("glUniform1ivARB");
274 ptr = list_next(&This->baseShader.constantsB, ptr);
281 * Loads the app-supplied constants into the currently set GLSL program.
283 void shader_glsl_load_constants(
284 IWineD3DDevice* device,
285 char usePixelShader,
286 char useVertexShader) {
288 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
289 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
290 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
292 GLhandleARB *constant_locations;
293 struct list *constant_list;
294 GLhandleARB programId;
295 GLint pos;
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 GLint pos;
307 constant_locations = stateBlock->glsl_program->vuniformF_locations;
308 constant_list = &stateBlock->set_vconstantsF;
310 /* Load DirectX 9 float constants/uniforms for vertex shader */
311 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
312 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
314 /* Load DirectX 9 integer constants/uniforms for vertex shader */
315 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
316 stateBlock->vertexShaderConstantI,
317 stateBlock->set.vertexShaderConstantsI);
319 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
320 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
321 stateBlock->vertexShaderConstantB,
322 stateBlock->set.vertexShaderConstantsB);
324 /* Upload the position fixup params */
325 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
326 checkGLcall("glGetUniformLocationARB");
327 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
328 checkGLcall("glUniform4fvARB");
331 if (usePixelShader) {
333 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
335 constant_locations = stateBlock->glsl_program->puniformF_locations;
336 constant_list = &stateBlock->set_pconstantsF;
338 /* Load pixel shader samplers */
339 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
341 /* Load DirectX 9 float constants/uniforms for pixel shader */
342 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
343 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
345 /* Load DirectX 9 integer constants/uniforms for pixel shader */
346 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
347 stateBlock->pixelShaderConstantI,
348 stateBlock->set.pixelShaderConstantsI);
350 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
351 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
352 stateBlock->pixelShaderConstantB,
353 stateBlock->set.pixelShaderConstantsB);
355 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
356 * It can't be 0 for a valid texbem instruction.
358 if(((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat != -1) {
359 float *data = (float *) &stateBlock->textureState[(int) ((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat][WINED3DTSS_BUMPENVMAT00];
360 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "bumpenvmat"));
361 checkGLcall("glGetUniformLocationARB");
362 GL_EXTCALL(glUniformMatrix2fvARB(pos, 1, 0, data));
363 checkGLcall("glUniform4fvARB");
368 /** Generate the variable & register declarations for the GLSL output target */
369 void shader_generate_glsl_declarations(
370 IWineD3DBaseShader *iface,
371 shader_reg_maps* reg_maps,
372 SHADER_BUFFER* buffer,
373 WineD3D_GL_Info* gl_info) {
375 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
376 int i;
378 /* There are some minor differences between pixel and vertex shaders */
379 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
380 char prefix = pshader ? 'P' : 'V';
382 /* Prototype the subroutines */
383 for (i = 0; i < This->baseShader.limits.label; i++) {
384 if (reg_maps->labels[i])
385 shader_addline(buffer, "void subroutine%lu();\n", i);
388 /* Declare the constants (aka uniforms) */
389 if (This->baseShader.limits.constant_float > 0) {
390 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
391 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
392 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
395 if (This->baseShader.limits.constant_int > 0)
396 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
398 if (This->baseShader.limits.constant_bool > 0)
399 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
401 if(!pshader)
402 shader_addline(buffer, "uniform vec4 posFixup;\n");
403 else if(reg_maps->bumpmat != -1)
404 shader_addline(buffer, "uniform mat2 bumpenvmat;\n");
406 /* Declare texture samplers */
407 for (i = 0; i < This->baseShader.limits.sampler; i++) {
408 if (reg_maps->samplers[i]) {
410 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
411 switch (stype) {
413 case WINED3DSTT_1D:
414 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
415 break;
416 case WINED3DSTT_2D:
417 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
418 break;
419 case WINED3DSTT_CUBE:
420 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
421 break;
422 case WINED3DSTT_VOLUME:
423 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
424 break;
425 default:
426 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
427 FIXME("Unrecognized sampler type: %#x\n", stype);
428 break;
433 /* Declare address variables */
434 for (i = 0; i < This->baseShader.limits.address; i++) {
435 if (reg_maps->address[i])
436 shader_addline(buffer, "ivec4 A%d;\n", i);
439 /* Declare texture coordinate temporaries and initialize them */
440 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
441 if (reg_maps->texcoord[i])
442 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
445 /* Declare input register temporaries */
446 for (i=0; i < This->baseShader.limits.packed_input; i++) {
447 if (reg_maps->packed_input[i])
448 shader_addline(buffer, "vec4 IN%lu;\n", i);
451 /* Declare output register temporaries */
452 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
453 if (reg_maps->packed_output[i])
454 shader_addline(buffer, "vec4 OUT%lu;\n", i);
457 /* Declare temporary variables */
458 for(i = 0; i < This->baseShader.limits.temporary; i++) {
459 if (reg_maps->temporary[i])
460 shader_addline(buffer, "vec4 R%lu;\n", i);
463 /* Declare attributes */
464 for (i = 0; i < This->baseShader.limits.attributes; i++) {
465 if (reg_maps->attributes[i])
466 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
469 /* Declare loop register aL */
470 if (reg_maps->loop) {
471 shader_addline(buffer, "int aL;\n");
472 shader_addline(buffer, "int tmpInt;\n");
475 /* Temporary variables for matrix operations */
476 shader_addline(buffer, "vec4 tmp0;\n");
477 shader_addline(buffer, "vec4 tmp1;\n");
479 /* Start the main program */
480 shader_addline(buffer, "void main() {\n");
483 /*****************************************************************************
484 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
486 * For more information, see http://wiki.winehq.org/DirectX-Shaders
487 ****************************************************************************/
489 /* Prototypes */
490 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
491 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
493 /** Used for opcode modifiers - They multiply the result by the specified amount */
494 static const char * const shift_glsl_tab[] = {
495 "", /* 0 (none) */
496 "2.0 * ", /* 1 (x2) */
497 "4.0 * ", /* 2 (x4) */
498 "8.0 * ", /* 3 (x8) */
499 "16.0 * ", /* 4 (x16) */
500 "32.0 * ", /* 5 (x32) */
501 "", /* 6 (x64) */
502 "", /* 7 (x128) */
503 "", /* 8 (d256) */
504 "", /* 9 (d128) */
505 "", /* 10 (d64) */
506 "", /* 11 (d32) */
507 "0.0625 * ", /* 12 (d16) */
508 "0.125 * ", /* 13 (d8) */
509 "0.25 * ", /* 14 (d4) */
510 "0.5 * " /* 15 (d2) */
513 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
514 static void shader_glsl_gen_modifier (
515 const DWORD instr,
516 const char *in_reg,
517 const char *in_regswizzle,
518 char *out_str) {
520 out_str[0] = 0;
522 if (instr == WINED3DSIO_TEXKILL)
523 return;
525 switch (instr & WINED3DSP_SRCMOD_MASK) {
526 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
527 case WINED3DSPSM_DW:
528 case WINED3DSPSM_NONE:
529 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
530 break;
531 case WINED3DSPSM_NEG:
532 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
533 break;
534 case WINED3DSPSM_NOT:
535 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
536 break;
537 case WINED3DSPSM_BIAS:
538 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
539 break;
540 case WINED3DSPSM_BIASNEG:
541 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
542 break;
543 case WINED3DSPSM_SIGN:
544 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
545 break;
546 case WINED3DSPSM_SIGNNEG:
547 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
548 break;
549 case WINED3DSPSM_COMP:
550 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
551 break;
552 case WINED3DSPSM_X2:
553 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
554 break;
555 case WINED3DSPSM_X2NEG:
556 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
557 break;
558 case WINED3DSPSM_ABS:
559 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
560 break;
561 case WINED3DSPSM_ABSNEG:
562 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
563 break;
564 default:
565 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
566 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
570 /** Writes the GLSL variable name that corresponds to the register that the
571 * DX opcode parameter is trying to access */
572 static void shader_glsl_get_register_name(
573 const DWORD param,
574 const DWORD addr_token,
575 char* regstr,
576 BOOL* is_color,
577 SHADER_OPCODE_ARG* arg) {
579 /* oPos, oFog and oPts in D3D */
580 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
582 DWORD reg = param & WINED3DSP_REGNUM_MASK;
583 DWORD regtype = shader_get_regtype(param);
584 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
585 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
586 WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
588 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
589 char tmpStr[50];
591 *is_color = FALSE;
593 switch (regtype) {
594 case WINED3DSPR_TEMP:
595 sprintf(tmpStr, "R%u", reg);
596 break;
597 case WINED3DSPR_INPUT:
598 if (pshader) {
599 /* Pixel shaders >= 3.0 */
600 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
601 sprintf(tmpStr, "IN%u", reg);
602 else {
603 if (reg==0)
604 strcpy(tmpStr, "gl_Color");
605 else
606 strcpy(tmpStr, "gl_SecondaryColor");
608 } else {
609 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
610 *is_color = TRUE;
611 sprintf(tmpStr, "attrib%u", reg);
613 break;
614 case WINED3DSPR_CONST:
616 const char* prefix = pshader? "PC":"VC";
618 /* Relative addressing */
619 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
621 /* Relative addressing on shaders 2.0+ have a relative address token,
622 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
623 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
624 glsl_src_param_t rel_param;
625 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
626 sprintf(tmpStr, "%s[%s + %u]", prefix, rel_param.param_str, reg);
627 } else
628 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
630 } else
631 sprintf(tmpStr, "%s[%u]", prefix, reg);
633 break;
635 case WINED3DSPR_CONSTINT:
636 if (pshader)
637 sprintf(tmpStr, "PI[%u]", reg);
638 else
639 sprintf(tmpStr, "VI[%u]", reg);
640 break;
641 case WINED3DSPR_CONSTBOOL:
642 if (pshader)
643 sprintf(tmpStr, "PB[%u]", reg);
644 else
645 sprintf(tmpStr, "VB[%u]", reg);
646 break;
647 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
648 if (pshader) {
649 sprintf(tmpStr, "T%u", reg);
650 } else {
651 sprintf(tmpStr, "A%u", reg);
653 break;
654 case WINED3DSPR_LOOP:
655 sprintf(tmpStr, "aL");
656 break;
657 case WINED3DSPR_SAMPLER:
658 if (pshader)
659 sprintf(tmpStr, "Psampler%u", reg);
660 else
661 sprintf(tmpStr, "Vsampler%u", reg);
662 break;
663 case WINED3DSPR_COLOROUT:
664 if (reg >= GL_LIMITS(buffers)) {
665 WARN("Write to render target %u, only %d supported\n", reg, 4);
667 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
668 sprintf(tmpStr, "gl_FragData[%u]", reg);
669 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
670 sprintf(tmpStr, "gl_FragColor");
672 break;
673 case WINED3DSPR_RASTOUT:
674 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
675 break;
676 case WINED3DSPR_DEPTHOUT:
677 sprintf(tmpStr, "gl_FragDepth");
678 break;
679 case WINED3DSPR_ATTROUT:
680 if (reg == 0) {
681 sprintf(tmpStr, "gl_FrontColor");
682 } else {
683 sprintf(tmpStr, "gl_FrontSecondaryColor");
685 break;
686 case WINED3DSPR_TEXCRDOUT:
687 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
688 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
689 sprintf(tmpStr, "OUT%u", reg);
690 else
691 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
692 break;
693 case WINED3DSPR_MISCTYPE:
694 if (reg == 0) {
695 /* vPos */
696 sprintf(tmpStr, "gl_FragCoord");
697 } else {
698 /* gl_FrontFacing could be used for vFace, but note that
699 * gl_FrontFacing is a bool, while vFace is a float for
700 * which the sign determines front/back */
701 FIXME("Unhandled misctype register %d\n", reg);
702 sprintf(tmpStr, "unrecognized_register");
704 break;
705 default:
706 FIXME("Unhandled register name Type(%d)\n", regtype);
707 sprintf(tmpStr, "unrecognized_register");
708 break;
711 strcat(regstr, tmpStr);
714 /* Get the GLSL write mask for the destination register */
715 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
716 char *ptr = write_mask;
717 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
719 if (shader_is_scalar(param)) {
720 mask = WINED3DSP_WRITEMASK_0;
721 } else {
722 *ptr++ = '.';
723 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
724 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
725 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
726 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
729 *ptr = '\0';
731 return mask;
734 static size_t shader_glsl_get_write_mask_size(DWORD write_mask) {
735 size_t size = 0;
737 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
738 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
739 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
740 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
742 return size;
745 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
746 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
747 * but addressed as "rgba". To fix this we need to swap the register's x
748 * and z components. */
749 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
750 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
751 char *ptr = swizzle_str;
753 if (!shader_is_scalar(param)) {
754 *ptr++ = '.';
755 /* swizzle bits fields: wwzzyyxx */
756 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
757 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
758 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
759 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
762 *ptr = '\0';
765 /* From a given parameter token, generate the corresponding GLSL string.
766 * Also, return the actual register name and swizzle in case the
767 * caller needs this information as well. */
768 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
769 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param) {
770 BOOL is_color = FALSE;
771 char swizzle_str[6];
773 src_param->reg_name[0] = '\0';
774 src_param->param_str[0] = '\0';
775 swizzle_str[0] = '\0';
777 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
779 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
780 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
783 /* From a given parameter token, generate the corresponding GLSL string.
784 * Also, return the actual register name and swizzle in case the
785 * caller needs this information as well. */
786 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
787 const DWORD addr_token, glsl_dst_param_t *dst_param) {
788 BOOL is_color = FALSE;
790 dst_param->mask_str[0] = '\0';
791 dst_param->reg_name[0] = '\0';
793 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
794 return shader_glsl_get_write_mask(param, dst_param->mask_str);
797 /* Append the destination part of the instruction to the buffer, return the effective write mask */
798 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg, const DWORD param) {
799 glsl_dst_param_t dst_param;
800 DWORD mask;
801 int shift;
803 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
805 if(mask) {
806 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
807 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
810 return mask;
813 /* Append the destination part of the instruction to the buffer, return the effective write mask */
814 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
815 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
818 /** Process GLSL instruction modifiers */
819 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
821 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
823 if (arg->opcode->dst_token && mask != 0) {
824 glsl_dst_param_t dst_param;
826 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
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_param.reg_name,
831 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
833 if (mask & WINED3DSPDM_MSAMPCENTROID) {
834 FIXME("_centroid modifier not handled\n");
836 if (mask & WINED3DSPDM_PARTIALPRECISION) {
837 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
842 static inline const char* shader_get_comp_op(
843 const DWORD opcode) {
845 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
846 switch (op) {
847 case COMPARISON_GT: return ">";
848 case COMPARISON_EQ: return "==";
849 case COMPARISON_GE: return ">=";
850 case COMPARISON_LT: return "<";
851 case COMPARISON_NE: return "!=";
852 case COMPARISON_LE: return "<=";
853 default:
854 FIXME("Unrecognized comparison value: %u\n", op);
855 return "(\?\?)";
859 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, glsl_sample_function_t *sample_function) {
860 /* Note that there's no such thing as a projected cube texture. */
861 switch(sampler_type) {
862 case WINED3DSTT_1D:
863 sample_function->name = projected ? "texture1DProj" : "texture1D";
864 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
865 break;
866 case WINED3DSTT_2D:
867 sample_function->name = projected ? "texture2DProj" : "texture2D";
868 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
869 break;
870 case WINED3DSTT_CUBE:
871 sample_function->name = "textureCube";
872 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
873 break;
874 case WINED3DSTT_VOLUME:
875 sample_function->name = projected ? "texture3DProj" : "texture3D";
876 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
877 break;
878 default:
879 sample_function->name = "";
880 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
881 break;
886 /*****************************************************************************
888 * Begin processing individual instruction opcodes
890 ****************************************************************************/
892 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
893 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
894 CONST SHADER_OPCODE* curOpcode = arg->opcode;
895 SHADER_BUFFER* buffer = arg->buffer;
896 glsl_src_param_t src0_param;
897 glsl_src_param_t src1_param;
898 DWORD write_mask;
899 char op;
901 /* Determine the GLSL operator to use based on the opcode */
902 switch (curOpcode->opcode) {
903 case WINED3DSIO_MUL: op = '*'; break;
904 case WINED3DSIO_ADD: op = '+'; break;
905 case WINED3DSIO_SUB: op = '-'; break;
906 default:
907 op = ' ';
908 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
909 break;
912 write_mask = shader_glsl_append_dst(buffer, arg);
913 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
914 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
915 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
918 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
919 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
920 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
921 SHADER_BUFFER* buffer = arg->buffer;
922 glsl_src_param_t src0_param;
923 DWORD write_mask;
925 write_mask = shader_glsl_append_dst(buffer, arg);
926 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
928 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
929 * shader versions WINED3DSIO_MOVA is used for this. */
930 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
931 !shader_is_pshader_version(shader->baseShader.hex_version) &&
932 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR) ||
933 arg->opcode->opcode == WINED3DSIO_MOVA) {
934 /* We need to *round* to the nearest int here. */
935 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
936 if (mask_size > 1) {
937 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n", mask_size, src0_param.param_str, mask_size, src0_param.param_str);
938 } else {
939 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
941 } else {
942 shader_addline(buffer, "%s);\n", src0_param.param_str);
946 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
947 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
948 CONST SHADER_OPCODE* curOpcode = arg->opcode;
949 SHADER_BUFFER* buffer = arg->buffer;
950 glsl_src_param_t src0_param;
951 glsl_src_param_t src1_param;
952 DWORD dst_write_mask, src_write_mask;
953 size_t dst_size = 0;
955 dst_write_mask = shader_glsl_append_dst(buffer, arg);
956 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
958 /* dp3 works on vec3, dp4 on vec4 */
959 if (curOpcode->opcode == WINED3DSIO_DP4) {
960 src_write_mask = WINED3DSP_WRITEMASK_ALL;
961 } else {
962 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
965 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
966 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
968 if (dst_size > 1) {
969 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
970 } else {
971 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
975 /* Note that this instruction has some restrictions. The destination write mask
976 * can't contain the w component, and the source swizzles have to be .xyzw */
977 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
978 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
979 glsl_src_param_t src0_param;
980 glsl_src_param_t src1_param;
981 char dst_mask[6];
983 shader_glsl_get_write_mask(arg->dst, dst_mask);
984 shader_glsl_append_dst(arg->buffer, arg);
985 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
986 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
987 shader_addline(arg->buffer, "cross(%s, %s).%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
990 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
991 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
992 * GLSL uses the value as-is. */
993 void shader_glsl_pow(SHADER_OPCODE_ARG *arg) {
994 SHADER_BUFFER *buffer = arg->buffer;
995 glsl_src_param_t src0_param;
996 glsl_src_param_t src1_param;
997 DWORD dst_write_mask;
998 size_t dst_size;
1000 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1001 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1003 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1004 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1006 if (dst_size > 1) {
1007 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1008 } else {
1009 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1013 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1014 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1015 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1016 SHADER_BUFFER* buffer = arg->buffer;
1017 glsl_src_param_t src_param;
1018 const char *instruction;
1019 char arguments[256];
1020 DWORD write_mask;
1021 unsigned i;
1023 /* Determine the GLSL function to use based on the opcode */
1024 /* TODO: Possibly make this a table for faster lookups */
1025 switch (curOpcode->opcode) {
1026 case WINED3DSIO_MIN: instruction = "min"; break;
1027 case WINED3DSIO_MAX: instruction = "max"; break;
1028 case WINED3DSIO_RSQ: instruction = "inversesqrt"; break;
1029 case WINED3DSIO_ABS: instruction = "abs"; break;
1030 case WINED3DSIO_FRC: instruction = "fract"; break;
1031 case WINED3DSIO_NRM: instruction = "normalize"; break;
1032 case WINED3DSIO_LOGP:
1033 case WINED3DSIO_LOG: instruction = "log2"; break;
1034 case WINED3DSIO_EXP: instruction = "exp2"; break;
1035 case WINED3DSIO_SGN: instruction = "sign"; break;
1036 default: instruction = "";
1037 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1038 break;
1041 write_mask = shader_glsl_append_dst(buffer, arg);
1043 arguments[0] = '\0';
1044 if (curOpcode->num_params > 0) {
1045 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1046 strcat(arguments, src_param.param_str);
1047 for (i = 2; i < curOpcode->num_params; ++i) {
1048 strcat(arguments, ", ");
1049 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1050 strcat(arguments, src_param.param_str);
1054 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1057 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1058 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1059 * dst.x = 2^(floor(src))
1060 * dst.y = src - floor(src)
1061 * dst.z = 2^src (partial precision is allowed, but optional)
1062 * dst.w = 1.0;
1063 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1064 * dst = 2^src; (partial precision is allowed, but optional)
1066 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1067 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1068 glsl_src_param_t src_param;
1070 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1072 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1073 char dst_mask[6];
1075 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1076 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1077 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1078 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1080 shader_glsl_append_dst(arg->buffer, arg);
1081 shader_glsl_get_write_mask(arg->dst, dst_mask);
1082 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1083 } else {
1084 DWORD write_mask;
1085 size_t mask_size;
1087 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1088 mask_size = shader_glsl_get_write_mask_size(write_mask);
1090 if (mask_size > 1) {
1091 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1092 } else {
1093 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1098 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1099 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1100 glsl_src_param_t src_param;
1101 DWORD write_mask;
1102 size_t mask_size;
1104 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1105 mask_size = shader_glsl_get_write_mask_size(write_mask);
1106 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1108 if (mask_size > 1) {
1109 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1110 } else {
1111 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1115 /** Process signed comparison opcodes in GLSL. */
1116 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1117 glsl_src_param_t src0_param;
1118 glsl_src_param_t src1_param;
1119 DWORD write_mask;
1120 size_t mask_size;
1122 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1123 mask_size = shader_glsl_get_write_mask_size(write_mask);
1124 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1125 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1127 if (mask_size > 1) {
1128 const char *compare;
1130 switch(arg->opcode->opcode) {
1131 case WINED3DSIO_SLT: compare = "lessThan"; break;
1132 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1133 default: compare = "";
1134 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1137 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1138 src0_param.param_str, src1_param.param_str);
1139 } else {
1140 const char *compare;
1142 switch(arg->opcode->opcode) {
1143 case WINED3DSIO_SLT: compare = "<"; break;
1144 case WINED3DSIO_SGE: compare = ">="; break;
1145 default: compare = "";
1146 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1149 shader_addline(arg->buffer, "(%s %s %s) ? 1.0 : 0.0);\n",
1150 src0_param.param_str, compare, src1_param.param_str);
1154 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1155 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1156 glsl_src_param_t src0_param;
1157 glsl_src_param_t src1_param;
1158 glsl_src_param_t src2_param;
1159 DWORD write_mask, cmp_channel = 0;
1160 unsigned int i, j;
1162 /* Cycle through all source0 channels */
1163 for (i=0; i<4; i++) {
1164 write_mask = 0;
1165 /* Find the destination channels which use the current source0 channel */
1166 for (j=0; j<4; j++) {
1167 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1168 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1169 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1172 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1173 if (!write_mask) continue;
1175 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1176 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1177 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1179 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1180 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1184 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1185 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1186 * the compare is done per component of src0. */
1187 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1188 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1189 glsl_src_param_t src0_param;
1190 glsl_src_param_t src1_param;
1191 glsl_src_param_t src2_param;
1192 DWORD write_mask, cmp_channel = 0;
1193 unsigned int i, j;
1195 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1196 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1197 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1198 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1199 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1200 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1201 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1202 return;
1204 /* Cycle through all source0 channels */
1205 for (i=0; i<4; i++) {
1206 write_mask = 0;
1207 /* Find the destination channels which use the current source0 channel */
1208 for (j=0; j<4; j++) {
1209 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1210 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1211 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1214 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1215 if (!write_mask) continue;
1217 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1218 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1219 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1221 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1222 src0_param.param_str, src1_param.param_str, src2_param.param_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 glsl_src_param_t src0_param;
1229 glsl_src_param_t src1_param;
1230 glsl_src_param_t src2_param;
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_param);
1235 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1236 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1237 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1238 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1241 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1242 Vertex shaders to GLSL codes */
1243 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1244 int i;
1245 int nComponents = 0;
1246 SHADER_OPCODE_ARG tmpArg;
1248 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1250 /* Set constants for the temporary argument */
1251 tmpArg.shader = arg->shader;
1252 tmpArg.buffer = arg->buffer;
1253 tmpArg.src[0] = arg->src[0];
1254 tmpArg.src_addr[0] = arg->src_addr[0];
1255 tmpArg.src_addr[1] = arg->src_addr[1];
1256 tmpArg.reg_maps = arg->reg_maps;
1258 switch(arg->opcode->opcode) {
1259 case WINED3DSIO_M4x4:
1260 nComponents = 4;
1261 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1262 break;
1263 case WINED3DSIO_M4x3:
1264 nComponents = 3;
1265 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1266 break;
1267 case WINED3DSIO_M3x4:
1268 nComponents = 4;
1269 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1270 break;
1271 case WINED3DSIO_M3x3:
1272 nComponents = 3;
1273 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1274 break;
1275 case WINED3DSIO_M3x2:
1276 nComponents = 2;
1277 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1278 break;
1279 default:
1280 break;
1283 for (i = 0; i < nComponents; i++) {
1284 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1285 tmpArg.src[1] = arg->src[1]+i;
1286 shader_glsl_dot(&tmpArg);
1291 The LRP instruction performs a component-wise linear interpolation
1292 between the second and third operands using the first operand as the
1293 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1294 This is equivalent to mix(src2, src1, src0);
1296 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1297 glsl_src_param_t src0_param;
1298 glsl_src_param_t src1_param;
1299 glsl_src_param_t src2_param;
1300 DWORD write_mask;
1302 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1304 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1305 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1306 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1308 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1309 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1312 /** Process the WINED3DSIO_LIT instruction in GLSL:
1313 * dst.x = dst.w = 1.0
1314 * dst.y = (src0.x > 0) ? src0.x
1315 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1316 * where src.w is clamped at +- 128
1318 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1319 glsl_src_param_t src0_param;
1320 glsl_src_param_t src1_param;
1321 glsl_src_param_t src3_param;
1322 char dst_mask[6];
1324 shader_glsl_append_dst(arg->buffer, arg);
1325 shader_glsl_get_write_mask(arg->dst, dst_mask);
1327 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1328 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1329 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1331 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",
1332 src0_param.param_str, src0_param.param_str, src0_param.param_str, src1_param.param_str, src1_param.param_str, src3_param.param_str, dst_mask);
1335 /** Process the WINED3DSIO_DST instruction in GLSL:
1336 * dst.x = 1.0
1337 * dst.y = src0.x * src0.y
1338 * dst.z = src0.z
1339 * dst.w = src1.w
1341 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1342 glsl_src_param_t src0y_param;
1343 glsl_src_param_t src0z_param;
1344 glsl_src_param_t src1y_param;
1345 glsl_src_param_t src1w_param;
1346 char dst_mask[6];
1348 shader_glsl_append_dst(arg->buffer, arg);
1349 shader_glsl_get_write_mask(arg->dst, dst_mask);
1351 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1352 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1353 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1354 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1356 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1357 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1360 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1361 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1362 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1364 * dst.x = cos(src0.?)
1365 * dst.y = sin(src0.?)
1366 * dst.z = dst.z
1367 * dst.w = dst.w
1369 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1370 glsl_src_param_t src0_param;
1371 DWORD write_mask;
1373 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1374 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1376 switch (write_mask) {
1377 case WINED3DSP_WRITEMASK_0:
1378 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1379 break;
1381 case WINED3DSP_WRITEMASK_1:
1382 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1383 break;
1385 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1386 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1387 break;
1389 default:
1390 ERR("Write mask should be .x, .y or .xy\n");
1391 break;
1395 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1396 * Start a for() loop where src1.y is the initial value of aL,
1397 * increment aL by src1.z for a total of src1.x iterations.
1398 * Need to use a temporary variable for this operation.
1400 /* FIXME: I don't think nested loops will work correctly this way. */
1401 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1402 glsl_src_param_t src1_param;
1404 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1406 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1407 src1_param.reg_name, src1_param.reg_name, src1_param.reg_name);
1410 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1411 shader_addline(arg->buffer, "}\n");
1414 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1415 glsl_src_param_t src0_param;
1417 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1418 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s; tmpInt++) {\n", src0_param.param_str);
1421 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1422 glsl_src_param_t src0_param;
1424 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1425 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
1428 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1429 glsl_src_param_t src0_param;
1430 glsl_src_param_t src1_param;
1432 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1433 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1435 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1436 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1439 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1440 shader_addline(arg->buffer, "} else {\n");
1443 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1444 shader_addline(arg->buffer, "break;\n");
1447 /* FIXME: According to MSDN the compare is done per component. */
1448 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1449 glsl_src_param_t src0_param;
1450 glsl_src_param_t src1_param;
1452 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1453 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1455 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1456 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1459 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1461 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1462 shader_addline(arg->buffer, "}\n");
1463 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1466 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1467 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1468 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1471 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1472 glsl_src_param_t src1_param;
1474 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1475 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1476 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_param.param_str, snum);
1479 /*********************************************
1480 * Pixel Shader Specific Code begins here
1481 ********************************************/
1482 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1483 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1484 DWORD hex_version = This->baseShader.hex_version;
1485 char dst_swizzle[6];
1486 glsl_sample_function_t sample_function;
1487 DWORD sampler_type;
1488 DWORD sampler_idx;
1489 BOOL projected;
1490 DWORD mask = 0;
1492 /* All versions have a destination register */
1493 shader_glsl_append_dst(arg->buffer, arg);
1495 /* 1.0-1.4: Use destination register as sampler source.
1496 * 2.0+: Use provided sampler source. */
1497 if (hex_version < WINED3DPS_VERSION(1,4)) {
1498 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1499 DWORD flags;
1501 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1502 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1504 if (flags & WINED3DTTFF_PROJECTED) {
1505 projected = TRUE;
1506 switch (flags & ~WINED3DTTFF_PROJECTED) {
1507 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1508 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
1509 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
1510 case WINED3DTTFF_COUNT4:
1511 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
1513 } else {
1514 projected = FALSE;
1516 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
1517 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1518 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1520 if (src_mod == WINED3DSPSM_DZ) {
1521 projected = TRUE;
1522 mask = WINED3DSP_WRITEMASK_2;
1523 } else if (src_mod == WINED3DSPSM_DW) {
1524 projected = TRUE;
1525 mask = WINED3DSP_WRITEMASK_3;
1526 } else {
1527 projected = FALSE;
1529 } else {
1530 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1531 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
1532 /* ps 2.0 texldp instruction always divides by the fourth component. */
1533 projected = TRUE;
1534 mask = WINED3DSP_WRITEMASK_3;
1535 } else {
1536 projected = FALSE;
1540 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1541 shader_glsl_get_sample_function(sampler_type, projected, &sample_function);
1542 mask |= sample_function.coord_mask;
1544 if (hex_version < WINED3DPS_VERSION(2,0)) {
1545 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
1546 } else {
1547 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
1550 /* 1.0-1.3: Use destination register as coordinate source.
1551 1.4+: Use provided coordinate source register. */
1552 if (hex_version < WINED3DPS_VERSION(1,4)) {
1553 char coord_mask[6];
1554 shader_glsl_get_write_mask(mask, coord_mask);
1555 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
1556 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
1557 } else {
1558 glsl_src_param_t coord_param;
1559 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
1560 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
1561 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
1565 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1567 /* FIXME: Make this work for more than just 2D textures */
1569 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1570 SHADER_BUFFER* buffer = arg->buffer;
1571 DWORD hex_version = This->baseShader.hex_version;
1572 DWORD write_mask;
1573 char dst_mask[6];
1575 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1576 shader_glsl_get_write_mask(write_mask, dst_mask);
1578 if (hex_version != WINED3DPS_VERSION(1,4)) {
1579 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1580 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
1581 } else {
1582 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1583 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1584 char dst_swizzle[6];
1586 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
1588 if (src_mod == WINED3DSPSM_DZ) {
1589 glsl_src_param_t div_param;
1590 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
1591 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
1593 if (mask_size > 1) {
1594 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
1595 } else {
1596 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
1598 } else if (src_mod == WINED3DSPSM_DW) {
1599 glsl_src_param_t div_param;
1600 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
1601 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
1603 if (mask_size > 1) {
1604 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
1605 } else {
1606 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
1608 } else {
1609 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
1614 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1615 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1616 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1617 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1618 glsl_src_param_t src0_param;
1619 char dst_mask[6];
1620 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1621 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1623 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1625 shader_glsl_append_dst(arg->buffer, arg);
1626 shader_glsl_get_write_mask(arg->dst, dst_mask);
1627 shader_addline(arg->buffer, "texture1D(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
1628 sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
1631 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1632 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1633 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1634 glsl_src_param_t src0_param;
1635 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1636 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1637 DWORD dst_mask;
1638 size_t mask_size;
1640 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
1641 mask_size = shader_glsl_get_write_mask_size(dst_mask);
1642 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1644 if (mask_size > 1) {
1645 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
1646 } else {
1647 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
1651 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1652 * Calculate the depth as dst.x / dst.y */
1653 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1654 glsl_dst_param_t dst_param;
1656 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1658 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_param.reg_name, dst_param.reg_name);
1661 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1662 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1663 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1664 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1666 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1667 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1668 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1669 glsl_src_param_t src0_param;
1671 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1673 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
1674 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
1677 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1678 * Calculate the 1st of a 2-row matrix multiplication. */
1679 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1680 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1681 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1682 SHADER_BUFFER* buffer = arg->buffer;
1683 glsl_src_param_t src0_param;
1685 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1686 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1689 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1690 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1691 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1693 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1694 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1695 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1696 SHADER_BUFFER* buffer = arg->buffer;
1697 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1698 glsl_src_param_t src0_param;
1700 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1701 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
1702 current_state->texcoord_w[current_state->current_row++] = reg;
1705 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1706 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1707 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1708 SHADER_BUFFER* buffer = arg->buffer;
1709 glsl_src_param_t src0_param;
1710 char dst_mask[6];
1712 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1713 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1715 shader_glsl_append_dst(buffer, arg);
1716 shader_glsl_get_write_mask(arg->dst, dst_mask);
1718 /* Sample the texture using the calculated coordinates */
1719 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
1722 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1723 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1724 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1725 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1726 glsl_src_param_t src0_param;
1727 char dst_mask[6];
1728 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1729 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1730 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1731 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1732 glsl_sample_function_t sample_function;
1734 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1735 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1737 shader_glsl_append_dst(arg->buffer, arg);
1738 shader_glsl_get_write_mask(arg->dst, dst_mask);
1739 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1741 /* Sample the texture using the calculated coordinates */
1742 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
1744 current_state->current_row = 0;
1747 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1748 * Perform the 3rd row of a 3x3 matrix multiply */
1749 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
1750 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1751 glsl_src_param_t src0_param;
1752 char dst_mask[6];
1753 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1754 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1755 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1757 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1759 shader_glsl_append_dst(arg->buffer, arg);
1760 shader_glsl_get_write_mask(arg->dst, dst_mask);
1761 shader_addline(arg->buffer, "vec4(tmp.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
1763 current_state->current_row = 0;
1766 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1767 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1768 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1770 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1771 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1772 glsl_src_param_t src0_param;
1773 glsl_src_param_t src1_param;
1774 char dst_mask[6];
1775 SHADER_BUFFER* buffer = arg->buffer;
1776 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1777 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1778 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1779 glsl_sample_function_t sample_function;
1781 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1782 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1784 /* Perform the last matrix multiply operation */
1785 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1787 /* Calculate reflection vector, 2*(tmp0.src1)*tmp0-src1
1788 * This is equivalent to reflect(-src1, tmp0); */
1789 shader_addline(buffer, "tmp0.xyz = reflect(-(%s), tmp0.xyz);\n", src1_param.param_str);
1791 shader_glsl_append_dst(buffer, arg);
1792 shader_glsl_get_write_mask(arg->dst, dst_mask);
1793 shader_glsl_get_sample_function(stype, FALSE, &sample_function);
1795 /* Sample the texture */
1796 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
1798 current_state->current_row = 0;
1801 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1802 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1803 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1805 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1806 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1807 SHADER_BUFFER* buffer = arg->buffer;
1808 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1809 glsl_src_param_t src0_param;
1810 char dst_mask[6];
1811 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1812 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1813 glsl_sample_function_t sample_function;
1815 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1817 /* Perform the last matrix multiply operation */
1818 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
1820 /* Construct the eye-ray vector from w coordinates */
1821 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
1822 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
1824 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(tmp0.tmp1)*tmp0-tmp1
1825 * This is equivalent to reflect(-tmp1, tmp0); */
1826 shader_addline(buffer, "tmp0.xyz = reflect(-tmp1.xyz, tmp0.xyz);\n");
1828 shader_glsl_append_dst(buffer, arg);
1829 shader_glsl_get_write_mask(arg->dst, dst_mask);
1830 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1832 /* Sample the texture using the calculated coordinates */
1833 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
1835 current_state->current_row = 0;
1838 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1839 * Apply a fake bump map transform.
1840 * texbem is pshader <= 1.3 only, this saves a few version checks
1842 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1843 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1844 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1845 char dst_swizzle[6];
1846 glsl_sample_function_t sample_function;
1847 glsl_src_param_t coord_param;
1848 DWORD sampler_type;
1849 DWORD sampler_idx;
1850 DWORD mask;
1851 DWORD flags;
1852 char coord_mask[6];
1854 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1855 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1857 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1858 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1859 mask = sample_function.coord_mask;
1861 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
1863 shader_glsl_get_write_mask(mask, coord_mask);
1865 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1866 * so we can't let the GL handle this.
1868 if (flags & WINED3DTTFF_PROJECTED) {
1869 DWORD div_mask=0;
1870 char coord_div_mask[3];
1871 switch (flags & ~WINED3DTTFF_PROJECTED) {
1872 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1873 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
1874 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
1875 case WINED3DTTFF_COUNT4:
1876 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
1878 shader_glsl_get_write_mask(div_mask, coord_div_mask);
1879 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
1882 shader_glsl_append_dst(arg->buffer, arg);
1883 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
1884 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )%s);\n",
1885 sample_function.name, sampler_idx, sampler_idx, coord_mask, coord_param.param_str, coord_mask, dst_swizzle);
1888 void pshader_glsl_bem(SHADER_OPCODE_ARG* arg) {
1889 glsl_src_param_t src0_param, src1_param;
1891 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
1892 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
1894 shader_glsl_append_dst(arg->buffer, arg);
1895 shader_addline(arg->buffer, "%s + bumpenvmat * %s);\n",
1896 src0_param.param_str, src1_param.param_str);
1899 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1900 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1901 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1902 glsl_src_param_t src0_param;
1903 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1904 char dst_mask[6];
1906 shader_glsl_append_dst(arg->buffer, arg);
1907 shader_glsl_get_write_mask(arg->dst, dst_mask);
1908 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1910 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
1913 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1914 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1915 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1916 glsl_src_param_t src0_param;
1917 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1918 char dst_mask[6];
1920 shader_glsl_append_dst(arg->buffer, arg);
1921 shader_glsl_get_write_mask(arg->dst, dst_mask);
1922 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1924 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
1927 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
1928 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
1929 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1930 glsl_src_param_t src0_param;
1931 char dst_mask[6];
1932 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1933 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1934 glsl_sample_function_t sample_function;
1936 shader_glsl_append_dst(arg->buffer, arg);
1937 shader_glsl_get_write_mask(arg->dst, dst_mask);
1938 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1939 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
1941 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
1944 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
1945 * If any of the first 3 components are < 0, discard this pixel */
1946 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
1947 glsl_dst_param_t dst_param;
1949 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1950 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
1953 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
1954 * dst = dot2(src0, src1) + src2 */
1955 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
1956 glsl_src_param_t src0_param;
1957 glsl_src_param_t src1_param;
1958 glsl_src_param_t src2_param;
1959 DWORD write_mask;
1960 size_t mask_size;
1962 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1963 mask_size = shader_glsl_get_write_mask_size(write_mask);
1965 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
1966 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
1967 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
1969 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
1972 void pshader_glsl_input_pack(
1973 SHADER_BUFFER* buffer,
1974 semantic* semantics_in) {
1976 unsigned int i;
1978 for (i = 0; i < MAX_REG_INPUT; i++) {
1980 DWORD usage_token = semantics_in[i].usage;
1981 DWORD register_token = semantics_in[i].reg;
1982 DWORD usage, usage_idx;
1983 char reg_mask[6];
1985 /* Uninitialized */
1986 if (!usage_token) continue;
1987 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1988 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1989 shader_glsl_get_write_mask(register_token, reg_mask);
1991 switch(usage) {
1993 case WINED3DDECLUSAGE_COLOR:
1994 if (usage_idx == 0)
1995 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
1996 i, reg_mask, reg_mask);
1997 else if (usage_idx == 1)
1998 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
1999 i, reg_mask, reg_mask);
2000 else
2001 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
2002 i, reg_mask, reg_mask);
2003 break;
2005 case WINED3DDECLUSAGE_TEXCOORD:
2006 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
2007 i, reg_mask, usage_idx, reg_mask );
2008 break;
2010 case WINED3DDECLUSAGE_FOG:
2011 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
2012 i, reg_mask, reg_mask);
2013 break;
2015 default:
2016 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
2017 i, reg_mask, reg_mask);
2022 /*********************************************
2023 * Vertex Shader Specific Code begins here
2024 ********************************************/
2026 void vshader_glsl_output_unpack(
2027 SHADER_BUFFER* buffer,
2028 semantic* semantics_out) {
2030 unsigned int i;
2032 for (i = 0; i < MAX_REG_OUTPUT; i++) {
2034 DWORD usage_token = semantics_out[i].usage;
2035 DWORD register_token = semantics_out[i].reg;
2036 DWORD usage, usage_idx;
2037 char reg_mask[6];
2039 /* Uninitialized */
2040 if (!usage_token) continue;
2042 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2043 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2044 shader_glsl_get_write_mask(register_token, reg_mask);
2046 switch(usage) {
2048 case WINED3DDECLUSAGE_COLOR:
2049 if (usage_idx == 0)
2050 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2051 else if (usage_idx == 1)
2052 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2053 else
2054 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2055 break;
2057 case WINED3DDECLUSAGE_POSITION:
2058 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2059 break;
2061 case WINED3DDECLUSAGE_TEXCOORD:
2062 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
2063 usage_idx, reg_mask, i, reg_mask);
2064 break;
2066 case WINED3DDECLUSAGE_PSIZE:
2067 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
2068 break;
2070 case WINED3DDECLUSAGE_FOG:
2071 shader_addline(buffer, "gl_FogFragCoord = OUT%u%s;\n", i, reg_mask);
2072 break;
2074 default:
2075 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2080 static void add_glsl_program_entry(IWineD3DDeviceImpl *device, struct glsl_shader_prog_link *entry) {
2081 glsl_program_key_t *key;
2083 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2084 key->vshader = entry->vshader;
2085 key->pshader = entry->pshader;
2087 hash_table_put(device->glsl_program_lookup, key, entry);
2090 static struct glsl_shader_prog_link *get_glsl_program_entry(IWineD3DDeviceImpl *device,
2091 GLhandleARB vshader, GLhandleARB pshader) {
2092 glsl_program_key_t key;
2094 key.vshader = vshader;
2095 key.pshader = pshader;
2097 return (struct glsl_shader_prog_link *)hash_table_get(device->glsl_program_lookup, &key);
2100 void delete_glsl_program_entry(IWineD3DDevice *iface, struct glsl_shader_prog_link *entry) {
2101 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2102 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2103 glsl_program_key_t *key;
2105 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2106 key->vshader = entry->vshader;
2107 key->pshader = entry->pshader;
2108 hash_table_remove(This->glsl_program_lookup, key);
2110 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2111 if (entry->vshader) list_remove(&entry->vshader_entry);
2112 if (entry->pshader) list_remove(&entry->pshader_entry);
2113 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2114 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2115 HeapFree(GetProcessHeap(), 0, entry);
2118 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
2119 * It sets the programId on the current StateBlock (because it should be called
2120 * inside of the DrawPrimitive() part of the render loop).
2122 * If a program for the given combination does not exist, create one, and store
2123 * the program in the hash table. If it creates a program, it will link the
2124 * given objects, too.
2126 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
2127 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2128 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2129 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
2130 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
2131 struct glsl_shader_prog_link *entry = NULL;
2132 GLhandleARB programId = 0;
2133 int i;
2134 char glsl_name[8];
2136 GLhandleARB vshader_id = use_vs ? ((IWineD3DBaseShaderImpl*)vshader)->baseShader.prgId : 0;
2137 GLhandleARB pshader_id = use_ps ? ((IWineD3DBaseShaderImpl*)pshader)->baseShader.prgId : 0;
2138 entry = get_glsl_program_entry(This, vshader_id, pshader_id);
2139 if (entry) {
2140 This->stateBlock->glsl_program = entry;
2141 return;
2144 /* If we get to this point, then no matching program exists, so we create one */
2145 programId = GL_EXTCALL(glCreateProgramObjectARB());
2146 TRACE("Created new GLSL shader program %u\n", programId);
2148 /* Create the entry */
2149 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
2150 entry->programId = programId;
2151 entry->vshader = vshader_id;
2152 entry->pshader = pshader_id;
2153 /* Add the hash table entry */
2154 add_glsl_program_entry(This, entry);
2156 /* Set the current program */
2157 This->stateBlock->glsl_program = entry;
2159 /* Attach GLSL vshader */
2160 if (vshader_id) {
2161 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
2162 char tmp_name[10];
2164 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
2165 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
2166 checkGLcall("glAttachObjectARB");
2168 /* Bind vertex attributes to a corresponding index number to match
2169 * the same index numbers as ARB_vertex_programs (makes loading
2170 * vertex attributes simpler). With this method, we can use the
2171 * exact same code to load the attributes later for both ARB and
2172 * GLSL shaders.
2174 * We have to do this here because we need to know the Program ID
2175 * in order to make the bindings work, and it has to be done prior
2176 * to linking the GLSL program. */
2177 for (i = 0; i < max_attribs; ++i) {
2178 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
2179 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
2181 checkGLcall("glBindAttribLocationARB");
2183 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
2186 /* Attach GLSL pshader */
2187 if (pshader_id) {
2188 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
2189 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
2190 checkGLcall("glAttachObjectARB");
2192 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
2195 /* Link the program */
2196 TRACE("Linking GLSL shader program %u\n", programId);
2197 GL_EXTCALL(glLinkProgramARB(programId));
2198 print_glsl_info_log(&GLINFO_LOCATION, programId);
2200 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
2201 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
2202 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
2203 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2205 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
2206 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
2207 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
2208 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2212 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
2213 GLhandleARB program_id;
2214 GLhandleARB vshader_id, pshader_id;
2215 const char *blt_vshader[] = {
2216 "void main(void)\n"
2217 "{\n"
2218 " gl_Position = gl_Vertex;\n"
2219 " gl_FrontColor = vec4(1.0);\n"
2220 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2221 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2222 "}\n"
2225 const char *blt_pshader[] = {
2226 "uniform sampler2D sampler;\n"
2227 "void main(void)\n"
2228 "{\n"
2229 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2230 "}\n"
2233 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2234 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2235 GL_EXTCALL(glCompileShaderARB(vshader_id));
2237 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2238 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2239 GL_EXTCALL(glCompileShaderARB(pshader_id));
2241 program_id = GL_EXTCALL(glCreateProgramObjectARB());
2242 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2243 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2244 GL_EXTCALL(glLinkProgramARB(program_id));
2246 print_glsl_info_log(&GLINFO_LOCATION, program_id);
2248 return program_id;
2251 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2252 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2253 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2254 GLhandleARB program_id = 0;
2256 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
2257 else This->stateBlock->glsl_program = NULL;
2259 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2260 if (program_id) TRACE("Using GLSL program %u\n", program_id);
2261 GL_EXTCALL(glUseProgramObjectARB(program_id));
2262 checkGLcall("glUseProgramObjectARB");
2265 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2266 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2267 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2268 static GLhandleARB program_id = 0;
2269 static GLhandleARB loc = -1;
2271 if (!program_id) {
2272 program_id = create_glsl_blt_shader(gl_info);
2273 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2276 GL_EXTCALL(glUseProgramObjectARB(program_id));
2277 GL_EXTCALL(glUniform1iARB(loc, 0));
2280 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
2281 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2282 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2283 GL_EXTCALL(glUseProgramObjectARB(0));
2286 const shader_backend_t glsl_shader_backend = {
2287 &shader_glsl_select,
2288 &shader_glsl_select_depth_blt,
2289 &shader_glsl_load_constants,
2290 &shader_glsl_cleanup