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.
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)
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;
80 IWineD3DPixelShader *pshader;
81 struct ps_compile_args ps_args;
86 IWineD3DPixelShader *pshader;
87 struct ps_compile_args ps_args;
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;
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,
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));
127 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
128 if(strcmp(infoLog, spam[i]) == 0) {
134 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
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;
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");
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];
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");
185 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
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;
205 if (TRACE_ON(d3d_shader)) {
206 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
211 tmp_loc = constant_locations[i];
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)) {
226 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
231 tmp_loc = constant_locations[i];
233 /* We found this uniform name in the program - go ahead and send the data */
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));
253 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
258 tmp_loc = constant_locations[i];
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");
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];
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];
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)
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);
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)
338 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
339 const char* prefix = is_pshader? "PB":"VB";
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));
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);
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));
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,
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;
404 /* No GLSL program set - nothing to do. */
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;
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);
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
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);
538 shader_addline(buffer, "void order_ps_input();\n");
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]) {
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++;
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++;
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;
597 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
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);
604 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
607 case WINED3DSTT_CUBE:
608 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
610 case WINED3DSTT_VOLUME:
611 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
614 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
615 FIXME("Unrecognized sampler type: %#x\n", stype);
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)) {
638 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
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 ****************************************************************************/
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[] = {
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) */
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 (
740 const char *in_regswizzle,
745 if (instr == WINED3DSIO_TEXKILL)
748 switch (instr & WINED3DSP_SRCMOD_MASK) {
749 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
751 case WINED3DSPSM_NONE:
752 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
754 case WINED3DSPSM_NEG:
755 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
757 case WINED3DSPSM_NOT:
758 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
760 case WINED3DSPSM_BIAS:
761 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
763 case WINED3DSPSM_BIASNEG:
764 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
766 case WINED3DSPSM_SIGN:
767 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
769 case WINED3DSPSM_SIGNNEG:
770 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
772 case WINED3DSPSM_COMP:
773 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
776 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
778 case WINED3DSPSM_X2NEG:
779 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
781 case WINED3DSPSM_ABS:
782 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
784 case WINED3DSPSM_ABSNEG:
785 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
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);
813 case WINED3DSPR_TEMP:
814 sprintf(tmpStr, "R%u", reg);
816 case WINED3DSPR_INPUT:
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
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]);
836 sprintf(tmpStr, "IN[%s + %u]", rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
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);
845 sprintf(tmpStr, "IN[%s]", rel_param.param_str);
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");
855 sprintf(tmpStr, "IN[%u]", idx);
860 strcpy(tmpStr, "gl_Color");
862 strcpy(tmpStr, "gl_SecondaryColor");
865 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
867 sprintf(tmpStr, "attrib%u", reg);
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);
883 sprintf(tmpStr, "%cC[%s + %u]", prefix, rel_param.param_str, reg);
885 sprintf(tmpStr, "%cC[%s]", prefix, rel_param.param_str);
889 sprintf(tmpStr, "%cC[A0.x + %u]", prefix, reg);
891 sprintf(tmpStr, "%cC[A0.x]", prefix);
896 if(shader_constant_is_local(This, reg)) {
897 sprintf(tmpStr, "%cLC%u", prefix, reg);
899 sprintf(tmpStr, "%cC[%u]", prefix, reg);
905 case WINED3DSPR_CONSTINT:
907 sprintf(tmpStr, "PI[%u]", reg);
909 sprintf(tmpStr, "VI[%u]", reg);
911 case WINED3DSPR_CONSTBOOL:
913 sprintf(tmpStr, "PB[%u]", reg);
915 sprintf(tmpStr, "VB[%u]", reg);
917 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
919 sprintf(tmpStr, "T%u", reg);
921 sprintf(tmpStr, "A%u", reg);
924 case WINED3DSPR_LOOP:
925 sprintf(tmpStr, "aL%u", This->baseShader.cur_loop_regno - 1);
927 case WINED3DSPR_SAMPLER:
929 sprintf(tmpStr, "Psampler%u", reg);
931 sprintf(tmpStr, "Vsampler%u", reg);
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");
943 case WINED3DSPR_RASTOUT:
944 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
946 case WINED3DSPR_DEPTHOUT:
947 sprintf(tmpStr, "gl_FragDepth");
949 case WINED3DSPR_ATTROUT:
951 sprintf(tmpStr, "gl_FrontColor");
953 sprintf(tmpStr, "gl_FrontSecondaryColor");
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);
961 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
963 case WINED3DSPR_MISCTYPE:
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)");
973 FIXME("Unhandled misctype register %d\n", reg);
974 sprintf(tmpStr, "unrecognized_register");
978 FIXME("Unhandled register name Type(%d)\n", regtype);
979 sprintf(tmpStr, "unrecognized_register");
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;
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';
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;
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)) {
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];
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;
1078 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
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]);
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;
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 "<=";
1130 FIXME("Unrecognized comparison value: %u\n", op);
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) {
1139 sample_function->name = projected ? "texture1DProj" : "texture1D";
1140 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1144 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1146 sample_function->name = projected ? "texture2DProj" : "texture2D";
1148 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1150 case WINED3DSTT_CUBE:
1151 sample_function->name = "textureCube";
1152 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
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;
1159 sample_function->name = "";
1160 sample_function->coord_mask = 0;
1161 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
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");
1175 case CHANNEL_SOURCE_ONE:
1176 strcat(arguments, "1.0");
1179 case CHANNEL_SOURCE_X:
1180 strcat(arguments, reg_name);
1181 strcat(arguments, ".x");
1184 case CHANNEL_SOURCE_Y:
1185 strcat(arguments, reg_name);
1186 strcat(arguments, ".y");
1189 case CHANNEL_SOURCE_Z:
1190 strcat(arguments, reg_name);
1191 strcat(arguments, ".z");
1194 case CHANNEL_SOURCE_W:
1195 strcat(arguments, reg_name);
1196 strcat(arguments, ".w");
1200 FIXME("Unhandled channel source %#x\n", channel_source);
1201 strcat(arguments, "undefined");
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];
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;
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);
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, ", ");
1265 shader_addline(arg->buffer, "%s%s = vec%u(%s);\n",
1266 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
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;
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;
1297 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
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;
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);
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);
1336 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
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;
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);
1367 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
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;
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);
1407 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
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);
1429 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
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];
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);
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)
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)) {
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);
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);
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;
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);
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;
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);
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;
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);
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
1593 shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
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);
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;
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);
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++) {
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;
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;
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);
1703 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1704 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1708 /* Cycle through all source0 channels */
1709 for (i=0; i<4; i++) {
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;
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)
1751 int nComponents = 0;
1752 SHADER_OPCODE_ARG tmpArg;
1754 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1756 /* Set constants for the temporary argument */
1757 tmpArg.shader = arg->shader;
1758 tmpArg.buffer = arg->buffer;
1759 tmpArg.src[0] = arg->src[0];
1760 tmpArg.src_addr[0] = arg->src_addr[0];
1761 tmpArg.src_addr[1] = arg->src_addr[1];
1762 tmpArg.reg_maps = arg->reg_maps;
1764 switch(arg->opcode->opcode) {
1765 case WINED3DSIO_M4x4:
1767 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1769 case WINED3DSIO_M4x3:
1771 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1773 case WINED3DSIO_M3x4:
1775 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1777 case WINED3DSIO_M3x3:
1779 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1781 case WINED3DSIO_M3x2:
1783 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1789 for (i = 0; i < nComponents; i++) {
1790 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1791 tmpArg.src[1] = arg->src[1]+i;
1792 shader_glsl_dot(&tmpArg);
1797 The LRP instruction performs a component-wise linear interpolation
1798 between the second and third operands using the first operand as the
1799 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1800 This is equivalent to mix(src2, src1, src0);
1802 static void shader_glsl_lrp(const SHADER_OPCODE_ARG *arg)
1804 glsl_src_param_t src0_param;
1805 glsl_src_param_t src1_param;
1806 glsl_src_param_t src2_param;
1809 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1811 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1812 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1813 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1815 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1816 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1819 /** Process the WINED3DSIO_LIT instruction in GLSL:
1820 * dst.x = dst.w = 1.0
1821 * dst.y = (src0.x > 0) ? src0.x
1822 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1823 * where src.w is clamped at +- 128
1825 static void shader_glsl_lit(const SHADER_OPCODE_ARG *arg)
1827 glsl_src_param_t src0_param;
1828 glsl_src_param_t src1_param;
1829 glsl_src_param_t src3_param;
1832 shader_glsl_append_dst(arg->buffer, arg);
1833 shader_glsl_get_write_mask(arg->dst, dst_mask);
1835 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1836 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1837 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1839 /* The sdk specifies the instruction like this
1841 * if(src.x > 0.0) dst.y = src.x
1843 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1847 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1848 * dst.x = 1.0 ... No further explanation needed
1849 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1850 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1851 * dst.w = 1.0. ... Nothing fancy.
1853 * So we still have one conditional in there. So do this:
1854 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1856 * 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),
1857 * 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.
1858 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1860 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",
1861 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
1864 /** Process the WINED3DSIO_DST instruction in GLSL:
1866 * dst.y = src0.x * src0.y
1870 static void shader_glsl_dst(const SHADER_OPCODE_ARG *arg)
1872 glsl_src_param_t src0y_param;
1873 glsl_src_param_t src0z_param;
1874 glsl_src_param_t src1y_param;
1875 glsl_src_param_t src1w_param;
1878 shader_glsl_append_dst(arg->buffer, arg);
1879 shader_glsl_get_write_mask(arg->dst, dst_mask);
1881 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1882 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1883 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1884 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1886 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1887 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1890 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1891 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1892 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1894 * dst.x = cos(src0.?)
1895 * dst.y = sin(src0.?)
1899 static void shader_glsl_sincos(const SHADER_OPCODE_ARG *arg)
1901 glsl_src_param_t src0_param;
1904 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1905 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1907 switch (write_mask) {
1908 case WINED3DSP_WRITEMASK_0:
1909 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1912 case WINED3DSP_WRITEMASK_1:
1913 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1916 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1917 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1921 ERR("Write mask should be .x, .y or .xy\n");
1926 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1927 * Start a for() loop where src1.y is the initial value of aL,
1928 * increment aL by src1.z for a total of src1.x iterations.
1929 * Need to use a temporary variable for this operation.
1931 /* FIXME: I don't think nested loops will work correctly this way. */
1932 static void shader_glsl_loop(const SHADER_OPCODE_ARG *arg)
1934 glsl_src_param_t src1_param;
1935 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1936 DWORD regtype = shader_get_regtype(arg->src[1]);
1937 DWORD reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1938 const DWORD *control_values = NULL;
1939 const local_constant *constant;
1941 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1943 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
1944 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
1945 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
1948 if(regtype == WINED3DSPR_CONSTINT) {
1949 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
1950 if(constant->idx == reg) {
1951 control_values = constant->value;
1957 if(control_values) {
1958 if(control_values[2] > 0) {
1959 shader_addline(arg->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
1960 shader->baseShader.cur_loop_depth, control_values[1],
1961 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
1962 shader->baseShader.cur_loop_depth, control_values[2]);
1963 } else if(control_values[2] == 0) {
1964 shader_addline(arg->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
1965 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
1966 shader->baseShader.cur_loop_depth, control_values[0],
1967 shader->baseShader.cur_loop_depth);
1969 shader_addline(arg->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
1970 shader->baseShader.cur_loop_depth, control_values[1],
1971 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
1972 shader->baseShader.cur_loop_depth, control_values[2]);
1975 shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
1976 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
1977 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
1978 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
1981 shader->baseShader.cur_loop_depth++;
1982 shader->baseShader.cur_loop_regno++;
1985 static void shader_glsl_end(const SHADER_OPCODE_ARG *arg)
1987 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1989 shader_addline(arg->buffer, "}\n");
1991 if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
1992 shader->baseShader.cur_loop_depth--;
1993 shader->baseShader.cur_loop_regno--;
1995 if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
1996 shader->baseShader.cur_loop_depth--;
2000 static void shader_glsl_rep(const SHADER_OPCODE_ARG *arg)
2002 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2003 glsl_src_param_t src0_param;
2005 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2006 shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2007 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2008 src0_param.param_str, shader->baseShader.cur_loop_depth);
2009 shader->baseShader.cur_loop_depth++;
2012 static void shader_glsl_if(const SHADER_OPCODE_ARG *arg)
2014 glsl_src_param_t src0_param;
2016 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2017 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
2020 static void shader_glsl_ifc(const SHADER_OPCODE_ARG *arg)
2022 glsl_src_param_t src0_param;
2023 glsl_src_param_t src1_param;
2025 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2026 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2028 shader_addline(arg->buffer, "if (%s %s %s) {\n",
2029 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2032 static void shader_glsl_else(const SHADER_OPCODE_ARG *arg)
2034 shader_addline(arg->buffer, "} else {\n");
2037 static void shader_glsl_break(const SHADER_OPCODE_ARG *arg)
2039 shader_addline(arg->buffer, "break;\n");
2042 /* FIXME: According to MSDN the compare is done per component. */
2043 static void shader_glsl_breakc(const SHADER_OPCODE_ARG *arg)
2045 glsl_src_param_t src0_param;
2046 glsl_src_param_t src1_param;
2048 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2049 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2051 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
2052 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2055 static void shader_glsl_label(const SHADER_OPCODE_ARG *arg)
2058 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2059 shader_addline(arg->buffer, "}\n");
2060 shader_addline(arg->buffer, "void subroutine%u () {\n", snum);
2063 static void shader_glsl_call(const SHADER_OPCODE_ARG *arg)
2065 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2066 shader_addline(arg->buffer, "subroutine%u();\n", snum);
2069 static void shader_glsl_callnz(const SHADER_OPCODE_ARG *arg)
2071 glsl_src_param_t src1_param;
2073 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2074 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2075 shader_addline(arg->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2078 /*********************************************
2079 * Pixel Shader Specific Code begins here
2080 ********************************************/
2081 static void pshader_glsl_tex(const SHADER_OPCODE_ARG *arg)
2083 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2084 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2085 DWORD hex_version = This->baseShader.hex_version;
2086 char dst_swizzle[6];
2087 glsl_sample_function_t sample_function;
2090 BOOL projected, texrect = FALSE;
2093 /* All versions have a destination register */
2094 shader_glsl_append_dst(arg->buffer, arg);
2096 /* 1.0-1.4: Use destination register as sampler source.
2097 * 2.0+: Use provided sampler source. */
2098 if (hex_version < WINED3DPS_VERSION(2,0)) {
2099 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2101 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2103 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2105 if (hex_version < WINED3DPS_VERSION(1,4)) {
2106 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2108 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2109 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2111 switch (flags & ~WINED3DTTFF_PROJECTED) {
2112 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2113 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2114 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2115 case WINED3DTTFF_COUNT4:
2116 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2121 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
2122 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2124 if (src_mod == WINED3DSPSM_DZ) {
2126 mask = WINED3DSP_WRITEMASK_2;
2127 } else if (src_mod == WINED3DSPSM_DW) {
2129 mask = WINED3DSP_WRITEMASK_3;
2134 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
2135 /* ps 2.0 texldp instruction always divides by the fourth component. */
2137 mask = WINED3DSP_WRITEMASK_3;
2143 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2144 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2148 shader_glsl_get_sample_function(sampler_type, projected, texrect, &sample_function);
2149 mask |= sample_function.coord_mask;
2151 if (hex_version < WINED3DPS_VERSION(2,0)) {
2152 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2154 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2157 /* 1.0-1.3: Use destination register as coordinate source.
2158 1.4+: Use provided coordinate source register. */
2159 if (hex_version < WINED3DPS_VERSION(1,4)) {
2161 shader_glsl_get_write_mask(mask, coord_mask);
2162 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
2163 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
2165 glsl_src_param_t coord_param;
2166 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
2167 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
2168 glsl_src_param_t bias;
2169 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2171 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2172 sample_function.name, sampler_idx, coord_param.param_str,
2173 bias.param_str, dst_swizzle);
2175 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2176 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2181 static void shader_glsl_texldl(const SHADER_OPCODE_ARG *arg)
2183 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2184 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2185 glsl_sample_function_t sample_function;
2186 glsl_src_param_t coord_param, lod_param;
2187 char dst_swizzle[6];
2190 BOOL texrect = FALSE;
2192 shader_glsl_append_dst(arg->buffer, arg);
2193 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2195 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2196 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2197 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2198 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2201 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);
2203 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2205 if (shader_is_pshader_version(This->baseShader.hex_version)) {
2206 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2207 * However, they seem to work just fine in fragment shaders as well. */
2208 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2209 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2210 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2212 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2213 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2217 static void pshader_glsl_texcoord(const SHADER_OPCODE_ARG *arg)
2219 /* FIXME: Make this work for more than just 2D textures */
2221 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2222 SHADER_BUFFER* buffer = arg->buffer;
2223 DWORD hex_version = This->baseShader.hex_version;
2227 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2228 shader_glsl_get_write_mask(write_mask, dst_mask);
2230 if (hex_version != WINED3DPS_VERSION(1,4)) {
2231 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2232 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2234 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2235 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2236 char dst_swizzle[6];
2238 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2240 if (src_mod == WINED3DSPSM_DZ) {
2241 glsl_src_param_t div_param;
2242 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2243 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2245 if (mask_size > 1) {
2246 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2248 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2250 } else if (src_mod == WINED3DSPSM_DW) {
2251 glsl_src_param_t div_param;
2252 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2253 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2255 if (mask_size > 1) {
2256 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2258 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2261 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2266 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2267 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2268 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2269 static void pshader_glsl_texdp3tex(const SHADER_OPCODE_ARG *arg)
2271 glsl_src_param_t src0_param;
2273 glsl_sample_function_t sample_function;
2274 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2275 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2276 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2278 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2280 shader_glsl_append_dst(arg->buffer, arg);
2281 shader_glsl_get_write_mask(arg->dst, dst_mask);
2283 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2284 * scalar, and projected sampling would require 4.
2286 * It is a dependent read - not valid with conditional NP2 textures
2288 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2290 switch(count_bits(sample_function.coord_mask)) {
2292 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2293 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2297 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2298 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2302 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2303 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2306 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2310 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2311 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2312 static void pshader_glsl_texdp3(const SHADER_OPCODE_ARG *arg)
2314 glsl_src_param_t src0_param;
2315 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2316 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2318 unsigned int mask_size;
2320 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2321 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2322 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2324 if (mask_size > 1) {
2325 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2327 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2331 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2332 * Calculate the depth as dst.x / dst.y */
2333 static void pshader_glsl_texdepth(const SHADER_OPCODE_ARG *arg)
2335 glsl_dst_param_t dst_param;
2337 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2339 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2340 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2341 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2342 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2345 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);
2348 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2349 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2350 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2351 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2353 static void pshader_glsl_texm3x2depth(const SHADER_OPCODE_ARG *arg)
2355 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2356 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2357 glsl_src_param_t src0_param;
2359 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2361 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2362 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2365 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2366 * Calculate the 1st of a 2-row matrix multiplication. */
2367 static void pshader_glsl_texm3x2pad(const SHADER_OPCODE_ARG *arg)
2369 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2370 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2371 SHADER_BUFFER* buffer = arg->buffer;
2372 glsl_src_param_t src0_param;
2374 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2375 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2378 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2379 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2380 static void pshader_glsl_texm3x3pad(const SHADER_OPCODE_ARG* arg)
2382 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2383 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2384 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2385 SHADER_BUFFER* buffer = arg->buffer;
2386 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2387 glsl_src_param_t src0_param;
2389 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2390 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2391 current_state->texcoord_w[current_state->current_row++] = reg;
2394 static void pshader_glsl_texm3x2tex(const SHADER_OPCODE_ARG *arg)
2396 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2397 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2398 SHADER_BUFFER* buffer = arg->buffer;
2399 glsl_src_param_t src0_param;
2402 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2403 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2405 shader_glsl_append_dst(buffer, arg);
2406 shader_glsl_get_write_mask(arg->dst, dst_mask);
2408 /* Sample the texture using the calculated coordinates */
2409 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2412 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2413 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2414 static void pshader_glsl_texm3x3tex(const SHADER_OPCODE_ARG *arg)
2416 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2417 glsl_src_param_t src0_param;
2419 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2420 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2421 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2422 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2423 glsl_sample_function_t sample_function;
2425 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2426 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2428 shader_glsl_append_dst(arg->buffer, arg);
2429 shader_glsl_get_write_mask(arg->dst, dst_mask);
2430 /* Dependent read, not valid with conditional NP2 */
2431 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2433 /* Sample the texture using the calculated coordinates */
2434 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2436 current_state->current_row = 0;
2439 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2440 * Perform the 3rd row of a 3x3 matrix multiply */
2441 static void pshader_glsl_texm3x3(const SHADER_OPCODE_ARG *arg)
2443 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2444 glsl_src_param_t src0_param;
2446 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2447 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2448 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2450 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2452 shader_glsl_append_dst(arg->buffer, arg);
2453 shader_glsl_get_write_mask(arg->dst, dst_mask);
2454 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2456 current_state->current_row = 0;
2459 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2460 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2461 static void pshader_glsl_texm3x3spec(const SHADER_OPCODE_ARG *arg)
2463 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2464 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2465 glsl_src_param_t src0_param;
2466 glsl_src_param_t src1_param;
2468 SHADER_BUFFER* buffer = arg->buffer;
2469 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2470 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2471 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2472 glsl_sample_function_t sample_function;
2474 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2475 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2477 /* Perform the last matrix multiply operation */
2478 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2479 /* Reflection calculation */
2480 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2482 shader_glsl_append_dst(buffer, arg);
2483 shader_glsl_get_write_mask(arg->dst, dst_mask);
2484 /* Dependent read, not valid with conditional NP2 */
2485 shader_glsl_get_sample_function(stype, FALSE, FALSE, &sample_function);
2487 /* Sample the texture */
2488 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2490 current_state->current_row = 0;
2493 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2494 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2495 static void pshader_glsl_texm3x3vspec(const SHADER_OPCODE_ARG *arg)
2497 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2498 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2499 SHADER_BUFFER* buffer = arg->buffer;
2500 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2501 glsl_src_param_t src0_param;
2503 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2504 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2505 glsl_sample_function_t sample_function;
2507 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2509 /* Perform the last matrix multiply operation */
2510 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2512 /* Construct the eye-ray vector from w coordinates */
2513 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2514 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2515 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2517 shader_glsl_append_dst(buffer, arg);
2518 shader_glsl_get_write_mask(arg->dst, dst_mask);
2519 /* Dependent read, not valid with conditional NP2 */
2520 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2522 /* Sample the texture using the calculated coordinates */
2523 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2525 current_state->current_row = 0;
2528 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2529 * Apply a fake bump map transform.
2530 * texbem is pshader <= 1.3 only, this saves a few version checks
2532 static void pshader_glsl_texbem(const SHADER_OPCODE_ARG *arg)
2534 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2535 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2536 char dst_swizzle[6];
2537 glsl_sample_function_t sample_function;
2538 glsl_src_param_t coord_param;
2545 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2546 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2548 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2549 /* Dependent read, not valid with conditional NP2 */
2550 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2551 mask = sample_function.coord_mask;
2553 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2555 shader_glsl_get_write_mask(mask, coord_mask);
2557 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2558 * so we can't let the GL handle this.
2560 if (flags & WINED3DTTFF_PROJECTED) {
2562 char coord_div_mask[3];
2563 switch (flags & ~WINED3DTTFF_PROJECTED) {
2564 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2565 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2566 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2567 case WINED3DTTFF_COUNT4:
2568 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2570 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2571 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2574 shader_glsl_append_dst(arg->buffer, arg);
2575 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2576 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2577 glsl_src_param_t luminance_param;
2578 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2579 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",
2580 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
2581 luminance_param.param_str, sampler_idx, sampler_idx, dst_swizzle);
2583 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2584 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
2588 static void pshader_glsl_bem(const SHADER_OPCODE_ARG *arg)
2590 glsl_src_param_t src0_param, src1_param;
2591 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2593 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2594 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2596 shader_glsl_append_dst(arg->buffer, arg);
2597 shader_addline(arg->buffer, "%s + bumpenvmat%d * %s);\n",
2598 src0_param.param_str, sampler_idx, src1_param.param_str);
2601 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2602 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2603 static void pshader_glsl_texreg2ar(const SHADER_OPCODE_ARG *arg)
2605 glsl_src_param_t src0_param;
2606 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2609 shader_glsl_append_dst(arg->buffer, arg);
2610 shader_glsl_get_write_mask(arg->dst, dst_mask);
2611 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2613 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2616 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2617 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2618 static void pshader_glsl_texreg2gb(const SHADER_OPCODE_ARG *arg)
2620 glsl_src_param_t src0_param;
2621 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2624 shader_glsl_append_dst(arg->buffer, arg);
2625 shader_glsl_get_write_mask(arg->dst, dst_mask);
2626 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2628 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2631 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2632 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2633 static void pshader_glsl_texreg2rgb(const SHADER_OPCODE_ARG *arg)
2635 glsl_src_param_t src0_param;
2637 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2638 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2639 glsl_sample_function_t sample_function;
2641 shader_glsl_append_dst(arg->buffer, arg);
2642 shader_glsl_get_write_mask(arg->dst, dst_mask);
2643 /* Dependent read, not valid with conditional NP2 */
2644 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2645 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2647 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2650 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2651 * If any of the first 3 components are < 0, discard this pixel */
2652 static void pshader_glsl_texkill(const SHADER_OPCODE_ARG *arg)
2654 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2655 DWORD hex_version = This->baseShader.hex_version;
2656 glsl_dst_param_t dst_param;
2658 /* The argument is a destination parameter, and no writemasks are allowed */
2659 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2660 if((hex_version >= WINED3DPS_VERSION(2,0))) {
2661 /* 2.0 shaders compare all 4 components in texkill */
2662 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2664 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2665 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2666 * 4 components are defined, only the first 3 are used
2668 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2672 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2673 * dst = dot2(src0, src1) + src2 */
2674 static void pshader_glsl_dp2add(const SHADER_OPCODE_ARG *arg)
2676 glsl_src_param_t src0_param;
2677 glsl_src_param_t src1_param;
2678 glsl_src_param_t src2_param;
2680 unsigned int mask_size;
2682 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2683 mask_size = shader_glsl_get_write_mask_size(write_mask);
2685 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2686 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2687 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2689 if (mask_size > 1) {
2690 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);
2692 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2696 static void pshader_glsl_input_pack(SHADER_BUFFER* buffer, const struct semantic* semantics_in,
2697 IWineD3DPixelShader *iface, enum vertexprocessing_mode vertexprocessing)
2700 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
2702 for (i = 0; i < MAX_REG_INPUT; i++) {
2704 DWORD usage_token = semantics_in[i].usage;
2705 DWORD register_token = semantics_in[i].reg;
2706 DWORD usage, usage_idx;
2710 if (!usage_token) continue;
2711 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2712 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2713 shader_glsl_get_write_mask(register_token, reg_mask);
2717 case WINED3DDECLUSAGE_TEXCOORD:
2718 if(usage_idx < 8 && vertexprocessing == pretransformed) {
2719 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2720 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
2722 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2723 This->input_reg_map[i], reg_mask, reg_mask);
2727 case WINED3DDECLUSAGE_COLOR:
2729 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
2730 This->input_reg_map[i], reg_mask, reg_mask);
2731 else if (usage_idx == 1)
2732 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2733 This->input_reg_map[i], reg_mask, reg_mask);
2735 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2736 This->input_reg_map[i], reg_mask, reg_mask);
2740 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2741 This->input_reg_map[i], reg_mask, reg_mask);
2746 /*********************************************
2747 * Vertex Shader Specific Code begins here
2748 ********************************************/
2750 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
2751 glsl_program_key_t *key;
2753 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2754 key->vshader = entry->vshader;
2755 key->pshader = entry->pshader;
2756 key->ps_args = entry->ps_args;
2758 hash_table_put(priv->glsl_program_lookup, key, entry);
2761 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
2762 GLhandleARB vshader, IWineD3DPixelShader *pshader, struct ps_compile_args *ps_args) {
2763 glsl_program_key_t key;
2765 key.vshader = vshader;
2766 key.pshader = pshader;
2767 key.ps_args = *ps_args;
2769 return (struct glsl_shader_prog_link *)hash_table_get(priv->glsl_program_lookup, &key);
2772 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
2773 struct glsl_shader_prog_link *entry)
2775 glsl_program_key_t *key;
2777 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2778 key->vshader = entry->vshader;
2779 key->pshader = entry->pshader;
2780 key->ps_args = entry->ps_args;
2781 hash_table_remove(priv->glsl_program_lookup, key);
2783 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2784 if (entry->vshader) list_remove(&entry->vshader_entry);
2785 if (entry->pshader) list_remove(&entry->pshader_entry);
2786 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2787 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2788 HeapFree(GetProcessHeap(), 0, entry);
2791 static void handle_ps3_input(SHADER_BUFFER *buffer, const struct semantic *semantics_in,
2792 const struct semantic *semantics_out, const WineD3D_GL_Info *gl_info, const DWORD *map)
2795 DWORD usage_token, usage_token_out;
2796 DWORD register_token, register_token_out;
2797 DWORD usage, usage_idx, usage_out, usage_idx_out;
2800 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
2801 char reg_mask[6], reg_mask_out[6];
2802 char destination[50];
2804 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
2806 if (!semantics_out) {
2807 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
2808 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
2809 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
2812 for(i = 0; i < MAX_REG_INPUT; i++) {
2813 usage_token = semantics_in[i].usage;
2814 if (!usage_token) continue;
2817 if (in_idx >= (in_count + 2)) {
2818 FIXME("More input varyings declared than supported, expect issues\n");
2820 } else if(map[i] == -1) {
2821 /* Declared, but not read register */
2825 if (in_idx == in_count) {
2826 sprintf(destination, "gl_FrontColor");
2827 } else if (in_idx == in_count + 1) {
2828 sprintf(destination, "gl_FrontSecondaryColor");
2830 sprintf(destination, "IN[%u]", in_idx);
2833 register_token = semantics_in[i].reg;
2835 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2836 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2837 set[map[i]] = shader_glsl_get_write_mask(register_token, reg_mask);
2839 if(!semantics_out) {
2841 case WINED3DDECLUSAGE_COLOR:
2843 shader_addline(buffer, "%s%s = front_color%s;\n",
2844 destination, reg_mask, reg_mask);
2845 else if (usage_idx == 1)
2846 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
2847 destination, reg_mask, reg_mask);
2849 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2850 destination, reg_mask, reg_mask);
2853 case WINED3DDECLUSAGE_TEXCOORD:
2854 if (usage_idx < 8) {
2855 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
2856 destination, reg_mask, usage_idx, reg_mask);
2858 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2859 destination, reg_mask, reg_mask);
2863 case WINED3DDECLUSAGE_FOG:
2864 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2865 destination, reg_mask, reg_mask);
2869 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2870 destination, reg_mask, reg_mask);
2874 for(j = 0; j < MAX_REG_OUTPUT; j++) {
2875 usage_token_out = semantics_out[j].usage;
2876 if (!usage_token_out) continue;
2877 register_token_out = semantics_out[j].reg;
2879 usage_out = (usage_token_out & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2880 usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2881 shader_glsl_get_write_mask(register_token_out, reg_mask_out);
2883 if(usage == usage_out &&
2884 usage_idx == usage_idx_out) {
2885 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
2886 destination, reg_mask, j, reg_mask);
2891 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2892 destination, reg_mask, reg_mask);
2897 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2898 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2899 * input varyings are assigned above, if the optimizer works properly.
2901 for(i = 0; i < in_count + 2; i++) {
2902 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
2903 unsigned int size = 0;
2904 memset(reg_mask, 0, sizeof(reg_mask));
2905 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
2906 reg_mask[size] = 'x';
2909 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
2910 reg_mask[size] = 'y';
2913 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
2914 reg_mask[size] = 'z';
2917 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
2918 reg_mask[size] = 'w';
2922 if (i == in_count) {
2923 sprintf(destination, "gl_FrontColor");
2924 } else if (i == in_count + 1) {
2925 sprintf(destination, "gl_FrontSecondaryColor");
2927 sprintf(destination, "IN[%u]", i);
2931 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
2933 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
2938 HeapFree(GetProcessHeap(), 0, set);
2941 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
2942 IWineD3DPixelShader *pixelshader, const WineD3D_GL_Info *gl_info)
2944 GLhandleARB ret = 0;
2945 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
2946 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
2947 IWineD3DDeviceImpl *device;
2948 DWORD vs_major = WINED3DSHADER_VERSION_MAJOR(vs->baseShader.hex_version);
2949 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.hex_version) : 0;
2951 SHADER_BUFFER buffer;
2953 DWORD register_token;
2954 DWORD usage, usage_idx, writemask;
2956 const struct semantic *semantics_out, *semantics_in;
2958 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
2961 buffer.newline = TRUE;
2963 shader_addline(&buffer, "#version 120\n");
2965 if(vs_major < 3 && ps_major < 3) {
2966 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
2967 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
2969 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
2970 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
2971 !device->frag_pipe->ffp_proj_control) {
2972 shader_addline(&buffer, "void order_ps_input() {\n");
2973 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
2974 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
2975 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
2976 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
2979 shader_addline(&buffer, "}\n");
2981 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
2983 } else if(ps_major < 3 && vs_major >= 3) {
2984 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
2985 semantics_out = vs->semantics_out;
2987 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
2988 for(i = 0; i < MAX_REG_OUTPUT; i++) {
2989 usage_token = semantics_out[i].usage;
2990 if (!usage_token) continue;
2991 register_token = semantics_out[i].reg;
2993 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2994 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2995 writemask = shader_glsl_get_write_mask(register_token, reg_mask);
2998 case WINED3DDECLUSAGE_COLOR:
3000 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3001 else if (usage_idx == 1)
3002 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3005 case WINED3DDECLUSAGE_POSITION:
3006 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3009 case WINED3DDECLUSAGE_TEXCOORD:
3010 if (usage_idx < 8) {
3011 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3013 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3014 usage_idx, reg_mask, i, reg_mask);
3015 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3016 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3021 case WINED3DDECLUSAGE_PSIZE:
3022 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3025 case WINED3DDECLUSAGE_FOG:
3026 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3033 shader_addline(&buffer, "}\n");
3035 } else if(ps_major >= 3 && vs_major >= 3) {
3036 semantics_out = vs->semantics_out;
3037 semantics_in = ps->semantics_in;
3039 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3040 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3041 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3043 /* First, sort out position and point size. Those are not passed to the pixel shader */
3044 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3045 usage_token = semantics_out[i].usage;
3046 if (!usage_token) continue;
3047 register_token = semantics_out[i].reg;
3049 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3050 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3051 shader_glsl_get_write_mask(register_token, reg_mask);
3054 case WINED3DDECLUSAGE_POSITION:
3055 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3058 case WINED3DDECLUSAGE_PSIZE:
3059 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3067 /* Then, fix the pixel shader input */
3068 handle_ps3_input(&buffer, semantics_in, semantics_out, gl_info, ps->input_reg_map);
3070 shader_addline(&buffer, "}\n");
3071 } else if(ps_major >= 3 && vs_major < 3) {
3072 semantics_in = ps->semantics_in;
3074 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3075 shader_addline(&buffer, "void order_ps_input() {\n");
3076 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3077 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3078 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3080 handle_ps3_input(&buffer, semantics_in, NULL, gl_info, ps->input_reg_map);
3081 shader_addline(&buffer, "}\n");
3083 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3086 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3087 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3088 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3089 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3090 GL_EXTCALL(glCompileShaderARB(ret));
3091 checkGLcall("glCompileShaderARB(ret)");
3093 HeapFree(GetProcessHeap(), 0, buffer.buffer);
3097 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
3098 GLhandleARB programId, char prefix)
3100 const local_constant *lconst;
3105 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3106 value = (const float *)lconst->value;
3107 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3108 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3109 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3111 checkGLcall("Hardcoding local constants\n");
3114 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3115 * It sets the programId on the current StateBlock (because it should be called
3116 * inside of the DrawPrimitive() part of the render loop).
3118 * If a program for the given combination does not exist, create one, and store
3119 * the program in the hash table. If it creates a program, it will link the
3120 * given objects, too.
3122 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3123 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3124 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3125 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3126 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3127 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3128 struct glsl_shader_prog_link *entry = NULL;
3129 GLhandleARB programId = 0;
3130 GLhandleARB reorder_shader_id = 0;
3133 GLhandleARB vshader_id, pshader_id;
3134 struct ps_compile_args compile_args;
3137 IWineD3DVertexShaderImpl_CompileShader(vshader);
3138 vshader_id = ((IWineD3DVertexShaderImpl*)vshader)->prgId;
3143 find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &compile_args);
3145 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3146 memset(&compile_args, 0, sizeof(compile_args));
3148 entry = get_glsl_program_entry(priv, vshader_id, pshader, &compile_args);
3150 priv->glsl_program = entry;
3154 /* If we get to this point, then no matching program exists, so we create one */
3155 programId = GL_EXTCALL(glCreateProgramObjectARB());
3156 TRACE("Created new GLSL shader program %u\n", programId);
3158 /* Create the entry */
3159 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3160 entry->programId = programId;
3161 entry->vshader = vshader_id;
3162 entry->pshader = pshader;
3163 entry->ps_args = compile_args;
3164 /* Add the hash table entry */
3165 add_glsl_program_entry(priv, entry);
3167 /* Set the current program */
3168 priv->glsl_program = entry;
3170 /* Attach GLSL vshader */
3172 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3175 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3176 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3177 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3178 checkGLcall("glAttachObjectARB");
3179 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3182 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3184 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3185 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3186 checkGLcall("glAttachObjectARB");
3188 /* Bind vertex attributes to a corresponding index number to match
3189 * the same index numbers as ARB_vertex_programs (makes loading
3190 * vertex attributes simpler). With this method, we can use the
3191 * exact same code to load the attributes later for both ARB and
3194 * We have to do this here because we need to know the Program ID
3195 * in order to make the bindings work, and it has to be done prior
3196 * to linking the GLSL program. */
3197 for (i = 0; i < max_attribs; ++i) {
3198 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3199 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3200 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3203 checkGLcall("glBindAttribLocationARB");
3205 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3209 pshader_id = find_gl_pshader((IWineD3DPixelShaderImpl *) pshader, &compile_args);
3214 /* Attach GLSL pshader */
3216 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3217 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3218 checkGLcall("glAttachObjectARB");
3220 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3223 /* Link the program */
3224 TRACE("Linking GLSL shader program %u\n", programId);
3225 GL_EXTCALL(glLinkProgramARB(programId));
3226 print_glsl_info_log(&GLINFO_LOCATION, programId);
3228 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3229 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3230 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3231 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3233 for (i = 0; i < MAX_CONST_I; ++i) {
3234 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3235 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3237 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3238 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3239 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3240 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3242 for (i = 0; i < MAX_CONST_I; ++i) {
3243 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3244 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3248 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3250 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3251 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3252 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3253 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3254 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3255 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3260 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3261 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3262 checkGLcall("Find glsl program uniform locations");
3264 if (pshader && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.hex_version) >= 3
3265 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4) {
3266 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3267 entry->vertex_color_clamp = GL_FALSE;
3269 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3272 /* Set the shader to allow uniform loading on it */
3273 GL_EXTCALL(glUseProgramObjectARB(programId));
3274 checkGLcall("glUseProgramObjectARB(programId)");
3276 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3277 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3278 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3279 * vertex shader with fixed function pixel processing is used we make sure that the card
3280 * supports enough samplers to allow the max number of vertex samplers with all possible
3281 * fixed function fragment processing setups. So once the program is linked these samplers
3285 /* Load vertex shader samplers */
3286 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3289 /* Load pixel shader samplers */
3290 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3293 /* If the local constants do not have to be loaded with the environment constants,
3294 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3297 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3298 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3300 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3301 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3305 static GLhandleARB create_glsl_blt_shader(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3307 GLhandleARB program_id;
3308 GLhandleARB vshader_id, pshader_id;
3309 const char *blt_vshader[] = {
3313 " gl_Position = gl_Vertex;\n"
3314 " gl_FrontColor = vec4(1.0);\n"
3315 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3319 const char *blt_pshaders[tex_type_count] = {
3324 "uniform sampler2D sampler;\n"
3327 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3333 "uniform samplerCube sampler;\n"
3336 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3340 "#extension GL_ARB_texture_rectangle : enable\n"
3341 "uniform sampler2DRect sampler;\n"
3344 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3348 if (!blt_pshaders[tex_type])
3350 FIXME("tex_type %#x not supported\n", tex_type);
3354 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3355 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3356 GL_EXTCALL(glCompileShaderARB(vshader_id));
3358 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3359 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
3360 GL_EXTCALL(glCompileShaderARB(pshader_id));
3362 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3363 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3364 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3365 GL_EXTCALL(glLinkProgramARB(program_id));
3367 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3369 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3372 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3373 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3377 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3378 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3379 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3380 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3381 GLhandleARB program_id = 0;
3382 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3384 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3386 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3387 else priv->glsl_program = NULL;
3389 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3391 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3392 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3393 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3394 checkGLcall("glClampColorARB");
3396 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3400 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3401 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3402 GL_EXTCALL(glUseProgramObjectARB(program_id));
3403 checkGLcall("glUseProgramObjectARB");
3406 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
3407 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3408 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3409 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3410 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
3412 if (!*blt_program) {
3414 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
3415 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
3416 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3417 GL_EXTCALL(glUniform1iARB(loc, 0));
3419 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3423 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3424 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3425 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3426 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3427 GLhandleARB program_id;
3429 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3430 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3432 GL_EXTCALL(glUseProgramObjectARB(program_id));
3433 checkGLcall("glUseProgramObjectARB");
3436 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
3437 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3438 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3439 GL_EXTCALL(glUseProgramObjectARB(0));
3442 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3443 const struct list *linked_programs;
3444 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3445 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3446 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)device->shader_priv;
3447 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3448 IWineD3DPixelShaderImpl *ps = NULL;
3449 IWineD3DVertexShaderImpl *vs = NULL;
3451 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3452 * can be called from IWineD3DBaseShader::Release
3454 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
3457 ps = (IWineD3DPixelShaderImpl *) This;
3458 if(ps->num_gl_shaders == 0) return;
3460 vs = (IWineD3DVertexShaderImpl *) This;
3461 if(vs->prgId == 0) return;
3464 linked_programs = &This->baseShader.linked_programs;
3466 TRACE("Deleting linked programs\n");
3467 if (linked_programs->next) {
3468 struct glsl_shader_prog_link *entry, *entry2;
3471 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3472 delete_glsl_program_entry(priv, gl_info, entry);
3475 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3476 delete_glsl_program_entry(priv, gl_info, entry);
3485 for(i = 0; i < ps->num_gl_shaders; i++) {
3486 TRACE("deleting pshader %u\n", ps->gl_shaders[i].prgId);
3487 GL_EXTCALL(glDeleteObjectARB(ps->gl_shaders[i].prgId));
3488 checkGLcall("glDeleteObjectARB");
3491 HeapFree(GetProcessHeap(), 0, ps->gl_shaders);
3492 ps->gl_shaders = NULL;
3493 ps->num_gl_shaders = 0;
3495 TRACE("Deleting shader object %u\n", vs->prgId);
3497 GL_EXTCALL(glDeleteObjectARB(vs->prgId));
3498 checkGLcall("glDeleteObjectARB");
3501 vs->baseShader.is_compiled = FALSE;
3505 static unsigned int glsl_program_key_hash(const void *key)
3507 const glsl_program_key_t *k = (const glsl_program_key_t *)key;
3509 unsigned int hash = k->vshader | ((DWORD_PTR) k->pshader) << 16;
3510 hash += ~(hash << 15);
3511 hash ^= (hash >> 10);
3512 hash += (hash << 3);
3513 hash ^= (hash >> 6);
3514 hash += ~(hash << 11);
3515 hash ^= (hash >> 16);
3520 static BOOL glsl_program_key_compare(const void *keya, const void *keyb)
3522 const glsl_program_key_t *ka = (const glsl_program_key_t *)keya;
3523 const glsl_program_key_t *kb = (const glsl_program_key_t *)keyb;
3525 return ka->vshader == kb->vshader && ka->pshader == kb->pshader &&
3526 (memcmp(&ka->ps_args, &kb->ps_args, sizeof(kb->ps_args)) == 0);
3529 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3530 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3531 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3532 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
3533 This->shader_priv = priv;
3537 static void shader_glsl_free(IWineD3DDevice *iface) {
3538 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3539 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3540 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3543 for (i = 0; i < tex_type_count; ++i)
3545 if (priv->depth_blt_program[i])
3547 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
3551 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
3553 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3554 This->shader_priv = NULL;
3557 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3558 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3562 static GLuint shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
3563 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3564 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3565 CONST DWORD *function = This->baseShader.function;
3566 const char *fragcolor;
3567 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3569 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3570 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3572 shader_addline(buffer, "#version 120\n");
3574 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3575 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3577 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3578 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3579 * drivers write a warning if we don't do so
3581 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3584 /* Base Declarations */
3585 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3587 /* Pack 3.0 inputs */
3588 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
3590 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
3591 pshader_glsl_input_pack(buffer, This->semantics_in, iface, pretransformed);
3592 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
3593 pshader_glsl_input_pack(buffer, This->semantics_in, iface, fixedfunction);
3597 /* Base Shader Body */
3598 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3600 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3601 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
3602 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3603 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3604 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3606 shader_addline(buffer, "gl_FragColor = R0;\n");
3609 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3610 fragcolor = "gl_FragData[0]";
3612 fragcolor = "gl_FragColor";
3614 if(((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
3615 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3616 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
3617 srgb_sub_high, srgb_sub_high, srgb_sub_high);
3618 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
3619 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
3620 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
3621 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
3622 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
3624 /* Pixel shader < 3.0 do not replace the fog stage.
3625 * This implements linear fog computation and blending.
3626 * TODO: non linear fog
3627 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3628 * -1/(e-s) and e/(e-s) respectively.
3630 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
3631 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3632 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
3635 shader_addline(buffer, "}\n");
3637 TRACE("Compiling shader object %u\n", shader_obj);
3638 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3639 GL_EXTCALL(glCompileShaderARB(shader_obj));
3640 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3642 /* Store the shader object */
3646 static void shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
3647 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
3648 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3649 CONST DWORD *function = This->baseShader.function;
3650 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3652 /* Create the hw GLSL shader program and assign it as the shader->prgId */
3653 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3655 shader_addline(buffer, "#version 120\n");
3657 /* Base Declarations */
3658 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3660 /* Base Shader Body */
3661 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3663 /* Unpack 3.0 outputs */
3664 if (This->baseShader.hex_version >= WINED3DVS_VERSION(3,0)) {
3665 shader_addline(buffer, "order_ps_input(OUT);\n");
3667 shader_addline(buffer, "order_ps_input();\n");
3670 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3672 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
3674 /* Write the final position.
3676 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3677 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3678 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3679 * contains 1.0 to allow a mad.
3681 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3682 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3684 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3686 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3687 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3688 * which is the same as z = z * 2 - w.
3690 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3692 shader_addline(buffer, "}\n");
3694 TRACE("Compiling shader object %u\n", shader_obj);
3695 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3696 GL_EXTCALL(glCompileShaderARB(shader_obj));
3697 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3699 /* Store the shader object */
3700 This->prgId = shader_obj;
3703 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
3705 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3706 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3707 * vs_nv_version which is based on NV_vertex_program.
3708 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3709 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3710 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3711 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3713 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3714 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
3716 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
3717 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
3718 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
3720 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3721 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3722 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3723 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3724 * in max native instructions. Intel and others also offer the info in this extension but they
3725 * don't support GLSL (at least on Windows).
3727 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3728 * of instructions is 512 or less we have to do with ps2.0 hardware.
3729 * 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.
3731 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3732 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
3734 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
3736 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3737 * Direct3D minimum requirement.
3739 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3740 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3742 * The problem is that the refrast clamps temporary results in the shader to
3743 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3744 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3745 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3746 * offer a way to query this.
3748 pCaps->PixelShader1xMaxValue = 8.0;
3749 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
3752 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
3754 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
3756 TRACE("Checking support for fixup:\n");
3757 dump_color_fixup_desc(fixup);
3760 /* We support everything except YUV conversions. */
3761 if (!is_yuv_fixup(fixup))
3767 TRACE("[FAILED]\n");
3771 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
3773 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
3774 /* WINED3DSIH_ADD */ shader_glsl_arith,
3775 /* WINED3DSIH_BEM */ pshader_glsl_bem,
3776 /* WINED3DSIH_BREAK */ shader_glsl_break,
3777 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
3778 /* WINED3DSIH_BREAKP */ NULL,
3779 /* WINED3DSIH_CALL */ shader_glsl_call,
3780 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
3781 /* WINED3DSIH_CMP */ shader_glsl_cmp,
3782 /* WINED3DSIH_CND */ shader_glsl_cnd,
3783 /* WINED3DSIH_CRS */ shader_glsl_cross,
3784 /* WINED3DSIH_DCL */ NULL,
3785 /* WINED3DSIH_DEF */ NULL,
3786 /* WINED3DSIH_DEFB */ NULL,
3787 /* WINED3DSIH_DEFI */ NULL,
3788 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
3789 /* WINED3DSIH_DP3 */ shader_glsl_dot,
3790 /* WINED3DSIH_DP4 */ shader_glsl_dot,
3791 /* WINED3DSIH_DST */ shader_glsl_dst,
3792 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
3793 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
3794 /* WINED3DSIH_ELSE */ shader_glsl_else,
3795 /* WINED3DSIH_ENDIF */ shader_glsl_end,
3796 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
3797 /* WINED3DSIH_ENDREP */ shader_glsl_end,
3798 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
3799 /* WINED3DSIH_EXPP */ shader_glsl_expp,
3800 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
3801 /* WINED3DSIH_IF */ shader_glsl_if,
3802 /* WINED3DSIH_IFC */ shader_glsl_ifc,
3803 /* WINED3DSIH_LABEL */ shader_glsl_label,
3804 /* WINED3DSIH_LIT */ shader_glsl_lit,
3805 /* WINED3DSIH_LOG */ shader_glsl_log,
3806 /* WINED3DSIH_LOGP */ shader_glsl_log,
3807 /* WINED3DSIH_LOOP */ shader_glsl_loop,
3808 /* WINED3DSIH_LRP */ shader_glsl_lrp,
3809 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
3810 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
3811 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
3812 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
3813 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
3814 /* WINED3DSIH_MAD */ shader_glsl_mad,
3815 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
3816 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
3817 /* WINED3DSIH_MOV */ shader_glsl_mov,
3818 /* WINED3DSIH_MOVA */ shader_glsl_mov,
3819 /* WINED3DSIH_MUL */ shader_glsl_arith,
3820 /* WINED3DSIH_NOP */ NULL,
3821 /* WINED3DSIH_NRM */ shader_glsl_map2gl,
3822 /* WINED3DSIH_PHASE */ NULL,
3823 /* WINED3DSIH_POW */ shader_glsl_pow,
3824 /* WINED3DSIH_RCP */ shader_glsl_rcp,
3825 /* WINED3DSIH_REP */ shader_glsl_rep,
3826 /* WINED3DSIH_RET */ NULL,
3827 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
3828 /* WINED3DSIH_SETP */ NULL,
3829 /* WINED3DSIH_SGE */ shader_glsl_compare,
3830 /* WINED3DSIH_SGN */ shader_glsl_map2gl,
3831 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
3832 /* WINED3DSIH_SLT */ shader_glsl_compare,
3833 /* WINED3DSIH_SUB */ shader_glsl_arith,
3834 /* WINED3DSIH_TEX */ pshader_glsl_tex,
3835 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
3836 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
3837 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
3838 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
3839 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
3840 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
3841 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
3842 /* WINED3DSIH_TEXLDD */ NULL,
3843 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
3844 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
3845 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
3846 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
3847 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
3848 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
3849 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
3850 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
3851 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
3852 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
3853 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
3854 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
3855 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
3858 const shader_backend_t glsl_shader_backend = {
3859 shader_glsl_instruction_handler_table,
3861 shader_glsl_select_depth_blt,
3862 shader_glsl_deselect_depth_blt,
3863 shader_glsl_load_constants,
3864 shader_glsl_cleanup,
3865 shader_glsl_color_correction,
3866 shader_glsl_destroy,
3869 shader_glsl_dirty_const,
3870 shader_glsl_generate_pshader,
3871 shader_glsl_generate_vshader,
3872 shader_glsl_get_caps,
3873 shader_glsl_color_fixup_supported,