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.
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)
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;
60 GL_EXTCALL(glGetObjectParameterivARB(obj,
61 GL_OBJECT_INFO_LOG_LENGTH_ARB,
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);
76 * Loads (pixel shader) samplers
78 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;
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));
93 TRACE("Loading %s for texture %d\n", sampler_name, i);
94 GL_EXTCALL(glUniform1iARB(name_loc, i));
95 checkGLcall("glUniform1iARB");
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;
114 if (TRACE_ON(d3d_shader)) {
115 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
120 tmp_loc = constant_locations[i];
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) {
134 tmp_loc = constant_locations[i];
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];
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];
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()");
165 * Loads integer constants (aka uniforms) into the currently set GLSL program.
166 * When @constants_set == NULL, it will load all the constants.
168 void shader_glsl_load_constantsI(
169 IWineD3DBaseShaderImpl* This,
170 WineD3D_GL_Info *gl_info,
171 GLhandleARB programId,
172 unsigned max_constants,
174 BOOL* constants_set) {
179 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
180 const char* prefix = is_pshader? "PI":"VI";
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));
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);
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));
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);
223 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
224 * When @constants_set == NULL, it will load all the constants.
226 void shader_glsl_load_constantsB(
227 IWineD3DBaseShaderImpl* This,
228 WineD3D_GL_Info *gl_info,
229 GLhandleARB programId,
230 unsigned max_constants,
232 BOOL* constants_set) {
237 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
238 const char* prefix = is_pshader? "PB":"VB";
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));
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);
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));
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,
286 char useVertexShader) {
288 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
289 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
290 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*) deviceImpl->wineD3D)->gl_info;
292 GLhandleARB *constant_locations;
293 struct list *constant_list;
294 GLhandleARB programId;
297 if (!stateBlock->glsl_program) {
298 /* No GLSL program set - nothing to do. */
301 programId = stateBlock->glsl_program->programId;
303 if (useVertexShader) {
304 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
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 != 0) {
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(glUniform4fvARB(pos, 1, 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;
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);
402 shader_addline(buffer, "uniform vec4 posFixup;\n");
403 else if(reg_maps->bumpmat)
404 shader_addline(buffer, "uniform vec4 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;
414 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
417 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
419 case WINED3DSTT_CUBE:
420 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
422 case WINED3DSTT_VOLUME:
423 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
426 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
427 FIXME("Unrecognized sampler type: %#x\n", stype);
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 ****************************************************************************/
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[] = {
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) */
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 (
517 const char *in_regswizzle,
522 if (instr == WINED3DSIO_TEXKILL)
525 switch (instr & WINED3DSP_SRCMOD_MASK) {
526 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
528 case WINED3DSPSM_NONE:
529 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
531 case WINED3DSPSM_NEG:
532 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
534 case WINED3DSPSM_NOT:
535 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
537 case WINED3DSPSM_BIAS:
538 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
540 case WINED3DSPSM_BIASNEG:
541 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
543 case WINED3DSPSM_SIGN:
544 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
546 case WINED3DSPSM_SIGNNEG:
547 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
549 case WINED3DSPSM_COMP:
550 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
553 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
555 case WINED3DSPSM_X2NEG:
556 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
558 case WINED3DSPSM_ABS:
559 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
561 case WINED3DSPSM_ABSNEG:
562 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
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(
574 const DWORD addr_token,
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 = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
588 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
594 case WINED3DSPR_TEMP:
595 sprintf(tmpStr, "R%u", reg);
597 case WINED3DSPR_INPUT:
599 /* Pixel shaders >= 3.0 */
600 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
601 sprintf(tmpStr, "IN%u", reg);
604 strcpy(tmpStr, "gl_Color");
606 strcpy(tmpStr, "gl_SecondaryColor");
609 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
611 sprintf(tmpStr, "attrib%u", reg);
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);
628 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
631 sprintf(tmpStr, "%s[%u]", prefix, reg);
635 case WINED3DSPR_CONSTINT:
637 sprintf(tmpStr, "PI[%u]", reg);
639 sprintf(tmpStr, "VI[%u]", reg);
641 case WINED3DSPR_CONSTBOOL:
643 sprintf(tmpStr, "PB[%u]", reg);
645 sprintf(tmpStr, "VB[%u]", reg);
647 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
649 sprintf(tmpStr, "T%u", reg);
651 sprintf(tmpStr, "A%u", reg);
654 case WINED3DSPR_LOOP:
655 sprintf(tmpStr, "aL");
657 case WINED3DSPR_SAMPLER:
659 sprintf(tmpStr, "Psampler%u", reg);
661 sprintf(tmpStr, "Vsampler%u", reg);
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");
673 case WINED3DSPR_RASTOUT:
674 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
676 case WINED3DSPR_DEPTHOUT:
677 sprintf(tmpStr, "gl_FragDepth");
679 case WINED3DSPR_ATTROUT:
681 sprintf(tmpStr, "gl_FrontColor");
683 sprintf(tmpStr, "gl_FrontSecondaryColor");
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);
691 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
694 FIXME("Unhandled register name Type(%d)\n", regtype);
695 sprintf(tmpStr, "unrecognized_register");
699 strcat(regstr, tmpStr);
702 /* Get the GLSL write mask for the destination register */
703 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
704 char *ptr = write_mask;
705 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
706 DWORD reg_type = shader_get_regtype(param);
708 /* gl_FogFragCoord, gl_PointSize and gl_FragDepth are floats, fixup the write mask. */
709 if (((reg_type == WINED3DSPR_RASTOUT) && ((param & WINED3DSP_REGNUM_MASK) != 0))
710 || reg_type == WINED3DSPR_DEPTHOUT) {
711 mask = WINED3DSP_WRITEMASK_0;
714 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
715 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
716 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
717 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
725 static size_t shader_glsl_get_write_mask_size(DWORD write_mask) {
728 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
729 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
730 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
731 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
736 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
737 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
738 * but addressed as "rgba". To fix this we need to swap the register's x
739 * and z components. */
740 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
741 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
742 char *ptr = swizzle_str;
745 /* swizzle bits fields: wwzzyyxx */
746 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
747 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
748 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
749 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
754 /* From a given parameter token, generate the corresponding GLSL string.
755 * Also, return the actual register name and swizzle in case the
756 * caller needs this information as well. */
757 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
758 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param) {
759 BOOL is_color = FALSE;
762 src_param->reg_name[0] = '\0';
763 src_param->param_str[0] = '\0';
764 swizzle_str[0] = '\0';
766 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
768 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
769 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
772 /* From a given parameter token, generate the corresponding GLSL string.
773 * Also, return the actual register name and swizzle in case the
774 * caller needs this information as well. */
775 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
776 const DWORD addr_token, glsl_dst_param_t *dst_param) {
777 BOOL is_color = FALSE;
779 dst_param->mask_str[0] = '\0';
780 dst_param->reg_name[0] = '\0';
782 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
783 return shader_glsl_get_write_mask(param, dst_param->mask_str);
786 /* Append the destination part of the instruction to the buffer, return the effective write mask */
787 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
788 glsl_dst_param_t dst_param;
792 shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
793 mask = shader_glsl_add_dst_param(arg, arg->dst, arg->dst_addr, &dst_param);
794 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
799 /** Process GLSL instruction modifiers */
800 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
802 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
804 if (arg->opcode->dst_token && mask != 0) {
805 glsl_dst_param_t dst_param;
807 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
809 if (mask & WINED3DSPDM_SATURATE) {
810 /* _SAT means to clamp the value of the register to between 0 and 1 */
811 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
812 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
814 if (mask & WINED3DSPDM_MSAMPCENTROID) {
815 FIXME("_centroid modifier not handled\n");
817 if (mask & WINED3DSPDM_PARTIALPRECISION) {
818 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
823 static inline const char* shader_get_comp_op(
824 const DWORD opcode) {
826 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
828 case COMPARISON_GT: return ">";
829 case COMPARISON_EQ: return "==";
830 case COMPARISON_GE: return ">=";
831 case COMPARISON_LT: return "<";
832 case COMPARISON_NE: return "!=";
833 case COMPARISON_LE: return "<=";
835 FIXME("Unrecognized comparison value: %u\n", op);
840 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, glsl_sample_function_t *sample_function) {
841 /* Note that there's no such thing as a projected cube texture. */
842 switch(sampler_type) {
844 sample_function->name = projected ? "texture1DProj" : "texture1D";
845 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
848 sample_function->name = projected ? "texture2DProj" : "texture2D";
849 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
851 case WINED3DSTT_CUBE:
852 sample_function->name = "textureCube";
853 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
855 case WINED3DSTT_VOLUME:
856 sample_function->name = projected ? "texture3DProj" : "texture3D";
857 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
860 sample_function->name = "";
861 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
867 /*****************************************************************************
869 * Begin processing individual instruction opcodes
871 ****************************************************************************/
873 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
874 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
875 CONST SHADER_OPCODE* curOpcode = arg->opcode;
876 SHADER_BUFFER* buffer = arg->buffer;
877 glsl_src_param_t src0_param;
878 glsl_src_param_t src1_param;
882 /* Determine the GLSL operator to use based on the opcode */
883 switch (curOpcode->opcode) {
884 case WINED3DSIO_MUL: op = '*'; break;
885 case WINED3DSIO_ADD: op = '+'; break;
886 case WINED3DSIO_SUB: op = '-'; break;
889 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
893 write_mask = shader_glsl_append_dst(buffer, arg);
894 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
895 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
896 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
899 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
900 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
901 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
902 SHADER_BUFFER* buffer = arg->buffer;
903 glsl_src_param_t src0_param;
906 write_mask = shader_glsl_append_dst(buffer, arg);
907 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
909 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
910 * shader versions WINED3DSIO_MOVA is used for this. */
911 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
912 !shader_is_pshader_version(shader->baseShader.hex_version) &&
913 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR) ||
914 arg->opcode->opcode == WINED3DSIO_MOVA) {
915 /* We need to *round* to the nearest int here. */
916 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
918 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);
920 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
923 shader_addline(buffer, "%s);\n", src0_param.param_str);
927 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
928 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
929 CONST SHADER_OPCODE* curOpcode = arg->opcode;
930 SHADER_BUFFER* buffer = arg->buffer;
931 glsl_src_param_t src0_param;
932 glsl_src_param_t src1_param;
933 DWORD dst_write_mask, src_write_mask;
936 dst_write_mask = shader_glsl_append_dst(buffer, arg);
937 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
939 /* dp3 works on vec3, dp4 on vec4 */
940 if (curOpcode->opcode == WINED3DSIO_DP4) {
941 src_write_mask = WINED3DSP_WRITEMASK_ALL;
943 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
946 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
947 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
950 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
952 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
956 /* Note that this instruction has some restrictions. The destination write mask
957 * can't contain the w component, and the source swizzles have to be .xyzw */
958 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
959 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
960 glsl_src_param_t src0_param;
961 glsl_src_param_t src1_param;
964 shader_glsl_get_write_mask(arg->dst, dst_mask);
965 shader_glsl_append_dst(arg->buffer, arg);
966 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
967 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
968 shader_addline(arg->buffer, "cross(%s, %s).%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
971 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
972 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
973 CONST SHADER_OPCODE* curOpcode = arg->opcode;
974 SHADER_BUFFER* buffer = arg->buffer;
975 glsl_src_param_t src_param;
976 const char *instruction;
981 /* Determine the GLSL function to use based on the opcode */
982 /* TODO: Possibly make this a table for faster lookups */
983 switch (curOpcode->opcode) {
984 case WINED3DSIO_MIN: instruction = "min"; break;
985 case WINED3DSIO_MAX: instruction = "max"; break;
986 case WINED3DSIO_RSQ: instruction = "inversesqrt"; break;
987 case WINED3DSIO_ABS: instruction = "abs"; break;
988 case WINED3DSIO_FRC: instruction = "fract"; break;
989 case WINED3DSIO_POW: instruction = "pow"; break;
990 case WINED3DSIO_NRM: instruction = "normalize"; break;
991 case WINED3DSIO_LOGP:
992 case WINED3DSIO_LOG: instruction = "log2"; break;
993 case WINED3DSIO_EXP: instruction = "exp2"; break;
994 case WINED3DSIO_SGN: instruction = "sign"; break;
995 default: instruction = "";
996 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1000 write_mask = shader_glsl_append_dst(buffer, arg);
1002 arguments[0] = '\0';
1003 if (curOpcode->num_params > 0) {
1004 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1005 strcat(arguments, src_param.param_str);
1006 for (i = 2; i < curOpcode->num_params; ++i) {
1007 strcat(arguments, ", ");
1008 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1009 strcat(arguments, src_param.param_str);
1013 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1016 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1017 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1018 * dst.x = 2^(floor(src))
1019 * dst.y = src - floor(src)
1020 * dst.z = 2^src (partial precision is allowed, but optional)
1022 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1023 * dst = 2^src; (partial precision is allowed, but optional)
1025 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1026 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1027 glsl_src_param_t src_param;
1029 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1031 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1034 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1035 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1036 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1037 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1039 shader_glsl_append_dst(arg->buffer, arg);
1040 shader_glsl_get_write_mask(arg->dst, dst_mask);
1041 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1046 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1047 mask_size = shader_glsl_get_write_mask_size(write_mask);
1049 if (mask_size > 1) {
1050 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1052 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1057 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1058 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1059 glsl_src_param_t src_param;
1063 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1064 mask_size = shader_glsl_get_write_mask_size(write_mask);
1065 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1067 if (mask_size > 1) {
1068 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1070 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1074 /** Process signed comparison opcodes in GLSL. */
1075 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1076 glsl_src_param_t src0_param;
1077 glsl_src_param_t src1_param;
1081 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1082 mask_size = shader_glsl_get_write_mask_size(write_mask);
1083 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1084 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1086 if (mask_size > 1) {
1087 const char *compare;
1089 switch(arg->opcode->opcode) {
1090 case WINED3DSIO_SLT: compare = "lessThan"; break;
1091 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1092 default: compare = "";
1093 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1096 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1097 src0_param.param_str, src1_param.param_str);
1099 const char *compare;
1101 switch(arg->opcode->opcode) {
1102 case WINED3DSIO_SLT: compare = "<"; break;
1103 case WINED3DSIO_SGE: compare = ">="; break;
1104 default: compare = "";
1105 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1108 shader_addline(arg->buffer, "(%s %s %s) ? 1.0 : 0.0);\n",
1109 src0_param.param_str, compare, src1_param.param_str);
1113 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1114 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1115 glsl_src_param_t src0_param;
1116 glsl_src_param_t src1_param;
1117 glsl_src_param_t src2_param;
1121 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1122 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);
1126 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1128 if (mask_size > 1) {
1129 shader_addline(arg->buffer, "mix(%s, %s, vec%d(lessThan(%s, vec%d(0.0)))));\n",
1130 src1_param.param_str, src2_param.param_str, mask_size, src0_param.param_str, mask_size);
1132 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1133 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1137 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
1138 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1139 * the compare is done per component of src0. */
1140 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1141 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1142 glsl_src_param_t src0_param;
1143 glsl_src_param_t src1_param;
1144 glsl_src_param_t src2_param;
1148 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1150 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1152 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1154 mask_size = shader_glsl_get_write_mask_size(write_mask);
1155 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1158 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1159 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1161 if (mask_size > 1) {
1162 shader_addline(arg->buffer, "mix(%s, %s, vec%d(lessThan(%s, vec%d(0.5)))));\n",
1163 src2_param.param_str, src1_param.param_str, mask_size, src0_param.param_str, mask_size);
1165 shader_addline(arg->buffer, "%s < 0.5 ? %s : %s);\n",
1166 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1170 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1171 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1172 glsl_src_param_t src0_param;
1173 glsl_src_param_t src1_param;
1174 glsl_src_param_t src2_param;
1177 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1178 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1179 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1180 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1181 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1182 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1185 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1186 Vertex shaders to GLSL codes */
1187 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1189 int nComponents = 0;
1190 SHADER_OPCODE_ARG tmpArg;
1192 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1194 /* Set constants for the temporary argument */
1195 tmpArg.shader = arg->shader;
1196 tmpArg.buffer = arg->buffer;
1197 tmpArg.src[0] = arg->src[0];
1198 tmpArg.src_addr[0] = arg->src_addr[0];
1199 tmpArg.src_addr[1] = arg->src_addr[1];
1200 tmpArg.reg_maps = arg->reg_maps;
1202 switch(arg->opcode->opcode) {
1203 case WINED3DSIO_M4x4:
1205 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1207 case WINED3DSIO_M4x3:
1209 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1211 case WINED3DSIO_M3x4:
1213 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1215 case WINED3DSIO_M3x3:
1217 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1219 case WINED3DSIO_M3x2:
1221 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1227 for (i = 0; i < nComponents; i++) {
1228 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1229 tmpArg.src[1] = arg->src[1]+i;
1230 shader_glsl_dot(&tmpArg);
1235 The LRP instruction performs a component-wise linear interpolation
1236 between the second and third operands using the first operand as the
1237 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1238 This is equivalent to mix(src2, src1, src0);
1240 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1241 glsl_src_param_t src0_param;
1242 glsl_src_param_t src1_param;
1243 glsl_src_param_t src2_param;
1246 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1248 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1249 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1250 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1252 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1253 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1256 /** Process the WINED3DSIO_LIT instruction in GLSL:
1257 * dst.x = dst.w = 1.0
1258 * dst.y = (src0.x > 0) ? src0.x
1259 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1260 * where src.w is clamped at +- 128
1262 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1263 glsl_src_param_t src0_param;
1264 glsl_src_param_t src1_param;
1265 glsl_src_param_t src3_param;
1268 shader_glsl_append_dst(arg->buffer, arg);
1269 shader_glsl_get_write_mask(arg->dst, dst_mask);
1271 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1272 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1273 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1275 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",
1276 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);
1279 /** Process the WINED3DSIO_DST instruction in GLSL:
1281 * dst.y = src0.x * src0.y
1285 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1286 glsl_src_param_t src0y_param;
1287 glsl_src_param_t src0z_param;
1288 glsl_src_param_t src1y_param;
1289 glsl_src_param_t src1w_param;
1292 shader_glsl_append_dst(arg->buffer, arg);
1293 shader_glsl_get_write_mask(arg->dst, dst_mask);
1295 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1296 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1297 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1298 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1300 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1301 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1304 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1305 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1306 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1308 * dst.x = cos(src0.?)
1309 * dst.y = sin(src0.?)
1313 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1314 glsl_src_param_t src0_param;
1317 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1318 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1320 switch (write_mask) {
1321 case WINED3DSP_WRITEMASK_0:
1322 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1325 case WINED3DSP_WRITEMASK_1:
1326 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1329 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1330 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1334 ERR("Write mask should be .x, .y or .xy\n");
1339 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1340 * Start a for() loop where src1.y is the initial value of aL,
1341 * increment aL by src1.z for a total of src1.x iterations.
1342 * Need to use a temporary variable for this operation.
1344 /* FIXME: I don't think nested loops will work correctly this way. */
1345 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1346 glsl_src_param_t src1_param;
1348 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1350 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1351 src1_param.reg_name, src1_param.reg_name, src1_param.reg_name);
1354 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1355 shader_addline(arg->buffer, "}\n");
1358 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1359 glsl_src_param_t src0_param;
1361 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1362 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s; tmpInt++) {\n", src0_param.param_str);
1365 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1366 glsl_src_param_t src0_param;
1368 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1369 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
1372 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1373 glsl_src_param_t src0_param;
1374 glsl_src_param_t src1_param;
1376 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1377 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1379 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1380 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1383 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1384 shader_addline(arg->buffer, "} else {\n");
1387 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1388 shader_addline(arg->buffer, "break;\n");
1391 /* FIXME: According to MSDN the compare is done per component. */
1392 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1393 glsl_src_param_t src0_param;
1394 glsl_src_param_t src1_param;
1396 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1397 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1399 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1400 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1403 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1405 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1406 shader_addline(arg->buffer, "}\n");
1407 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1410 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1411 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1412 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1415 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1416 glsl_src_param_t src1_param;
1418 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1419 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1420 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_param.param_str, snum);
1423 /*********************************************
1424 * Pixel Shader Specific Code begins here
1425 ********************************************/
1426 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1427 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1428 DWORD hex_version = This->baseShader.hex_version;
1429 char dst_swizzle[6];
1430 glsl_sample_function_t sample_function;
1436 /* All versions have a destination register */
1437 shader_glsl_append_dst(arg->buffer, arg);
1439 /* 1.0-1.4: Use destination register as sampler source.
1440 * 2.0+: Use provided sampler source. */
1441 if (hex_version < WINED3DPS_VERSION(1,4)) {
1442 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1445 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1446 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1448 if (flags & WINED3DTTFF_PROJECTED) {
1450 switch (flags & ~WINED3DTTFF_PROJECTED) {
1451 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1452 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
1453 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
1454 case WINED3DTTFF_COUNT4:
1455 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
1460 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
1461 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1462 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1464 if (src_mod == WINED3DSPSM_DZ) {
1466 mask = WINED3DSP_WRITEMASK_2;
1467 } else if (src_mod == WINED3DSPSM_DW) {
1469 mask = WINED3DSP_WRITEMASK_3;
1474 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1475 /* TODO: Handle D3DSI_TEXLD_PROJECTED... */
1479 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1480 shader_glsl_get_sample_function(sampler_type, projected, &sample_function);
1481 mask |= sample_function.coord_mask;
1483 if (hex_version < WINED3DPS_VERSION(2,0)) {
1484 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
1486 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
1489 /* 1.0-1.3: Use destination register as coordinate source.
1490 1.4+: Use provided coordinate source register. */
1491 if (hex_version < WINED3DPS_VERSION(1,4)) {
1493 shader_glsl_get_write_mask(mask, coord_mask);
1494 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
1495 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
1497 glsl_src_param_t coord_param;
1498 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
1499 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
1500 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
1504 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1506 /* FIXME: Make this work for more than just 2D textures */
1508 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1509 SHADER_BUFFER* buffer = arg->buffer;
1510 DWORD hex_version = This->baseShader.hex_version;
1514 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1515 shader_glsl_get_write_mask(write_mask, dst_mask);
1517 if (hex_version != WINED3DPS_VERSION(1,4)) {
1518 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1519 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
1521 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1522 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1523 char dst_swizzle[6];
1525 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
1527 if (src_mod == WINED3DSPSM_DZ) {
1528 glsl_src_param_t div_param;
1529 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
1530 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
1532 if (mask_size > 1) {
1533 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
1535 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
1537 } else if (src_mod == WINED3DSPSM_DW) {
1538 glsl_src_param_t div_param;
1539 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
1540 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
1542 if (mask_size > 1) {
1543 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
1545 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
1548 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
1553 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1554 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1555 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1556 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1557 glsl_src_param_t src0_param;
1559 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1560 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1562 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1564 shader_glsl_append_dst(arg->buffer, arg);
1565 shader_glsl_get_write_mask(arg->dst, dst_mask);
1566 shader_addline(arg->buffer, "texture1D(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
1567 sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
1570 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1571 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1572 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1573 glsl_src_param_t src0_param;
1574 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1575 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1579 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
1580 mask_size = shader_glsl_get_write_mask_size(dst_mask);
1581 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1583 if (mask_size > 1) {
1584 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
1586 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
1590 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1591 * Calculate the depth as dst.x / dst.y */
1592 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1593 glsl_dst_param_t dst_param;
1595 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1597 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_param.reg_name, dst_param.reg_name);
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) {
1606 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1607 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1608 glsl_src_param_t src0_param;
1610 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1612 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
1613 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
1616 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1617 * Calculate the 1st of a 2-row matrix multiplication. */
1618 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1619 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1620 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1621 SHADER_BUFFER* buffer = arg->buffer;
1622 glsl_src_param_t src0_param;
1624 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1625 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1628 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1629 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1630 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1632 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1633 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1634 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1635 SHADER_BUFFER* buffer = arg->buffer;
1636 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1637 glsl_src_param_t src0_param;
1639 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1640 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
1641 current_state->texcoord_w[current_state->current_row++] = reg;
1644 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1645 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1646 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1647 SHADER_BUFFER* buffer = arg->buffer;
1648 glsl_src_param_t src0_param;
1651 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1652 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1654 shader_glsl_append_dst(buffer, arg);
1655 shader_glsl_get_write_mask(arg->dst, dst_mask);
1657 /* Sample the texture using the calculated coordinates */
1658 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
1661 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1662 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1663 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1664 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1665 glsl_src_param_t src0_param;
1667 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1668 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1669 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1670 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1671 glsl_sample_function_t sample_function;
1673 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1674 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1676 shader_glsl_append_dst(arg->buffer, arg);
1677 shader_glsl_get_write_mask(arg->dst, dst_mask);
1678 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1680 /* Sample the texture using the calculated coordinates */
1681 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
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) {
1689 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1690 glsl_src_param_t src0_param;
1692 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1693 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1694 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1696 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1698 shader_glsl_append_dst(arg->buffer, arg);
1699 shader_glsl_get_write_mask(arg->dst, dst_mask);
1700 shader_addline(arg->buffer, "vec4(tmp.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
1702 current_state->current_row = 0;
1705 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1706 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1707 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1709 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1710 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1711 glsl_src_param_t src0_param;
1712 glsl_src_param_t src1_param;
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;
1717 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1718 glsl_sample_function_t sample_function;
1720 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1721 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1723 /* Perform the last matrix multiply operation */
1724 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1726 /* Calculate reflection vector, 2*(tmp0.src1)*tmp0-src1
1727 * This is equivalent to reflect(-src1, tmp0); */
1728 shader_addline(buffer, "tmp0.xyz = reflect(-(%s), tmp0.xyz);\n", src1_param.param_str);
1730 shader_glsl_append_dst(buffer, arg);
1731 shader_glsl_get_write_mask(arg->dst, dst_mask);
1732 shader_glsl_get_sample_function(stype, FALSE, &sample_function);
1734 /* Sample the texture */
1735 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
1737 current_state->current_row = 0;
1740 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1741 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1742 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1744 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1745 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1746 SHADER_BUFFER* buffer = arg->buffer;
1747 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1748 glsl_src_param_t src0_param;
1750 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1751 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1752 glsl_sample_function_t sample_function;
1754 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1756 /* Perform the last matrix multiply operation */
1757 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
1759 /* Construct the eye-ray vector from w coordinates */
1760 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
1761 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
1763 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(tmp0.tmp1)*tmp0-tmp1
1764 * This is equivalent to reflect(-tmp1, tmp0); */
1765 shader_addline(buffer, "tmp0.xyz = reflect(-tmp1.xyz, tmp0.xyz);\n");
1767 shader_glsl_append_dst(buffer, arg);
1768 shader_glsl_get_write_mask(arg->dst, dst_mask);
1769 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1771 /* Sample the texture using the calculated coordinates */
1772 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
1774 current_state->current_row = 0;
1777 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1778 * Apply a fake bump map transform.
1779 * texbem is pshader <= 1.3 only, this saves a few version checks
1781 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1782 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1783 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1784 char dst_swizzle[6];
1785 glsl_sample_function_t sample_function;
1793 /* All versions have a destination register */
1794 shader_glsl_append_dst(arg->buffer, arg);
1796 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1797 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1799 /* TODO: Does texbem even support projected textures? half-life 2 uses it */
1800 if (flags & WINED3DTTFF_PROJECTED) {
1802 switch (flags & ~WINED3DTTFF_PROJECTED) {
1803 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1804 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
1805 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
1806 case WINED3DTTFF_COUNT4:
1807 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
1813 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1814 shader_glsl_get_sample_function(sampler_type, projected, &sample_function);
1815 mask |= sample_function.coord_mask;
1817 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
1819 shader_glsl_get_write_mask(mask, coord_mask);
1820 FIXME("Bump map transform not handled yet\n");
1821 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
1822 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
1825 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1826 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1827 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1828 glsl_src_param_t src0_param;
1829 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1832 shader_glsl_append_dst(arg->buffer, arg);
1833 shader_glsl_get_write_mask(arg->dst, dst_mask);
1834 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1836 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
1839 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1840 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1841 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1842 glsl_src_param_t src0_param;
1843 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1846 shader_glsl_append_dst(arg->buffer, arg);
1847 shader_glsl_get_write_mask(arg->dst, dst_mask);
1848 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1850 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
1853 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
1854 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
1855 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1856 glsl_src_param_t src0_param;
1858 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1859 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1860 glsl_sample_function_t sample_function;
1862 shader_glsl_append_dst(arg->buffer, arg);
1863 shader_glsl_get_write_mask(arg->dst, dst_mask);
1864 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1865 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
1867 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
1870 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
1871 * If any of the first 3 components are < 0, discard this pixel */
1872 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
1873 glsl_dst_param_t dst_param;
1875 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1876 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
1879 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
1880 * dst = dot2(src0, src1) + src2 */
1881 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
1882 glsl_src_param_t src0_param;
1883 glsl_src_param_t src1_param;
1884 glsl_src_param_t src2_param;
1888 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1889 mask_size = shader_glsl_get_write_mask_size(write_mask);
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);
1893 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
1895 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
1898 void pshader_glsl_input_pack(
1899 SHADER_BUFFER* buffer,
1900 semantic* semantics_in) {
1904 for (i = 0; i < MAX_REG_INPUT; i++) {
1906 DWORD usage_token = semantics_in[i].usage;
1907 DWORD register_token = semantics_in[i].reg;
1908 DWORD usage, usage_idx;
1912 if (!usage_token) continue;
1913 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1914 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1915 shader_glsl_get_write_mask(register_token, reg_mask);
1919 case WINED3DDECLUSAGE_COLOR:
1921 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
1922 i, reg_mask, reg_mask);
1923 else if (usage_idx == 1)
1924 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
1925 i, reg_mask, reg_mask);
1927 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
1928 i, reg_mask, reg_mask);
1931 case WINED3DDECLUSAGE_TEXCOORD:
1932 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
1933 i, reg_mask, usage_idx, reg_mask );
1936 case WINED3DDECLUSAGE_FOG:
1937 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
1938 i, reg_mask, reg_mask);
1942 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
1943 i, reg_mask, reg_mask);
1948 /*********************************************
1949 * Vertex Shader Specific Code begins here
1950 ********************************************/
1952 void vshader_glsl_output_unpack(
1953 SHADER_BUFFER* buffer,
1954 semantic* semantics_out) {
1958 for (i = 0; i < MAX_REG_OUTPUT; i++) {
1960 DWORD usage_token = semantics_out[i].usage;
1961 DWORD register_token = semantics_out[i].reg;
1962 DWORD usage, usage_idx;
1966 if (!usage_token) continue;
1968 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1969 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1970 shader_glsl_get_write_mask(register_token, reg_mask);
1974 case WINED3DDECLUSAGE_COLOR:
1976 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1977 else if (usage_idx == 1)
1978 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1980 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1983 case WINED3DDECLUSAGE_POSITION:
1984 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1987 case WINED3DDECLUSAGE_TEXCOORD:
1988 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
1989 usage_idx, reg_mask, i, reg_mask);
1992 case WINED3DDECLUSAGE_PSIZE:
1993 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
1996 case WINED3DDECLUSAGE_FOG:
1997 shader_addline(buffer, "gl_FogFragCoord%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2001 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2006 static void add_glsl_program_entry(IWineD3DDeviceImpl *device, struct glsl_shader_prog_link *entry) {
2007 glsl_program_key_t *key;
2009 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2010 key->vshader = entry->vshader;
2011 key->pshader = entry->pshader;
2013 hash_table_put(device->glsl_program_lookup, key, entry);
2016 static struct glsl_shader_prog_link *get_glsl_program_entry(IWineD3DDeviceImpl *device,
2017 GLhandleARB vshader, GLhandleARB pshader) {
2018 glsl_program_key_t key;
2020 key.vshader = vshader;
2021 key.pshader = pshader;
2023 return (struct glsl_shader_prog_link *)hash_table_get(device->glsl_program_lookup, &key);
2026 void delete_glsl_program_entry(IWineD3DDevice *iface, struct glsl_shader_prog_link *entry) {
2027 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2028 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2029 glsl_program_key_t *key;
2031 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2032 key->vshader = entry->vshader;
2033 key->pshader = entry->pshader;
2034 hash_table_remove(This->glsl_program_lookup, key);
2036 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2037 if (entry->vshader) list_remove(&entry->vshader_entry);
2038 if (entry->pshader) list_remove(&entry->pshader_entry);
2039 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2040 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2041 HeapFree(GetProcessHeap(), 0, entry);
2044 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
2045 * It sets the programId on the current StateBlock (because it should be called
2046 * inside of the DrawPrimitive() part of the render loop).
2048 * If a program for the given combination does not exist, create one, and store
2049 * the program in the hash table. If it creates a program, it will link the
2050 * given objects, too.
2052 static void set_glsl_shader_program(IWineD3DDevice *iface) {
2053 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2054 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2055 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
2056 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
2057 struct glsl_shader_prog_link *entry = NULL;
2058 GLhandleARB programId = 0;
2062 GLhandleARB vshader_id = (vshader && This->vs_selected_mode == SHADER_GLSL) ? ((IWineD3DBaseShaderImpl*)vshader)->baseShader.prgId : 0;
2063 GLhandleARB pshader_id = (pshader && This->ps_selected_mode == SHADER_GLSL) ? ((IWineD3DBaseShaderImpl*)pshader)->baseShader.prgId : 0;
2064 entry = get_glsl_program_entry(This, vshader_id, pshader_id);
2066 This->stateBlock->glsl_program = entry;
2070 /* If we get to this point, then no matching program exists, so we create one */
2071 programId = GL_EXTCALL(glCreateProgramObjectARB());
2072 TRACE("Created new GLSL shader program %u\n", programId);
2074 /* Create the entry */
2075 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
2076 entry->programId = programId;
2077 entry->vshader = vshader_id;
2078 entry->pshader = pshader_id;
2079 /* Add the hash table entry */
2080 add_glsl_program_entry(This, entry);
2082 /* Set the current program */
2083 This->stateBlock->glsl_program = entry;
2085 /* Attach GLSL vshader */
2087 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
2090 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
2091 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
2092 checkGLcall("glAttachObjectARB");
2094 /* Bind vertex attributes to a corresponding index number to match
2095 * the same index numbers as ARB_vertex_programs (makes loading
2096 * vertex attributes simpler). With this method, we can use the
2097 * exact same code to load the attributes later for both ARB and
2100 * We have to do this here because we need to know the Program ID
2101 * in order to make the bindings work, and it has to be done prior
2102 * to linking the GLSL program. */
2103 for (i = 0; i < max_attribs; ++i) {
2104 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
2105 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
2107 checkGLcall("glBindAttribLocationARB");
2109 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
2112 /* Attach GLSL pshader */
2114 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
2115 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
2116 checkGLcall("glAttachObjectARB");
2118 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
2121 /* Link the program */
2122 TRACE("Linking GLSL shader program %u\n", programId);
2123 GL_EXTCALL(glLinkProgramARB(programId));
2124 print_glsl_info_log(&GLINFO_LOCATION, programId);
2126 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
2127 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
2128 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
2129 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2131 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
2132 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
2133 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
2134 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2138 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
2139 GLhandleARB program_id;
2140 GLhandleARB vshader_id, pshader_id;
2141 const char *blt_vshader[] = {
2144 " gl_Position = gl_Vertex;\n"
2145 " gl_FrontColor = vec4(1.0);\n"
2146 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2147 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2151 const char *blt_pshader[] = {
2152 "uniform sampler2D sampler;\n"
2155 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2159 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2160 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2161 GL_EXTCALL(glCompileShaderARB(vshader_id));
2163 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2164 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2165 GL_EXTCALL(glCompileShaderARB(pshader_id));
2167 program_id = GL_EXTCALL(glCreateProgramObjectARB());
2168 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2169 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2170 GL_EXTCALL(glLinkProgramARB(program_id));
2172 print_glsl_info_log(&GLINFO_LOCATION, program_id);
2177 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2178 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2179 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2180 GLhandleARB program_id = 0;
2182 if (useVS || usePS) set_glsl_shader_program(iface);
2183 else This->stateBlock->glsl_program = NULL;
2185 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2186 if (program_id) TRACE("Using GLSL program %u\n", program_id);
2187 GL_EXTCALL(glUseProgramObjectARB(program_id));
2188 checkGLcall("glUseProgramObjectARB");
2191 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2192 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2193 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2194 static GLhandleARB program_id = 0;
2195 static GLhandleARB loc = -1;
2198 program_id = create_glsl_blt_shader(gl_info);
2199 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2202 GL_EXTCALL(glUseProgramObjectARB(program_id));
2203 GL_EXTCALL(glUniform1iARB(loc, 0));
2206 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
2207 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2208 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2209 GL_EXTCALL(glUseProgramObjectARB(0));
2212 const shader_backend_t glsl_shader_backend = {
2213 &shader_glsl_select,
2214 &shader_glsl_select_depth_blt,
2215 &shader_glsl_load_constants,
2216 &shader_glsl_cleanup