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);
39 #define GLINFO_LOCATION (*gl_info)
54 } glsl_sample_function_t;
56 /** Prints the GLSL info log which will contain error messages if they exist */
57 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
59 int infologLength = 0;
64 const char *spam[] = {
65 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
66 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
67 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
68 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
69 "Fragment shader was successfully compiled to run on hardware.\nWARNING: 0:1: extension 'GL_ARB_draw_buffers' is not supported",
70 "Fragment shader(s) linked, no vertex shader(s) defined." /* fglrx, no \n */
73 GL_EXTCALL(glGetObjectParameterivARB(obj,
74 GL_OBJECT_INFO_LOG_LENGTH_ARB,
77 /* A size of 1 is just a null-terminated string, so the log should be bigger than
78 * that if there are errors. */
79 if (infologLength > 1)
81 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
82 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
84 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
85 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
88 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
89 if(strcmp(infoLog, spam[i]) == 0) {
95 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
97 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
99 HeapFree(GetProcessHeap(), 0, infoLog);
104 * Loads (pixel shader) samplers
106 static void shader_glsl_load_psamplers(
107 WineD3D_GL_Info *gl_info,
108 IWineD3DStateBlock* iface,
109 GLhandleARB programId) {
111 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
112 GLhandleARB name_loc;
114 char sampler_name[20];
116 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
117 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
118 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
119 if (name_loc != -1) {
120 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
121 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
122 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
123 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
124 checkGLcall("glUniform1iARB");
126 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
132 static void shader_glsl_load_vsamplers(WineD3D_GL_Info *gl_info, IWineD3DStateBlock* iface, GLhandleARB programId) {
133 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
134 GLhandleARB name_loc;
135 char sampler_name[20];
138 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
139 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
140 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
141 if (name_loc != -1) {
142 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
143 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
144 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
145 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
146 checkGLcall("glUniform1iARB");
148 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
155 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
156 * When constant_list == NULL, it will load all the constants.
158 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
159 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
160 struct list *constant_list) {
161 constants_entry *constant;
162 local_constant* lconst;
167 if (TRACE_ON(d3d_shader)) {
168 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
173 tmp_loc = constant_locations[i];
175 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
176 constants[i * 4 + 0], constants[i * 4 + 1],
177 constants[i * 4 + 2], constants[i * 4 + 3]);
183 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
184 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
185 shader_is_pshader_version(This->baseShader.hex_version)) {
188 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
193 tmp_loc = constant_locations[i];
195 /* We found this uniform name in the program - go ahead and send the data */
197 if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
198 else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
199 else lcl_const[0] = constants[k + 0];
200 if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
201 else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
202 else lcl_const[1] = constants[k + 1];
203 if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
204 else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
205 else lcl_const[2] = constants[k + 2];
206 if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
207 else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
208 else lcl_const[3] = constants[k + 3];
210 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
215 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
220 tmp_loc = constant_locations[i];
222 /* We found this uniform name in the program - go ahead and send the data */
223 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
228 checkGLcall("glUniform4fvARB()");
230 if(!This->baseShader.load_local_constsF) {
231 TRACE("No need to load local float constants for this shader\n");
235 /* Load immediate constants */
236 if (TRACE_ON(d3d_shader)) {
237 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
238 tmp_loc = constant_locations[lconst->idx];
240 GLfloat* values = (GLfloat*)lconst->value;
241 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
242 values[0], values[1], values[2], values[3]);
246 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
247 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
248 tmp_loc = constant_locations[lconst->idx];
250 /* We found this uniform name in the program - go ahead and send the data */
251 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
254 checkGLcall("glUniform4fvARB()");
258 * Loads integer constants (aka uniforms) into the currently set GLSL program.
259 * When @constants_set == NULL, it will load all the constants.
261 static void shader_glsl_load_constantsI(
262 IWineD3DBaseShaderImpl* This,
263 WineD3D_GL_Info *gl_info,
264 GLhandleARB programId,
265 GLhandleARB locations[MAX_CONST_I],
266 unsigned max_constants,
268 BOOL* constants_set) {
273 for (i=0; i<max_constants; ++i) {
274 if (NULL == constants_set || constants_set[i]) {
276 TRACE_(d3d_constants)("Loading constants %i: %i, %i, %i, %i\n",
277 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
279 /* We found this uniform name in the program - go ahead and send the data */
280 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
281 checkGLcall("glUniform4ivARB");
285 /* Load immediate constants */
286 ptr = list_head(&This->baseShader.constantsI);
288 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
289 unsigned int idx = lconst->idx;
290 GLint* values = (GLint*) lconst->value;
292 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
293 values[0], values[1], values[2], values[3]);
295 /* We found this uniform name in the program - go ahead and send the data */
296 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
297 checkGLcall("glUniform4ivARB");
298 ptr = list_next(&This->baseShader.constantsI, ptr);
303 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
304 * When @constants_set == NULL, it will load all the constants.
306 static void shader_glsl_load_constantsB(
307 IWineD3DBaseShaderImpl* This,
308 WineD3D_GL_Info *gl_info,
309 GLhandleARB programId,
310 unsigned max_constants,
312 BOOL* constants_set) {
317 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
318 const char* prefix = is_pshader? "PB":"VB";
321 for (i=0; i<max_constants; ++i) {
322 if (NULL == constants_set || constants_set[i]) {
324 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
326 /* TODO: Benchmark and see if it would be beneficial to store the
327 * locations of the constants to avoid looking up each time */
328 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
329 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
331 /* We found this uniform name in the program - go ahead and send the data */
332 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
333 checkGLcall("glUniform1ivARB");
338 /* Load immediate constants */
339 ptr = list_head(&This->baseShader.constantsB);
341 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
342 unsigned int idx = lconst->idx;
343 GLint* values = (GLint*) lconst->value;
345 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
347 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
348 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
350 /* We found this uniform name in the program - go ahead and send the data */
351 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
352 checkGLcall("glUniform1ivARB");
354 ptr = list_next(&This->baseShader.constantsB, ptr);
361 * Loads the app-supplied constants into the currently set GLSL program.
363 void shader_glsl_load_constants(
364 IWineD3DDevice* device,
366 char useVertexShader) {
368 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
369 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
370 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
372 GLhandleARB *constant_locations;
373 struct list *constant_list;
374 GLhandleARB programId;
375 struct glsl_shader_prog_link *prog = stateBlock->glsl_program;
379 /* No GLSL program set - nothing to do. */
382 programId = prog->programId;
384 if (useVertexShader) {
385 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
387 constant_locations = prog->vuniformF_locations;
388 constant_list = &stateBlock->set_vconstantsF;
390 /* Load DirectX 9 float constants/uniforms for vertex shader */
391 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
392 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
394 /* Load DirectX 9 integer constants/uniforms for vertex shader */
395 shader_glsl_load_constantsI(vshader, gl_info, programId,
396 prog->vuniformI_locations, MAX_CONST_I,
397 stateBlock->vertexShaderConstantI,
398 stateBlock->changed.vertexShaderConstantsI);
400 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
401 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
402 stateBlock->vertexShaderConstantB,
403 stateBlock->changed.vertexShaderConstantsB);
405 /* Upload the position fixup params */
406 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
407 checkGLcall("glUniform4fvARB");
410 if (usePixelShader) {
412 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
414 constant_locations = prog->puniformF_locations;
415 constant_list = &stateBlock->set_pconstantsF;
417 /* Load DirectX 9 float constants/uniforms for pixel shader */
418 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
419 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
421 /* Load DirectX 9 integer constants/uniforms for pixel shader */
422 shader_glsl_load_constantsI(pshader, gl_info, programId,
423 prog->puniformI_locations, MAX_CONST_I,
424 stateBlock->pixelShaderConstantI,
425 stateBlock->changed.pixelShaderConstantsI);
427 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
428 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
429 stateBlock->pixelShaderConstantB,
430 stateBlock->changed.pixelShaderConstantsB);
432 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
433 * It can't be 0 for a valid texbem instruction.
435 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
436 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
437 int stage = ps->luminanceconst[i].texunit;
439 float *data = (float *) &stateBlock->textureState[(int) ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
440 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
441 checkGLcall("glUniformMatrix2fvARB");
443 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
444 * is set too, so we can check that in the needsbumpmat check
446 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
447 GLfloat *scale = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
448 GLfloat *offset = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
450 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
451 checkGLcall("glUniform1fvARB");
452 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
453 checkGLcall("glUniform1fvARB");
457 if(((IWineD3DPixelShaderImpl *) pshader)->srgb_enabled &&
458 !((IWineD3DPixelShaderImpl *) pshader)->srgb_mode_hardcoded) {
462 if(stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
463 comparison[0] = srgb_cmp; comparison[1] = srgb_cmp;
464 comparison[2] = srgb_cmp; comparison[3] = srgb_cmp;
466 mul_low[0] = srgb_mul_low; mul_low[1] = srgb_mul_low;
467 mul_low[2] = srgb_mul_low; mul_low[3] = srgb_mul_low;
469 comparison[0] = 1.0 / 0.0; comparison[1] = 1.0 / 0.0;
470 comparison[2] = 1.0 / 0.0; comparison[3] = 1.0 / 0.0;
472 mul_low[0] = 1.0; mul_low[1] = 1.0;
473 mul_low[2] = 1.0; mul_low[3] = 1.0;
476 GL_EXTCALL(glUniform4fvARB(prog->srgb_comparison_location, 1, comparison));
477 GL_EXTCALL(glUniform4fvARB(prog->srgb_mul_low_location, 1, mul_low));
479 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
480 float correction_params[4];
481 if(deviceImpl->render_offscreen) {
482 correction_params[0] = 0.0;
483 correction_params[1] = 1.0;
485 /* position is window relative, not viewport relative */
486 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
487 correction_params[1] = -1.0;
489 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
494 /** Generate the variable & register declarations for the GLSL output target */
495 void shader_generate_glsl_declarations(
496 IWineD3DBaseShader *iface,
497 shader_reg_maps* reg_maps,
498 SHADER_BUFFER* buffer,
499 WineD3D_GL_Info* gl_info) {
501 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
502 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
504 unsigned int extra_constants_needed = 0;
505 local_constant* lconst;
507 /* There are some minor differences between pixel and vertex shaders */
508 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
509 char prefix = pshader ? 'P' : 'V';
511 /* Prototype the subroutines */
512 for (i = 0; i < This->baseShader.limits.label; i++) {
513 if (reg_maps->labels[i])
514 shader_addline(buffer, "void subroutine%u();\n", i);
517 /* Declare the constants (aka uniforms) */
518 if (This->baseShader.limits.constant_float > 0) {
519 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
520 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
521 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
524 if (This->baseShader.limits.constant_int > 0)
525 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
527 if (This->baseShader.limits.constant_bool > 0)
528 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
531 shader_addline(buffer, "uniform vec4 posFixup;\n");
532 /* Predeclaration; This function is added at link time based on the pixel shader.
533 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
534 * that. We know the input to the reorder function at vertex shader compile time, so
535 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
536 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
537 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
538 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
539 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
542 if(This->baseShader.hex_version >= WINED3DVS_VERSION(3, 0)) {
543 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
545 shader_addline(buffer, "void order_ps_input();\n");
548 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
550 ps_impl->numbumpenvmatconsts = 0;
551 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
552 if(!reg_maps->bumpmat[i]) {
556 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
557 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
559 if(reg_maps->luminanceparams) {
560 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
561 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
562 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
563 extra_constants_needed++;
565 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
568 extra_constants_needed++;
569 ps_impl->numbumpenvmatconsts++;
572 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
573 ps_impl->srgb_enabled = 1;
574 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
575 shader_addline(buffer, "uniform vec4 srgb_mul_low;\n");
576 shader_addline(buffer, "uniform vec4 srgb_comparison;\n");
577 ps_impl->srgb_mode_hardcoded = 0;
578 extra_constants_needed++;
580 ps_impl->srgb_mode_hardcoded = 1;
581 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
582 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
583 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
584 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
587 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
589 /* Do not write any srgb fixup into the shader to save shader size and processing time.
590 * As a consequence, we can't toggle srgb write on without recompilation
592 ps_impl->srgb_enabled = 0;
593 ps_impl->srgb_mode_hardcoded = 1;
595 if(reg_maps->vpos || reg_maps->usesdsy) {
596 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
597 shader_addline(buffer, "uniform vec4 ycorrection;\n");
598 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
599 extra_constants_needed++;
601 /* This happens because we do not have proper tracking of the constant registers that are
602 * actually used, only the max limit of the shader version
604 FIXME("Cannot find a free uniform for vpos correction params\n");
605 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
606 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
607 device->render_offscreen ? 1.0 : -1.0);
609 shader_addline(buffer, "vec4 vpos;\n");
613 /* Declare texture samplers */
614 for (i = 0; i < This->baseShader.limits.sampler; i++) {
615 if (reg_maps->samplers[i]) {
617 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
621 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
624 if(device->stateBlock->textures[i] &&
625 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
626 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
628 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
631 case WINED3DSTT_CUBE:
632 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
634 case WINED3DSTT_VOLUME:
635 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
638 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
639 FIXME("Unrecognized sampler type: %#x\n", stype);
645 /* Declare address variables */
646 for (i = 0; i < This->baseShader.limits.address; i++) {
647 if (reg_maps->address[i])
648 shader_addline(buffer, "ivec4 A%d;\n", i);
651 /* Declare texture coordinate temporaries and initialize them */
652 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
653 if (reg_maps->texcoord[i])
654 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
657 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
658 * helper function shader that is linked in at link time
660 if(pshader && This->baseShader.hex_version >= WINED3DPS_VERSION(3, 0)) {
662 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
664 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
665 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
666 * pixel shader that reads the fixed function color into the packed input registers.
668 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
672 /* Declare output register temporaries */
673 if(This->baseShader.limits.packed_output) {
674 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
677 /* Declare temporary variables */
678 for(i = 0; i < This->baseShader.limits.temporary; i++) {
679 if (reg_maps->temporary[i])
680 shader_addline(buffer, "vec4 R%u;\n", i);
683 /* Declare attributes */
684 for (i = 0; i < This->baseShader.limits.attributes; i++) {
685 if (reg_maps->attributes[i])
686 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
689 /* Declare loop registers aLx */
690 for (i = 0; i < reg_maps->loop_depth; i++) {
691 shader_addline(buffer, "int aL%u;\n", i);
692 shader_addline(buffer, "int tmpInt%u;\n", i);
695 /* Temporary variables for matrix operations */
696 shader_addline(buffer, "vec4 tmp0;\n");
697 shader_addline(buffer, "vec4 tmp1;\n");
699 /* Hardcodable local constants */
700 if(!This->baseShader.load_local_constsF) {
701 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
702 float *value = (float *) lconst->value;
703 shader_addline(buffer, "const vec4 LC%u = vec4(%f, %f, %f, %f);\n", lconst->idx,
704 value[0], value[1], value[2], value[3]);
708 /* Start the main program */
709 shader_addline(buffer, "void main() {\n");
710 if(pshader && reg_maps->vpos) {
711 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
712 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
713 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
714 * precision troubles when we just substract 0.5.
716 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
718 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
720 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
721 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
722 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
723 * correctly on drivers that returns integer values.
725 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
729 /*****************************************************************************
730 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
732 * For more information, see http://wiki.winehq.org/DirectX-Shaders
733 ****************************************************************************/
736 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
737 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
739 /** Used for opcode modifiers - They multiply the result by the specified amount */
740 static const char * const shift_glsl_tab[] = {
742 "2.0 * ", /* 1 (x2) */
743 "4.0 * ", /* 2 (x4) */
744 "8.0 * ", /* 3 (x8) */
745 "16.0 * ", /* 4 (x16) */
746 "32.0 * ", /* 5 (x32) */
753 "0.0625 * ", /* 12 (d16) */
754 "0.125 * ", /* 13 (d8) */
755 "0.25 * ", /* 14 (d4) */
756 "0.5 * " /* 15 (d2) */
759 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
760 static void shader_glsl_gen_modifier (
763 const char *in_regswizzle,
768 if (instr == WINED3DSIO_TEXKILL)
771 switch (instr & WINED3DSP_SRCMOD_MASK) {
772 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
774 case WINED3DSPSM_NONE:
775 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
777 case WINED3DSPSM_NEG:
778 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
780 case WINED3DSPSM_NOT:
781 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
783 case WINED3DSPSM_BIAS:
784 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
786 case WINED3DSPSM_BIASNEG:
787 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
789 case WINED3DSPSM_SIGN:
790 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
792 case WINED3DSPSM_SIGNNEG:
793 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
795 case WINED3DSPSM_COMP:
796 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
799 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
801 case WINED3DSPSM_X2NEG:
802 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
804 case WINED3DSPSM_ABS:
805 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
807 case WINED3DSPSM_ABSNEG:
808 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
811 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
812 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
816 /** Writes the GLSL variable name that corresponds to the register that the
817 * DX opcode parameter is trying to access */
818 static void shader_glsl_get_register_name(
820 const DWORD addr_token,
823 SHADER_OPCODE_ARG* arg) {
825 /* oPos, oFog and oPts in D3D */
826 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
828 DWORD reg = param & WINED3DSP_REGNUM_MASK;
829 DWORD regtype = shader_get_regtype(param);
830 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
831 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
832 WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
834 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
840 case WINED3DSPR_TEMP:
841 sprintf(tmpStr, "R%u", reg);
843 case WINED3DSPR_INPUT:
845 /* Pixel shaders >= 3.0 */
846 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3) {
847 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
848 glsl_src_param_t rel_param;
849 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
851 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
854 if(((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg]) {
855 sprintf(tmpStr, "IN[%s + %u]", rel_param.param_str,
856 ((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg]);
858 sprintf(tmpStr, "IN[%s]", rel_param.param_str);
861 sprintf(tmpStr, "IN[%u]",
862 ((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg]);
866 strcpy(tmpStr, "gl_Color");
868 strcpy(tmpStr, "gl_SecondaryColor");
871 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
873 sprintf(tmpStr, "attrib%u", reg);
876 case WINED3DSPR_CONST:
878 const char* prefix = pshader? "PC":"VC";
880 /* Relative addressing */
881 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
883 /* Relative addressing on shaders 2.0+ have a relative address token,
884 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
885 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
886 glsl_src_param_t rel_param;
887 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
889 sprintf(tmpStr, "%s[%s + %u]", prefix, rel_param.param_str, reg);
891 sprintf(tmpStr, "%s[%s]", prefix, rel_param.param_str);
895 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
897 sprintf(tmpStr, "%s[A0.x]", prefix);
902 if(shader_constant_is_local(This, reg)) {
903 sprintf(tmpStr, "LC%u", reg);
905 sprintf(tmpStr, "%s[%u]", prefix, reg);
911 case WINED3DSPR_CONSTINT:
913 sprintf(tmpStr, "PI[%u]", reg);
915 sprintf(tmpStr, "VI[%u]", reg);
917 case WINED3DSPR_CONSTBOOL:
919 sprintf(tmpStr, "PB[%u]", reg);
921 sprintf(tmpStr, "VB[%u]", reg);
923 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
925 sprintf(tmpStr, "T%u", reg);
927 sprintf(tmpStr, "A%u", reg);
930 case WINED3DSPR_LOOP:
931 sprintf(tmpStr, "aL%u", This->baseShader.cur_loop_regno - 1);
933 case WINED3DSPR_SAMPLER:
935 sprintf(tmpStr, "Psampler%u", reg);
937 sprintf(tmpStr, "Vsampler%u", reg);
939 case WINED3DSPR_COLOROUT:
940 if (reg >= GL_LIMITS(buffers)) {
941 WARN("Write to render target %u, only %d supported\n", reg, 4);
943 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
944 sprintf(tmpStr, "gl_FragData[%u]", reg);
945 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
946 sprintf(tmpStr, "gl_FragColor");
949 case WINED3DSPR_RASTOUT:
950 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
952 case WINED3DSPR_DEPTHOUT:
953 sprintf(tmpStr, "gl_FragDepth");
955 case WINED3DSPR_ATTROUT:
957 sprintf(tmpStr, "gl_FrontColor");
959 sprintf(tmpStr, "gl_FrontSecondaryColor");
962 case WINED3DSPR_TEXCRDOUT:
963 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
964 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
965 sprintf(tmpStr, "OUT[%u]", reg);
967 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
969 case WINED3DSPR_MISCTYPE:
972 sprintf(tmpStr, "vpos");
973 } else if (reg == 1){
974 /* Note that gl_FrontFacing is a bool, while vFace is
975 * a float for which the sign determines front/back
977 sprintf(tmpStr, "(gl_FrontFacing ? 1.0 : -1.0)");
979 FIXME("Unhandled misctype register %d\n", reg);
980 sprintf(tmpStr, "unrecognized_register");
984 FIXME("Unhandled register name Type(%d)\n", regtype);
985 sprintf(tmpStr, "unrecognized_register");
989 strcat(regstr, tmpStr);
992 /* Get the GLSL write mask for the destination register */
993 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
994 char *ptr = write_mask;
995 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
997 if (shader_is_scalar(param)) {
998 mask = WINED3DSP_WRITEMASK_0;
1001 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
1002 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
1003 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
1004 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1012 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1013 unsigned int size = 0;
1015 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1016 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1017 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1018 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1023 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
1024 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1025 * but addressed as "rgba". To fix this we need to swap the register's x
1026 * and z components. */
1027 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1028 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1029 char *ptr = swizzle_str;
1031 if (!shader_is_scalar(param)) {
1033 /* swizzle bits fields: wwzzyyxx */
1034 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
1035 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
1036 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
1037 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
1043 /* From a given parameter token, generate the corresponding GLSL string.
1044 * Also, return the actual register name and swizzle in case the
1045 * caller needs this information as well. */
1046 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
1047 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param) {
1048 BOOL is_color = FALSE;
1049 char swizzle_str[6];
1051 src_param->reg_name[0] = '\0';
1052 src_param->param_str[0] = '\0';
1053 swizzle_str[0] = '\0';
1055 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
1057 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
1058 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
1061 /* From a given parameter token, generate the corresponding GLSL string.
1062 * Also, return the actual register name and swizzle in case the
1063 * caller needs this information as well. */
1064 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
1065 const DWORD addr_token, glsl_dst_param_t *dst_param) {
1066 BOOL is_color = FALSE;
1068 dst_param->mask_str[0] = '\0';
1069 dst_param->reg_name[0] = '\0';
1071 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
1072 return shader_glsl_get_write_mask(param, dst_param->mask_str);
1075 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1076 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg, const DWORD param) {
1077 glsl_dst_param_t dst_param;
1081 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
1084 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1085 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
1091 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1092 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
1093 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
1096 /** Process GLSL instruction modifiers */
1097 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
1099 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1101 if (arg->opcode->dst_token && mask != 0) {
1102 glsl_dst_param_t dst_param;
1104 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1106 if (mask & WINED3DSPDM_SATURATE) {
1107 /* _SAT means to clamp the value of the register to between 0 and 1 */
1108 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1109 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1111 if (mask & WINED3DSPDM_MSAMPCENTROID) {
1112 FIXME("_centroid modifier not handled\n");
1114 if (mask & WINED3DSPDM_PARTIALPRECISION) {
1115 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1120 static inline const char* shader_get_comp_op(
1121 const DWORD opcode) {
1123 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1125 case COMPARISON_GT: return ">";
1126 case COMPARISON_EQ: return "==";
1127 case COMPARISON_GE: return ">=";
1128 case COMPARISON_LT: return "<";
1129 case COMPARISON_NE: return "!=";
1130 case COMPARISON_LE: return "<=";
1132 FIXME("Unrecognized comparison value: %u\n", op);
1137 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, BOOL texrect, glsl_sample_function_t *sample_function) {
1138 /* Note that there's no such thing as a projected cube texture. */
1139 switch(sampler_type) {
1141 sample_function->name = projected ? "texture1DProj" : "texture1D";
1142 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1146 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1148 sample_function->name = projected ? "texture2DProj" : "texture2D";
1150 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1152 case WINED3DSTT_CUBE:
1153 sample_function->name = "textureCube";
1154 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1156 case WINED3DSTT_VOLUME:
1157 sample_function->name = projected ? "texture3DProj" : "texture3D";
1158 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1161 sample_function->name = "";
1162 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1167 static void shader_glsl_color_correction(SHADER_OPCODE_ARG* arg) {
1168 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1169 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) shader->baseShader.device;
1170 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
1171 glsl_dst_param_t dst_param;
1172 glsl_dst_param_t dst_param2;
1174 WINED3DFORMAT conversion_group;
1175 IWineD3DBaseTextureImpl *texture;
1176 DWORD mask, mask_size;
1178 BOOL recorded = FALSE;
1180 DWORD hex_version = shader->baseShader.hex_version;
1182 switch(arg->opcode->opcode) {
1183 case WINED3DSIO_TEX:
1184 if (hex_version < WINED3DPS_VERSION(2,0)) {
1185 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1187 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1191 case WINED3DSIO_TEXLDL:
1192 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1195 case WINED3DSIO_TEXDP3TEX:
1196 case WINED3DSIO_TEXM3x3TEX:
1197 case WINED3DSIO_TEXM3x3SPEC:
1198 case WINED3DSIO_TEXM3x3VSPEC:
1199 case WINED3DSIO_TEXBEM:
1200 case WINED3DSIO_TEXREG2AR:
1201 case WINED3DSIO_TEXREG2GB:
1202 case WINED3DSIO_TEXREG2RGB:
1203 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1207 /* Not a texture sampling instruction, nothing to do */
1211 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler_idx];
1213 fmt = texture->resource.format;
1214 conversion_group = texture->baseTexture.shader_conversion_group;
1216 fmt = WINED3DFMT_UNKNOWN;
1217 conversion_group = WINED3DFMT_UNKNOWN;
1220 /* before doing anything, record the sampler with the format in the format conversion list,
1221 * but check if it's not there already
1223 for(i = 0; i < shader->baseShader.num_sampled_samplers; i++) {
1224 if(shader->baseShader.sampled_samplers[i] == sampler_idx) {
1230 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
1231 shader->baseShader.num_sampled_samplers++;
1232 shader->baseShader.sampled_format[sampler_idx] = conversion_group;
1236 case WINED3DFMT_V8U8:
1237 case WINED3DFMT_V16U16:
1238 if(GL_SUPPORT(NV_TEXTURE_SHADER) ||
1239 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP) && fmt == WINED3DFMT_V8U8)) {
1240 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1241 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_2, &dst_param);
1242 mask_size = shader_glsl_get_write_mask_size(mask);
1243 if(mask_size >= 3) {
1244 shader_addline(arg->buffer, "%s.%c = 1.0;\n", dst_param.reg_name, dst_param.mask_str[3]);
1247 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1248 mask = shader_glsl_add_dst_param(arg, arg->dst,
1249 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1,
1251 mask_size = shader_glsl_get_write_mask_size(mask);
1252 if(mask_size >= 2) {
1253 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1254 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1255 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1256 } else if(mask_size == 1) {
1257 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str[1],
1258 dst_param.reg_name, dst_param.mask_str[1]);
1263 case WINED3DFMT_X8L8V8U8:
1264 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1265 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1266 * and a(X) is always 1.0
1268 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1269 mask_size = shader_glsl_get_write_mask_size(mask);
1270 if(mask_size >= 2) {
1271 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1272 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1273 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1274 } else if(mask_size == 1) {
1275 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1276 dst_param.reg_name, dst_param.mask_str[1],
1277 dst_param.reg_name, dst_param.mask_str[1]);
1282 case WINED3DFMT_L6V5U5:
1283 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1284 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1285 mask_size = shader_glsl_get_write_mask_size(mask);
1286 shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_2, &dst_param2);
1287 if(mask_size >= 3) {
1288 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1289 shader_addline(arg->buffer, "tmp0.g = %s.%c;\n",
1290 dst_param.reg_name, dst_param.mask_str[2]);
1291 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1292 dst_param.reg_name, dst_param.mask_str[2], dst_param.mask_str[1],
1293 dst_param2.reg_name, dst_param.mask_str[1], dst_param.mask_str[3]);
1294 shader_addline(arg->buffer, "%s.%c = tmp0.g;\n", dst_param.reg_name,
1295 dst_param.mask_str[3]);
1296 } else if(mask_size == 2) {
1297 /* This is bad: We have VL, but we need VU */
1298 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1300 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1301 dst_param.reg_name, dst_param.mask_str[1],
1302 dst_param2.reg_name, dst_param.mask_str[1]);
1307 case WINED3DFMT_Q8W8V8U8:
1308 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1309 /* Correct the sign in all channels. The writemask just applies as-is, no
1310 * need for checking the mask size
1312 shader_glsl_add_dst_param(arg, arg->dst,
1313 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 |
1314 WINED3DSP_WRITEMASK_2 | WINED3DSP_WRITEMASK_3,
1316 shader_addline(arg->buffer, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str,
1317 dst_param.reg_name, dst_param.mask_str);
1321 /* stupid compiler */
1327 /*****************************************************************************
1329 * Begin processing individual instruction opcodes
1331 ****************************************************************************/
1333 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1334 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
1335 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1336 SHADER_BUFFER* buffer = arg->buffer;
1337 glsl_src_param_t src0_param;
1338 glsl_src_param_t src1_param;
1342 /* Determine the GLSL operator to use based on the opcode */
1343 switch (curOpcode->opcode) {
1344 case WINED3DSIO_MUL: op = '*'; break;
1345 case WINED3DSIO_ADD: op = '+'; break;
1346 case WINED3DSIO_SUB: op = '-'; break;
1349 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1353 write_mask = shader_glsl_append_dst(buffer, arg);
1354 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1355 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1356 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1359 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1360 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
1361 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1362 SHADER_BUFFER* buffer = arg->buffer;
1363 glsl_src_param_t src0_param;
1366 write_mask = shader_glsl_append_dst(buffer, arg);
1367 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1369 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1370 * shader versions WINED3DSIO_MOVA is used for this. */
1371 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
1372 !shader_is_pshader_version(shader->baseShader.hex_version) &&
1373 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR)) {
1374 /* This is a simple floor() */
1375 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1376 if (mask_size > 1) {
1377 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1379 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1381 } else if(arg->opcode->opcode == WINED3DSIO_MOVA) {
1382 /* We need to *round* to the nearest int here. */
1383 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1384 if (mask_size > 1) {
1385 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);
1387 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1390 shader_addline(buffer, "%s);\n", src0_param.param_str);
1394 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1395 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
1396 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1397 SHADER_BUFFER* buffer = arg->buffer;
1398 glsl_src_param_t src0_param;
1399 glsl_src_param_t src1_param;
1400 DWORD dst_write_mask, src_write_mask;
1401 unsigned int dst_size = 0;
1403 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1404 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1406 /* dp3 works on vec3, dp4 on vec4 */
1407 if (curOpcode->opcode == WINED3DSIO_DP4) {
1408 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1410 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1413 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1414 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1417 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1419 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1423 /* Note that this instruction has some restrictions. The destination write mask
1424 * can't contain the w component, and the source swizzles have to be .xyzw */
1425 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
1426 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1427 glsl_src_param_t src0_param;
1428 glsl_src_param_t src1_param;
1431 shader_glsl_get_write_mask(arg->dst, dst_mask);
1432 shader_glsl_append_dst(arg->buffer, arg);
1433 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1434 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1435 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1438 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1439 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1440 * GLSL uses the value as-is. */
1441 void shader_glsl_pow(SHADER_OPCODE_ARG *arg) {
1442 SHADER_BUFFER *buffer = arg->buffer;
1443 glsl_src_param_t src0_param;
1444 glsl_src_param_t src1_param;
1445 DWORD dst_write_mask;
1446 unsigned int dst_size;
1448 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1449 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1451 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1452 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1455 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1457 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1461 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1462 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1463 * GLSL uses the value as-is. */
1464 void shader_glsl_log(SHADER_OPCODE_ARG *arg) {
1465 SHADER_BUFFER *buffer = arg->buffer;
1466 glsl_src_param_t src0_param;
1467 DWORD dst_write_mask;
1468 unsigned int dst_size;
1470 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1471 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1473 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1476 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1478 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1482 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1483 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1484 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1485 SHADER_BUFFER* buffer = arg->buffer;
1486 glsl_src_param_t src_param;
1487 const char *instruction;
1488 char arguments[256];
1492 /* Determine the GLSL function to use based on the opcode */
1493 /* TODO: Possibly make this a table for faster lookups */
1494 switch (curOpcode->opcode) {
1495 case WINED3DSIO_MIN: instruction = "min"; break;
1496 case WINED3DSIO_MAX: instruction = "max"; break;
1497 case WINED3DSIO_ABS: instruction = "abs"; break;
1498 case WINED3DSIO_FRC: instruction = "fract"; break;
1499 case WINED3DSIO_NRM: instruction = "normalize"; break;
1500 case WINED3DSIO_LOGP:
1501 case WINED3DSIO_LOG: instruction = "log2"; break;
1502 case WINED3DSIO_EXP: instruction = "exp2"; break;
1503 case WINED3DSIO_SGN: instruction = "sign"; break;
1504 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1505 case WINED3DSIO_DSY: instruction = "ycorrection.y * dFdy"; break;
1506 default: instruction = "";
1507 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1511 write_mask = shader_glsl_append_dst(buffer, arg);
1513 arguments[0] = '\0';
1514 if (curOpcode->num_params > 0) {
1515 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1516 strcat(arguments, src_param.param_str);
1517 for (i = 2; i < curOpcode->num_params; ++i) {
1518 strcat(arguments, ", ");
1519 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1520 strcat(arguments, src_param.param_str);
1524 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1527 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1528 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1529 * dst.x = 2^(floor(src))
1530 * dst.y = src - floor(src)
1531 * dst.z = 2^src (partial precision is allowed, but optional)
1533 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1534 * dst = 2^src; (partial precision is allowed, but optional)
1536 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1537 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1538 glsl_src_param_t src_param;
1540 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1542 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1545 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1546 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1547 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1548 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1550 shader_glsl_append_dst(arg->buffer, arg);
1551 shader_glsl_get_write_mask(arg->dst, dst_mask);
1552 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1555 unsigned int mask_size;
1557 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1558 mask_size = shader_glsl_get_write_mask_size(write_mask);
1560 if (mask_size > 1) {
1561 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1563 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1568 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1569 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1570 glsl_src_param_t src_param;
1572 unsigned int mask_size;
1574 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1575 mask_size = shader_glsl_get_write_mask_size(write_mask);
1576 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1578 if (mask_size > 1) {
1579 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1581 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1585 void shader_glsl_rsq(SHADER_OPCODE_ARG* arg) {
1586 SHADER_BUFFER* buffer = arg->buffer;
1587 glsl_src_param_t src_param;
1589 unsigned int mask_size;
1591 write_mask = shader_glsl_append_dst(buffer, arg);
1592 mask_size = shader_glsl_get_write_mask_size(write_mask);
1594 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1596 if (mask_size > 1) {
1597 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1599 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1603 /** Process signed comparison opcodes in GLSL. */
1604 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1605 glsl_src_param_t src0_param;
1606 glsl_src_param_t src1_param;
1608 unsigned int mask_size;
1610 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1611 mask_size = shader_glsl_get_write_mask_size(write_mask);
1612 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1613 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1615 if (mask_size > 1) {
1616 const char *compare;
1618 switch(arg->opcode->opcode) {
1619 case WINED3DSIO_SLT: compare = "lessThan"; break;
1620 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1621 default: compare = "";
1622 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1625 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1626 src0_param.param_str, src1_param.param_str);
1628 switch(arg->opcode->opcode) {
1629 case WINED3DSIO_SLT:
1630 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1631 * to return 0.0 but step returns 1.0 because step is not < x
1632 * An alternative is a bvec compare padded with an unused second component.
1633 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1634 * issue. Playing with not() is not possible either because not() does not accept
1637 shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
1639 case WINED3DSIO_SGE:
1640 /* Here we can use the step() function and safe a conditional */
1641 shader_addline(arg->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1644 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1650 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1651 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1652 glsl_src_param_t src0_param;
1653 glsl_src_param_t src1_param;
1654 glsl_src_param_t src2_param;
1655 DWORD write_mask, cmp_channel = 0;
1658 BOOL temp_destination = FALSE;
1660 if(shader_is_scalar(arg->src[0])) {
1661 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1663 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1664 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1665 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1667 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1668 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1670 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1671 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1672 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1673 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1674 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1675 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1676 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1677 DWORD dstregtype = shader_get_regtype(arg->dst);
1679 /* Cycle through all source0 channels */
1680 for (i=0; i<4; i++) {
1682 /* Find the destination channels which use the current source0 channel */
1683 for (j=0; j<4; j++) {
1684 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1685 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1686 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1690 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1691 * The first lines may overwrite source parameters of the following lines.
1692 * Deal with that by using a temporary destination register if needed
1694 if((src0reg == dstreg && src0regtype == dstregtype) ||
1695 (src1reg == dstreg && src1regtype == dstregtype) ||
1696 (src2reg == dstreg && src2regtype == dstregtype)) {
1698 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1699 if (!write_mask) continue;
1700 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1701 temp_destination = TRUE;
1703 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1704 if (!write_mask) continue;
1707 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1708 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1709 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1711 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1712 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1715 if(temp_destination) {
1716 shader_glsl_get_write_mask(arg->dst, mask_char);
1717 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1718 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1724 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1725 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1726 * the compare is done per component of src0. */
1727 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1728 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1729 glsl_src_param_t src0_param;
1730 glsl_src_param_t src1_param;
1731 glsl_src_param_t src2_param;
1732 DWORD write_mask, cmp_channel = 0;
1735 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1736 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1737 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1738 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1739 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1741 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1742 if(arg->opcode_token & WINED3DSI_COISSUE) {
1743 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1745 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1746 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1750 /* Cycle through all source0 channels */
1751 for (i=0; i<4; i++) {
1753 /* Find the destination channels which use the current source0 channel */
1754 for (j=0; j<4; j++) {
1755 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1756 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1757 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1760 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1761 if (!write_mask) continue;
1763 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1764 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1765 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1767 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1768 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1772 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1773 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1774 glsl_src_param_t src0_param;
1775 glsl_src_param_t src1_param;
1776 glsl_src_param_t src2_param;
1779 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1780 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1781 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1782 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1783 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1784 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1787 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1788 Vertex shaders to GLSL codes */
1789 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1791 int nComponents = 0;
1792 SHADER_OPCODE_ARG tmpArg;
1794 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1796 /* Set constants for the temporary argument */
1797 tmpArg.shader = arg->shader;
1798 tmpArg.buffer = arg->buffer;
1799 tmpArg.src[0] = arg->src[0];
1800 tmpArg.src_addr[0] = arg->src_addr[0];
1801 tmpArg.src_addr[1] = arg->src_addr[1];
1802 tmpArg.reg_maps = arg->reg_maps;
1804 switch(arg->opcode->opcode) {
1805 case WINED3DSIO_M4x4:
1807 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1809 case WINED3DSIO_M4x3:
1811 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1813 case WINED3DSIO_M3x4:
1815 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1817 case WINED3DSIO_M3x3:
1819 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1821 case WINED3DSIO_M3x2:
1823 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1829 for (i = 0; i < nComponents; i++) {
1830 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1831 tmpArg.src[1] = arg->src[1]+i;
1832 shader_glsl_dot(&tmpArg);
1837 The LRP instruction performs a component-wise linear interpolation
1838 between the second and third operands using the first operand as the
1839 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1840 This is equivalent to mix(src2, src1, src0);
1842 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1843 glsl_src_param_t src0_param;
1844 glsl_src_param_t src1_param;
1845 glsl_src_param_t src2_param;
1848 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1850 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1851 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1852 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1854 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1855 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1858 /** Process the WINED3DSIO_LIT instruction in GLSL:
1859 * dst.x = dst.w = 1.0
1860 * dst.y = (src0.x > 0) ? src0.x
1861 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1862 * where src.w is clamped at +- 128
1864 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1865 glsl_src_param_t src0_param;
1866 glsl_src_param_t src1_param;
1867 glsl_src_param_t src3_param;
1870 shader_glsl_append_dst(arg->buffer, arg);
1871 shader_glsl_get_write_mask(arg->dst, dst_mask);
1873 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1874 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1875 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1877 /* The sdk specifies the instruction like this
1879 * if(src.x > 0.0) dst.y = src.x
1881 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1885 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1886 * dst.x = 1.0 ... No further explanation needed
1887 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1888 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1889 * dst.w = 1.0. ... Nothing fancy.
1891 * So we still have one conditional in there. So do this:
1892 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1894 * 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),
1895 * 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.
1896 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1898 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",
1899 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
1902 /** Process the WINED3DSIO_DST instruction in GLSL:
1904 * dst.y = src0.x * src0.y
1908 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1909 glsl_src_param_t src0y_param;
1910 glsl_src_param_t src0z_param;
1911 glsl_src_param_t src1y_param;
1912 glsl_src_param_t src1w_param;
1915 shader_glsl_append_dst(arg->buffer, arg);
1916 shader_glsl_get_write_mask(arg->dst, dst_mask);
1918 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1919 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1920 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1921 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1923 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1924 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1927 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1928 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1929 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1931 * dst.x = cos(src0.?)
1932 * dst.y = sin(src0.?)
1936 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1937 glsl_src_param_t src0_param;
1940 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1941 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1943 switch (write_mask) {
1944 case WINED3DSP_WRITEMASK_0:
1945 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1948 case WINED3DSP_WRITEMASK_1:
1949 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1952 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1953 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1957 ERR("Write mask should be .x, .y or .xy\n");
1962 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1963 * Start a for() loop where src1.y is the initial value of aL,
1964 * increment aL by src1.z for a total of src1.x iterations.
1965 * Need to use a temporary variable for this operation.
1967 /* FIXME: I don't think nested loops will work correctly this way. */
1968 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1969 glsl_src_param_t src1_param;
1970 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1971 DWORD regtype = shader_get_regtype(arg->src[1]);
1972 DWORD reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1973 const DWORD *control_values = NULL;
1974 local_constant *constant;
1976 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1978 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
1979 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
1980 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
1983 if(regtype == WINED3DSPR_CONSTINT) {
1984 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
1985 if(constant->idx == reg) {
1986 control_values = constant->value;
1992 if(control_values) {
1993 if(control_values[2] > 0) {
1994 shader_addline(arg->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
1995 shader->baseShader.cur_loop_depth, control_values[1],
1996 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
1997 shader->baseShader.cur_loop_depth, control_values[2]);
1998 } else if(control_values[2] == 0) {
1999 shader_addline(arg->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2000 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
2001 shader->baseShader.cur_loop_depth, control_values[0],
2002 shader->baseShader.cur_loop_depth);
2004 shader_addline(arg->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2005 shader->baseShader.cur_loop_depth, control_values[1],
2006 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2007 shader->baseShader.cur_loop_depth, control_values[2]);
2010 shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2011 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2012 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2013 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2016 shader->baseShader.cur_loop_depth++;
2017 shader->baseShader.cur_loop_regno++;
2020 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
2021 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2023 shader_addline(arg->buffer, "}\n");
2025 if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
2026 shader->baseShader.cur_loop_depth--;
2027 shader->baseShader.cur_loop_regno--;
2029 if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
2030 shader->baseShader.cur_loop_depth--;
2034 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
2035 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2036 glsl_src_param_t src0_param;
2038 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2039 shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2040 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2041 src0_param.param_str, shader->baseShader.cur_loop_depth);
2042 shader->baseShader.cur_loop_depth++;
2045 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
2046 glsl_src_param_t src0_param;
2048 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2049 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
2052 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
2053 glsl_src_param_t src0_param;
2054 glsl_src_param_t src1_param;
2056 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2057 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2059 shader_addline(arg->buffer, "if (%s %s %s) {\n",
2060 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2063 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
2064 shader_addline(arg->buffer, "} else {\n");
2067 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
2068 shader_addline(arg->buffer, "break;\n");
2071 /* FIXME: According to MSDN the compare is done per component. */
2072 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
2073 glsl_src_param_t src0_param;
2074 glsl_src_param_t src1_param;
2076 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2077 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2079 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
2080 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2083 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
2085 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2086 shader_addline(arg->buffer, "}\n");
2087 shader_addline(arg->buffer, "void subroutine%u () {\n", snum);
2090 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
2091 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2092 shader_addline(arg->buffer, "subroutine%u();\n", snum);
2095 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
2096 glsl_src_param_t src1_param;
2098 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2099 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2100 shader_addline(arg->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2103 /*********************************************
2104 * Pixel Shader Specific Code begins here
2105 ********************************************/
2106 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
2107 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2108 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2109 DWORD hex_version = This->baseShader.hex_version;
2110 char dst_swizzle[6];
2111 glsl_sample_function_t sample_function;
2114 BOOL projected, texrect = FALSE;
2117 /* All versions have a destination register */
2118 shader_glsl_append_dst(arg->buffer, arg);
2120 /* 1.0-1.4: Use destination register as sampler source.
2121 * 2.0+: Use provided sampler source. */
2122 if (hex_version < WINED3DPS_VERSION(1,4)) {
2125 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2126 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2128 if (flags & WINED3DTTFF_PROJECTED) {
2130 switch (flags & ~WINED3DTTFF_PROJECTED) {
2131 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2132 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2133 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2134 case WINED3DTTFF_COUNT4:
2135 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2140 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
2141 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2142 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2144 if (src_mod == WINED3DSPSM_DZ) {
2146 mask = WINED3DSP_WRITEMASK_2;
2147 } else if (src_mod == WINED3DSPSM_DW) {
2149 mask = WINED3DSP_WRITEMASK_3;
2154 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2155 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
2156 /* ps 2.0 texldp instruction always divides by the fourth component. */
2158 mask = WINED3DSP_WRITEMASK_3;
2164 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2165 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2169 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2170 shader_glsl_get_sample_function(sampler_type, projected, texrect, &sample_function);
2171 mask |= sample_function.coord_mask;
2173 if (hex_version < WINED3DPS_VERSION(2,0)) {
2174 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2176 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2179 /* 1.0-1.3: Use destination register as coordinate source.
2180 1.4+: Use provided coordinate source register. */
2181 if (hex_version < WINED3DPS_VERSION(1,4)) {
2183 shader_glsl_get_write_mask(mask, coord_mask);
2184 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
2185 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
2187 glsl_src_param_t coord_param;
2188 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
2189 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
2190 glsl_src_param_t bias;
2191 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2193 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2194 sample_function.name, sampler_idx, coord_param.param_str,
2195 bias.param_str, dst_swizzle);
2197 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2198 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2203 void shader_glsl_texldl(SHADER_OPCODE_ARG* arg) {
2204 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2205 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2206 glsl_sample_function_t sample_function;
2207 glsl_src_param_t coord_param, lod_param;
2208 char dst_swizzle[6];
2211 BOOL texrect = FALSE;
2213 shader_glsl_append_dst(arg->buffer, arg);
2214 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2216 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2217 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2218 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2219 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2222 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);
2224 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2226 if (shader_is_pshader_version(This->baseShader.hex_version)) {
2227 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2228 * However, they seem to work just fine in fragment shaders as well. */
2229 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2230 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2231 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2233 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2234 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2238 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
2240 /* FIXME: Make this work for more than just 2D textures */
2242 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2243 SHADER_BUFFER* buffer = arg->buffer;
2244 DWORD hex_version = This->baseShader.hex_version;
2248 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2249 shader_glsl_get_write_mask(write_mask, dst_mask);
2251 if (hex_version != WINED3DPS_VERSION(1,4)) {
2252 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2253 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2255 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2256 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2257 char dst_swizzle[6];
2259 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2261 if (src_mod == WINED3DSPSM_DZ) {
2262 glsl_src_param_t div_param;
2263 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2264 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2266 if (mask_size > 1) {
2267 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2269 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2271 } else if (src_mod == WINED3DSPSM_DW) {
2272 glsl_src_param_t div_param;
2273 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2274 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2276 if (mask_size > 1) {
2277 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2279 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2282 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2287 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2288 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2289 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2290 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
2291 glsl_src_param_t src0_param;
2293 glsl_sample_function_t sample_function;
2294 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2295 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2296 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2298 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2300 shader_glsl_append_dst(arg->buffer, arg);
2301 shader_glsl_get_write_mask(arg->dst, dst_mask);
2303 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2304 * scalar, and projected sampling would require 4.
2306 * It is a dependent read - not valid with conditional NP2 textures
2308 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2310 switch(count_bits(sample_function.coord_mask)) {
2312 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2313 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2317 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2318 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2322 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2323 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2326 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2330 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2331 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2332 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
2333 glsl_src_param_t src0_param;
2334 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2335 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2337 unsigned int mask_size;
2339 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2340 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2341 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2343 if (mask_size > 1) {
2344 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2346 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2350 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2351 * Calculate the depth as dst.x / dst.y */
2352 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
2353 glsl_dst_param_t dst_param;
2355 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2357 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2358 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2359 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2360 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2363 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);
2366 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2367 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2368 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2369 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2371 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
2372 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2373 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2374 glsl_src_param_t src0_param;
2376 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2378 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2379 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2382 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2383 * Calculate the 1st of a 2-row matrix multiplication. */
2384 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
2385 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2386 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2387 SHADER_BUFFER* buffer = arg->buffer;
2388 glsl_src_param_t src0_param;
2390 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2391 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2394 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2395 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2396 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
2398 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2399 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2400 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2401 SHADER_BUFFER* buffer = arg->buffer;
2402 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2403 glsl_src_param_t src0_param;
2405 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2406 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2407 current_state->texcoord_w[current_state->current_row++] = reg;
2410 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
2411 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2412 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2413 SHADER_BUFFER* buffer = arg->buffer;
2414 glsl_src_param_t src0_param;
2417 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2418 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2420 shader_glsl_append_dst(buffer, arg);
2421 shader_glsl_get_write_mask(arg->dst, dst_mask);
2423 /* Sample the texture using the calculated coordinates */
2424 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2427 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2428 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2429 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
2430 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2431 glsl_src_param_t src0_param;
2433 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2434 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2435 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2436 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2437 glsl_sample_function_t sample_function;
2439 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2440 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2442 shader_glsl_append_dst(arg->buffer, arg);
2443 shader_glsl_get_write_mask(arg->dst, dst_mask);
2444 /* Dependent read, not valid with conditional NP2 */
2445 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2447 /* Sample the texture using the calculated coordinates */
2448 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2450 current_state->current_row = 0;
2453 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2454 * Perform the 3rd row of a 3x3 matrix multiply */
2455 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
2456 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2457 glsl_src_param_t src0_param;
2459 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2460 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2461 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2463 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2465 shader_glsl_append_dst(arg->buffer, arg);
2466 shader_glsl_get_write_mask(arg->dst, dst_mask);
2467 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2469 current_state->current_row = 0;
2472 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2473 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2474 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
2476 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2477 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2478 glsl_src_param_t src0_param;
2479 glsl_src_param_t src1_param;
2481 SHADER_BUFFER* buffer = arg->buffer;
2482 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2483 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2484 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2485 glsl_sample_function_t sample_function;
2487 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2488 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2490 /* Perform the last matrix multiply operation */
2491 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2492 /* Reflection calculation */
2493 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2495 shader_glsl_append_dst(buffer, arg);
2496 shader_glsl_get_write_mask(arg->dst, dst_mask);
2497 /* Dependent read, not valid with conditional NP2 */
2498 shader_glsl_get_sample_function(stype, FALSE, FALSE, &sample_function);
2500 /* Sample the texture */
2501 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2503 current_state->current_row = 0;
2506 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2507 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2508 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
2510 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2511 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2512 SHADER_BUFFER* buffer = arg->buffer;
2513 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2514 glsl_src_param_t src0_param;
2516 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2517 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2518 glsl_sample_function_t sample_function;
2520 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2522 /* Perform the last matrix multiply operation */
2523 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2525 /* Construct the eye-ray vector from w coordinates */
2526 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2527 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2528 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2530 shader_glsl_append_dst(buffer, arg);
2531 shader_glsl_get_write_mask(arg->dst, dst_mask);
2532 /* Dependent read, not valid with conditional NP2 */
2533 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2535 /* Sample the texture using the calculated coordinates */
2536 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2538 current_state->current_row = 0;
2541 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2542 * Apply a fake bump map transform.
2543 * texbem is pshader <= 1.3 only, this saves a few version checks
2545 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
2546 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2547 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2548 char dst_swizzle[6];
2549 glsl_sample_function_t sample_function;
2550 glsl_src_param_t coord_param;
2557 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2558 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2560 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2561 /* Dependent read, not valid with conditional NP2 */
2562 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2563 mask = sample_function.coord_mask;
2565 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2567 shader_glsl_get_write_mask(mask, coord_mask);
2569 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2570 * so we can't let the GL handle this.
2572 if (flags & WINED3DTTFF_PROJECTED) {
2574 char coord_div_mask[3];
2575 switch (flags & ~WINED3DTTFF_PROJECTED) {
2576 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2577 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2578 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2579 case WINED3DTTFF_COUNT4:
2580 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2582 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2583 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2586 shader_glsl_append_dst(arg->buffer, arg);
2587 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2588 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2589 glsl_src_param_t luminance_param;
2590 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2591 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",
2592 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
2593 luminance_param.param_str, sampler_idx, sampler_idx, dst_swizzle);
2595 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2596 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
2600 void pshader_glsl_bem(SHADER_OPCODE_ARG* arg) {
2601 glsl_src_param_t src0_param, src1_param;
2602 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2604 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2605 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2607 shader_glsl_append_dst(arg->buffer, arg);
2608 shader_addline(arg->buffer, "%s + bumpenvmat%d * %s);\n",
2609 src0_param.param_str, sampler_idx, src1_param.param_str);
2612 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2613 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2614 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
2616 glsl_src_param_t src0_param;
2617 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2620 shader_glsl_append_dst(arg->buffer, arg);
2621 shader_glsl_get_write_mask(arg->dst, dst_mask);
2622 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2624 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2627 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2628 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2629 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
2630 glsl_src_param_t src0_param;
2631 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2634 shader_glsl_append_dst(arg->buffer, arg);
2635 shader_glsl_get_write_mask(arg->dst, dst_mask);
2636 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2638 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2641 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2642 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2643 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
2644 glsl_src_param_t src0_param;
2646 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2647 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2648 glsl_sample_function_t sample_function;
2650 shader_glsl_append_dst(arg->buffer, arg);
2651 shader_glsl_get_write_mask(arg->dst, dst_mask);
2652 /* Dependent read, not valid with conditional NP2 */
2653 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2654 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2656 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2659 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2660 * If any of the first 3 components are < 0, discard this pixel */
2661 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
2662 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2663 DWORD hex_version = This->baseShader.hex_version;
2664 glsl_dst_param_t dst_param;
2666 /* The argument is a destination parameter, and no writemasks are allowed */
2667 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2668 if((hex_version >= WINED3DPS_VERSION(2,0))) {
2669 /* 2.0 shaders compare all 4 components in texkill */
2670 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2672 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2673 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2674 * 4 components are defined, only the first 3 are used
2676 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2680 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2681 * dst = dot2(src0, src1) + src2 */
2682 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
2683 glsl_src_param_t src0_param;
2684 glsl_src_param_t src1_param;
2685 glsl_src_param_t src2_param;
2687 unsigned int mask_size;
2689 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2690 mask_size = shader_glsl_get_write_mask_size(write_mask);
2692 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2693 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2694 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2696 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2699 void pshader_glsl_input_pack(
2700 SHADER_BUFFER* buffer,
2701 semantic* semantics_in,
2702 IWineD3DPixelShader *iface) {
2705 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
2707 for (i = 0; i < MAX_REG_INPUT; i++) {
2709 DWORD usage_token = semantics_in[i].usage;
2710 DWORD register_token = semantics_in[i].reg;
2711 DWORD usage, usage_idx;
2715 if (!usage_token) continue;
2716 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2717 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2718 shader_glsl_get_write_mask(register_token, reg_mask);
2722 case WINED3DDECLUSAGE_TEXCOORD:
2723 if(usage_idx < 8 && This->vertexprocessing == pretransformed) {
2724 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2725 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
2727 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2728 This->input_reg_map[i], reg_mask, reg_mask);
2732 case WINED3DDECLUSAGE_COLOR:
2734 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
2735 This->input_reg_map[i], reg_mask, reg_mask);
2736 else if (usage_idx == 1)
2737 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2738 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);
2745 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2746 This->input_reg_map[i], reg_mask, reg_mask);
2751 /*********************************************
2752 * Vertex Shader Specific Code begins here
2753 ********************************************/
2755 static void add_glsl_program_entry(IWineD3DDeviceImpl *device, struct glsl_shader_prog_link *entry) {
2756 glsl_program_key_t *key;
2758 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2759 key->vshader = entry->vshader;
2760 key->pshader = entry->pshader;
2762 hash_table_put(device->glsl_program_lookup, key, entry);
2765 static struct glsl_shader_prog_link *get_glsl_program_entry(IWineD3DDeviceImpl *device,
2766 GLhandleARB vshader, GLhandleARB pshader) {
2767 glsl_program_key_t key;
2769 key.vshader = vshader;
2770 key.pshader = pshader;
2772 return (struct glsl_shader_prog_link *)hash_table_get(device->glsl_program_lookup, &key);
2775 void delete_glsl_program_entry(IWineD3DDevice *iface, struct glsl_shader_prog_link *entry) {
2776 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2777 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2778 glsl_program_key_t *key;
2780 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2781 key->vshader = entry->vshader;
2782 key->pshader = entry->pshader;
2783 hash_table_remove(This->glsl_program_lookup, key);
2785 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2786 if (entry->vshader) list_remove(&entry->vshader_entry);
2787 if (entry->pshader) list_remove(&entry->pshader_entry);
2788 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2789 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2790 HeapFree(GetProcessHeap(), 0, entry);
2793 static void handle_ps3_input(SHADER_BUFFER *buffer, semantic *semantics_in, semantic *semantics_out, WineD3D_GL_Info *gl_info, 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;
2799 char reg_mask[6], reg_mask_out[6];
2801 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (GL_LIMITS(glsl_varyings) / 4));
2803 for(i = 0; i < MAX_REG_INPUT; i++) {
2804 usage_token = semantics_in[i].usage;
2805 if (!usage_token) continue;
2806 if(map[i] >= (GL_LIMITS(glsl_varyings) / 4)) {
2807 FIXME("More input varyings declared than supported, expect issues\n");
2809 } else if(map[i] == -1) {
2810 /* Declared, but not read register */
2813 register_token = semantics_in[i].reg;
2815 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2816 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2817 set[map[i]] = shader_glsl_get_write_mask(register_token, reg_mask);
2819 if(!semantics_out) {
2821 case WINED3DDECLUSAGE_COLOR:
2823 shader_addline(buffer, "IN[%u]%s = gl_FrontColor%s;\n",
2824 map[i], reg_mask, reg_mask);
2825 else if (usage_idx == 1)
2826 shader_addline(buffer, "IN[%u]%s = gl_FrontSecondaryColor%s;\n",
2827 map[i], reg_mask, reg_mask);
2829 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2830 map[i], reg_mask, reg_mask);
2833 case WINED3DDECLUSAGE_TEXCOORD:
2834 if (usage_idx < 8) {
2835 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2836 map[i], reg_mask, usage_idx, reg_mask);
2838 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2839 map[i], reg_mask, reg_mask);
2843 case WINED3DDECLUSAGE_FOG:
2844 shader_addline(buffer, "IN[%u]%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2845 map[i], reg_mask, reg_mask);
2849 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2850 map[i], reg_mask, reg_mask);
2854 for(j = 0; j < MAX_REG_OUTPUT; j++) {
2855 usage_token_out = semantics_out[j].usage;
2856 if (!usage_token_out) continue;
2857 register_token_out = semantics_out[j].reg;
2859 usage_out = (usage_token_out & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2860 usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2861 shader_glsl_get_write_mask(register_token_out, reg_mask_out);
2863 if(usage == usage_out &&
2864 usage_idx == usage_idx_out) {
2865 shader_addline(buffer, "IN[%u]%s = OUT[%u]%s;\n",
2866 map[i], reg_mask, j, reg_mask);
2871 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2872 map[i], reg_mask, reg_mask);
2877 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2878 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2879 * input varyings are assigned above, if the optimizer works properly.
2881 for(i = 0; i < GL_LIMITS(glsl_varyings) / 4; i++) {
2882 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
2883 unsigned int size = 0;
2884 memset(reg_mask, 0, sizeof(reg_mask));
2885 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
2886 reg_mask[size] = 'x';
2889 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
2890 reg_mask[size] = 'y';
2893 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
2894 reg_mask[size] = 'z';
2897 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
2898 reg_mask[size] = 'w';
2903 shader_addline(buffer, "IN[%u].%s = 0.0;\n", i, reg_mask);
2906 shader_addline(buffer, "IN[%u].%s = vec2(0.0, 0.0);\n", i, reg_mask);
2909 shader_addline(buffer, "IN[%u].%s = vec3(0.0, 0.0, 0.0);\n", i, reg_mask);
2912 shader_addline(buffer, "IN[%u].%s = vec4(0.0, 0.0, 0.0, 0.0);\n", i, reg_mask);
2918 HeapFree(GetProcessHeap(), 0, set);
2921 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
2922 IWineD3DPixelShader *pixelshader,
2923 WineD3D_GL_Info *gl_info) {
2924 GLhandleARB ret = 0;
2925 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
2926 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
2927 DWORD vs_major = vs ? WINED3DSHADER_VERSION_MAJOR(vs->baseShader.hex_version) : 0;
2928 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.hex_version) : 0;
2930 SHADER_BUFFER buffer;
2932 DWORD register_token;
2933 DWORD usage, usage_idx, writemask;
2935 semantic *semantics_out, *semantics_in;
2937 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
2940 buffer.newline = TRUE;
2942 if(vs_major < 3 && ps_major < 3) {
2943 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
2944 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
2946 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0) {
2947 shader_addline(&buffer, "void order_ps_input() {\n");
2948 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
2949 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
2950 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
2951 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
2954 shader_addline(&buffer, "}\n");
2956 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
2958 } else if(ps_major < 3 && vs_major >= 3) {
2959 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
2960 semantics_out = vs->semantics_out;
2962 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
2963 for(i = 0; i < MAX_REG_OUTPUT; i++) {
2964 usage_token = semantics_out[i].usage;
2965 if (!usage_token) continue;
2966 register_token = semantics_out[i].reg;
2968 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2969 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2970 writemask = shader_glsl_get_write_mask(register_token, reg_mask);
2973 case WINED3DDECLUSAGE_COLOR:
2975 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
2976 else if (usage_idx == 1)
2977 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
2980 case WINED3DDECLUSAGE_POSITION:
2981 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
2984 case WINED3DDECLUSAGE_TEXCOORD:
2985 if (usage_idx < 8) {
2986 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
2988 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
2989 usage_idx, reg_mask, i, reg_mask);
2990 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
2991 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
2996 case WINED3DDECLUSAGE_PSIZE:
2997 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3000 case WINED3DDECLUSAGE_FOG:
3001 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3008 shader_addline(&buffer, "}\n");
3010 } else if(ps_major >= 3 && vs_major >= 3) {
3011 semantics_out = vs->semantics_out;
3012 semantics_in = ps->semantics_in;
3014 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3015 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3016 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3018 /* First, sort out position and point size. Those are not passed to the pixel shader */
3019 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3020 usage_token = semantics_out[i].usage;
3021 if (!usage_token) continue;
3022 register_token = semantics_out[i].reg;
3024 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3025 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3026 shader_glsl_get_write_mask(register_token, reg_mask);
3029 case WINED3DDECLUSAGE_POSITION:
3030 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3033 case WINED3DDECLUSAGE_PSIZE:
3034 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3042 /* Then, fix the pixel shader input */
3043 handle_ps3_input(&buffer, semantics_in, semantics_out, gl_info, ps->input_reg_map);
3045 shader_addline(&buffer, "}\n");
3046 } else if(ps_major >= 3 && vs_major < 3) {
3047 semantics_in = ps->semantics_in;
3049 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3050 shader_addline(&buffer, "void order_ps_input() {\n");
3051 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3052 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3053 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3055 handle_ps3_input(&buffer, semantics_in, NULL, gl_info, ps->input_reg_map);
3056 shader_addline(&buffer, "}\n");
3058 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3061 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3062 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3063 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3064 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3065 GL_EXTCALL(glCompileShaderARB(ret));
3066 checkGLcall("glCompileShaderARB(ret)");
3068 HeapFree(GetProcessHeap(), 0, buffer.buffer);
3072 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3073 * It sets the programId on the current StateBlock (because it should be called
3074 * inside of the DrawPrimitive() part of the render loop).
3076 * If a program for the given combination does not exist, create one, and store
3077 * the program in the hash table. If it creates a program, it will link the
3078 * given objects, too.
3080 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3081 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3082 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3083 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3084 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3085 struct glsl_shader_prog_link *entry = NULL;
3086 GLhandleARB programId = 0;
3087 GLhandleARB reorder_shader_id = 0;
3091 GLhandleARB vshader_id = use_vs ? ((IWineD3DBaseShaderImpl*)vshader)->baseShader.prgId : 0;
3092 GLhandleARB pshader_id = use_ps ? ((IWineD3DBaseShaderImpl*)pshader)->baseShader.prgId : 0;
3093 entry = get_glsl_program_entry(This, vshader_id, pshader_id);
3095 This->stateBlock->glsl_program = entry;
3099 /* If we get to this point, then no matching program exists, so we create one */
3100 programId = GL_EXTCALL(glCreateProgramObjectARB());
3101 TRACE("Created new GLSL shader program %u\n", programId);
3103 /* Create the entry */
3104 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3105 entry->programId = programId;
3106 entry->vshader = vshader_id;
3107 entry->pshader = pshader_id;
3108 /* Add the hash table entry */
3109 add_glsl_program_entry(This, entry);
3111 /* Set the current program */
3112 This->stateBlock->glsl_program = entry;
3114 /* Attach GLSL vshader */
3116 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3119 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3120 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3121 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3122 checkGLcall("glAttachObjectARB");
3123 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3126 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3128 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3129 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3130 checkGLcall("glAttachObjectARB");
3132 /* Bind vertex attributes to a corresponding index number to match
3133 * the same index numbers as ARB_vertex_programs (makes loading
3134 * vertex attributes simpler). With this method, we can use the
3135 * exact same code to load the attributes later for both ARB and
3138 * We have to do this here because we need to know the Program ID
3139 * in order to make the bindings work, and it has to be done prior
3140 * to linking the GLSL program. */
3141 for (i = 0; i < max_attribs; ++i) {
3142 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3143 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3144 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3147 checkGLcall("glBindAttribLocationARB");
3149 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3152 /* Attach GLSL pshader */
3154 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3155 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3156 checkGLcall("glAttachObjectARB");
3158 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3161 /* Link the program */
3162 TRACE("Linking GLSL shader program %u\n", programId);
3163 GL_EXTCALL(glLinkProgramARB(programId));
3164 print_glsl_info_log(&GLINFO_LOCATION, programId);
3166 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3167 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3168 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3169 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3171 for (i = 0; i < MAX_CONST_I; ++i) {
3172 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3173 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3175 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3176 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3177 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3178 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3180 for (i = 0; i < MAX_CONST_I; ++i) {
3181 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3182 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3186 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3188 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3189 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3190 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3191 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3192 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3193 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3198 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3199 entry->srgb_comparison_location = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_comparison"));
3200 entry->srgb_mul_low_location = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_mul_low"));
3201 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3202 checkGLcall("Find glsl program uniform locations");
3204 /* Set the shader to allow uniform loading on it */
3205 GL_EXTCALL(glUseProgramObjectARB(programId));
3206 checkGLcall("glUseProgramObjectARB(programId)");
3208 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3209 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3210 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3211 * vertex shader with fixed function pixel processing is used we make sure that the card
3212 * supports enough samplers to allow the max number of vertex samplers with all possible
3213 * fixed function fragment processing setups. So once the program is linked these samplers
3217 /* Load vertex shader samplers */
3218 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3221 /* Load pixel shader samplers */
3222 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3226 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
3227 GLhandleARB program_id;
3228 GLhandleARB vshader_id, pshader_id;
3229 const char *blt_vshader[] = {
3232 " gl_Position = gl_Vertex;\n"
3233 " gl_FrontColor = vec4(1.0);\n"
3234 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
3235 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
3239 const char *blt_pshader[] = {
3240 "uniform sampler2D sampler;\n"
3243 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3247 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3248 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3249 GL_EXTCALL(glCompileShaderARB(vshader_id));
3251 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3252 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
3253 GL_EXTCALL(glCompileShaderARB(pshader_id));
3255 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3256 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3257 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3258 GL_EXTCALL(glLinkProgramARB(program_id));
3260 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3262 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3265 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3266 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3270 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3271 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3272 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3273 GLhandleARB program_id = 0;
3275 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3276 else This->stateBlock->glsl_program = NULL;
3278 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
3279 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3280 GL_EXTCALL(glUseProgramObjectARB(program_id));
3281 checkGLcall("glUseProgramObjectARB");
3284 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
3285 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3286 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3287 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3288 static GLhandleARB loc = -1;
3290 if (!priv->depth_blt_glsl_program_id) {
3291 priv->depth_blt_glsl_program_id = create_glsl_blt_shader(gl_info);
3292 loc = GL_EXTCALL(glGetUniformLocationARB(priv->depth_blt_glsl_program_id, "sampler"));
3295 GL_EXTCALL(glUseProgramObjectARB(priv->depth_blt_glsl_program_id));
3296 GL_EXTCALL(glUniform1iARB(loc, 0));
3299 static void shader_glsl_destroy_depth_blt(IWineD3DDevice *iface) {
3300 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3301 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3302 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3304 if(priv->depth_blt_glsl_program_id) {
3305 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_glsl_program_id));
3306 priv->depth_blt_glsl_program_id = 0;
3310 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
3311 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3312 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3313 GL_EXTCALL(glUseProgramObjectARB(0));
3316 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3317 struct list *linked_programs;
3318 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3319 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info;
3321 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3322 * can be called from IWineD3DBaseShader::Release
3324 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
3326 if(This->baseShader.prgId == 0) return;
3327 linked_programs = &This->baseShader.linked_programs;
3329 TRACE("Deleting linked programs\n");
3330 if (linked_programs->next) {
3331 struct glsl_shader_prog_link *entry, *entry2;
3334 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3335 delete_glsl_program_entry(This->baseShader.device, entry);
3338 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3339 delete_glsl_program_entry(This->baseShader.device, entry);
3344 TRACE("Deleting shader object %u\n", This->baseShader.prgId);
3345 GL_EXTCALL(glDeleteObjectARB(This->baseShader.prgId));
3346 checkGLcall("glDeleteObjectARB");
3347 This->baseShader.prgId = 0;
3348 This->baseShader.is_compiled = FALSE;
3351 static unsigned int glsl_program_key_hash(void *key) {
3352 glsl_program_key_t *k = (glsl_program_key_t *)key;
3354 unsigned int hash = k->vshader | k->pshader << 16;
3355 hash += ~(hash << 15);
3356 hash ^= (hash >> 10);
3357 hash += (hash << 3);
3358 hash ^= (hash >> 6);
3359 hash += ~(hash << 11);
3360 hash ^= (hash >> 16);
3365 static BOOL glsl_program_key_compare(void *keya, void *keyb) {
3366 glsl_program_key_t *ka = (glsl_program_key_t *)keya;
3367 glsl_program_key_t *kb = (glsl_program_key_t *)keyb;
3369 return ka->vshader == kb->vshader && ka->pshader == kb->pshader;
3372 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3373 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3374 This->shader_priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3375 This->glsl_program_lookup = hash_table_create(&glsl_program_key_hash, &glsl_program_key_compare);
3379 static void shader_glsl_free(IWineD3DDevice *iface) {
3380 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3381 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3382 This->shader_priv = NULL;
3385 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3386 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3390 static void shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
3391 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3392 shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
3393 CONST DWORD *function = This->baseShader.function;
3394 const char *fragcolor;
3395 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3397 /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
3398 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3400 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3401 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3403 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3404 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3405 * drivers write a warning if we don't do so
3407 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3410 /* Base Declarations */
3411 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3413 /* Pack 3.0 inputs */
3414 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
3416 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
3417 This->vertexprocessing = pretransformed;
3418 pshader_glsl_input_pack(buffer, This->semantics_in, iface);
3419 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
3420 This->vertexprocessing = fixedfunction;
3421 pshader_glsl_input_pack(buffer, This->semantics_in, iface);
3423 This->vertexprocessing = vertexshader;
3427 /* Base Shader Body */
3428 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3430 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3431 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
3432 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3433 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3434 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3436 shader_addline(buffer, "gl_FragColor = R0;\n");
3439 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3440 fragcolor = "gl_FragData[0]";
3442 fragcolor = "gl_FragColor";
3444 if(This->srgb_enabled) {
3445 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3446 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
3447 srgb_sub_high, srgb_sub_high, srgb_sub_high);
3448 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
3449 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
3450 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
3451 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
3452 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
3454 /* Pixel shader < 3.0 do not replace the fog stage.
3455 * This implements linear fog computation and blending.
3456 * TODO: non linear fog
3457 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3458 * -1/(e-s) and e/(e-s) respectively.
3460 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
3461 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3462 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
3465 shader_addline(buffer, "}\n");
3467 TRACE("Compiling shader object %u\n", shader_obj);
3468 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3469 GL_EXTCALL(glCompileShaderARB(shader_obj));
3470 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3472 /* Store the shader object */
3473 This->baseShader.prgId = shader_obj;
3476 static void shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
3477 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
3478 shader_reg_maps* reg_maps = &This->baseShader.reg_maps;
3479 CONST DWORD *function = This->baseShader.function;
3480 WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3482 /* Create the hw GLSL shader program and assign it as the baseShader.prgId */
3483 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3485 /* Base Declarations */
3486 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3488 /* Base Shader Body */
3489 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3491 /* Unpack 3.0 outputs */
3492 if (This->baseShader.hex_version >= WINED3DVS_VERSION(3,0)) {
3493 shader_addline(buffer, "order_ps_input(OUT);\n");
3495 shader_addline(buffer, "order_ps_input();\n");
3498 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3500 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
3502 /* Write the final position.
3504 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3505 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3506 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3507 * contains 1.0 to allow a mad.
3509 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3510 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3512 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3514 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3515 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3516 * which is the same as z = z / 2 - w.
3518 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3520 shader_addline(buffer, "}\n");
3522 TRACE("Compiling shader object %u\n", shader_obj);
3523 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3524 GL_EXTCALL(glCompileShaderARB(shader_obj));
3525 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3527 /* Store the shader object */
3528 This->baseShader.prgId = shader_obj;
3531 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, WineD3D_GL_Info *gl_info, struct shader_caps *pCaps) {
3532 /* We don't have a GLSL fixed function pipeline yet, so let the none backend set its caps,
3533 * then overwrite the shader specific ones
3535 none_shader_backend.shader_get_caps(devtype, gl_info, pCaps);
3537 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3538 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3539 * vs_nv_version which is based on NV_vertex_program.
3540 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3541 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3542 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3543 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3545 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3546 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
3548 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
3549 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
3550 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
3552 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3553 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3554 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3555 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3556 * in max native instructions. Intel and others also offer the info in this extension but they
3557 * don't support GLSL (at least on Windows).
3559 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3560 * of instructions is 512 or less we have to do with ps2.0 hardware.
3561 * 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.
3563 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3564 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
3566 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
3568 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3569 * Direct3D minimum requirement.
3571 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3572 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3574 * The problem is that the refrast clamps temporary results in the shader to
3575 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3576 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3577 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3578 * offer a way to query this.
3580 pCaps->PixelShader1xMaxValue = 8.0;
3581 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
3584 static void shader_glsl_load_init(void) {}
3586 const shader_backend_t glsl_shader_backend = {
3587 &shader_glsl_select,
3588 &shader_glsl_select_depth_blt,
3589 &shader_glsl_destroy_depth_blt,
3590 &shader_glsl_load_constants,
3591 &shader_glsl_cleanup,
3592 &shader_glsl_color_correction,
3593 &shader_glsl_destroy,
3596 &shader_glsl_dirty_const,
3597 &shader_glsl_generate_pshader,
3598 &shader_glsl_generate_vshader,
3599 &shader_glsl_get_caps,
3600 &shader_glsl_load_init,