push 915ca0aa9d2991375796d4b573b1be50b5f1fb2c
[wine/hacks.git] / dlls / wined3d / glsl_shader.c
blob4c1d2239a610512d6a5ff61f4df2368300f8bd13
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * D3D shader asm has swizzles on source parameters, and write masks for
25 * destination parameters. GLSL uses swizzles for both. The result of this is
26 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
27 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
28 * mask for the destination parameter into account.
31 #include "config.h"
32 #include <stdio.h>
33 #include "wined3d_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
37 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d);
40 #define GLINFO_LOCATION (*gl_info)
42 typedef struct {
43 char reg_name[150];
44 char mask_str[6];
45 } glsl_dst_param_t;
47 typedef struct {
48 char reg_name[150];
49 char param_str[100];
50 } glsl_src_param_t;
52 typedef struct {
53 const char *name;
54 DWORD coord_mask;
55 } glsl_sample_function_t;
57 /* GLSL shader private data */
58 struct shader_glsl_priv {
59 struct hash_table_t *glsl_program_lookup;
60 const struct glsl_shader_prog_link *glsl_program;
61 GLhandleARB depth_blt_program[tex_type_count];
64 /* Struct to maintain data about a linked GLSL program */
65 struct glsl_shader_prog_link {
66 struct list vshader_entry;
67 struct list pshader_entry;
68 GLhandleARB programId;
69 GLhandleARB *vuniformF_locations;
70 GLhandleARB *puniformF_locations;
71 GLhandleARB vuniformI_locations[MAX_CONST_I];
72 GLhandleARB puniformI_locations[MAX_CONST_I];
73 GLhandleARB posFixup_location;
74 GLhandleARB bumpenvmat_location[MAX_TEXTURES];
75 GLhandleARB luminancescale_location[MAX_TEXTURES];
76 GLhandleARB luminanceoffset_location[MAX_TEXTURES];
77 GLhandleARB ycorrection_location;
78 GLenum vertex_color_clamp;
79 GLhandleARB vshader;
80 IWineD3DPixelShader *pshader;
81 struct ps_compile_args ps_args;
84 typedef struct {
85 GLhandleARB vshader;
86 IWineD3DPixelShader *pshader;
87 struct ps_compile_args ps_args;
88 } glsl_program_key_t;
91 /** Prints the GLSL info log which will contain error messages if they exist */
92 static void print_glsl_info_log(const WineD3D_GL_Info *gl_info, GLhandleARB obj)
94 int infologLength = 0;
95 char *infoLog;
96 unsigned int i;
97 BOOL is_spam;
99 const char *spam[] = {
100 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
101 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
102 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
103 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
104 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
105 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
106 "Fragment shader was successfully compiled to run on hardware.\nWARNING: 0:1: extension 'GL_ARB_draw_buffers' is not supported",
107 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
108 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
109 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
112 GL_EXTCALL(glGetObjectParameterivARB(obj,
113 GL_OBJECT_INFO_LOG_LENGTH_ARB,
114 &infologLength));
116 /* A size of 1 is just a null-terminated string, so the log should be bigger than
117 * that if there are errors. */
118 if (infologLength > 1)
120 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
121 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
123 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
124 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
125 is_spam = FALSE;
127 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
128 if(strcmp(infoLog, spam[i]) == 0) {
129 is_spam = TRUE;
130 break;
133 if(is_spam) {
134 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
135 } else {
136 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
138 HeapFree(GetProcessHeap(), 0, infoLog);
143 * Loads (pixel shader) samplers
145 static void shader_glsl_load_psamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
147 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
148 GLhandleARB name_loc;
149 int i;
150 char sampler_name[20];
152 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
153 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
154 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
155 if (name_loc != -1) {
156 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
157 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
158 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
159 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
160 checkGLcall("glUniform1iARB");
161 } else {
162 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
168 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
170 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
171 GLhandleARB name_loc;
172 char sampler_name[20];
173 int i;
175 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
176 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
177 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
178 if (name_loc != -1) {
179 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
180 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
181 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
182 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
183 checkGLcall("glUniform1iARB");
184 } else {
185 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
191 /**
192 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
193 * When constant_list == NULL, it will load all the constants.
195 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
196 unsigned int max_constants, const float *constants, const GLhandleARB *constant_locations,
197 const struct list *constant_list)
199 const constants_entry *constant;
200 const local_constant *lconst;
201 GLhandleARB tmp_loc;
202 DWORD i, j, k;
203 const DWORD *idx;
205 if (TRACE_ON(d3d_shader)) {
206 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
207 idx = constant->idx;
208 j = constant->count;
209 while (j--) {
210 i = *idx++;
211 tmp_loc = constant_locations[i];
212 if (tmp_loc != -1) {
213 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
214 constants[i * 4 + 0], constants[i * 4 + 1],
215 constants[i * 4 + 2], constants[i * 4 + 3]);
221 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
222 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
223 shader_is_pshader_version(This->baseShader.hex_version)) {
224 float lcl_const[4];
226 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
227 idx = constant->idx;
228 j = constant->count;
229 while (j--) {
230 i = *idx++;
231 tmp_loc = constant_locations[i];
232 if (tmp_loc != -1) {
233 /* We found this uniform name in the program - go ahead and send the data */
234 k = i * 4;
235 if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
236 else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
237 else lcl_const[0] = constants[k + 0];
238 if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
239 else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
240 else lcl_const[1] = constants[k + 1];
241 if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
242 else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
243 else lcl_const[2] = constants[k + 2];
244 if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
245 else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
246 else lcl_const[3] = constants[k + 3];
248 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
252 } else {
253 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
254 idx = constant->idx;
255 j = constant->count;
256 while (j--) {
257 i = *idx++;
258 tmp_loc = constant_locations[i];
259 if (tmp_loc != -1) {
260 /* We found this uniform name in the program - go ahead and send the data */
261 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
266 checkGLcall("glUniform4fvARB()");
268 if(!This->baseShader.load_local_constsF) {
269 TRACE("No need to load local float constants for this shader\n");
270 return;
273 /* Load immediate constants */
274 if (TRACE_ON(d3d_shader)) {
275 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
276 tmp_loc = constant_locations[lconst->idx];
277 if (tmp_loc != -1) {
278 const GLfloat *values = (const GLfloat *)lconst->value;
279 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
280 values[0], values[1], values[2], values[3]);
284 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
285 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
286 tmp_loc = constant_locations[lconst->idx];
287 if (tmp_loc != -1) {
288 /* We found this uniform name in the program - go ahead and send the data */
289 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (const GLfloat *)lconst->value));
292 checkGLcall("glUniform4fvARB()");
295 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
296 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
297 const GLhandleARB locations[MAX_CONST_I], const int *constants, WORD constants_set)
299 unsigned int i;
300 struct list* ptr;
302 for (i = 0; constants_set; constants_set >>= 1, ++i)
304 if (!(constants_set & 1)) continue;
306 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
307 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
309 /* We found this uniform name in the program - go ahead and send the data */
310 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
311 checkGLcall("glUniform4ivARB");
314 /* Load immediate constants */
315 ptr = list_head(&This->baseShader.constantsI);
316 while (ptr) {
317 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
318 unsigned int idx = lconst->idx;
319 const GLint *values = (const GLint *)lconst->value;
321 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
322 values[0], values[1], values[2], values[3]);
324 /* We found this uniform name in the program - go ahead and send the data */
325 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
326 checkGLcall("glUniform4ivARB");
327 ptr = list_next(&This->baseShader.constantsI, ptr);
331 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
332 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
333 GLhandleARB programId, const BOOL *constants, WORD constants_set)
335 GLhandleARB tmp_loc;
336 unsigned int i;
337 char tmp_name[8];
338 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
339 const char* prefix = is_pshader? "PB":"VB";
340 struct list* ptr;
342 /* TODO: Benchmark and see if it would be beneficial to store the
343 * locations of the constants to avoid looking up each time */
344 for (i = 0; constants_set; constants_set >>= 1, ++i)
346 if (!(constants_set & 1)) continue;
348 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
350 /* TODO: Benchmark and see if it would be beneficial to store the
351 * locations of the constants to avoid looking up each time */
352 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
353 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
354 if (tmp_loc != -1)
356 /* We found this uniform name in the program - go ahead and send the data */
357 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
358 checkGLcall("glUniform1ivARB");
362 /* Load immediate constants */
363 ptr = list_head(&This->baseShader.constantsB);
364 while (ptr) {
365 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
366 unsigned int idx = lconst->idx;
367 const GLint *values = (const GLint *)lconst->value;
369 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
371 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
372 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
373 if (tmp_loc != -1) {
374 /* We found this uniform name in the program - go ahead and send the data */
375 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
376 checkGLcall("glUniform1ivARB");
378 ptr = list_next(&This->baseShader.constantsB, ptr);
385 * Loads the app-supplied constants into the currently set GLSL program.
387 static void shader_glsl_load_constants(
388 IWineD3DDevice* device,
389 char usePixelShader,
390 char useVertexShader) {
392 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
393 const struct shader_glsl_priv *priv = (struct shader_glsl_priv *)deviceImpl->shader_priv;
394 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
395 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
397 const GLhandleARB *constant_locations;
398 const struct list *constant_list;
399 GLhandleARB programId;
400 const struct glsl_shader_prog_link *prog = priv->glsl_program;
401 int i;
403 if (!prog) {
404 /* No GLSL program set - nothing to do. */
405 return;
407 programId = prog->programId;
409 if (useVertexShader) {
410 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
412 constant_locations = prog->vuniformF_locations;
413 constant_list = &stateBlock->set_vconstantsF;
415 /* Load DirectX 9 float constants/uniforms for vertex shader */
416 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
417 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
419 /* Load DirectX 9 integer constants/uniforms for vertex shader */
420 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations,
421 stateBlock->vertexShaderConstantI, stateBlock->changed.vertexShaderConstantsI);
423 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
424 shader_glsl_load_constantsB(vshader, gl_info, programId,
425 stateBlock->vertexShaderConstantB, stateBlock->changed.vertexShaderConstantsB);
427 /* Upload the position fixup params */
428 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
429 checkGLcall("glUniform4fvARB");
432 if (usePixelShader) {
434 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
436 constant_locations = prog->puniformF_locations;
437 constant_list = &stateBlock->set_pconstantsF;
439 /* Load DirectX 9 float constants/uniforms for pixel shader */
440 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
441 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
443 /* Load DirectX 9 integer constants/uniforms for pixel shader */
444 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations,
445 stateBlock->pixelShaderConstantI, stateBlock->changed.pixelShaderConstantsI);
447 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
448 shader_glsl_load_constantsB(pshader, gl_info, programId,
449 stateBlock->pixelShaderConstantB, stateBlock->changed.pixelShaderConstantsB);
451 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
452 * It can't be 0 for a valid texbem instruction.
454 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
455 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
456 int stage = ps->luminanceconst[i].texunit;
458 const float *data = (const float *)&stateBlock->textureState[(int)ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
459 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
460 checkGLcall("glUniformMatrix2fvARB");
462 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
463 * is set too, so we can check that in the needsbumpmat check
465 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
466 const GLfloat *scale = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
467 const GLfloat *offset = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
469 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
470 checkGLcall("glUniform1fvARB");
471 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
472 checkGLcall("glUniform1fvARB");
476 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
477 float correction_params[4];
478 if(deviceImpl->render_offscreen) {
479 correction_params[0] = 0.0;
480 correction_params[1] = 1.0;
481 } else {
482 /* position is window relative, not viewport relative */
483 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
484 correction_params[1] = -1.0;
486 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
491 /** Generate the variable & register declarations for the GLSL output target */
492 static void shader_generate_glsl_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
493 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info)
495 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
496 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
497 unsigned int i, extra_constants_needed = 0;
498 const local_constant *lconst;
500 /* There are some minor differences between pixel and vertex shaders */
501 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
502 char prefix = pshader ? 'P' : 'V';
504 /* Prototype the subroutines */
505 for (i = 0; i < This->baseShader.limits.label; i++) {
506 if (reg_maps->labels[i])
507 shader_addline(buffer, "void subroutine%u();\n", i);
510 /* Declare the constants (aka uniforms) */
511 if (This->baseShader.limits.constant_float > 0) {
512 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
513 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
514 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
517 if (This->baseShader.limits.constant_int > 0)
518 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
520 if (This->baseShader.limits.constant_bool > 0)
521 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
523 if(!pshader) {
524 shader_addline(buffer, "uniform vec4 posFixup;\n");
525 /* Predeclaration; This function is added at link time based on the pixel shader.
526 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
527 * that. We know the input to the reorder function at vertex shader compile time, so
528 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
529 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
530 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
531 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
532 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
533 * inout.
535 if(This->baseShader.hex_version >= WINED3DVS_VERSION(3, 0)) {
536 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
537 } else {
538 shader_addline(buffer, "void order_ps_input();\n");
540 } else {
541 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
543 ps_impl->numbumpenvmatconsts = 0;
544 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
545 if(!reg_maps->bumpmat[i]) {
546 continue;
549 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
550 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
552 if(reg_maps->luminanceparams) {
553 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
554 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
555 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
556 extra_constants_needed++;
557 } else {
558 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
561 extra_constants_needed++;
562 ps_impl->numbumpenvmatconsts++;
565 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
566 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
567 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
568 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
569 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
571 if(reg_maps->vpos || reg_maps->usesdsy) {
572 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
573 shader_addline(buffer, "uniform vec4 ycorrection;\n");
574 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
575 extra_constants_needed++;
576 } else {
577 /* This happens because we do not have proper tracking of the constant registers that are
578 * actually used, only the max limit of the shader version
580 FIXME("Cannot find a free uniform for vpos correction params\n");
581 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
582 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
583 device->render_offscreen ? 1.0 : -1.0);
585 shader_addline(buffer, "vec4 vpos;\n");
589 /* Declare texture samplers */
590 for (i = 0; i < This->baseShader.limits.sampler; i++) {
591 if (reg_maps->samplers[i]) {
593 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
594 switch (stype) {
596 case WINED3DSTT_1D:
597 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
598 break;
599 case WINED3DSTT_2D:
600 if(device->stateBlock->textures[i] &&
601 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
602 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
603 } else {
604 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
606 break;
607 case WINED3DSTT_CUBE:
608 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
609 break;
610 case WINED3DSTT_VOLUME:
611 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
612 break;
613 default:
614 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
615 FIXME("Unrecognized sampler type: %#x\n", stype);
616 break;
621 /* Declare address variables */
622 for (i = 0; i < This->baseShader.limits.address; i++) {
623 if (reg_maps->address[i])
624 shader_addline(buffer, "ivec4 A%d;\n", i);
627 /* Declare texture coordinate temporaries and initialize them */
628 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
629 if (reg_maps->texcoord[i])
630 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
633 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
634 * helper function shader that is linked in at link time
636 if(pshader && This->baseShader.hex_version >= WINED3DPS_VERSION(3, 0)) {
637 if(use_vs(device)) {
638 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
639 } else {
640 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
641 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
642 * pixel shader that reads the fixed function color into the packed input registers.
644 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
648 /* Declare output register temporaries */
649 if(This->baseShader.limits.packed_output) {
650 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
653 /* Declare temporary variables */
654 for(i = 0; i < This->baseShader.limits.temporary; i++) {
655 if (reg_maps->temporary[i])
656 shader_addline(buffer, "vec4 R%u;\n", i);
659 /* Declare attributes */
660 for (i = 0; i < This->baseShader.limits.attributes; i++) {
661 if (reg_maps->attributes[i])
662 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
665 /* Declare loop registers aLx */
666 for (i = 0; i < reg_maps->loop_depth; i++) {
667 shader_addline(buffer, "int aL%u;\n", i);
668 shader_addline(buffer, "int tmpInt%u;\n", i);
671 /* Temporary variables for matrix operations */
672 shader_addline(buffer, "vec4 tmp0;\n");
673 shader_addline(buffer, "vec4 tmp1;\n");
675 /* Local constants use a different name so they can be loaded once at shader link time
676 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
677 * float -> string conversion can cause precision loss.
679 if(!This->baseShader.load_local_constsF) {
680 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
681 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
685 /* Start the main program */
686 shader_addline(buffer, "void main() {\n");
687 if(pshader && reg_maps->vpos) {
688 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
689 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
690 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
691 * precision troubles when we just substract 0.5.
693 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
695 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
697 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
698 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
699 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
700 * correctly on drivers that returns integer values.
702 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
706 /*****************************************************************************
707 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
709 * For more information, see http://wiki.winehq.org/DirectX-Shaders
710 ****************************************************************************/
712 /* Prototypes */
713 static void shader_glsl_add_src_param(const SHADER_OPCODE_ARG *arg, const DWORD param,
714 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
716 /** Used for opcode modifiers - They multiply the result by the specified amount */
717 static const char * const shift_glsl_tab[] = {
718 "", /* 0 (none) */
719 "2.0 * ", /* 1 (x2) */
720 "4.0 * ", /* 2 (x4) */
721 "8.0 * ", /* 3 (x8) */
722 "16.0 * ", /* 4 (x16) */
723 "32.0 * ", /* 5 (x32) */
724 "", /* 6 (x64) */
725 "", /* 7 (x128) */
726 "", /* 8 (d256) */
727 "", /* 9 (d128) */
728 "", /* 10 (d64) */
729 "", /* 11 (d32) */
730 "0.0625 * ", /* 12 (d16) */
731 "0.125 * ", /* 13 (d8) */
732 "0.25 * ", /* 14 (d4) */
733 "0.5 * " /* 15 (d2) */
736 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
737 static void shader_glsl_gen_modifier (
738 const DWORD instr,
739 const char *in_reg,
740 const char *in_regswizzle,
741 char *out_str) {
743 out_str[0] = 0;
745 if (instr == WINED3DSIO_TEXKILL)
746 return;
748 switch (instr & WINED3DSP_SRCMOD_MASK) {
749 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
750 case WINED3DSPSM_DW:
751 case WINED3DSPSM_NONE:
752 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
753 break;
754 case WINED3DSPSM_NEG:
755 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
756 break;
757 case WINED3DSPSM_NOT:
758 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
759 break;
760 case WINED3DSPSM_BIAS:
761 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
762 break;
763 case WINED3DSPSM_BIASNEG:
764 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
765 break;
766 case WINED3DSPSM_SIGN:
767 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
768 break;
769 case WINED3DSPSM_SIGNNEG:
770 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
771 break;
772 case WINED3DSPSM_COMP:
773 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
774 break;
775 case WINED3DSPSM_X2:
776 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
777 break;
778 case WINED3DSPSM_X2NEG:
779 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
780 break;
781 case WINED3DSPSM_ABS:
782 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
783 break;
784 case WINED3DSPSM_ABSNEG:
785 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
786 break;
787 default:
788 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
789 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
793 /** Writes the GLSL variable name that corresponds to the register that the
794 * DX opcode parameter is trying to access */
795 static void shader_glsl_get_register_name(const DWORD param, const DWORD addr_token,
796 char *regstr, BOOL *is_color, const SHADER_OPCODE_ARG *arg)
798 /* oPos, oFog and oPts in D3D */
799 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
801 DWORD reg = param & WINED3DSP_REGNUM_MASK;
802 DWORD regtype = shader_get_regtype(param);
803 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
804 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
805 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
807 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
808 char tmpStr[150];
810 *is_color = FALSE;
812 switch (regtype) {
813 case WINED3DSPR_TEMP:
814 sprintf(tmpStr, "R%u", reg);
815 break;
816 case WINED3DSPR_INPUT:
817 if (pshader) {
818 /* Pixel shaders >= 3.0 */
819 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
820 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
822 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
823 glsl_src_param_t rel_param;
824 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
826 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
827 * operation there
829 if(((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg]) {
830 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
831 sprintf(tmpStr, "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
832 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count - 1,
833 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count,
834 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
835 } else {
836 sprintf(tmpStr, "IN[%s + %u]", rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
838 } else {
839 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
840 sprintf(tmpStr, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
841 rel_param.param_str, in_count - 1,
842 rel_param.param_str, in_count,
843 rel_param.param_str);
844 } else {
845 sprintf(tmpStr, "IN[%s]", rel_param.param_str);
848 } else {
849 DWORD idx = ((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg];
850 if (idx == in_count) {
851 sprintf(tmpStr, "gl_Color");
852 } else if (idx == in_count + 1) {
853 sprintf(tmpStr, "gl_SecondaryColor");
854 } else {
855 sprintf(tmpStr, "IN[%u]", idx);
858 } else {
859 if (reg==0)
860 strcpy(tmpStr, "gl_Color");
861 else
862 strcpy(tmpStr, "gl_SecondaryColor");
864 } else {
865 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
866 *is_color = TRUE;
867 sprintf(tmpStr, "attrib%u", reg);
869 break;
870 case WINED3DSPR_CONST:
872 const char prefix = pshader? 'P':'V';
874 /* Relative addressing */
875 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
877 /* Relative addressing on shaders 2.0+ have a relative address token,
878 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
879 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
880 glsl_src_param_t rel_param;
881 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
882 if(reg) {
883 sprintf(tmpStr, "%cC[%s + %u]", prefix, rel_param.param_str, reg);
884 } else {
885 sprintf(tmpStr, "%cC[%s]", prefix, rel_param.param_str);
887 } else {
888 if(reg) {
889 sprintf(tmpStr, "%cC[A0.x + %u]", prefix, reg);
890 } else {
891 sprintf(tmpStr, "%cC[A0.x]", prefix);
895 } else {
896 if(shader_constant_is_local(This, reg)) {
897 sprintf(tmpStr, "%cLC%u", prefix, reg);
898 } else {
899 sprintf(tmpStr, "%cC[%u]", prefix, reg);
903 break;
905 case WINED3DSPR_CONSTINT:
906 if (pshader)
907 sprintf(tmpStr, "PI[%u]", reg);
908 else
909 sprintf(tmpStr, "VI[%u]", reg);
910 break;
911 case WINED3DSPR_CONSTBOOL:
912 if (pshader)
913 sprintf(tmpStr, "PB[%u]", reg);
914 else
915 sprintf(tmpStr, "VB[%u]", reg);
916 break;
917 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
918 if (pshader) {
919 sprintf(tmpStr, "T%u", reg);
920 } else {
921 sprintf(tmpStr, "A%u", reg);
923 break;
924 case WINED3DSPR_LOOP:
925 sprintf(tmpStr, "aL%u", This->baseShader.cur_loop_regno - 1);
926 break;
927 case WINED3DSPR_SAMPLER:
928 if (pshader)
929 sprintf(tmpStr, "Psampler%u", reg);
930 else
931 sprintf(tmpStr, "Vsampler%u", reg);
932 break;
933 case WINED3DSPR_COLOROUT:
934 if (reg >= GL_LIMITS(buffers)) {
935 WARN("Write to render target %u, only %d supported\n", reg, 4);
937 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
938 sprintf(tmpStr, "gl_FragData[%u]", reg);
939 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
940 sprintf(tmpStr, "gl_FragColor");
942 break;
943 case WINED3DSPR_RASTOUT:
944 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
945 break;
946 case WINED3DSPR_DEPTHOUT:
947 sprintf(tmpStr, "gl_FragDepth");
948 break;
949 case WINED3DSPR_ATTROUT:
950 if (reg == 0) {
951 sprintf(tmpStr, "gl_FrontColor");
952 } else {
953 sprintf(tmpStr, "gl_FrontSecondaryColor");
955 break;
956 case WINED3DSPR_TEXCRDOUT:
957 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
958 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
959 sprintf(tmpStr, "OUT[%u]", reg);
960 else
961 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
962 break;
963 case WINED3DSPR_MISCTYPE:
964 if (reg == 0) {
965 /* vPos */
966 sprintf(tmpStr, "vpos");
967 } else if (reg == 1){
968 /* Note that gl_FrontFacing is a bool, while vFace is
969 * a float for which the sign determines front/back
971 sprintf(tmpStr, "(gl_FrontFacing ? 1.0 : -1.0)");
972 } else {
973 FIXME("Unhandled misctype register %d\n", reg);
974 sprintf(tmpStr, "unrecognized_register");
976 break;
977 default:
978 FIXME("Unhandled register name Type(%d)\n", regtype);
979 sprintf(tmpStr, "unrecognized_register");
980 break;
983 strcat(regstr, tmpStr);
986 /* Get the GLSL write mask for the destination register */
987 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
988 char *ptr = write_mask;
989 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
991 if (shader_is_scalar(param)) {
992 mask = WINED3DSP_WRITEMASK_0;
993 } else {
994 *ptr++ = '.';
995 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
996 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
997 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
998 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1001 *ptr = '\0';
1003 return mask;
1006 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1007 unsigned int size = 0;
1009 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1010 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1011 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1012 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1014 return size;
1017 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
1018 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1019 * but addressed as "rgba". To fix this we need to swap the register's x
1020 * and z components. */
1021 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1022 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1023 char *ptr = swizzle_str;
1025 if (!shader_is_scalar(param)) {
1026 *ptr++ = '.';
1027 /* swizzle bits fields: wwzzyyxx */
1028 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
1029 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
1030 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
1031 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
1034 *ptr = '\0';
1037 /* From a given parameter token, generate the corresponding GLSL string.
1038 * Also, return the actual register name and swizzle in case the
1039 * caller needs this information as well. */
1040 static void shader_glsl_add_src_param(const SHADER_OPCODE_ARG *arg, const DWORD param,
1041 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param)
1043 BOOL is_color = FALSE;
1044 char swizzle_str[6];
1046 src_param->reg_name[0] = '\0';
1047 src_param->param_str[0] = '\0';
1048 swizzle_str[0] = '\0';
1050 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
1052 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
1053 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
1056 /* From a given parameter token, generate the corresponding GLSL string.
1057 * Also, return the actual register name and swizzle in case the
1058 * caller needs this information as well. */
1059 static DWORD shader_glsl_add_dst_param(const SHADER_OPCODE_ARG* arg, const DWORD param,
1060 const DWORD addr_token, glsl_dst_param_t *dst_param)
1062 BOOL is_color = FALSE;
1064 dst_param->mask_str[0] = '\0';
1065 dst_param->reg_name[0] = '\0';
1067 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
1068 return shader_glsl_get_write_mask(param, dst_param->mask_str);
1071 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1072 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, const SHADER_OPCODE_ARG *arg, const DWORD param)
1074 glsl_dst_param_t dst_param;
1075 DWORD mask;
1076 int shift;
1078 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
1080 if(mask) {
1081 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1082 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
1085 return mask;
1088 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1089 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, const SHADER_OPCODE_ARG *arg)
1091 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
1094 /** Process GLSL instruction modifiers */
1095 void shader_glsl_add_instruction_modifiers(const SHADER_OPCODE_ARG* arg)
1097 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1099 if (arg->opcode->dst_token && mask != 0) {
1100 glsl_dst_param_t dst_param;
1102 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1104 if (mask & WINED3DSPDM_SATURATE) {
1105 /* _SAT means to clamp the value of the register to between 0 and 1 */
1106 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1107 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1109 if (mask & WINED3DSPDM_MSAMPCENTROID) {
1110 FIXME("_centroid modifier not handled\n");
1112 if (mask & WINED3DSPDM_PARTIALPRECISION) {
1113 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1118 static inline const char* shader_get_comp_op(
1119 const DWORD opcode) {
1121 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1122 switch (op) {
1123 case COMPARISON_GT: return ">";
1124 case COMPARISON_EQ: return "==";
1125 case COMPARISON_GE: return ">=";
1126 case COMPARISON_LT: return "<";
1127 case COMPARISON_NE: return "!=";
1128 case COMPARISON_LE: return "<=";
1129 default:
1130 FIXME("Unrecognized comparison value: %u\n", op);
1131 return "(\?\?)";
1135 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, BOOL texrect, glsl_sample_function_t *sample_function) {
1136 /* Note that there's no such thing as a projected cube texture. */
1137 switch(sampler_type) {
1138 case WINED3DSTT_1D:
1139 sample_function->name = projected ? "texture1DProj" : "texture1D";
1140 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1141 break;
1142 case WINED3DSTT_2D:
1143 if(texrect) {
1144 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1145 } else {
1146 sample_function->name = projected ? "texture2DProj" : "texture2D";
1148 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1149 break;
1150 case WINED3DSTT_CUBE:
1151 sample_function->name = "textureCube";
1152 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1153 break;
1154 case WINED3DSTT_VOLUME:
1155 sample_function->name = projected ? "texture3DProj" : "texture3D";
1156 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1157 break;
1158 default:
1159 sample_function->name = "";
1160 sample_function->coord_mask = 0;
1161 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1162 break;
1166 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1167 BOOL sign_fixup, enum fixup_channel_source channel_source)
1169 switch(channel_source)
1171 case CHANNEL_SOURCE_ZERO:
1172 strcat(arguments, "0.0");
1173 break;
1175 case CHANNEL_SOURCE_ONE:
1176 strcat(arguments, "1.0");
1177 break;
1179 case CHANNEL_SOURCE_X:
1180 strcat(arguments, reg_name);
1181 strcat(arguments, ".x");
1182 break;
1184 case CHANNEL_SOURCE_Y:
1185 strcat(arguments, reg_name);
1186 strcat(arguments, ".y");
1187 break;
1189 case CHANNEL_SOURCE_Z:
1190 strcat(arguments, reg_name);
1191 strcat(arguments, ".z");
1192 break;
1194 case CHANNEL_SOURCE_W:
1195 strcat(arguments, reg_name);
1196 strcat(arguments, ".w");
1197 break;
1199 default:
1200 FIXME("Unhandled channel source %#x\n", channel_source);
1201 strcat(arguments, "undefined");
1202 break;
1205 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1208 static void shader_glsl_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup)
1210 unsigned int mask_size, remaining;
1211 glsl_dst_param_t dst_param;
1212 char arguments[256];
1213 DWORD mask;
1214 BOOL dummy;
1216 mask = 0;
1217 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1218 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1219 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1220 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1221 mask &= arg->dst;
1223 if (!mask) return; /* Nothing to do */
1225 if (is_yuv_fixup(fixup))
1227 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
1228 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
1229 return;
1232 mask_size = shader_glsl_get_write_mask_size(mask);
1234 dst_param.mask_str[0] = '\0';
1235 shader_glsl_get_write_mask(mask, dst_param.mask_str);
1237 dst_param.reg_name[0] = '\0';
1238 shader_glsl_get_register_name(arg->dst, arg->dst_addr, dst_param.reg_name, &dummy, arg);
1240 arguments[0] = '\0';
1241 remaining = mask_size;
1242 if (mask & WINED3DSP_WRITEMASK_0)
1244 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1245 if (--remaining) strcat(arguments, ", ");
1247 if (mask & WINED3DSP_WRITEMASK_1)
1249 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1250 if (--remaining) strcat(arguments, ", ");
1252 if (mask & WINED3DSP_WRITEMASK_2)
1254 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1255 if (--remaining) strcat(arguments, ", ");
1257 if (mask & WINED3DSP_WRITEMASK_3)
1259 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1260 if (--remaining) strcat(arguments, ", ");
1263 if (mask_size > 1)
1265 shader_addline(arg->buffer, "%s%s = vec%u(%s);\n",
1266 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1268 else
1270 shader_addline(arg->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
1274 /*****************************************************************************
1276 * Begin processing individual instruction opcodes
1278 ****************************************************************************/
1280 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1281 static void shader_glsl_arith(const SHADER_OPCODE_ARG *arg)
1283 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1284 SHADER_BUFFER* buffer = arg->buffer;
1285 glsl_src_param_t src0_param;
1286 glsl_src_param_t src1_param;
1287 DWORD write_mask;
1288 char op;
1290 /* Determine the GLSL operator to use based on the opcode */
1291 switch (curOpcode->opcode) {
1292 case WINED3DSIO_MUL: op = '*'; break;
1293 case WINED3DSIO_ADD: op = '+'; break;
1294 case WINED3DSIO_SUB: op = '-'; break;
1295 default:
1296 op = ' ';
1297 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1298 break;
1301 write_mask = shader_glsl_append_dst(buffer, arg);
1302 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1303 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1304 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1307 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1308 static void shader_glsl_mov(const SHADER_OPCODE_ARG *arg)
1310 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1311 SHADER_BUFFER* buffer = arg->buffer;
1312 glsl_src_param_t src0_param;
1313 DWORD write_mask;
1315 write_mask = shader_glsl_append_dst(buffer, arg);
1316 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1318 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1319 * shader versions WINED3DSIO_MOVA is used for this. */
1320 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
1321 !shader_is_pshader_version(shader->baseShader.hex_version) &&
1322 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR)) {
1323 /* This is a simple floor() */
1324 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1325 if (mask_size > 1) {
1326 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1327 } else {
1328 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1330 } else if(arg->opcode->opcode == WINED3DSIO_MOVA) {
1331 /* We need to *round* to the nearest int here. */
1332 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1333 if (mask_size > 1) {
1334 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);
1335 } else {
1336 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1338 } else {
1339 shader_addline(buffer, "%s);\n", src0_param.param_str);
1343 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1344 static void shader_glsl_dot(const SHADER_OPCODE_ARG *arg)
1346 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1347 SHADER_BUFFER* buffer = arg->buffer;
1348 glsl_src_param_t src0_param;
1349 glsl_src_param_t src1_param;
1350 DWORD dst_write_mask, src_write_mask;
1351 unsigned int dst_size = 0;
1353 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1354 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1356 /* dp3 works on vec3, dp4 on vec4 */
1357 if (curOpcode->opcode == WINED3DSIO_DP4) {
1358 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1359 } else {
1360 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1363 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1364 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1366 if (dst_size > 1) {
1367 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1368 } else {
1369 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1373 /* Note that this instruction has some restrictions. The destination write mask
1374 * can't contain the w component, and the source swizzles have to be .xyzw */
1375 static void shader_glsl_cross(const SHADER_OPCODE_ARG *arg)
1377 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1378 glsl_src_param_t src0_param;
1379 glsl_src_param_t src1_param;
1380 char dst_mask[6];
1382 shader_glsl_get_write_mask(arg->dst, dst_mask);
1383 shader_glsl_append_dst(arg->buffer, arg);
1384 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1385 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1386 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1389 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1390 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1391 * GLSL uses the value as-is. */
1392 static void shader_glsl_pow(const SHADER_OPCODE_ARG *arg)
1394 SHADER_BUFFER *buffer = arg->buffer;
1395 glsl_src_param_t src0_param;
1396 glsl_src_param_t src1_param;
1397 DWORD dst_write_mask;
1398 unsigned int dst_size;
1400 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1401 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1403 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1404 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1406 if (dst_size > 1) {
1407 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1408 } else {
1409 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1413 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1414 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1415 * GLSL uses the value as-is. */
1416 static void shader_glsl_log(const SHADER_OPCODE_ARG *arg)
1418 SHADER_BUFFER *buffer = arg->buffer;
1419 glsl_src_param_t src0_param;
1420 DWORD dst_write_mask;
1421 unsigned int dst_size;
1423 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1424 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1426 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1428 if (dst_size > 1) {
1429 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1430 } else {
1431 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1435 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1436 static void shader_glsl_map2gl(const SHADER_OPCODE_ARG *arg)
1438 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1439 SHADER_BUFFER* buffer = arg->buffer;
1440 glsl_src_param_t src_param;
1441 const char *instruction;
1442 char arguments[256];
1443 DWORD write_mask;
1444 unsigned i;
1446 /* Determine the GLSL function to use based on the opcode */
1447 /* TODO: Possibly make this a table for faster lookups */
1448 switch (curOpcode->opcode) {
1449 case WINED3DSIO_MIN: instruction = "min"; break;
1450 case WINED3DSIO_MAX: instruction = "max"; break;
1451 case WINED3DSIO_ABS: instruction = "abs"; break;
1452 case WINED3DSIO_FRC: instruction = "fract"; break;
1453 case WINED3DSIO_NRM: instruction = "normalize"; break;
1454 case WINED3DSIO_EXP: instruction = "exp2"; break;
1455 case WINED3DSIO_SGN: instruction = "sign"; break;
1456 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1457 case WINED3DSIO_DSY: instruction = "ycorrection.y * dFdy"; break;
1458 default: instruction = "";
1459 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1460 break;
1463 write_mask = shader_glsl_append_dst(buffer, arg);
1465 arguments[0] = '\0';
1466 if (curOpcode->num_params > 0) {
1467 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1468 strcat(arguments, src_param.param_str);
1469 for (i = 2; i < curOpcode->num_params; ++i) {
1470 strcat(arguments, ", ");
1471 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1472 strcat(arguments, src_param.param_str);
1476 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1479 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1480 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1481 * dst.x = 2^(floor(src))
1482 * dst.y = src - floor(src)
1483 * dst.z = 2^src (partial precision is allowed, but optional)
1484 * dst.w = 1.0;
1485 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1486 * dst = 2^src; (partial precision is allowed, but optional)
1488 static void shader_glsl_expp(const SHADER_OPCODE_ARG *arg)
1490 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1491 glsl_src_param_t src_param;
1493 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1495 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1496 char dst_mask[6];
1498 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1499 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1500 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1501 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1503 shader_glsl_append_dst(arg->buffer, arg);
1504 shader_glsl_get_write_mask(arg->dst, dst_mask);
1505 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1506 } else {
1507 DWORD write_mask;
1508 unsigned int mask_size;
1510 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1511 mask_size = shader_glsl_get_write_mask_size(write_mask);
1513 if (mask_size > 1) {
1514 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1515 } else {
1516 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1521 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1522 static void shader_glsl_rcp(const SHADER_OPCODE_ARG *arg)
1524 glsl_src_param_t src_param;
1525 DWORD write_mask;
1526 unsigned int mask_size;
1528 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1529 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_3, &src_param);
1532 if (mask_size > 1) {
1533 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1534 } else {
1535 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1539 static void shader_glsl_rsq(const SHADER_OPCODE_ARG *arg)
1541 SHADER_BUFFER* buffer = arg->buffer;
1542 glsl_src_param_t src_param;
1543 DWORD write_mask;
1544 unsigned int mask_size;
1546 write_mask = shader_glsl_append_dst(buffer, arg);
1547 mask_size = shader_glsl_get_write_mask_size(write_mask);
1549 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1551 if (mask_size > 1) {
1552 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1553 } else {
1554 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1558 /** Process signed comparison opcodes in GLSL. */
1559 static void shader_glsl_compare(const SHADER_OPCODE_ARG *arg)
1561 glsl_src_param_t src0_param;
1562 glsl_src_param_t src1_param;
1563 DWORD write_mask;
1564 unsigned int mask_size;
1566 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1567 mask_size = shader_glsl_get_write_mask_size(write_mask);
1568 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1569 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1571 if (mask_size > 1) {
1572 const char *compare;
1574 switch(arg->opcode->opcode) {
1575 case WINED3DSIO_SLT: compare = "lessThan"; break;
1576 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1577 default: compare = "";
1578 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1581 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1582 src0_param.param_str, src1_param.param_str);
1583 } else {
1584 switch(arg->opcode->opcode) {
1585 case WINED3DSIO_SLT:
1586 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1587 * to return 0.0 but step returns 1.0 because step is not < x
1588 * An alternative is a bvec compare padded with an unused second component.
1589 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1590 * issue. Playing with not() is not possible either because not() does not accept
1591 * a scalar.
1593 shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
1594 break;
1595 case WINED3DSIO_SGE:
1596 /* Here we can use the step() function and safe a conditional */
1597 shader_addline(arg->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1598 break;
1599 default:
1600 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1606 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1607 static void shader_glsl_cmp(const SHADER_OPCODE_ARG *arg)
1609 glsl_src_param_t src0_param;
1610 glsl_src_param_t src1_param;
1611 glsl_src_param_t src2_param;
1612 DWORD write_mask, cmp_channel = 0;
1613 unsigned int i, j;
1614 char mask_char[6];
1615 BOOL temp_destination = FALSE;
1617 if(shader_is_scalar(arg->src[0])) {
1618 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1620 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1621 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1622 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1624 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1625 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1626 } else {
1627 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1628 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1629 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1630 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1631 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1632 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1633 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1634 DWORD dstregtype = shader_get_regtype(arg->dst);
1636 /* Cycle through all source0 channels */
1637 for (i=0; i<4; i++) {
1638 write_mask = 0;
1639 /* Find the destination channels which use the current source0 channel */
1640 for (j=0; j<4; j++) {
1641 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1642 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1643 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1647 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1648 * The first lines may overwrite source parameters of the following lines.
1649 * Deal with that by using a temporary destination register if needed
1651 if((src0reg == dstreg && src0regtype == dstregtype) ||
1652 (src1reg == dstreg && src1regtype == dstregtype) ||
1653 (src2reg == dstreg && src2regtype == dstregtype)) {
1655 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1656 if (!write_mask) continue;
1657 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1658 temp_destination = TRUE;
1659 } else {
1660 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1661 if (!write_mask) continue;
1664 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1665 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1666 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1668 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1669 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1672 if(temp_destination) {
1673 shader_glsl_get_write_mask(arg->dst, mask_char);
1674 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1675 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1681 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1682 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1683 * the compare is done per component of src0. */
1684 static void shader_glsl_cnd(const SHADER_OPCODE_ARG *arg)
1686 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1687 glsl_src_param_t src0_param;
1688 glsl_src_param_t src1_param;
1689 glsl_src_param_t src2_param;
1690 DWORD write_mask, cmp_channel = 0;
1691 unsigned int i, j;
1693 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1694 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1695 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1696 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1697 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1699 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1700 if(arg->opcode_token & WINED3DSI_COISSUE) {
1701 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1702 } else {
1703 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1704 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1706 return;
1708 /* Cycle through all source0 channels */
1709 for (i=0; i<4; i++) {
1710 write_mask = 0;
1711 /* Find the destination channels which use the current source0 channel */
1712 for (j=0; j<4; j++) {
1713 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1714 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1715 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1718 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1719 if (!write_mask) continue;
1721 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1722 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1723 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1725 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1726 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1730 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1731 static void shader_glsl_mad(const SHADER_OPCODE_ARG *arg)
1733 glsl_src_param_t src0_param;
1734 glsl_src_param_t src1_param;
1735 glsl_src_param_t src2_param;
1736 DWORD write_mask;
1738 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1739 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1740 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1741 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1742 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1743 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1746 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1747 Vertex shaders to GLSL codes */
1748 static void shader_glsl_mnxn(const SHADER_OPCODE_ARG *arg)
1750 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1751 const SHADER_OPCODE *opcode_table = shader->baseShader.shader_ins;
1752 DWORD shader_version = shader->baseShader.hex_version;
1753 int i;
1754 int nComponents = 0;
1755 SHADER_OPCODE_ARG tmpArg;
1757 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1759 /* Set constants for the temporary argument */
1760 tmpArg.shader = arg->shader;
1761 tmpArg.buffer = arg->buffer;
1762 tmpArg.src[0] = arg->src[0];
1763 tmpArg.src_addr[0] = arg->src_addr[0];
1764 tmpArg.src_addr[1] = arg->src_addr[1];
1765 tmpArg.reg_maps = arg->reg_maps;
1767 switch(arg->opcode->opcode) {
1768 case WINED3DSIO_M4x4:
1769 nComponents = 4;
1770 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1771 break;
1772 case WINED3DSIO_M4x3:
1773 nComponents = 3;
1774 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1775 break;
1776 case WINED3DSIO_M3x4:
1777 nComponents = 4;
1778 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1779 break;
1780 case WINED3DSIO_M3x3:
1781 nComponents = 3;
1782 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1783 break;
1784 case WINED3DSIO_M3x2:
1785 nComponents = 2;
1786 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1787 break;
1788 default:
1789 break;
1792 for (i = 0; i < nComponents; i++) {
1793 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1794 tmpArg.src[1] = arg->src[1]+i;
1795 shader_glsl_dot(&tmpArg);
1800 The LRP instruction performs a component-wise linear interpolation
1801 between the second and third operands using the first operand as the
1802 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1803 This is equivalent to mix(src2, src1, src0);
1805 static void shader_glsl_lrp(const SHADER_OPCODE_ARG *arg)
1807 glsl_src_param_t src0_param;
1808 glsl_src_param_t src1_param;
1809 glsl_src_param_t src2_param;
1810 DWORD write_mask;
1812 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1814 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1815 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1816 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1818 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1819 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1822 /** Process the WINED3DSIO_LIT instruction in GLSL:
1823 * dst.x = dst.w = 1.0
1824 * dst.y = (src0.x > 0) ? src0.x
1825 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1826 * where src.w is clamped at +- 128
1828 static void shader_glsl_lit(const SHADER_OPCODE_ARG *arg)
1830 glsl_src_param_t src0_param;
1831 glsl_src_param_t src1_param;
1832 glsl_src_param_t src3_param;
1833 char dst_mask[6];
1835 shader_glsl_append_dst(arg->buffer, arg);
1836 shader_glsl_get_write_mask(arg->dst, dst_mask);
1838 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1839 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1840 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1842 /* The sdk specifies the instruction like this
1843 * dst.x = 1.0;
1844 * if(src.x > 0.0) dst.y = src.x
1845 * else dst.y = 0.0.
1846 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1847 * else dst.z = 0.0;
1848 * dst.w = 1.0;
1850 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1851 * dst.x = 1.0 ... No further explanation needed
1852 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1853 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1854 * dst.w = 1.0. ... Nothing fancy.
1856 * So we still have one conditional in there. So do this:
1857 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1859 * step(0.0, x) will return 1 if src.x > 0.0, and 0 otherwise. So if y is 0 we get pow(0.0 * 1.0, power),
1860 * which sets dst.z to 0. If y > 0, but x = 0.0, we get pow(y * 0.0, power), which results in 0 too.
1861 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1863 shader_addline(arg->buffer, "vec4(1.0, max(%s, 0.0), pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
1864 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
1867 /** Process the WINED3DSIO_DST instruction in GLSL:
1868 * dst.x = 1.0
1869 * dst.y = src0.x * src0.y
1870 * dst.z = src0.z
1871 * dst.w = src1.w
1873 static void shader_glsl_dst(const SHADER_OPCODE_ARG *arg)
1875 glsl_src_param_t src0y_param;
1876 glsl_src_param_t src0z_param;
1877 glsl_src_param_t src1y_param;
1878 glsl_src_param_t src1w_param;
1879 char dst_mask[6];
1881 shader_glsl_append_dst(arg->buffer, arg);
1882 shader_glsl_get_write_mask(arg->dst, dst_mask);
1884 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1885 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1886 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1887 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1889 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1890 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1893 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1894 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1895 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1897 * dst.x = cos(src0.?)
1898 * dst.y = sin(src0.?)
1899 * dst.z = dst.z
1900 * dst.w = dst.w
1902 static void shader_glsl_sincos(const SHADER_OPCODE_ARG *arg)
1904 glsl_src_param_t src0_param;
1905 DWORD write_mask;
1907 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1908 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1910 switch (write_mask) {
1911 case WINED3DSP_WRITEMASK_0:
1912 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1913 break;
1915 case WINED3DSP_WRITEMASK_1:
1916 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1917 break;
1919 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1920 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1921 break;
1923 default:
1924 ERR("Write mask should be .x, .y or .xy\n");
1925 break;
1929 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1930 * Start a for() loop where src1.y is the initial value of aL,
1931 * increment aL by src1.z for a total of src1.x iterations.
1932 * Need to use a temporary variable for this operation.
1934 /* FIXME: I don't think nested loops will work correctly this way. */
1935 static void shader_glsl_loop(const SHADER_OPCODE_ARG *arg)
1937 glsl_src_param_t src1_param;
1938 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1939 DWORD regtype = shader_get_regtype(arg->src[1]);
1940 DWORD reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1941 const DWORD *control_values = NULL;
1942 const local_constant *constant;
1944 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1946 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
1947 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
1948 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
1949 * addressing.
1951 if(regtype == WINED3DSPR_CONSTINT) {
1952 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
1953 if(constant->idx == reg) {
1954 control_values = constant->value;
1955 break;
1960 if(control_values) {
1961 if(control_values[2] > 0) {
1962 shader_addline(arg->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
1963 shader->baseShader.cur_loop_depth, control_values[1],
1964 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
1965 shader->baseShader.cur_loop_depth, control_values[2]);
1966 } else if(control_values[2] == 0) {
1967 shader_addline(arg->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
1968 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
1969 shader->baseShader.cur_loop_depth, control_values[0],
1970 shader->baseShader.cur_loop_depth);
1971 } else {
1972 shader_addline(arg->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
1973 shader->baseShader.cur_loop_depth, control_values[1],
1974 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
1975 shader->baseShader.cur_loop_depth, control_values[2]);
1977 } else {
1978 shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
1979 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
1980 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
1981 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
1984 shader->baseShader.cur_loop_depth++;
1985 shader->baseShader.cur_loop_regno++;
1988 static void shader_glsl_end(const SHADER_OPCODE_ARG *arg)
1990 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1992 shader_addline(arg->buffer, "}\n");
1994 if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
1995 shader->baseShader.cur_loop_depth--;
1996 shader->baseShader.cur_loop_regno--;
1998 if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
1999 shader->baseShader.cur_loop_depth--;
2003 static void shader_glsl_rep(const SHADER_OPCODE_ARG *arg)
2005 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2006 glsl_src_param_t src0_param;
2008 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2009 shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2010 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2011 src0_param.param_str, shader->baseShader.cur_loop_depth);
2012 shader->baseShader.cur_loop_depth++;
2015 static void shader_glsl_if(const SHADER_OPCODE_ARG *arg)
2017 glsl_src_param_t src0_param;
2019 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2020 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
2023 static void shader_glsl_ifc(const SHADER_OPCODE_ARG *arg)
2025 glsl_src_param_t src0_param;
2026 glsl_src_param_t src1_param;
2028 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2029 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2031 shader_addline(arg->buffer, "if (%s %s %s) {\n",
2032 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2035 static void shader_glsl_else(const SHADER_OPCODE_ARG *arg)
2037 shader_addline(arg->buffer, "} else {\n");
2040 static void shader_glsl_break(const SHADER_OPCODE_ARG *arg)
2042 shader_addline(arg->buffer, "break;\n");
2045 /* FIXME: According to MSDN the compare is done per component. */
2046 static void shader_glsl_breakc(const SHADER_OPCODE_ARG *arg)
2048 glsl_src_param_t src0_param;
2049 glsl_src_param_t src1_param;
2051 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2052 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2054 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
2055 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2058 static void shader_glsl_label(const SHADER_OPCODE_ARG *arg)
2061 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2062 shader_addline(arg->buffer, "}\n");
2063 shader_addline(arg->buffer, "void subroutine%u () {\n", snum);
2066 static void shader_glsl_call(const SHADER_OPCODE_ARG *arg)
2068 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2069 shader_addline(arg->buffer, "subroutine%u();\n", snum);
2072 static void shader_glsl_callnz(const SHADER_OPCODE_ARG *arg)
2074 glsl_src_param_t src1_param;
2076 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2077 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2078 shader_addline(arg->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2081 /*********************************************
2082 * Pixel Shader Specific Code begins here
2083 ********************************************/
2084 static void pshader_glsl_tex(const SHADER_OPCODE_ARG *arg)
2086 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2087 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2088 DWORD hex_version = This->baseShader.hex_version;
2089 char dst_swizzle[6];
2090 glsl_sample_function_t sample_function;
2091 DWORD sampler_type;
2092 DWORD sampler_idx;
2093 BOOL projected, texrect = FALSE;
2094 DWORD mask = 0;
2096 /* All versions have a destination register */
2097 shader_glsl_append_dst(arg->buffer, arg);
2099 /* 1.0-1.4: Use destination register as sampler source.
2100 * 2.0+: Use provided sampler source. */
2101 if (hex_version < WINED3DPS_VERSION(2,0)) {
2102 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2103 } else {
2104 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2106 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2108 if (hex_version < WINED3DPS_VERSION(1,4)) {
2109 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2111 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2112 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2113 projected = TRUE;
2114 switch (flags & ~WINED3DTTFF_PROJECTED) {
2115 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2116 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2117 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2118 case WINED3DTTFF_COUNT4:
2119 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2121 } else {
2122 projected = FALSE;
2124 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
2125 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2127 if (src_mod == WINED3DSPSM_DZ) {
2128 projected = TRUE;
2129 mask = WINED3DSP_WRITEMASK_2;
2130 } else if (src_mod == WINED3DSPSM_DW) {
2131 projected = TRUE;
2132 mask = WINED3DSP_WRITEMASK_3;
2133 } else {
2134 projected = FALSE;
2136 } else {
2137 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
2138 /* ps 2.0 texldp instruction always divides by the fourth component. */
2139 projected = TRUE;
2140 mask = WINED3DSP_WRITEMASK_3;
2141 } else {
2142 projected = FALSE;
2146 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2147 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2148 texrect = TRUE;
2151 shader_glsl_get_sample_function(sampler_type, projected, texrect, &sample_function);
2152 mask |= sample_function.coord_mask;
2154 if (hex_version < WINED3DPS_VERSION(2,0)) {
2155 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2156 } else {
2157 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2160 /* 1.0-1.3: Use destination register as coordinate source.
2161 1.4+: Use provided coordinate source register. */
2162 if (hex_version < WINED3DPS_VERSION(1,4)) {
2163 char coord_mask[6];
2164 shader_glsl_get_write_mask(mask, coord_mask);
2165 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
2166 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
2167 } else {
2168 glsl_src_param_t coord_param;
2169 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
2170 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
2171 glsl_src_param_t bias;
2172 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2174 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2175 sample_function.name, sampler_idx, coord_param.param_str,
2176 bias.param_str, dst_swizzle);
2177 } else {
2178 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2179 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2184 static void shader_glsl_texldl(const SHADER_OPCODE_ARG *arg)
2186 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2187 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2188 glsl_sample_function_t sample_function;
2189 glsl_src_param_t coord_param, lod_param;
2190 char dst_swizzle[6];
2191 DWORD sampler_type;
2192 DWORD sampler_idx;
2193 BOOL texrect = FALSE;
2195 shader_glsl_append_dst(arg->buffer, arg);
2196 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2198 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2199 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2200 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2201 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2202 texrect = TRUE;
2204 shader_glsl_get_sample_function(sampler_type, FALSE, texrect, &sample_function); shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &coord_param);
2206 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2208 if (shader_is_pshader_version(This->baseShader.hex_version)) {
2209 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2210 * However, they seem to work just fine in fragment shaders as well. */
2211 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2212 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2213 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2214 } else {
2215 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2216 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2220 static void pshader_glsl_texcoord(const SHADER_OPCODE_ARG *arg)
2222 /* FIXME: Make this work for more than just 2D textures */
2224 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2225 SHADER_BUFFER* buffer = arg->buffer;
2226 DWORD hex_version = This->baseShader.hex_version;
2227 DWORD write_mask;
2228 char dst_mask[6];
2230 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2231 shader_glsl_get_write_mask(write_mask, dst_mask);
2233 if (hex_version != WINED3DPS_VERSION(1,4)) {
2234 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2235 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2236 } else {
2237 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2238 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2239 char dst_swizzle[6];
2241 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2243 if (src_mod == WINED3DSPSM_DZ) {
2244 glsl_src_param_t div_param;
2245 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2246 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2248 if (mask_size > 1) {
2249 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2250 } else {
2251 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2253 } else if (src_mod == WINED3DSPSM_DW) {
2254 glsl_src_param_t div_param;
2255 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2256 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2258 if (mask_size > 1) {
2259 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2260 } else {
2261 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2263 } else {
2264 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2269 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2270 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2271 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2272 static void pshader_glsl_texdp3tex(const SHADER_OPCODE_ARG *arg)
2274 glsl_src_param_t src0_param;
2275 char dst_mask[6];
2276 glsl_sample_function_t sample_function;
2277 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2278 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2279 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2281 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2283 shader_glsl_append_dst(arg->buffer, arg);
2284 shader_glsl_get_write_mask(arg->dst, dst_mask);
2286 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2287 * scalar, and projected sampling would require 4.
2289 * It is a dependent read - not valid with conditional NP2 textures
2291 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2293 switch(count_bits(sample_function.coord_mask)) {
2294 case 1:
2295 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2296 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2297 break;
2299 case 2:
2300 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2301 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2302 break;
2304 case 3:
2305 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2306 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2307 break;
2308 default:
2309 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2313 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2314 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2315 static void pshader_glsl_texdp3(const SHADER_OPCODE_ARG *arg)
2317 glsl_src_param_t src0_param;
2318 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2319 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2320 DWORD dst_mask;
2321 unsigned int mask_size;
2323 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2324 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2325 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2327 if (mask_size > 1) {
2328 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2329 } else {
2330 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2334 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2335 * Calculate the depth as dst.x / dst.y */
2336 static void pshader_glsl_texdepth(const SHADER_OPCODE_ARG *arg)
2338 glsl_dst_param_t dst_param;
2340 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2342 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2343 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2344 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2345 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2346 * >= 1.0 or < 0.0
2348 shader_addline(arg->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n", dst_param.reg_name, dst_param.reg_name);
2351 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2352 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2353 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2354 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2356 static void pshader_glsl_texm3x2depth(const SHADER_OPCODE_ARG *arg)
2358 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2359 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2360 glsl_src_param_t src0_param;
2362 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2364 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2365 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2368 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2369 * Calculate the 1st of a 2-row matrix multiplication. */
2370 static void pshader_glsl_texm3x2pad(const SHADER_OPCODE_ARG *arg)
2372 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2373 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2374 SHADER_BUFFER* buffer = arg->buffer;
2375 glsl_src_param_t src0_param;
2377 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2378 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2381 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2382 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2383 static void pshader_glsl_texm3x3pad(const SHADER_OPCODE_ARG* arg)
2385 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2386 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2387 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2388 SHADER_BUFFER* buffer = arg->buffer;
2389 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2390 glsl_src_param_t src0_param;
2392 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2393 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2394 current_state->texcoord_w[current_state->current_row++] = reg;
2397 static void pshader_glsl_texm3x2tex(const SHADER_OPCODE_ARG *arg)
2399 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2400 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2401 SHADER_BUFFER* buffer = arg->buffer;
2402 glsl_src_param_t src0_param;
2403 char dst_mask[6];
2405 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2406 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2408 shader_glsl_append_dst(buffer, arg);
2409 shader_glsl_get_write_mask(arg->dst, dst_mask);
2411 /* Sample the texture using the calculated coordinates */
2412 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2415 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2416 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2417 static void pshader_glsl_texm3x3tex(const SHADER_OPCODE_ARG *arg)
2419 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2420 glsl_src_param_t src0_param;
2421 char dst_mask[6];
2422 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2423 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2424 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2425 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2426 glsl_sample_function_t sample_function;
2428 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2429 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2431 shader_glsl_append_dst(arg->buffer, arg);
2432 shader_glsl_get_write_mask(arg->dst, dst_mask);
2433 /* Dependent read, not valid with conditional NP2 */
2434 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2436 /* Sample the texture using the calculated coordinates */
2437 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2439 current_state->current_row = 0;
2442 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2443 * Perform the 3rd row of a 3x3 matrix multiply */
2444 static void pshader_glsl_texm3x3(const SHADER_OPCODE_ARG *arg)
2446 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2447 glsl_src_param_t src0_param;
2448 char dst_mask[6];
2449 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2450 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2451 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2453 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2455 shader_glsl_append_dst(arg->buffer, arg);
2456 shader_glsl_get_write_mask(arg->dst, dst_mask);
2457 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2459 current_state->current_row = 0;
2462 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2463 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2464 static void pshader_glsl_texm3x3spec(const SHADER_OPCODE_ARG *arg)
2466 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2467 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2468 glsl_src_param_t src0_param;
2469 glsl_src_param_t src1_param;
2470 char dst_mask[6];
2471 SHADER_BUFFER* buffer = arg->buffer;
2472 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2473 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2474 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2475 glsl_sample_function_t sample_function;
2477 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2478 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2480 /* Perform the last matrix multiply operation */
2481 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2482 /* Reflection calculation */
2483 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2485 shader_glsl_append_dst(buffer, arg);
2486 shader_glsl_get_write_mask(arg->dst, dst_mask);
2487 /* Dependent read, not valid with conditional NP2 */
2488 shader_glsl_get_sample_function(stype, FALSE, FALSE, &sample_function);
2490 /* Sample the texture */
2491 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2493 current_state->current_row = 0;
2496 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2497 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2498 static void pshader_glsl_texm3x3vspec(const SHADER_OPCODE_ARG *arg)
2500 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2501 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2502 SHADER_BUFFER* buffer = arg->buffer;
2503 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2504 glsl_src_param_t src0_param;
2505 char dst_mask[6];
2506 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2507 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2508 glsl_sample_function_t sample_function;
2510 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2512 /* Perform the last matrix multiply operation */
2513 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2515 /* Construct the eye-ray vector from w coordinates */
2516 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2517 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2518 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2520 shader_glsl_append_dst(buffer, arg);
2521 shader_glsl_get_write_mask(arg->dst, dst_mask);
2522 /* Dependent read, not valid with conditional NP2 */
2523 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2525 /* Sample the texture using the calculated coordinates */
2526 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2528 current_state->current_row = 0;
2531 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2532 * Apply a fake bump map transform.
2533 * texbem is pshader <= 1.3 only, this saves a few version checks
2535 static void pshader_glsl_texbem(const SHADER_OPCODE_ARG *arg)
2537 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2538 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2539 char dst_swizzle[6];
2540 glsl_sample_function_t sample_function;
2541 glsl_src_param_t coord_param;
2542 DWORD sampler_type;
2543 DWORD sampler_idx;
2544 DWORD mask;
2545 DWORD flags;
2546 char coord_mask[6];
2548 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2549 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2551 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2552 /* Dependent read, not valid with conditional NP2 */
2553 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2554 mask = sample_function.coord_mask;
2556 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2558 shader_glsl_get_write_mask(mask, coord_mask);
2560 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2561 * so we can't let the GL handle this.
2563 if (flags & WINED3DTTFF_PROJECTED) {
2564 DWORD div_mask=0;
2565 char coord_div_mask[3];
2566 switch (flags & ~WINED3DTTFF_PROJECTED) {
2567 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2568 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2569 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2570 case WINED3DTTFF_COUNT4:
2571 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2573 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2574 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2577 shader_glsl_append_dst(arg->buffer, arg);
2578 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2579 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2580 glsl_src_param_t luminance_param;
2581 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2582 shader_addline(arg->buffer, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )*(%s * luminancescale%d + luminanceoffset%d))%s);\n",
2583 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
2584 luminance_param.param_str, sampler_idx, sampler_idx, dst_swizzle);
2585 } else {
2586 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2587 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
2591 static void pshader_glsl_bem(const SHADER_OPCODE_ARG *arg)
2593 glsl_src_param_t src0_param, src1_param;
2594 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2596 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2597 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2599 shader_glsl_append_dst(arg->buffer, arg);
2600 shader_addline(arg->buffer, "%s + bumpenvmat%d * %s);\n",
2601 src0_param.param_str, sampler_idx, src1_param.param_str);
2604 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2605 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2606 static void pshader_glsl_texreg2ar(const SHADER_OPCODE_ARG *arg)
2608 glsl_src_param_t src0_param;
2609 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2610 char dst_mask[6];
2612 shader_glsl_append_dst(arg->buffer, arg);
2613 shader_glsl_get_write_mask(arg->dst, dst_mask);
2614 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2616 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2619 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2620 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2621 static void pshader_glsl_texreg2gb(const SHADER_OPCODE_ARG *arg)
2623 glsl_src_param_t src0_param;
2624 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2625 char dst_mask[6];
2627 shader_glsl_append_dst(arg->buffer, arg);
2628 shader_glsl_get_write_mask(arg->dst, dst_mask);
2629 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2631 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2634 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2635 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2636 static void pshader_glsl_texreg2rgb(const SHADER_OPCODE_ARG *arg)
2638 glsl_src_param_t src0_param;
2639 char dst_mask[6];
2640 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2641 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2642 glsl_sample_function_t sample_function;
2644 shader_glsl_append_dst(arg->buffer, arg);
2645 shader_glsl_get_write_mask(arg->dst, dst_mask);
2646 /* Dependent read, not valid with conditional NP2 */
2647 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2648 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2650 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2653 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2654 * If any of the first 3 components are < 0, discard this pixel */
2655 static void pshader_glsl_texkill(const SHADER_OPCODE_ARG *arg)
2657 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2658 DWORD hex_version = This->baseShader.hex_version;
2659 glsl_dst_param_t dst_param;
2661 /* The argument is a destination parameter, and no writemasks are allowed */
2662 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2663 if((hex_version >= WINED3DPS_VERSION(2,0))) {
2664 /* 2.0 shaders compare all 4 components in texkill */
2665 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2666 } else {
2667 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2668 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2669 * 4 components are defined, only the first 3 are used
2671 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2675 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2676 * dst = dot2(src0, src1) + src2 */
2677 static void pshader_glsl_dp2add(const SHADER_OPCODE_ARG *arg)
2679 glsl_src_param_t src0_param;
2680 glsl_src_param_t src1_param;
2681 glsl_src_param_t src2_param;
2682 DWORD write_mask;
2683 unsigned int mask_size;
2685 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2686 mask_size = shader_glsl_get_write_mask_size(write_mask);
2688 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2689 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2690 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2692 if (mask_size > 1) {
2693 shader_addline(arg->buffer, "vec%d(dot(%s, %s) + %s));\n", mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
2694 } else {
2695 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2699 static void pshader_glsl_input_pack(SHADER_BUFFER* buffer, const struct semantic* semantics_in,
2700 IWineD3DPixelShader *iface, enum vertexprocessing_mode vertexprocessing)
2702 unsigned int i;
2703 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
2705 for (i = 0; i < MAX_REG_INPUT; i++) {
2707 DWORD usage_token = semantics_in[i].usage;
2708 DWORD register_token = semantics_in[i].reg;
2709 DWORD usage, usage_idx;
2710 char reg_mask[6];
2712 /* Uninitialized */
2713 if (!usage_token) continue;
2714 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2715 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2716 shader_glsl_get_write_mask(register_token, reg_mask);
2718 switch(usage) {
2720 case WINED3DDECLUSAGE_TEXCOORD:
2721 if(usage_idx < 8 && vertexprocessing == pretransformed) {
2722 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2723 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
2724 } else {
2725 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2726 This->input_reg_map[i], reg_mask, reg_mask);
2728 break;
2730 case WINED3DDECLUSAGE_COLOR:
2731 if (usage_idx == 0)
2732 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
2733 This->input_reg_map[i], reg_mask, reg_mask);
2734 else if (usage_idx == 1)
2735 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2736 This->input_reg_map[i], reg_mask, reg_mask);
2737 else
2738 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2739 This->input_reg_map[i], reg_mask, reg_mask);
2740 break;
2742 default:
2743 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2744 This->input_reg_map[i], reg_mask, reg_mask);
2749 /*********************************************
2750 * Vertex Shader Specific Code begins here
2751 ********************************************/
2753 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
2754 glsl_program_key_t *key;
2756 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2757 key->vshader = entry->vshader;
2758 key->pshader = entry->pshader;
2759 key->ps_args = entry->ps_args;
2761 hash_table_put(priv->glsl_program_lookup, key, entry);
2764 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
2765 GLhandleARB vshader, IWineD3DPixelShader *pshader, struct ps_compile_args *ps_args) {
2766 glsl_program_key_t key;
2768 key.vshader = vshader;
2769 key.pshader = pshader;
2770 key.ps_args = *ps_args;
2772 return (struct glsl_shader_prog_link *)hash_table_get(priv->glsl_program_lookup, &key);
2775 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
2776 struct glsl_shader_prog_link *entry)
2778 glsl_program_key_t *key;
2780 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2781 key->vshader = entry->vshader;
2782 key->pshader = entry->pshader;
2783 key->ps_args = entry->ps_args;
2784 hash_table_remove(priv->glsl_program_lookup, key);
2786 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2787 if (entry->vshader) list_remove(&entry->vshader_entry);
2788 if (entry->pshader) list_remove(&entry->pshader_entry);
2789 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2790 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2791 HeapFree(GetProcessHeap(), 0, entry);
2794 static void handle_ps3_input(SHADER_BUFFER *buffer, const struct semantic *semantics_in,
2795 const struct semantic *semantics_out, const WineD3D_GL_Info *gl_info, const DWORD *map)
2797 unsigned int i, j;
2798 DWORD usage_token, usage_token_out;
2799 DWORD register_token, register_token_out;
2800 DWORD usage, usage_idx, usage_out, usage_idx_out;
2801 DWORD *set;
2802 DWORD in_idx;
2803 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
2804 char reg_mask[6], reg_mask_out[6];
2805 char destination[50];
2807 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
2809 if (!semantics_out) {
2810 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
2811 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
2812 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
2815 for(i = 0; i < MAX_REG_INPUT; i++) {
2816 usage_token = semantics_in[i].usage;
2817 if (!usage_token) continue;
2819 in_idx = map[i];
2820 if (in_idx >= (in_count + 2)) {
2821 FIXME("More input varyings declared than supported, expect issues\n");
2822 continue;
2823 } else if(map[i] == -1) {
2824 /* Declared, but not read register */
2825 continue;
2828 if (in_idx == in_count) {
2829 sprintf(destination, "gl_FrontColor");
2830 } else if (in_idx == in_count + 1) {
2831 sprintf(destination, "gl_FrontSecondaryColor");
2832 } else {
2833 sprintf(destination, "IN[%u]", in_idx);
2836 register_token = semantics_in[i].reg;
2838 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2839 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2840 set[map[i]] = shader_glsl_get_write_mask(register_token, reg_mask);
2842 if(!semantics_out) {
2843 switch(usage) {
2844 case WINED3DDECLUSAGE_COLOR:
2845 if (usage_idx == 0)
2846 shader_addline(buffer, "%s%s = front_color%s;\n",
2847 destination, reg_mask, reg_mask);
2848 else if (usage_idx == 1)
2849 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
2850 destination, reg_mask, reg_mask);
2851 else
2852 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2853 destination, reg_mask, reg_mask);
2854 break;
2856 case WINED3DDECLUSAGE_TEXCOORD:
2857 if (usage_idx < 8) {
2858 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
2859 destination, reg_mask, usage_idx, reg_mask);
2860 } else {
2861 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2862 destination, reg_mask, reg_mask);
2864 break;
2866 case WINED3DDECLUSAGE_FOG:
2867 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2868 destination, reg_mask, reg_mask);
2869 break;
2871 default:
2872 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2873 destination, reg_mask, reg_mask);
2875 } else {
2876 BOOL found = FALSE;
2877 for(j = 0; j < MAX_REG_OUTPUT; j++) {
2878 usage_token_out = semantics_out[j].usage;
2879 if (!usage_token_out) continue;
2880 register_token_out = semantics_out[j].reg;
2882 usage_out = (usage_token_out & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2883 usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2884 shader_glsl_get_write_mask(register_token_out, reg_mask_out);
2886 if(usage == usage_out &&
2887 usage_idx == usage_idx_out) {
2888 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
2889 destination, reg_mask, j, reg_mask);
2890 found = TRUE;
2893 if(!found) {
2894 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2895 destination, reg_mask, reg_mask);
2900 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2901 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2902 * input varyings are assigned above, if the optimizer works properly.
2904 for(i = 0; i < in_count + 2; i++) {
2905 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
2906 unsigned int size = 0;
2907 memset(reg_mask, 0, sizeof(reg_mask));
2908 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
2909 reg_mask[size] = 'x';
2910 size++;
2912 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
2913 reg_mask[size] = 'y';
2914 size++;
2916 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
2917 reg_mask[size] = 'z';
2918 size++;
2920 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
2921 reg_mask[size] = 'w';
2922 size++;
2925 if (i == in_count) {
2926 sprintf(destination, "gl_FrontColor");
2927 } else if (i == in_count + 1) {
2928 sprintf(destination, "gl_FrontSecondaryColor");
2929 } else {
2930 sprintf(destination, "IN[%u]", i);
2933 if (size == 1) {
2934 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
2935 } else {
2936 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
2941 HeapFree(GetProcessHeap(), 0, set);
2944 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
2945 IWineD3DPixelShader *pixelshader, const WineD3D_GL_Info *gl_info)
2947 GLhandleARB ret = 0;
2948 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
2949 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
2950 IWineD3DDeviceImpl *device;
2951 DWORD vs_major = WINED3DSHADER_VERSION_MAJOR(vs->baseShader.hex_version);
2952 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.hex_version) : 0;
2953 unsigned int i;
2954 SHADER_BUFFER buffer;
2955 DWORD usage_token;
2956 DWORD register_token;
2957 DWORD usage, usage_idx, writemask;
2958 char reg_mask[6];
2959 const struct semantic *semantics_out, *semantics_in;
2961 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
2962 buffer.bsize = 0;
2963 buffer.lineNo = 0;
2964 buffer.newline = TRUE;
2966 shader_addline(&buffer, "#version 120\n");
2968 if(vs_major < 3 && ps_major < 3) {
2969 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
2970 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
2972 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
2973 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
2974 !device->frag_pipe->ffp_proj_control) {
2975 shader_addline(&buffer, "void order_ps_input() {\n");
2976 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
2977 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
2978 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
2979 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
2982 shader_addline(&buffer, "}\n");
2983 } else {
2984 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
2986 } else if(ps_major < 3 && vs_major >= 3) {
2987 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
2988 semantics_out = vs->semantics_out;
2990 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
2991 for(i = 0; i < MAX_REG_OUTPUT; i++) {
2992 usage_token = semantics_out[i].usage;
2993 if (!usage_token) continue;
2994 register_token = semantics_out[i].reg;
2996 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2997 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2998 writemask = shader_glsl_get_write_mask(register_token, reg_mask);
3000 switch(usage) {
3001 case WINED3DDECLUSAGE_COLOR:
3002 if (usage_idx == 0)
3003 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3004 else if (usage_idx == 1)
3005 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3006 break;
3008 case WINED3DDECLUSAGE_POSITION:
3009 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3010 break;
3012 case WINED3DDECLUSAGE_TEXCOORD:
3013 if (usage_idx < 8) {
3014 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3016 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3017 usage_idx, reg_mask, i, reg_mask);
3018 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3019 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3022 break;
3024 case WINED3DDECLUSAGE_PSIZE:
3025 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3026 break;
3028 case WINED3DDECLUSAGE_FOG:
3029 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3030 break;
3032 default:
3033 break;
3036 shader_addline(&buffer, "}\n");
3038 } else if(ps_major >= 3 && vs_major >= 3) {
3039 semantics_out = vs->semantics_out;
3040 semantics_in = ps->semantics_in;
3042 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3043 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3044 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3046 /* First, sort out position and point size. Those are not passed to the pixel shader */
3047 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3048 usage_token = semantics_out[i].usage;
3049 if (!usage_token) continue;
3050 register_token = semantics_out[i].reg;
3052 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3053 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3054 shader_glsl_get_write_mask(register_token, reg_mask);
3056 switch(usage) {
3057 case WINED3DDECLUSAGE_POSITION:
3058 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3059 break;
3061 case WINED3DDECLUSAGE_PSIZE:
3062 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3063 break;
3065 default:
3066 break;
3070 /* Then, fix the pixel shader input */
3071 handle_ps3_input(&buffer, semantics_in, semantics_out, gl_info, ps->input_reg_map);
3073 shader_addline(&buffer, "}\n");
3074 } else if(ps_major >= 3 && vs_major < 3) {
3075 semantics_in = ps->semantics_in;
3077 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3078 shader_addline(&buffer, "void order_ps_input() {\n");
3079 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3080 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3081 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3083 handle_ps3_input(&buffer, semantics_in, NULL, gl_info, ps->input_reg_map);
3084 shader_addline(&buffer, "}\n");
3085 } else {
3086 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3089 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3090 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3091 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3092 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3093 GL_EXTCALL(glCompileShaderARB(ret));
3094 checkGLcall("glCompileShaderARB(ret)");
3096 HeapFree(GetProcessHeap(), 0, buffer.buffer);
3097 return ret;
3100 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
3101 GLhandleARB programId, char prefix)
3103 const local_constant *lconst;
3104 GLuint tmp_loc;
3105 const float *value;
3106 char glsl_name[8];
3108 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3109 value = (const float *)lconst->value;
3110 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3111 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3112 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3114 checkGLcall("Hardcoding local constants\n");
3117 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3118 * It sets the programId on the current StateBlock (because it should be called
3119 * inside of the DrawPrimitive() part of the render loop).
3121 * If a program for the given combination does not exist, create one, and store
3122 * the program in the hash table. If it creates a program, it will link the
3123 * given objects, too.
3125 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3126 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3127 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3128 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3129 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3130 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3131 struct glsl_shader_prog_link *entry = NULL;
3132 GLhandleARB programId = 0;
3133 GLhandleARB reorder_shader_id = 0;
3134 int i;
3135 char glsl_name[8];
3136 GLhandleARB vshader_id, pshader_id;
3137 struct ps_compile_args compile_args;
3139 if(use_vs) {
3140 IWineD3DVertexShaderImpl_CompileShader(vshader);
3141 vshader_id = ((IWineD3DVertexShaderImpl*)vshader)->prgId;
3142 } else {
3143 vshader_id = 0;
3145 if(use_ps) {
3146 find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &compile_args);
3147 } else {
3148 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3149 memset(&compile_args, 0, sizeof(compile_args));
3151 entry = get_glsl_program_entry(priv, vshader_id, pshader, &compile_args);
3152 if (entry) {
3153 priv->glsl_program = entry;
3154 return;
3157 /* If we get to this point, then no matching program exists, so we create one */
3158 programId = GL_EXTCALL(glCreateProgramObjectARB());
3159 TRACE("Created new GLSL shader program %u\n", programId);
3161 /* Create the entry */
3162 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3163 entry->programId = programId;
3164 entry->vshader = vshader_id;
3165 entry->pshader = pshader;
3166 entry->ps_args = compile_args;
3167 /* Add the hash table entry */
3168 add_glsl_program_entry(priv, entry);
3170 /* Set the current program */
3171 priv->glsl_program = entry;
3173 /* Attach GLSL vshader */
3174 if (vshader_id) {
3175 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3176 char tmp_name[10];
3178 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3179 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3180 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3181 checkGLcall("glAttachObjectARB");
3182 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3183 * is destroyed
3185 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3187 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3188 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3189 checkGLcall("glAttachObjectARB");
3191 /* Bind vertex attributes to a corresponding index number to match
3192 * the same index numbers as ARB_vertex_programs (makes loading
3193 * vertex attributes simpler). With this method, we can use the
3194 * exact same code to load the attributes later for both ARB and
3195 * GLSL shaders.
3197 * We have to do this here because we need to know the Program ID
3198 * in order to make the bindings work, and it has to be done prior
3199 * to linking the GLSL program. */
3200 for (i = 0; i < max_attribs; ++i) {
3201 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3202 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3203 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3206 checkGLcall("glBindAttribLocationARB");
3208 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3211 if(use_ps) {
3212 pshader_id = find_gl_pshader((IWineD3DPixelShaderImpl *) pshader, &compile_args);
3213 } else {
3214 pshader_id = 0;
3217 /* Attach GLSL pshader */
3218 if (pshader_id) {
3219 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3220 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3221 checkGLcall("glAttachObjectARB");
3223 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3226 /* Link the program */
3227 TRACE("Linking GLSL shader program %u\n", programId);
3228 GL_EXTCALL(glLinkProgramARB(programId));
3229 print_glsl_info_log(&GLINFO_LOCATION, programId);
3231 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3232 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3233 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3234 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3236 for (i = 0; i < MAX_CONST_I; ++i) {
3237 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3238 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3240 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3241 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3242 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3243 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3245 for (i = 0; i < MAX_CONST_I; ++i) {
3246 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3247 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3250 if(pshader) {
3251 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3252 char name[32];
3253 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3254 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3255 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3256 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3257 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3258 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3263 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3264 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3265 checkGLcall("Find glsl program uniform locations");
3267 if (pshader && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.hex_version) >= 3
3268 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4) {
3269 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3270 entry->vertex_color_clamp = GL_FALSE;
3271 } else {
3272 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3275 /* Set the shader to allow uniform loading on it */
3276 GL_EXTCALL(glUseProgramObjectARB(programId));
3277 checkGLcall("glUseProgramObjectARB(programId)");
3279 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3280 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3281 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3282 * vertex shader with fixed function pixel processing is used we make sure that the card
3283 * supports enough samplers to allow the max number of vertex samplers with all possible
3284 * fixed function fragment processing setups. So once the program is linked these samplers
3285 * won't change.
3287 if(vshader_id) {
3288 /* Load vertex shader samplers */
3289 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3291 if(pshader_id) {
3292 /* Load pixel shader samplers */
3293 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3296 /* If the local constants do not have to be loaded with the environment constants,
3297 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3298 * later
3300 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3301 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3303 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3304 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3308 static GLhandleARB create_glsl_blt_shader(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3310 GLhandleARB program_id;
3311 GLhandleARB vshader_id, pshader_id;
3312 const char *blt_vshader[] = {
3313 "#version 120\n"
3314 "void main(void)\n"
3315 "{\n"
3316 " gl_Position = gl_Vertex;\n"
3317 " gl_FrontColor = vec4(1.0);\n"
3318 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3319 "}\n"
3322 const char *blt_pshaders[tex_type_count] = {
3323 /* tex_1d */
3324 NULL,
3325 /* tex_2d */
3326 "#version 120\n"
3327 "uniform sampler2D sampler;\n"
3328 "void main(void)\n"
3329 "{\n"
3330 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3331 "}\n",
3332 /* tex_3d */
3333 NULL,
3334 /* tex_cube */
3335 "#version 120\n"
3336 "uniform samplerCube sampler;\n"
3337 "void main(void)\n"
3338 "{\n"
3339 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3340 "}\n",
3341 /* tex_rect */
3342 "#version 120\n"
3343 "#extension GL_ARB_texture_rectangle : enable\n"
3344 "uniform sampler2DRect sampler;\n"
3345 "void main(void)\n"
3346 "{\n"
3347 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3348 "}\n",
3351 if (!blt_pshaders[tex_type])
3353 FIXME("tex_type %#x not supported\n", tex_type);
3354 tex_type = tex_2d;
3357 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3358 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3359 GL_EXTCALL(glCompileShaderARB(vshader_id));
3361 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3362 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
3363 GL_EXTCALL(glCompileShaderARB(pshader_id));
3365 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3366 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3367 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3368 GL_EXTCALL(glLinkProgramARB(program_id));
3370 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3372 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3373 * is destroyed
3375 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3376 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3377 return program_id;
3380 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3381 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3382 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3383 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3384 GLhandleARB program_id = 0;
3385 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3387 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3389 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3390 else priv->glsl_program = NULL;
3392 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3394 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3395 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3396 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3397 checkGLcall("glClampColorARB");
3398 } else {
3399 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3403 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3404 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3405 GL_EXTCALL(glUseProgramObjectARB(program_id));
3406 checkGLcall("glUseProgramObjectARB");
3409 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
3410 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3411 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3412 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3413 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
3415 if (!*blt_program) {
3416 GLhandleARB loc;
3417 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
3418 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
3419 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3420 GL_EXTCALL(glUniform1iARB(loc, 0));
3421 } else {
3422 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3426 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3427 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3428 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3429 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3430 GLhandleARB program_id;
3432 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3433 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3435 GL_EXTCALL(glUseProgramObjectARB(program_id));
3436 checkGLcall("glUseProgramObjectARB");
3439 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
3440 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3441 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3442 GL_EXTCALL(glUseProgramObjectARB(0));
3445 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3446 const struct list *linked_programs;
3447 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3448 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3449 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)device->shader_priv;
3450 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3451 IWineD3DPixelShaderImpl *ps = NULL;
3452 IWineD3DVertexShaderImpl *vs = NULL;
3454 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3455 * can be called from IWineD3DBaseShader::Release
3457 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
3459 if(pshader) {
3460 ps = (IWineD3DPixelShaderImpl *) This;
3461 if(ps->num_gl_shaders == 0) return;
3462 } else {
3463 vs = (IWineD3DVertexShaderImpl *) This;
3464 if(vs->prgId == 0) return;
3467 linked_programs = &This->baseShader.linked_programs;
3469 TRACE("Deleting linked programs\n");
3470 if (linked_programs->next) {
3471 struct glsl_shader_prog_link *entry, *entry2;
3473 if(pshader) {
3474 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3475 delete_glsl_program_entry(priv, gl_info, entry);
3477 } else {
3478 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3479 delete_glsl_program_entry(priv, gl_info, entry);
3484 if(pshader) {
3485 UINT i;
3487 ENTER_GL();
3488 for(i = 0; i < ps->num_gl_shaders; i++) {
3489 TRACE("deleting pshader %u\n", ps->gl_shaders[i].prgId);
3490 GL_EXTCALL(glDeleteObjectARB(ps->gl_shaders[i].prgId));
3491 checkGLcall("glDeleteObjectARB");
3493 LEAVE_GL();
3494 HeapFree(GetProcessHeap(), 0, ps->gl_shaders);
3495 ps->gl_shaders = NULL;
3496 ps->num_gl_shaders = 0;
3497 } else {
3498 TRACE("Deleting shader object %u\n", vs->prgId);
3499 ENTER_GL();
3500 GL_EXTCALL(glDeleteObjectARB(vs->prgId));
3501 checkGLcall("glDeleteObjectARB");
3502 LEAVE_GL();
3503 vs->prgId = 0;
3504 vs->baseShader.is_compiled = FALSE;
3508 static unsigned int glsl_program_key_hash(const void *key)
3510 const glsl_program_key_t *k = (const glsl_program_key_t *)key;
3512 unsigned int hash = k->vshader | ((DWORD_PTR) k->pshader) << 16;
3513 hash += ~(hash << 15);
3514 hash ^= (hash >> 10);
3515 hash += (hash << 3);
3516 hash ^= (hash >> 6);
3517 hash += ~(hash << 11);
3518 hash ^= (hash >> 16);
3520 return hash;
3523 static BOOL glsl_program_key_compare(const void *keya, const void *keyb)
3525 const glsl_program_key_t *ka = (const glsl_program_key_t *)keya;
3526 const glsl_program_key_t *kb = (const glsl_program_key_t *)keyb;
3528 return ka->vshader == kb->vshader && ka->pshader == kb->pshader &&
3529 (memcmp(&ka->ps_args, &kb->ps_args, sizeof(kb->ps_args)) == 0);
3532 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3533 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3534 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3535 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
3536 This->shader_priv = priv;
3537 return WINED3D_OK;
3540 static void shader_glsl_free(IWineD3DDevice *iface) {
3541 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3542 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3543 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3544 int i;
3546 for (i = 0; i < tex_type_count; ++i)
3548 if (priv->depth_blt_program[i])
3550 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
3554 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
3556 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3557 This->shader_priv = NULL;
3560 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3561 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3562 return FALSE;
3565 static GLuint shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
3566 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3567 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3568 CONST DWORD *function = This->baseShader.function;
3569 const char *fragcolor;
3570 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3572 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3573 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3575 shader_addline(buffer, "#version 120\n");
3577 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3578 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3580 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3581 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3582 * drivers write a warning if we don't do so
3584 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3587 /* Base Declarations */
3588 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3590 /* Pack 3.0 inputs */
3591 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
3593 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
3594 pshader_glsl_input_pack(buffer, This->semantics_in, iface, pretransformed);
3595 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
3596 pshader_glsl_input_pack(buffer, This->semantics_in, iface, fixedfunction);
3600 /* Base Shader Body */
3601 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3603 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3604 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
3605 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3606 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3607 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3608 else
3609 shader_addline(buffer, "gl_FragColor = R0;\n");
3612 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3613 fragcolor = "gl_FragData[0]";
3614 } else {
3615 fragcolor = "gl_FragColor";
3617 if(((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
3618 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3619 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
3620 srgb_sub_high, srgb_sub_high, srgb_sub_high);
3621 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
3622 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
3623 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
3624 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
3625 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
3627 /* Pixel shader < 3.0 do not replace the fog stage.
3628 * This implements linear fog computation and blending.
3629 * TODO: non linear fog
3630 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3631 * -1/(e-s) and e/(e-s) respectively.
3633 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
3634 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3635 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
3638 shader_addline(buffer, "}\n");
3640 TRACE("Compiling shader object %u\n", shader_obj);
3641 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3642 GL_EXTCALL(glCompileShaderARB(shader_obj));
3643 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3645 /* Store the shader object */
3646 return shader_obj;
3649 static void shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
3650 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
3651 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3652 CONST DWORD *function = This->baseShader.function;
3653 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3655 /* Create the hw GLSL shader program and assign it as the shader->prgId */
3656 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3658 shader_addline(buffer, "#version 120\n");
3660 /* Base Declarations */
3661 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3663 /* Base Shader Body */
3664 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3666 /* Unpack 3.0 outputs */
3667 if (This->baseShader.hex_version >= WINED3DVS_VERSION(3,0)) {
3668 shader_addline(buffer, "order_ps_input(OUT);\n");
3669 } else {
3670 shader_addline(buffer, "order_ps_input();\n");
3673 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3674 if (!reg_maps->fog)
3675 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
3677 /* Write the final position.
3679 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3680 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3681 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3682 * contains 1.0 to allow a mad.
3684 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3685 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3687 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3689 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3690 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3691 * which is the same as z = z * 2 - w.
3693 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3695 shader_addline(buffer, "}\n");
3697 TRACE("Compiling shader object %u\n", shader_obj);
3698 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3699 GL_EXTCALL(glCompileShaderARB(shader_obj));
3700 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3702 /* Store the shader object */
3703 This->prgId = shader_obj;
3706 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
3708 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3709 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3710 * vs_nv_version which is based on NV_vertex_program.
3711 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3712 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3713 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3714 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3716 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3717 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
3718 else
3719 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
3720 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
3721 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
3723 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3724 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3725 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3726 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3727 * in max native instructions. Intel and others also offer the info in this extension but they
3728 * don't support GLSL (at least on Windows).
3730 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3731 * of instructions is 512 or less we have to do with ps2.0 hardware.
3732 * NOTE: ps3.0 hardware requires 512 or more instructions but ati and nvidia offer 'enough' (1024 vs 4096) on their most basic ps3.0 hardware.
3734 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3735 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
3736 else
3737 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
3739 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3740 * Direct3D minimum requirement.
3742 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3743 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3745 * The problem is that the refrast clamps temporary results in the shader to
3746 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3747 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3748 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3749 * offer a way to query this.
3751 pCaps->PixelShader1xMaxValue = 8.0;
3752 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
3755 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
3757 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
3759 TRACE("Checking support for fixup:\n");
3760 dump_color_fixup_desc(fixup);
3763 /* We support everything except YUV conversions. */
3764 if (!is_yuv_fixup(fixup))
3766 TRACE("[OK]\n");
3767 return TRUE;
3770 TRACE("[FAILED]\n");
3771 return FALSE;
3774 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
3776 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
3777 /* WINED3DSIH_ADD */ shader_glsl_arith,
3778 /* WINED3DSIH_BEM */ pshader_glsl_bem,
3779 /* WINED3DSIH_BREAK */ shader_glsl_break,
3780 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
3781 /* WINED3DSIH_BREAKP */ NULL,
3782 /* WINED3DSIH_CALL */ shader_glsl_call,
3783 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
3784 /* WINED3DSIH_CMP */ shader_glsl_cmp,
3785 /* WINED3DSIH_CND */ shader_glsl_cnd,
3786 /* WINED3DSIH_CRS */ shader_glsl_cross,
3787 /* WINED3DSIH_DCL */ NULL,
3788 /* WINED3DSIH_DEF */ NULL,
3789 /* WINED3DSIH_DEFB */ NULL,
3790 /* WINED3DSIH_DEFI */ NULL,
3791 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
3792 /* WINED3DSIH_DP3 */ shader_glsl_dot,
3793 /* WINED3DSIH_DP4 */ shader_glsl_dot,
3794 /* WINED3DSIH_DST */ shader_glsl_dst,
3795 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
3796 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
3797 /* WINED3DSIH_ELSE */ shader_glsl_else,
3798 /* WINED3DSIH_ENDIF */ shader_glsl_end,
3799 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
3800 /* WINED3DSIH_ENDREP */ shader_glsl_end,
3801 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
3802 /* WINED3DSIH_EXPP */ shader_glsl_expp,
3803 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
3804 /* WINED3DSIH_IF */ shader_glsl_if,
3805 /* WINED3DSIH_IFC */ shader_glsl_ifc,
3806 /* WINED3DSIH_LABEL */ shader_glsl_label,
3807 /* WINED3DSIH_LIT */ shader_glsl_lit,
3808 /* WINED3DSIH_LOG */ shader_glsl_log,
3809 /* WINED3DSIH_LOGP */ shader_glsl_log,
3810 /* WINED3DSIH_LOOP */ shader_glsl_loop,
3811 /* WINED3DSIH_LRP */ shader_glsl_lrp,
3812 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
3813 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
3814 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
3815 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
3816 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
3817 /* WINED3DSIH_MAD */ shader_glsl_mad,
3818 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
3819 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
3820 /* WINED3DSIH_MOV */ shader_glsl_mov,
3821 /* WINED3DSIH_MOVA */ shader_glsl_mov,
3822 /* WINED3DSIH_MUL */ shader_glsl_arith,
3823 /* WINED3DSIH_NOP */ NULL,
3824 /* WINED3DSIH_NRM */ shader_glsl_map2gl,
3825 /* WINED3DSIH_PHASE */ NULL,
3826 /* WINED3DSIH_POW */ shader_glsl_pow,
3827 /* WINED3DSIH_RCP */ shader_glsl_rcp,
3828 /* WINED3DSIH_REP */ shader_glsl_rep,
3829 /* WINED3DSIH_RET */ NULL,
3830 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
3831 /* WINED3DSIH_SETP */ NULL,
3832 /* WINED3DSIH_SGE */ shader_glsl_compare,
3833 /* WINED3DSIH_SGN */ shader_glsl_map2gl,
3834 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
3835 /* WINED3DSIH_SLT */ shader_glsl_compare,
3836 /* WINED3DSIH_SUB */ shader_glsl_arith,
3837 /* WINED3DSIH_TEX */ pshader_glsl_tex,
3838 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
3839 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
3840 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
3841 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
3842 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
3843 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
3844 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
3845 /* WINED3DSIH_TEXLDD */ NULL,
3846 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
3847 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
3848 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
3849 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
3850 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
3851 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
3852 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
3853 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
3854 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
3855 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
3856 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
3857 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
3858 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
3861 const shader_backend_t glsl_shader_backend = {
3862 shader_glsl_instruction_handler_table,
3863 shader_glsl_select,
3864 shader_glsl_select_depth_blt,
3865 shader_glsl_deselect_depth_blt,
3866 shader_glsl_load_constants,
3867 shader_glsl_cleanup,
3868 shader_glsl_color_correction,
3869 shader_glsl_destroy,
3870 shader_glsl_alloc,
3871 shader_glsl_free,
3872 shader_glsl_dirty_const,
3873 shader_glsl_generate_pshader,
3874 shader_glsl_generate_vshader,
3875 shader_glsl_get_caps,
3876 shader_glsl_color_fixup_supported,