push(c) 7dcddf6204ed5518706a76fe66fd836d81e70dd4
[wine/hacks.git] / dlls / wined3d / glsl_shader.c
blob90062b8b0002b32252c76e1ccd77f029b383b6bf
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * D3D shader asm has swizzles on source parameters, and write masks for
24 * destination parameters. GLSL uses swizzles for both. The result of this is
25 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
26 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
27 * mask for the destination parameter into account.
30 #include "config.h"
31 #include <stdio.h>
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
37 #define GLINFO_LOCATION (*gl_info)
39 typedef struct {
40 char reg_name[50];
41 char mask_str[6];
42 } glsl_dst_param_t;
44 typedef struct {
45 char reg_name[50];
46 char param_str[100];
47 } glsl_src_param_t;
49 typedef struct {
50 const char *name;
51 DWORD coord_mask;
52 } glsl_sample_function_t;
54 /** Prints the GLSL info log which will contain error messages if they exist */
55 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
57 int infologLength = 0;
58 char *infoLog;
60 GL_EXTCALL(glGetObjectParameterivARB(obj,
61 GL_OBJECT_INFO_LOG_LENGTH_ARB,
62 &infologLength));
64 /* A size of 1 is just a null-terminated string, so the log should be bigger than
65 * that if there are errors. */
66 if (infologLength > 1)
68 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
69 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
70 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
71 HeapFree(GetProcessHeap(), 0, infoLog);
75 /**
76 * Loads (pixel shader) samplers
78 static void shader_glsl_load_psamplers(
79 WineD3D_GL_Info *gl_info,
80 IWineD3DStateBlock* iface) {
82 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
83 GLhandleARB programId = stateBlock->glsl_program->programId;
84 GLhandleARB name_loc;
85 int i;
86 char sampler_name[20];
88 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
89 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
90 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
91 if (name_loc != -1) {
92 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
93 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
94 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
95 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
96 checkGLcall("glUniform1iARB");
97 } else {
98 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
104 static void shader_glsl_load_vsamplers(WineD3D_GL_Info *gl_info, IWineD3DStateBlock* iface) {
105 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
106 GLhandleARB programId = stateBlock->glsl_program->programId;
107 GLhandleARB name_loc;
108 char sampler_name[20];
109 int i;
111 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
112 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
113 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
114 if (name_loc != -1) {
115 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
116 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
117 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
118 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
119 checkGLcall("glUniform1iARB");
120 } else {
121 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
127 /**
128 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
129 * When constant_list == NULL, it will load all the constants.
131 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
132 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
133 struct list *constant_list) {
134 constants_entry *constant;
135 local_constant* lconst;
136 GLhandleARB tmp_loc;
137 DWORD i, j, k;
138 DWORD *idx;
140 if (TRACE_ON(d3d_shader)) {
141 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
142 idx = constant->idx;
143 j = constant->count;
144 while (j--) {
145 i = *idx++;
146 tmp_loc = constant_locations[i];
147 if (tmp_loc != -1) {
148 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
149 constants[i * 4 + 0], constants[i * 4 + 1],
150 constants[i * 4 + 2], constants[i * 4 + 3]);
156 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
157 if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
158 shader_is_pshader_version(This->baseShader.hex_version)) {
159 float lcl_const[4];
161 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
162 idx = constant->idx;
163 j = constant->count;
164 while (j--) {
165 i = *idx++;
166 tmp_loc = constant_locations[i];
167 if (tmp_loc != -1) {
168 /* We found this uniform name in the program - go ahead and send the data */
169 k = i * 4;
170 if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
171 else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
172 else lcl_const[0] = constants[k + 0];
173 if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
174 else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
175 else lcl_const[1] = constants[k + 1];
176 if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
177 else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
178 else lcl_const[2] = constants[k + 2];
179 if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
180 else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
181 else lcl_const[3] = constants[k + 3];
183 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
187 } else {
188 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
189 idx = constant->idx;
190 j = constant->count;
191 while (j--) {
192 i = *idx++;
193 tmp_loc = constant_locations[i];
194 if (tmp_loc != -1) {
195 /* We found this uniform name in the program - go ahead and send the data */
196 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
201 checkGLcall("glUniform4fvARB()");
203 /* Load immediate constants */
204 if (TRACE_ON(d3d_shader)) {
205 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
206 tmp_loc = constant_locations[lconst->idx];
207 if (tmp_loc != -1) {
208 GLfloat* values = (GLfloat*)lconst->value;
209 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
210 values[0], values[1], values[2], values[3]);
214 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
215 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
216 tmp_loc = constant_locations[lconst->idx];
217 if (tmp_loc != -1) {
218 /* We found this uniform name in the program - go ahead and send the data */
219 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
222 checkGLcall("glUniform4fvARB()");
225 /**
226 * Loads integer constants (aka uniforms) into the currently set GLSL program.
227 * When @constants_set == NULL, it will load all the constants.
229 static void shader_glsl_load_constantsI(
230 IWineD3DBaseShaderImpl* This,
231 WineD3D_GL_Info *gl_info,
232 GLhandleARB programId,
233 unsigned max_constants,
234 int* constants,
235 BOOL* constants_set) {
237 GLhandleARB tmp_loc;
238 int i;
239 char tmp_name[8];
240 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
241 const char* prefix = is_pshader? "PI":"VI";
242 struct list* ptr;
244 for (i=0; i<max_constants; ++i) {
245 if (NULL == constants_set || constants_set[i]) {
247 TRACE_(d3d_constants)("Loading constants %i: %i, %i, %i, %i\n",
248 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
250 /* TODO: Benchmark and see if it would be beneficial to store the
251 * locations of the constants to avoid looking up each time */
252 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
253 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
254 if (tmp_loc != -1) {
255 /* We found this uniform name in the program - go ahead and send the data */
256 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
257 checkGLcall("glUniform4ivARB");
262 /* Load immediate constants */
263 ptr = list_head(&This->baseShader.constantsI);
264 while (ptr) {
265 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
266 unsigned int idx = lconst->idx;
267 GLint* values = (GLint*) lconst->value;
269 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
270 values[0], values[1], values[2], values[3]);
272 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
273 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
274 if (tmp_loc != -1) {
275 /* We found this uniform name in the program - go ahead and send the data */
276 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
277 checkGLcall("glUniform4ivARB");
279 ptr = list_next(&This->baseShader.constantsI, ptr);
283 /**
284 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
285 * When @constants_set == NULL, it will load all the constants.
287 static void shader_glsl_load_constantsB(
288 IWineD3DBaseShaderImpl* This,
289 WineD3D_GL_Info *gl_info,
290 GLhandleARB programId,
291 unsigned max_constants,
292 BOOL* constants,
293 BOOL* constants_set) {
295 GLhandleARB tmp_loc;
296 int i;
297 char tmp_name[8];
298 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
299 const char* prefix = is_pshader? "PB":"VB";
300 struct list* ptr;
302 for (i=0; i<max_constants; ++i) {
303 if (NULL == constants_set || constants_set[i]) {
305 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i*4]);
307 /* TODO: Benchmark and see if it would be beneficial to store the
308 * locations of the constants to avoid looking up each time */
309 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
310 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
311 if (tmp_loc != -1) {
312 /* We found this uniform name in the program - go ahead and send the data */
313 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
314 checkGLcall("glUniform1ivARB");
319 /* Load immediate constants */
320 ptr = list_head(&This->baseShader.constantsB);
321 while (ptr) {
322 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
323 unsigned int idx = lconst->idx;
324 GLint* values = (GLint*) lconst->value;
326 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
328 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
329 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
330 if (tmp_loc != -1) {
331 /* We found this uniform name in the program - go ahead and send the data */
332 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
333 checkGLcall("glUniform1ivARB");
335 ptr = list_next(&This->baseShader.constantsB, ptr);
342 * Loads the app-supplied constants into the currently set GLSL program.
344 void shader_glsl_load_constants(
345 IWineD3DDevice* device,
346 char usePixelShader,
347 char useVertexShader) {
349 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
350 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
351 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
353 GLhandleARB *constant_locations;
354 struct list *constant_list;
355 GLhandleARB programId;
356 GLint pos;
358 if (!stateBlock->glsl_program) {
359 /* No GLSL program set - nothing to do. */
360 return;
362 programId = stateBlock->glsl_program->programId;
364 if (useVertexShader) {
365 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
366 GLint pos;
368 constant_locations = stateBlock->glsl_program->vuniformF_locations;
369 constant_list = &stateBlock->set_vconstantsF;
371 /* Load vertex shader samplers */
372 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)stateBlock);
374 /* Load DirectX 9 float constants/uniforms for vertex shader */
375 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
376 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
378 /* Load DirectX 9 integer constants/uniforms for vertex shader */
379 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
380 stateBlock->vertexShaderConstantI,
381 stateBlock->changed.vertexShaderConstantsI);
383 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
384 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
385 stateBlock->vertexShaderConstantB,
386 stateBlock->changed.vertexShaderConstantsB);
388 /* Upload the position fixup params */
389 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
390 checkGLcall("glGetUniformLocationARB");
391 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
392 checkGLcall("glUniform4fvARB");
395 if (usePixelShader) {
397 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
399 constant_locations = stateBlock->glsl_program->puniformF_locations;
400 constant_list = &stateBlock->set_pconstantsF;
402 /* Load pixel shader samplers */
403 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
405 /* Load DirectX 9 float constants/uniforms for pixel shader */
406 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
407 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
409 /* Load DirectX 9 integer constants/uniforms for pixel shader */
410 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
411 stateBlock->pixelShaderConstantI,
412 stateBlock->changed.pixelShaderConstantsI);
414 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
415 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
416 stateBlock->pixelShaderConstantB,
417 stateBlock->changed.pixelShaderConstantsB);
419 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
420 * It can't be 0 for a valid texbem instruction.
422 if(((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat != -1) {
423 float *data = (float *) &stateBlock->textureState[(int) ((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat][WINED3DTSS_BUMPENVMAT00];
424 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "bumpenvmat"));
425 checkGLcall("glGetUniformLocationARB");
426 GL_EXTCALL(glUniformMatrix2fvARB(pos, 1, 0, data));
427 checkGLcall("glUniformMatrix2fvARB");
429 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
430 * is set too, so we can check that in the needsbumpmat check
432 if(((IWineD3DPixelShaderImpl *) pshader)->baseShader.reg_maps.luminanceparams != -1) {
433 int stage = ((IWineD3DPixelShaderImpl *) pshader)->baseShader.reg_maps.luminanceparams;
434 GLfloat *scale = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
435 GLfloat *offset = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
437 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "luminancescale"));
438 checkGLcall("glGetUniformLocationARB");
439 GL_EXTCALL(glUniform1fvARB(pos, 1, scale));
440 checkGLcall("glUniform1fvARB");
441 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "luminanceoffset"));
442 checkGLcall("glGetUniformLocationARB");
443 GL_EXTCALL(glUniform1fvARB(pos, 1, offset));
444 checkGLcall("glUniform1fvARB");
446 } else if(((IWineD3DPixelShaderImpl *) pshader)->srgb_enabled &&
447 !((IWineD3DPixelShaderImpl *) pshader)->srgb_mode_hardcoded) {
448 float comparison[4];
449 float mul_low[4];
451 if(stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
452 comparison[0] = srgb_cmp; comparison[1] = srgb_cmp;
453 comparison[2] = srgb_cmp; comparison[3] = srgb_cmp;
455 mul_low[0] = srgb_mul_low; mul_low[1] = srgb_mul_low;
456 mul_low[2] = srgb_mul_low; mul_low[3] = srgb_mul_low;
457 } else {
458 comparison[0] = 1.0 / 0.0; comparison[1] = 1.0 / 0.0;
459 comparison[2] = 1.0 / 0.0; comparison[3] = 1.0 / 0.0;
461 mul_low[0] = 1.0; mul_low[1] = 1.0;
462 mul_low[2] = 1.0; mul_low[3] = 1.0;
465 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_comparison"));
466 checkGLcall("glGetUniformLocationARB");
467 GL_EXTCALL(glUniform4fvARB(pos, 1, comparison));
468 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_mul_low"));
469 checkGLcall("glGetUniformLocationARB");
470 GL_EXTCALL(glUniform4fvARB(pos, 1, mul_low));
472 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
473 float correction_params[4];
474 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
475 checkGLcall("glGetUniformLocationARB");
476 if(deviceImpl->render_offscreen) {
477 correction_params[0] = 0.0;
478 correction_params[1] = 1.0;
479 } else {
480 /* position is window relative, not viewport relative */
481 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
482 correction_params[1] = -1.0;
484 GL_EXTCALL(glUniform4fvARB(pos, 1, correction_params));
489 /** Generate the variable & register declarations for the GLSL output target */
490 void shader_generate_glsl_declarations(
491 IWineD3DBaseShader *iface,
492 shader_reg_maps* reg_maps,
493 SHADER_BUFFER* buffer,
494 WineD3D_GL_Info* gl_info) {
496 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
497 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
498 int i;
499 unsigned int extra_constants_needed = 0;
501 /* There are some minor differences between pixel and vertex shaders */
502 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
503 char prefix = pshader ? 'P' : 'V';
505 /* Prototype the subroutines */
506 for (i = 0; i < This->baseShader.limits.label; i++) {
507 if (reg_maps->labels[i])
508 shader_addline(buffer, "void subroutine%lu();\n", i);
511 /* Declare the constants (aka uniforms) */
512 if (This->baseShader.limits.constant_float > 0) {
513 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
514 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
515 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
518 if (This->baseShader.limits.constant_int > 0)
519 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
521 if (This->baseShader.limits.constant_bool > 0)
522 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
524 if(!pshader) {
525 shader_addline(buffer, "uniform vec4 posFixup;\n");
526 } else {
527 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
529 if(reg_maps->bumpmat != -1) {
530 shader_addline(buffer, "uniform mat2 bumpenvmat;\n");
531 if(reg_maps->luminanceparams) {
532 shader_addline(buffer, "uniform float luminancescale;\n");
533 shader_addline(buffer, "uniform float luminanceoffset;\n");
534 extra_constants_needed++;
536 extra_constants_needed++;
539 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
540 ps_impl->srgb_enabled = 1;
541 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
542 shader_addline(buffer, "uniform vec4 srgb_mul_low;\n");
543 shader_addline(buffer, "uniform vec4 srgb_comparison;\n");
544 ps_impl->srgb_mode_hardcoded = 0;
545 extra_constants_needed++;
546 } else {
547 ps_impl->srgb_mode_hardcoded = 1;
548 shader_addline(buffer, "const vec4 srgb_mul_low = {%f, %f, %f, %f};\n",
549 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
550 shader_addline(buffer, "const vec4 srgb_comparison = {%f, %f, %f, %f};\n",
551 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
553 } else {
554 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
556 /* Do not write any srgb fixup into the shader to save shader size and processing time.
557 * As a consequence, we can't toggle srgb write on without recompilation
559 ps_impl->srgb_enabled = 0;
560 ps_impl->srgb_mode_hardcoded = 1;
562 if(reg_maps->vpos) {
563 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
564 shader_addline(buffer, "uniform vec4 ycorrection;\n");
565 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
566 extra_constants_needed++;
567 } else {
568 /* This happens because we do not have proper tracking of the constant registers that are
569 * actually used, only the max limit of the shader version
571 FIXME("Cannot find a free uniform for vpos correction params\n");
572 shader_addline(buffer, "const vec4 ycorrection = {%f, %f, 0.0, 0.0};\n",
573 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
574 device->render_offscreen ? 1.0 : -1.0);
576 shader_addline(buffer, "vec4 vpos;\n");
580 /* Declare texture samplers */
581 for (i = 0; i < This->baseShader.limits.sampler; i++) {
582 if (reg_maps->samplers[i]) {
584 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
585 switch (stype) {
587 case WINED3DSTT_1D:
588 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
589 break;
590 case WINED3DSTT_2D:
591 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
592 break;
593 case WINED3DSTT_CUBE:
594 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
595 break;
596 case WINED3DSTT_VOLUME:
597 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
598 break;
599 default:
600 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
601 FIXME("Unrecognized sampler type: %#x\n", stype);
602 break;
607 /* Declare address variables */
608 for (i = 0; i < This->baseShader.limits.address; i++) {
609 if (reg_maps->address[i])
610 shader_addline(buffer, "ivec4 A%d;\n", i);
613 /* Declare texture coordinate temporaries and initialize them */
614 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
615 if (reg_maps->texcoord[i])
616 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
619 /* Declare input register temporaries */
620 for (i=0; i < This->baseShader.limits.packed_input; i++) {
621 if (reg_maps->packed_input[i])
622 shader_addline(buffer, "vec4 IN%lu;\n", i);
625 /* Declare output register temporaries */
626 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
627 if (reg_maps->packed_output[i])
628 shader_addline(buffer, "vec4 OUT%lu;\n", i);
631 /* Declare temporary variables */
632 for(i = 0; i < This->baseShader.limits.temporary; i++) {
633 if (reg_maps->temporary[i])
634 shader_addline(buffer, "vec4 R%lu;\n", i);
637 /* Declare attributes */
638 for (i = 0; i < This->baseShader.limits.attributes; i++) {
639 if (reg_maps->attributes[i])
640 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
643 /* Declare loop register aL */
644 if (reg_maps->loop) {
645 shader_addline(buffer, "int aL;\n");
646 shader_addline(buffer, "int tmpInt;\n");
649 /* Temporary variables for matrix operations */
650 shader_addline(buffer, "vec4 tmp0;\n");
651 shader_addline(buffer, "vec4 tmp1;\n");
653 /* Start the main program */
654 shader_addline(buffer, "void main() {\n");
655 if(pshader && reg_maps->vpos) {
656 shader_addline(buffer, "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1) - 0.5;\n");
660 /*****************************************************************************
661 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
663 * For more information, see http://wiki.winehq.org/DirectX-Shaders
664 ****************************************************************************/
666 /* Prototypes */
667 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
668 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
670 /** Used for opcode modifiers - They multiply the result by the specified amount */
671 static const char * const shift_glsl_tab[] = {
672 "", /* 0 (none) */
673 "2.0 * ", /* 1 (x2) */
674 "4.0 * ", /* 2 (x4) */
675 "8.0 * ", /* 3 (x8) */
676 "16.0 * ", /* 4 (x16) */
677 "32.0 * ", /* 5 (x32) */
678 "", /* 6 (x64) */
679 "", /* 7 (x128) */
680 "", /* 8 (d256) */
681 "", /* 9 (d128) */
682 "", /* 10 (d64) */
683 "", /* 11 (d32) */
684 "0.0625 * ", /* 12 (d16) */
685 "0.125 * ", /* 13 (d8) */
686 "0.25 * ", /* 14 (d4) */
687 "0.5 * " /* 15 (d2) */
690 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
691 static void shader_glsl_gen_modifier (
692 const DWORD instr,
693 const char *in_reg,
694 const char *in_regswizzle,
695 char *out_str) {
697 out_str[0] = 0;
699 if (instr == WINED3DSIO_TEXKILL)
700 return;
702 switch (instr & WINED3DSP_SRCMOD_MASK) {
703 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
704 case WINED3DSPSM_DW:
705 case WINED3DSPSM_NONE:
706 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
707 break;
708 case WINED3DSPSM_NEG:
709 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
710 break;
711 case WINED3DSPSM_NOT:
712 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
713 break;
714 case WINED3DSPSM_BIAS:
715 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
716 break;
717 case WINED3DSPSM_BIASNEG:
718 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
719 break;
720 case WINED3DSPSM_SIGN:
721 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
722 break;
723 case WINED3DSPSM_SIGNNEG:
724 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
725 break;
726 case WINED3DSPSM_COMP:
727 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
728 break;
729 case WINED3DSPSM_X2:
730 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
731 break;
732 case WINED3DSPSM_X2NEG:
733 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
734 break;
735 case WINED3DSPSM_ABS:
736 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
737 break;
738 case WINED3DSPSM_ABSNEG:
739 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
740 break;
741 default:
742 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
743 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
747 /** Writes the GLSL variable name that corresponds to the register that the
748 * DX opcode parameter is trying to access */
749 static void shader_glsl_get_register_name(
750 const DWORD param,
751 const DWORD addr_token,
752 char* regstr,
753 BOOL* is_color,
754 SHADER_OPCODE_ARG* arg) {
756 /* oPos, oFog and oPts in D3D */
757 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
759 DWORD reg = param & WINED3DSP_REGNUM_MASK;
760 DWORD regtype = shader_get_regtype(param);
761 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
762 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
763 WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
765 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
766 char tmpStr[50];
768 *is_color = FALSE;
770 switch (regtype) {
771 case WINED3DSPR_TEMP:
772 sprintf(tmpStr, "R%u", reg);
773 break;
774 case WINED3DSPR_INPUT:
775 if (pshader) {
776 /* Pixel shaders >= 3.0 */
777 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
778 sprintf(tmpStr, "IN%u", reg);
779 else {
780 if (reg==0)
781 strcpy(tmpStr, "gl_Color");
782 else
783 strcpy(tmpStr, "gl_SecondaryColor");
785 } else {
786 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
787 *is_color = TRUE;
788 sprintf(tmpStr, "attrib%u", reg);
790 break;
791 case WINED3DSPR_CONST:
793 const char* prefix = pshader? "PC":"VC";
795 /* Relative addressing */
796 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
798 /* Relative addressing on shaders 2.0+ have a relative address token,
799 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
800 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
801 glsl_src_param_t rel_param;
802 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
803 sprintf(tmpStr, "%s[%s + %u]", prefix, rel_param.param_str, reg);
804 } else
805 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
807 } else
808 sprintf(tmpStr, "%s[%u]", prefix, reg);
810 break;
812 case WINED3DSPR_CONSTINT:
813 if (pshader)
814 sprintf(tmpStr, "PI[%u]", reg);
815 else
816 sprintf(tmpStr, "VI[%u]", reg);
817 break;
818 case WINED3DSPR_CONSTBOOL:
819 if (pshader)
820 sprintf(tmpStr, "PB[%u]", reg);
821 else
822 sprintf(tmpStr, "VB[%u]", reg);
823 break;
824 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
825 if (pshader) {
826 sprintf(tmpStr, "T%u", reg);
827 } else {
828 sprintf(tmpStr, "A%u", reg);
830 break;
831 case WINED3DSPR_LOOP:
832 sprintf(tmpStr, "aL");
833 break;
834 case WINED3DSPR_SAMPLER:
835 if (pshader)
836 sprintf(tmpStr, "Psampler%u", reg);
837 else
838 sprintf(tmpStr, "Vsampler%u", reg);
839 break;
840 case WINED3DSPR_COLOROUT:
841 if (reg >= GL_LIMITS(buffers)) {
842 WARN("Write to render target %u, only %d supported\n", reg, 4);
844 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
845 sprintf(tmpStr, "gl_FragData[%u]", reg);
846 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
847 sprintf(tmpStr, "gl_FragColor");
849 break;
850 case WINED3DSPR_RASTOUT:
851 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
852 break;
853 case WINED3DSPR_DEPTHOUT:
854 sprintf(tmpStr, "gl_FragDepth");
855 break;
856 case WINED3DSPR_ATTROUT:
857 if (reg == 0) {
858 sprintf(tmpStr, "gl_FrontColor");
859 } else {
860 sprintf(tmpStr, "gl_FrontSecondaryColor");
862 break;
863 case WINED3DSPR_TEXCRDOUT:
864 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
865 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
866 sprintf(tmpStr, "OUT%u", reg);
867 else
868 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
869 break;
870 case WINED3DSPR_MISCTYPE:
871 if (reg == 0) {
872 /* vPos */
873 sprintf(tmpStr, "vpos");
874 } else {
875 /* gl_FrontFacing could be used for vFace, but note that
876 * gl_FrontFacing is a bool, while vFace is a float for
877 * which the sign determines front/back */
878 FIXME("Unhandled misctype register %d\n", reg);
879 sprintf(tmpStr, "unrecognized_register");
881 break;
882 default:
883 FIXME("Unhandled register name Type(%d)\n", regtype);
884 sprintf(tmpStr, "unrecognized_register");
885 break;
888 strcat(regstr, tmpStr);
891 /* Get the GLSL write mask for the destination register */
892 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
893 char *ptr = write_mask;
894 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
896 if (shader_is_scalar(param)) {
897 mask = WINED3DSP_WRITEMASK_0;
898 } else {
899 *ptr++ = '.';
900 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
901 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
902 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
903 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
906 *ptr = '\0';
908 return mask;
911 static size_t shader_glsl_get_write_mask_size(DWORD write_mask) {
912 size_t size = 0;
914 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
915 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
916 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
917 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
919 return size;
922 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
923 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
924 * but addressed as "rgba". To fix this we need to swap the register's x
925 * and z components. */
926 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
927 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
928 char *ptr = swizzle_str;
930 if (!shader_is_scalar(param)) {
931 *ptr++ = '.';
932 /* swizzle bits fields: wwzzyyxx */
933 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
934 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
935 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
936 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
939 *ptr = '\0';
942 /* From a given parameter token, generate the corresponding GLSL string.
943 * Also, return the actual register name and swizzle in case the
944 * caller needs this information as well. */
945 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
946 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param) {
947 BOOL is_color = FALSE;
948 char swizzle_str[6];
950 src_param->reg_name[0] = '\0';
951 src_param->param_str[0] = '\0';
952 swizzle_str[0] = '\0';
954 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
956 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
957 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
960 /* From a given parameter token, generate the corresponding GLSL string.
961 * Also, return the actual register name and swizzle in case the
962 * caller needs this information as well. */
963 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
964 const DWORD addr_token, glsl_dst_param_t *dst_param) {
965 BOOL is_color = FALSE;
967 dst_param->mask_str[0] = '\0';
968 dst_param->reg_name[0] = '\0';
970 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
971 return shader_glsl_get_write_mask(param, dst_param->mask_str);
974 /* Append the destination part of the instruction to the buffer, return the effective write mask */
975 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg, const DWORD param) {
976 glsl_dst_param_t dst_param;
977 DWORD mask;
978 int shift;
980 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
982 if(mask) {
983 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
984 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
987 return mask;
990 /* Append the destination part of the instruction to the buffer, return the effective write mask */
991 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
992 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
995 /** Process GLSL instruction modifiers */
996 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
998 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1000 if (arg->opcode->dst_token && mask != 0) {
1001 glsl_dst_param_t dst_param;
1003 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1005 if (mask & WINED3DSPDM_SATURATE) {
1006 /* _SAT means to clamp the value of the register to between 0 and 1 */
1007 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1008 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1010 if (mask & WINED3DSPDM_MSAMPCENTROID) {
1011 FIXME("_centroid modifier not handled\n");
1013 if (mask & WINED3DSPDM_PARTIALPRECISION) {
1014 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1019 static inline const char* shader_get_comp_op(
1020 const DWORD opcode) {
1022 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1023 switch (op) {
1024 case COMPARISON_GT: return ">";
1025 case COMPARISON_EQ: return "==";
1026 case COMPARISON_GE: return ">=";
1027 case COMPARISON_LT: return "<";
1028 case COMPARISON_NE: return "!=";
1029 case COMPARISON_LE: return "<=";
1030 default:
1031 FIXME("Unrecognized comparison value: %u\n", op);
1032 return "(\?\?)";
1036 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, glsl_sample_function_t *sample_function) {
1037 /* Note that there's no such thing as a projected cube texture. */
1038 switch(sampler_type) {
1039 case WINED3DSTT_1D:
1040 sample_function->name = projected ? "texture1DProj" : "texture1D";
1041 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1042 break;
1043 case WINED3DSTT_2D:
1044 sample_function->name = projected ? "texture2DProj" : "texture2D";
1045 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1046 break;
1047 case WINED3DSTT_CUBE:
1048 sample_function->name = "textureCube";
1049 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1050 break;
1051 case WINED3DSTT_VOLUME:
1052 sample_function->name = projected ? "texture3DProj" : "texture3D";
1053 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1054 break;
1055 default:
1056 sample_function->name = "";
1057 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1058 break;
1062 static void shader_glsl_color_correction(SHADER_OPCODE_ARG* arg) {
1063 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1064 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) shader->baseShader.device;
1065 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
1066 glsl_dst_param_t dst_param;
1067 glsl_dst_param_t dst_param2;
1068 WINED3DFORMAT fmt;
1069 WINED3DFORMAT conversion_group;
1070 IWineD3DBaseTextureImpl *texture;
1071 DWORD mask, mask_size;
1072 UINT i;
1073 BOOL recorded = FALSE;
1074 DWORD sampler_idx;
1075 DWORD hex_version = shader->baseShader.hex_version;
1077 switch(arg->opcode->opcode) {
1078 case WINED3DSIO_TEX:
1079 if (hex_version < WINED3DPS_VERSION(2,0)) {
1080 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1081 } else {
1082 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1084 break;
1086 case WINED3DSIO_TEXLDL:
1087 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1088 return;
1090 case WINED3DSIO_TEXDP3TEX:
1091 case WINED3DSIO_TEXM3x3TEX:
1092 case WINED3DSIO_TEXM3x3SPEC:
1093 case WINED3DSIO_TEXM3x3VSPEC:
1094 case WINED3DSIO_TEXBEM:
1095 case WINED3DSIO_TEXREG2AR:
1096 case WINED3DSIO_TEXREG2GB:
1097 case WINED3DSIO_TEXREG2RGB:
1098 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1099 break;
1101 default:
1102 /* Not a texture sampling instruction, nothing to do */
1103 return;
1106 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler_idx];
1107 if(texture) {
1108 fmt = texture->resource.format;
1109 conversion_group = texture->baseTexture.shader_conversion_group;
1110 } else {
1111 fmt = WINED3DFMT_UNKNOWN;
1112 conversion_group = WINED3DFMT_UNKNOWN;
1115 /* before doing anything, record the sampler with the format in the format conversion list,
1116 * but check if it's not there already
1118 for(i = 0; i < shader->baseShader.num_sampled_samplers; i++) {
1119 if(shader->baseShader.sampled_samplers[i] == sampler_idx) {
1120 recorded = TRUE;
1121 break;
1124 if(!recorded) {
1125 shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
1126 shader->baseShader.num_sampled_samplers++;
1127 shader->baseShader.sampled_format[sampler_idx] = conversion_group;
1130 switch(fmt) {
1131 case WINED3DFMT_V8U8:
1132 case WINED3DFMT_V16U16:
1133 if(GL_SUPPORT(NV_TEXTURE_SHADER) ||
1134 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP) && fmt == WINED3DFMT_V8U8)) {
1135 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1136 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_2, &dst_param);
1137 mask_size = shader_glsl_get_write_mask_size(mask);
1138 if(mask_size >= 3) {
1139 shader_addline(arg->buffer, "%s.%c = 1.0;\n", dst_param.reg_name, dst_param.mask_str[3]);
1141 } else {
1142 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1143 mask = shader_glsl_add_dst_param(arg, arg->dst,
1144 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1,
1145 &dst_param);
1146 mask_size = shader_glsl_get_write_mask_size(mask);
1147 if(mask_size >= 2) {
1148 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1149 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1150 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1151 } else if(mask_size == 1) {
1152 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str[1],
1153 dst_param.reg_name, dst_param.mask_str[1]);
1156 break;
1158 case WINED3DFMT_X8L8V8U8:
1159 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1160 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1161 * and a(X) is always 1.0
1163 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1164 mask_size = shader_glsl_get_write_mask_size(mask);
1165 if(mask_size >= 2) {
1166 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1167 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1168 dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1169 } else if(mask_size == 1) {
1170 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1171 dst_param.reg_name, dst_param.mask_str[1],
1172 dst_param.reg_name, dst_param.mask_str[1]);
1175 break;
1177 case WINED3DFMT_L6V5U5:
1178 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1179 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1180 mask_size = shader_glsl_get_write_mask_size(mask);
1181 shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_2, &dst_param2);
1182 if(mask_size >= 3) {
1183 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1184 shader_addline(arg->buffer, "tmp0.g = %s.%c;\n",
1185 dst_param.reg_name, dst_param.mask_str[2]);
1186 shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1187 dst_param.reg_name, dst_param.mask_str[2], dst_param.mask_str[1],
1188 dst_param2.reg_name, dst_param.mask_str[1], dst_param.mask_str[3]);
1189 shader_addline(arg->buffer, "%s.%c = tmp0.g;\n", dst_param.reg_name,
1190 dst_param.mask_str[3]);
1191 } else if(mask_size == 2) {
1192 /* This is bad: We have VL, but we need VU */
1193 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1194 } else {
1195 shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1196 dst_param.reg_name, dst_param.mask_str[1],
1197 dst_param2.reg_name, dst_param.mask_str[1]);
1200 break;
1202 case WINED3DFMT_Q8W8V8U8:
1203 if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1204 /* Correct the sign in all channels. The writemask just applies as-is, no
1205 * need for checking the mask size
1207 shader_glsl_add_dst_param(arg, arg->dst,
1208 WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 |
1209 WINED3DSP_WRITEMASK_2 | WINED3DSP_WRITEMASK_3,
1210 &dst_param);
1211 shader_addline(arg->buffer, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str,
1212 dst_param.reg_name, dst_param.mask_str);
1214 break;
1216 /* stupid compiler */
1217 default:
1218 break;
1222 /*****************************************************************************
1224 * Begin processing individual instruction opcodes
1226 ****************************************************************************/
1228 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1229 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
1230 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1231 SHADER_BUFFER* buffer = arg->buffer;
1232 glsl_src_param_t src0_param;
1233 glsl_src_param_t src1_param;
1234 DWORD write_mask;
1235 char op;
1237 /* Determine the GLSL operator to use based on the opcode */
1238 switch (curOpcode->opcode) {
1239 case WINED3DSIO_MUL: op = '*'; break;
1240 case WINED3DSIO_ADD: op = '+'; break;
1241 case WINED3DSIO_SUB: op = '-'; break;
1242 default:
1243 op = ' ';
1244 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1245 break;
1248 write_mask = shader_glsl_append_dst(buffer, arg);
1249 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1250 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1251 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1254 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1255 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
1256 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1257 SHADER_BUFFER* buffer = arg->buffer;
1258 glsl_src_param_t src0_param;
1259 DWORD write_mask;
1261 write_mask = shader_glsl_append_dst(buffer, arg);
1262 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1264 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1265 * shader versions WINED3DSIO_MOVA is used for this. */
1266 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
1267 !shader_is_pshader_version(shader->baseShader.hex_version) &&
1268 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR) ||
1269 arg->opcode->opcode == WINED3DSIO_MOVA) {
1270 /* We need to *round* to the nearest int here. */
1271 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
1272 if (mask_size > 1) {
1273 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);
1274 } else {
1275 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1277 } else {
1278 shader_addline(buffer, "%s);\n", src0_param.param_str);
1282 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1283 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
1284 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1285 SHADER_BUFFER* buffer = arg->buffer;
1286 glsl_src_param_t src0_param;
1287 glsl_src_param_t src1_param;
1288 DWORD dst_write_mask, src_write_mask;
1289 size_t dst_size = 0;
1291 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1292 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1294 /* dp3 works on vec3, dp4 on vec4 */
1295 if (curOpcode->opcode == WINED3DSIO_DP4) {
1296 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1297 } else {
1298 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1301 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1302 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1304 if (dst_size > 1) {
1305 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1306 } else {
1307 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1311 /* Note that this instruction has some restrictions. The destination write mask
1312 * can't contain the w component, and the source swizzles have to be .xyzw */
1313 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
1314 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1315 glsl_src_param_t src0_param;
1316 glsl_src_param_t src1_param;
1317 char dst_mask[6];
1319 shader_glsl_get_write_mask(arg->dst, dst_mask);
1320 shader_glsl_append_dst(arg->buffer, arg);
1321 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1322 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1323 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1326 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1327 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1328 * GLSL uses the value as-is. */
1329 void shader_glsl_pow(SHADER_OPCODE_ARG *arg) {
1330 SHADER_BUFFER *buffer = arg->buffer;
1331 glsl_src_param_t src0_param;
1332 glsl_src_param_t src1_param;
1333 DWORD dst_write_mask;
1334 size_t dst_size;
1336 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1337 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1339 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1340 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1342 if (dst_size > 1) {
1343 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1344 } else {
1345 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1349 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1350 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1351 * GLSL uses the value as-is. */
1352 void shader_glsl_log(SHADER_OPCODE_ARG *arg) {
1353 SHADER_BUFFER *buffer = arg->buffer;
1354 glsl_src_param_t src0_param;
1355 DWORD dst_write_mask;
1356 size_t dst_size;
1358 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1359 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1361 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1363 if (dst_size > 1) {
1364 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1365 } else {
1366 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1370 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1371 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1372 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1373 SHADER_BUFFER* buffer = arg->buffer;
1374 glsl_src_param_t src_param;
1375 const char *instruction;
1376 char arguments[256];
1377 DWORD write_mask;
1378 unsigned i;
1380 /* Determine the GLSL function to use based on the opcode */
1381 /* TODO: Possibly make this a table for faster lookups */
1382 switch (curOpcode->opcode) {
1383 case WINED3DSIO_MIN: instruction = "min"; break;
1384 case WINED3DSIO_MAX: instruction = "max"; break;
1385 case WINED3DSIO_ABS: instruction = "abs"; break;
1386 case WINED3DSIO_FRC: instruction = "fract"; break;
1387 case WINED3DSIO_NRM: instruction = "normalize"; break;
1388 case WINED3DSIO_LOGP:
1389 case WINED3DSIO_LOG: instruction = "log2"; break;
1390 case WINED3DSIO_EXP: instruction = "exp2"; break;
1391 case WINED3DSIO_SGN: instruction = "sign"; break;
1392 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1393 case WINED3DSIO_DSY: instruction = "dFdy"; break;
1394 default: instruction = "";
1395 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1396 break;
1399 write_mask = shader_glsl_append_dst(buffer, arg);
1401 arguments[0] = '\0';
1402 if (curOpcode->num_params > 0) {
1403 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1404 strcat(arguments, src_param.param_str);
1405 for (i = 2; i < curOpcode->num_params; ++i) {
1406 strcat(arguments, ", ");
1407 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1408 strcat(arguments, src_param.param_str);
1412 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1415 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1416 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1417 * dst.x = 2^(floor(src))
1418 * dst.y = src - floor(src)
1419 * dst.z = 2^src (partial precision is allowed, but optional)
1420 * dst.w = 1.0;
1421 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1422 * dst = 2^src; (partial precision is allowed, but optional)
1424 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1425 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1426 glsl_src_param_t src_param;
1428 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1430 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1431 char dst_mask[6];
1433 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1434 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1435 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1436 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1438 shader_glsl_append_dst(arg->buffer, arg);
1439 shader_glsl_get_write_mask(arg->dst, dst_mask);
1440 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1441 } else {
1442 DWORD write_mask;
1443 size_t mask_size;
1445 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1446 mask_size = shader_glsl_get_write_mask_size(write_mask);
1448 if (mask_size > 1) {
1449 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1450 } else {
1451 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1456 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1457 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1458 glsl_src_param_t src_param;
1459 DWORD write_mask;
1460 size_t mask_size;
1462 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1463 mask_size = shader_glsl_get_write_mask_size(write_mask);
1464 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1466 if (mask_size > 1) {
1467 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1468 } else {
1469 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1473 void shader_glsl_rsq(SHADER_OPCODE_ARG* arg) {
1474 SHADER_BUFFER* buffer = arg->buffer;
1475 glsl_src_param_t src_param;
1476 DWORD write_mask;
1477 size_t mask_size;
1479 write_mask = shader_glsl_append_dst(buffer, arg);
1480 mask_size = shader_glsl_get_write_mask_size(write_mask);
1482 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1484 if (mask_size > 1) {
1485 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1486 } else {
1487 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1491 /** Process signed comparison opcodes in GLSL. */
1492 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1493 glsl_src_param_t src0_param;
1494 glsl_src_param_t src1_param;
1495 DWORD write_mask;
1496 size_t mask_size;
1498 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1499 mask_size = shader_glsl_get_write_mask_size(write_mask);
1500 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1501 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1503 if (mask_size > 1) {
1504 const char *compare;
1506 switch(arg->opcode->opcode) {
1507 case WINED3DSIO_SLT: compare = "lessThan"; break;
1508 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1509 default: compare = "";
1510 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1513 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1514 src0_param.param_str, src1_param.param_str);
1515 } else {
1516 const char *compare;
1518 switch(arg->opcode->opcode) {
1519 case WINED3DSIO_SLT: compare = "<"; break;
1520 case WINED3DSIO_SGE: compare = ">="; break;
1521 default: compare = "";
1522 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1525 shader_addline(arg->buffer, "(%s %s %s) ? 1.0 : 0.0);\n",
1526 src0_param.param_str, compare, src1_param.param_str);
1530 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1531 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1532 glsl_src_param_t src0_param;
1533 glsl_src_param_t src1_param;
1534 glsl_src_param_t src2_param;
1535 DWORD write_mask, cmp_channel = 0;
1536 unsigned int i, j;
1537 char mask_char[6];
1538 BOOL temp_destination = FALSE;
1540 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1541 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1542 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1543 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1544 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1545 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1546 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1547 DWORD dstregtype = shader_get_regtype(arg->dst);
1549 /* Cycle through all source0 channels */
1550 for (i=0; i<4; i++) {
1551 write_mask = 0;
1552 /* Find the destination channels which use the current source0 channel */
1553 for (j=0; j<4; j++) {
1554 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1555 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1556 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1560 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1561 * The first lines may overwrite source parameters of the following lines.
1562 * Deal with that by using a temporary destination register if needed
1564 if((src0reg == dstreg && src0regtype == dstregtype) ||
1565 (src1reg == dstreg && src1regtype == dstregtype) ||
1566 (src2reg == dstreg && src2regtype == dstregtype)) {
1568 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1569 if (!write_mask) continue;
1570 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1571 temp_destination = TRUE;
1572 } else {
1573 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1574 if (!write_mask) continue;
1577 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1578 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1579 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1581 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1582 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1585 if(temp_destination) {
1586 shader_glsl_get_write_mask(arg->dst, mask_char);
1587 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1588 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1593 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1594 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1595 * the compare is done per component of src0. */
1596 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1597 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1598 glsl_src_param_t src0_param;
1599 glsl_src_param_t src1_param;
1600 glsl_src_param_t src2_param;
1601 DWORD write_mask, cmp_channel = 0;
1602 unsigned int i, j;
1604 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1605 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1606 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1607 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1608 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1610 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1611 if(arg->opcode_token & WINED3DSI_COISSUE) {
1612 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1613 } else {
1614 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1615 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1617 return;
1619 /* Cycle through all source0 channels */
1620 for (i=0; i<4; i++) {
1621 write_mask = 0;
1622 /* Find the destination channels which use the current source0 channel */
1623 for (j=0; j<4; j++) {
1624 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1625 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1626 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1629 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1630 if (!write_mask) continue;
1632 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1633 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1634 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1636 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1637 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1641 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1642 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1643 glsl_src_param_t src0_param;
1644 glsl_src_param_t src1_param;
1645 glsl_src_param_t src2_param;
1646 DWORD write_mask;
1648 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1649 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1650 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1651 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1652 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1653 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1656 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1657 Vertex shaders to GLSL codes */
1658 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1659 int i;
1660 int nComponents = 0;
1661 SHADER_OPCODE_ARG tmpArg;
1663 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1665 /* Set constants for the temporary argument */
1666 tmpArg.shader = arg->shader;
1667 tmpArg.buffer = arg->buffer;
1668 tmpArg.src[0] = arg->src[0];
1669 tmpArg.src_addr[0] = arg->src_addr[0];
1670 tmpArg.src_addr[1] = arg->src_addr[1];
1671 tmpArg.reg_maps = arg->reg_maps;
1673 switch(arg->opcode->opcode) {
1674 case WINED3DSIO_M4x4:
1675 nComponents = 4;
1676 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1677 break;
1678 case WINED3DSIO_M4x3:
1679 nComponents = 3;
1680 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1681 break;
1682 case WINED3DSIO_M3x4:
1683 nComponents = 4;
1684 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1685 break;
1686 case WINED3DSIO_M3x3:
1687 nComponents = 3;
1688 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1689 break;
1690 case WINED3DSIO_M3x2:
1691 nComponents = 2;
1692 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1693 break;
1694 default:
1695 break;
1698 for (i = 0; i < nComponents; i++) {
1699 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1700 tmpArg.src[1] = arg->src[1]+i;
1701 shader_glsl_dot(&tmpArg);
1706 The LRP instruction performs a component-wise linear interpolation
1707 between the second and third operands using the first operand as the
1708 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1709 This is equivalent to mix(src2, src1, src0);
1711 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1712 glsl_src_param_t src0_param;
1713 glsl_src_param_t src1_param;
1714 glsl_src_param_t src2_param;
1715 DWORD write_mask;
1717 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1719 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1720 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1721 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1723 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1724 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1727 /** Process the WINED3DSIO_LIT instruction in GLSL:
1728 * dst.x = dst.w = 1.0
1729 * dst.y = (src0.x > 0) ? src0.x
1730 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1731 * where src.w is clamped at +- 128
1733 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1734 glsl_src_param_t src0_param;
1735 glsl_src_param_t src1_param;
1736 glsl_src_param_t src3_param;
1737 char dst_mask[6];
1739 shader_glsl_append_dst(arg->buffer, arg);
1740 shader_glsl_get_write_mask(arg->dst, dst_mask);
1742 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1743 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1744 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1746 shader_addline(arg->buffer, "vec4(1.0, (%s > 0.0 ? %s : 0.0), (%s > 0.0 ? ((%s > 0.0) ? pow(%s, clamp(%s, -128.0, 128.0)) : 0.0) : 0.0), 1.0)%s);\n",
1747 src0_param.param_str, src0_param.param_str, src0_param.param_str, src1_param.param_str, src1_param.param_str, src3_param.param_str, dst_mask);
1750 /** Process the WINED3DSIO_DST instruction in GLSL:
1751 * dst.x = 1.0
1752 * dst.y = src0.x * src0.y
1753 * dst.z = src0.z
1754 * dst.w = src1.w
1756 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1757 glsl_src_param_t src0y_param;
1758 glsl_src_param_t src0z_param;
1759 glsl_src_param_t src1y_param;
1760 glsl_src_param_t src1w_param;
1761 char dst_mask[6];
1763 shader_glsl_append_dst(arg->buffer, arg);
1764 shader_glsl_get_write_mask(arg->dst, dst_mask);
1766 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1767 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1768 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1769 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1771 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1772 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1775 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1776 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1777 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1779 * dst.x = cos(src0.?)
1780 * dst.y = sin(src0.?)
1781 * dst.z = dst.z
1782 * dst.w = dst.w
1784 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1785 glsl_src_param_t src0_param;
1786 DWORD write_mask;
1788 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1789 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1791 switch (write_mask) {
1792 case WINED3DSP_WRITEMASK_0:
1793 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1794 break;
1796 case WINED3DSP_WRITEMASK_1:
1797 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1798 break;
1800 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1801 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1802 break;
1804 default:
1805 ERR("Write mask should be .x, .y or .xy\n");
1806 break;
1810 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1811 * Start a for() loop where src1.y is the initial value of aL,
1812 * increment aL by src1.z for a total of src1.x iterations.
1813 * Need to use a temporary variable for this operation.
1815 /* FIXME: I don't think nested loops will work correctly this way. */
1816 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1817 glsl_src_param_t src1_param;
1819 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1821 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1822 src1_param.reg_name, src1_param.reg_name, src1_param.reg_name);
1825 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1826 shader_addline(arg->buffer, "}\n");
1829 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1830 glsl_src_param_t src0_param;
1832 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1833 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s; tmpInt++) {\n", src0_param.param_str);
1836 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1837 glsl_src_param_t src0_param;
1839 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1840 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
1843 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1844 glsl_src_param_t src0_param;
1845 glsl_src_param_t src1_param;
1847 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1848 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1850 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1851 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1854 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1855 shader_addline(arg->buffer, "} else {\n");
1858 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1859 shader_addline(arg->buffer, "break;\n");
1862 /* FIXME: According to MSDN the compare is done per component. */
1863 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1864 glsl_src_param_t src0_param;
1865 glsl_src_param_t src1_param;
1867 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1868 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1870 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1871 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1874 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1876 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1877 shader_addline(arg->buffer, "}\n");
1878 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1881 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1882 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1883 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1886 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1887 glsl_src_param_t src1_param;
1889 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1890 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1891 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_param.param_str, snum);
1894 /*********************************************
1895 * Pixel Shader Specific Code begins here
1896 ********************************************/
1897 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1898 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1899 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1900 DWORD hex_version = This->baseShader.hex_version;
1901 char dst_swizzle[6];
1902 glsl_sample_function_t sample_function;
1903 DWORD sampler_type;
1904 DWORD sampler_idx;
1905 BOOL projected;
1906 DWORD mask = 0;
1908 /* All versions have a destination register */
1909 shader_glsl_append_dst(arg->buffer, arg);
1911 /* 1.0-1.4: Use destination register as sampler source.
1912 * 2.0+: Use provided sampler source. */
1913 if (hex_version < WINED3DPS_VERSION(1,4)) {
1914 DWORD flags;
1916 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1917 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1919 if (flags & WINED3DTTFF_PROJECTED) {
1920 projected = TRUE;
1921 switch (flags & ~WINED3DTTFF_PROJECTED) {
1922 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1923 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
1924 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
1925 case WINED3DTTFF_COUNT4:
1926 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
1928 } else {
1929 projected = FALSE;
1931 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
1932 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1933 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1935 if (src_mod == WINED3DSPSM_DZ) {
1936 projected = TRUE;
1937 mask = WINED3DSP_WRITEMASK_2;
1938 } else if (src_mod == WINED3DSPSM_DW) {
1939 projected = TRUE;
1940 mask = WINED3DSP_WRITEMASK_3;
1941 } else {
1942 projected = FALSE;
1944 } else {
1945 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1946 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
1947 /* ps 2.0 texldp instruction always divides by the fourth component. */
1948 projected = TRUE;
1949 mask = WINED3DSP_WRITEMASK_3;
1950 } else {
1951 projected = FALSE;
1955 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1956 shader_glsl_get_sample_function(sampler_type, projected, &sample_function);
1957 mask |= sample_function.coord_mask;
1959 if (hex_version < WINED3DPS_VERSION(2,0)) {
1960 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
1961 } else {
1962 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
1965 /* 1.0-1.3: Use destination register as coordinate source.
1966 1.4+: Use provided coordinate source register. */
1967 if (hex_version < WINED3DPS_VERSION(1,4)) {
1968 char coord_mask[6];
1969 shader_glsl_get_write_mask(mask, coord_mask);
1970 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
1971 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
1972 } else {
1973 glsl_src_param_t coord_param;
1974 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
1975 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
1976 glsl_src_param_t bias;
1977 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
1979 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
1980 sample_function.name, sampler_idx, coord_param.param_str,
1981 bias.param_str, dst_swizzle);
1982 } else {
1983 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
1984 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
1989 void shader_glsl_texldl(SHADER_OPCODE_ARG* arg) {
1990 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
1991 glsl_sample_function_t sample_function;
1992 glsl_src_param_t coord_param, lod_param;
1993 char dst_swizzle[6];
1994 DWORD sampler_type;
1995 DWORD sampler_idx;
1997 shader_glsl_append_dst(arg->buffer, arg);
1998 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2000 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2001 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2002 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2003 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &coord_param);
2005 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2007 if (shader_is_pshader_version(This->baseShader.hex_version)) {
2008 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2009 * However, they seem to work just fine in fragment shaders as well. */
2010 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2011 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2012 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2013 } else {
2014 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2015 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2019 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
2021 /* FIXME: Make this work for more than just 2D textures */
2023 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2024 SHADER_BUFFER* buffer = arg->buffer;
2025 DWORD hex_version = This->baseShader.hex_version;
2026 DWORD write_mask;
2027 char dst_mask[6];
2029 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2030 shader_glsl_get_write_mask(write_mask, dst_mask);
2032 if (hex_version != WINED3DPS_VERSION(1,4)) {
2033 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2034 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2035 } else {
2036 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2037 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2038 char dst_swizzle[6];
2040 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2042 if (src_mod == WINED3DSPSM_DZ) {
2043 glsl_src_param_t div_param;
2044 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
2045 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2047 if (mask_size > 1) {
2048 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2049 } else {
2050 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2052 } else if (src_mod == WINED3DSPSM_DW) {
2053 glsl_src_param_t div_param;
2054 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
2055 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2057 if (mask_size > 1) {
2058 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2059 } else {
2060 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2062 } else {
2063 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2068 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2069 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2070 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2071 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
2072 glsl_src_param_t src0_param;
2073 char dst_mask[6];
2074 glsl_sample_function_t sample_function;
2075 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2076 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2077 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2079 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2081 shader_glsl_append_dst(arg->buffer, arg);
2082 shader_glsl_get_write_mask(arg->dst, dst_mask);
2084 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2085 * scalar, and projected sampling would require 4
2087 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2089 switch(count_bits(sample_function.coord_mask)) {
2090 case 1:
2091 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2092 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2093 break;
2095 case 2:
2096 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2097 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2098 break;
2100 case 3:
2101 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2102 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2103 break;
2104 default:
2105 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2109 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2110 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2111 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
2112 glsl_src_param_t src0_param;
2113 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2114 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2115 DWORD dst_mask;
2116 size_t mask_size;
2118 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2119 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2120 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2122 if (mask_size > 1) {
2123 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2124 } else {
2125 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2129 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2130 * Calculate the depth as dst.x / dst.y */
2131 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
2132 glsl_dst_param_t dst_param;
2134 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2136 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2137 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2138 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2139 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2140 * >= 1.0 or < 0.0
2142 shader_addline(arg->buffer, "gl_FragDepth = (%s.y == 0.0) ? 1.0 : clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n", dst_param.reg_name, dst_param.reg_name, dst_param.reg_name);
2145 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2146 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2147 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2148 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2150 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
2151 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2152 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2153 glsl_src_param_t src0_param;
2155 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2157 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2158 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2161 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2162 * Calculate the 1st of a 2-row matrix multiplication. */
2163 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
2164 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2165 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2166 SHADER_BUFFER* buffer = arg->buffer;
2167 glsl_src_param_t src0_param;
2169 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2170 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2173 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2174 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2175 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
2177 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2178 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2179 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2180 SHADER_BUFFER* buffer = arg->buffer;
2181 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2182 glsl_src_param_t src0_param;
2184 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2185 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2186 current_state->texcoord_w[current_state->current_row++] = reg;
2189 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
2190 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2191 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2192 SHADER_BUFFER* buffer = arg->buffer;
2193 glsl_src_param_t src0_param;
2194 char dst_mask[6];
2196 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2197 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2199 shader_glsl_append_dst(buffer, arg);
2200 shader_glsl_get_write_mask(arg->dst, dst_mask);
2202 /* Sample the texture using the calculated coordinates */
2203 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2206 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2207 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2208 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
2209 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2210 glsl_src_param_t src0_param;
2211 char dst_mask[6];
2212 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2213 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2214 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2215 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2216 glsl_sample_function_t sample_function;
2218 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2219 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2221 shader_glsl_append_dst(arg->buffer, arg);
2222 shader_glsl_get_write_mask(arg->dst, dst_mask);
2223 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2225 /* Sample the texture using the calculated coordinates */
2226 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2228 current_state->current_row = 0;
2231 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2232 * Perform the 3rd row of a 3x3 matrix multiply */
2233 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
2234 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2235 glsl_src_param_t src0_param;
2236 char dst_mask[6];
2237 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2238 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2239 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2241 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2243 shader_glsl_append_dst(arg->buffer, arg);
2244 shader_glsl_get_write_mask(arg->dst, dst_mask);
2245 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2247 current_state->current_row = 0;
2250 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2251 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2252 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
2254 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2255 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2256 glsl_src_param_t src0_param;
2257 glsl_src_param_t src1_param;
2258 char dst_mask[6];
2259 SHADER_BUFFER* buffer = arg->buffer;
2260 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2261 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2262 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2263 glsl_sample_function_t sample_function;
2265 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2266 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2268 /* Perform the last matrix multiply operation */
2269 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2270 /* Reflection calculation */
2271 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2273 shader_glsl_append_dst(buffer, arg);
2274 shader_glsl_get_write_mask(arg->dst, dst_mask);
2275 shader_glsl_get_sample_function(stype, FALSE, &sample_function);
2277 /* Sample the texture */
2278 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2280 current_state->current_row = 0;
2283 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2284 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2285 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
2287 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2288 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2289 SHADER_BUFFER* buffer = arg->buffer;
2290 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2291 glsl_src_param_t src0_param;
2292 char dst_mask[6];
2293 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2294 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2295 glsl_sample_function_t sample_function;
2297 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2299 /* Perform the last matrix multiply operation */
2300 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2302 /* Construct the eye-ray vector from w coordinates */
2303 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2304 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2305 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2307 shader_glsl_append_dst(buffer, arg);
2308 shader_glsl_get_write_mask(arg->dst, dst_mask);
2309 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2311 /* Sample the texture using the calculated coordinates */
2312 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2314 current_state->current_row = 0;
2317 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2318 * Apply a fake bump map transform.
2319 * texbem is pshader <= 1.3 only, this saves a few version checks
2321 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
2322 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2323 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2324 char dst_swizzle[6];
2325 glsl_sample_function_t sample_function;
2326 glsl_src_param_t coord_param;
2327 DWORD sampler_type;
2328 DWORD sampler_idx;
2329 DWORD mask;
2330 DWORD flags;
2331 char coord_mask[6];
2333 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2334 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2336 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2337 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2338 mask = sample_function.coord_mask;
2340 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2342 shader_glsl_get_write_mask(mask, coord_mask);
2344 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2345 * so we can't let the GL handle this.
2347 if (flags & WINED3DTTFF_PROJECTED) {
2348 DWORD div_mask=0;
2349 char coord_div_mask[3];
2350 switch (flags & ~WINED3DTTFF_PROJECTED) {
2351 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2352 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2353 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2354 case WINED3DTTFF_COUNT4:
2355 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2357 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2358 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2361 shader_glsl_append_dst(arg->buffer, arg);
2362 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2363 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2364 glsl_src_param_t luminance_param;
2365 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2366 shader_addline(arg->buffer, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )*(%s * luminancescale + luminanceoffset))%s);\n",
2367 sample_function.name, sampler_idx, sampler_idx, coord_mask, coord_param.param_str, coord_mask,
2368 luminance_param.param_str, dst_swizzle);
2369 } else {
2370 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )%s);\n",
2371 sample_function.name, sampler_idx, sampler_idx, coord_mask, coord_param.param_str, coord_mask, dst_swizzle);
2375 void pshader_glsl_bem(SHADER_OPCODE_ARG* arg) {
2376 glsl_src_param_t src0_param, src1_param;
2378 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2379 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2381 shader_glsl_append_dst(arg->buffer, arg);
2382 shader_addline(arg->buffer, "%s + bumpenvmat * %s);\n",
2383 src0_param.param_str, src1_param.param_str);
2386 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2387 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2388 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
2390 glsl_src_param_t src0_param;
2391 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2392 char dst_mask[6];
2394 shader_glsl_append_dst(arg->buffer, arg);
2395 shader_glsl_get_write_mask(arg->dst, dst_mask);
2396 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2398 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2401 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2402 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2403 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
2404 glsl_src_param_t src0_param;
2405 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2406 char dst_mask[6];
2408 shader_glsl_append_dst(arg->buffer, arg);
2409 shader_glsl_get_write_mask(arg->dst, dst_mask);
2410 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2412 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2415 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2416 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2417 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
2418 glsl_src_param_t src0_param;
2419 char dst_mask[6];
2420 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2421 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2422 glsl_sample_function_t sample_function;
2424 shader_glsl_append_dst(arg->buffer, arg);
2425 shader_glsl_get_write_mask(arg->dst, dst_mask);
2426 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2427 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2429 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2432 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2433 * If any of the first 3 components are < 0, discard this pixel */
2434 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
2435 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2436 DWORD hex_version = This->baseShader.hex_version;
2437 glsl_dst_param_t dst_param;
2439 /* The argument is a destination parameter, and no writemasks are allowed */
2440 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2441 if((hex_version >= WINED3DPS_VERSION(2,0))) {
2442 /* 2.0 shaders compare all 4 components in texkill */
2443 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2444 } else {
2445 /* 1.X shaders only compare the first 3 components, propably due to the nature of the texkill
2446 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2447 * 4 components are defined, only the first 3 are used
2449 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2453 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2454 * dst = dot2(src0, src1) + src2 */
2455 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
2456 glsl_src_param_t src0_param;
2457 glsl_src_param_t src1_param;
2458 glsl_src_param_t src2_param;
2459 DWORD write_mask;
2460 size_t mask_size;
2462 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2463 mask_size = shader_glsl_get_write_mask_size(write_mask);
2465 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2466 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2467 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2469 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2472 void pshader_glsl_input_pack(
2473 SHADER_BUFFER* buffer,
2474 semantic* semantics_in) {
2476 unsigned int i;
2478 for (i = 0; i < MAX_REG_INPUT; i++) {
2480 DWORD usage_token = semantics_in[i].usage;
2481 DWORD register_token = semantics_in[i].reg;
2482 DWORD usage, usage_idx;
2483 char reg_mask[6];
2485 /* Uninitialized */
2486 if (!usage_token) continue;
2487 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2488 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2489 shader_glsl_get_write_mask(register_token, reg_mask);
2491 switch(usage) {
2493 case WINED3DDECLUSAGE_COLOR:
2494 if (usage_idx == 0)
2495 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
2496 i, reg_mask, reg_mask);
2497 else if (usage_idx == 1)
2498 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
2499 i, reg_mask, reg_mask);
2500 else
2501 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
2502 i, reg_mask, reg_mask);
2503 break;
2505 case WINED3DDECLUSAGE_TEXCOORD:
2506 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
2507 i, reg_mask, usage_idx, reg_mask );
2508 break;
2510 case WINED3DDECLUSAGE_FOG:
2511 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
2512 i, reg_mask, reg_mask);
2513 break;
2515 default:
2516 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
2517 i, reg_mask, reg_mask);
2522 /*********************************************
2523 * Vertex Shader Specific Code begins here
2524 ********************************************/
2526 void vshader_glsl_output_unpack(
2527 SHADER_BUFFER* buffer,
2528 semantic* semantics_out) {
2530 unsigned int i;
2532 for (i = 0; i < MAX_REG_OUTPUT; i++) {
2534 DWORD usage_token = semantics_out[i].usage;
2535 DWORD register_token = semantics_out[i].reg;
2536 DWORD usage, usage_idx;
2537 char reg_mask[6];
2539 /* Uninitialized */
2540 if (!usage_token) continue;
2542 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2543 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2544 shader_glsl_get_write_mask(register_token, reg_mask);
2546 switch(usage) {
2548 case WINED3DDECLUSAGE_COLOR:
2549 if (usage_idx == 0)
2550 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2551 else if (usage_idx == 1)
2552 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2553 else
2554 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2555 break;
2557 case WINED3DDECLUSAGE_POSITION:
2558 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2559 break;
2561 case WINED3DDECLUSAGE_TEXCOORD:
2562 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
2563 usage_idx, reg_mask, i, reg_mask);
2564 break;
2566 case WINED3DDECLUSAGE_PSIZE:
2567 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
2568 break;
2570 case WINED3DDECLUSAGE_FOG:
2571 shader_addline(buffer, "gl_FogFragCoord = OUT%u%s;\n", i, reg_mask);
2572 break;
2574 default:
2575 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2580 static void add_glsl_program_entry(IWineD3DDeviceImpl *device, struct glsl_shader_prog_link *entry) {
2581 glsl_program_key_t *key;
2583 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2584 key->vshader = entry->vshader;
2585 key->pshader = entry->pshader;
2587 hash_table_put(device->glsl_program_lookup, key, entry);
2590 static struct glsl_shader_prog_link *get_glsl_program_entry(IWineD3DDeviceImpl *device,
2591 GLhandleARB vshader, GLhandleARB pshader) {
2592 glsl_program_key_t key;
2594 key.vshader = vshader;
2595 key.pshader = pshader;
2597 return (struct glsl_shader_prog_link *)hash_table_get(device->glsl_program_lookup, &key);
2600 void delete_glsl_program_entry(IWineD3DDevice *iface, struct glsl_shader_prog_link *entry) {
2601 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2602 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2603 glsl_program_key_t *key;
2605 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2606 key->vshader = entry->vshader;
2607 key->pshader = entry->pshader;
2608 hash_table_remove(This->glsl_program_lookup, key);
2610 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2611 if (entry->vshader) list_remove(&entry->vshader_entry);
2612 if (entry->pshader) list_remove(&entry->pshader_entry);
2613 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2614 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2615 HeapFree(GetProcessHeap(), 0, entry);
2618 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
2619 * It sets the programId on the current StateBlock (because it should be called
2620 * inside of the DrawPrimitive() part of the render loop).
2622 * If a program for the given combination does not exist, create one, and store
2623 * the program in the hash table. If it creates a program, it will link the
2624 * given objects, too.
2626 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
2627 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2628 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2629 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
2630 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
2631 struct glsl_shader_prog_link *entry = NULL;
2632 GLhandleARB programId = 0;
2633 int i;
2634 char glsl_name[8];
2636 GLhandleARB vshader_id = use_vs ? ((IWineD3DBaseShaderImpl*)vshader)->baseShader.prgId : 0;
2637 GLhandleARB pshader_id = use_ps ? ((IWineD3DBaseShaderImpl*)pshader)->baseShader.prgId : 0;
2638 entry = get_glsl_program_entry(This, vshader_id, pshader_id);
2639 if (entry) {
2640 This->stateBlock->glsl_program = entry;
2641 return;
2644 /* If we get to this point, then no matching program exists, so we create one */
2645 programId = GL_EXTCALL(glCreateProgramObjectARB());
2646 TRACE("Created new GLSL shader program %u\n", programId);
2648 /* Create the entry */
2649 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
2650 entry->programId = programId;
2651 entry->vshader = vshader_id;
2652 entry->pshader = pshader_id;
2653 /* Add the hash table entry */
2654 add_glsl_program_entry(This, entry);
2656 /* Set the current program */
2657 This->stateBlock->glsl_program = entry;
2659 /* Attach GLSL vshader */
2660 if (vshader_id) {
2661 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
2662 char tmp_name[10];
2664 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
2665 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
2666 checkGLcall("glAttachObjectARB");
2668 /* Bind vertex attributes to a corresponding index number to match
2669 * the same index numbers as ARB_vertex_programs (makes loading
2670 * vertex attributes simpler). With this method, we can use the
2671 * exact same code to load the attributes later for both ARB and
2672 * GLSL shaders.
2674 * We have to do this here because we need to know the Program ID
2675 * in order to make the bindings work, and it has to be done prior
2676 * to linking the GLSL program. */
2677 for (i = 0; i < max_attribs; ++i) {
2678 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
2679 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
2681 checkGLcall("glBindAttribLocationARB");
2683 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
2686 /* Attach GLSL pshader */
2687 if (pshader_id) {
2688 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
2689 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
2690 checkGLcall("glAttachObjectARB");
2692 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
2695 /* Link the program */
2696 TRACE("Linking GLSL shader program %u\n", programId);
2697 GL_EXTCALL(glLinkProgramARB(programId));
2698 print_glsl_info_log(&GLINFO_LOCATION, programId);
2700 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
2701 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
2702 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
2703 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2705 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
2706 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
2707 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
2708 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2712 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
2713 GLhandleARB program_id;
2714 GLhandleARB vshader_id, pshader_id;
2715 const char *blt_vshader[] = {
2716 "void main(void)\n"
2717 "{\n"
2718 " gl_Position = gl_Vertex;\n"
2719 " gl_FrontColor = vec4(1.0);\n"
2720 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2721 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2722 "}\n"
2725 const char *blt_pshader[] = {
2726 "uniform sampler2D sampler;\n"
2727 "void main(void)\n"
2728 "{\n"
2729 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2730 "}\n"
2733 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2734 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2735 GL_EXTCALL(glCompileShaderARB(vshader_id));
2737 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2738 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2739 GL_EXTCALL(glCompileShaderARB(pshader_id));
2741 program_id = GL_EXTCALL(glCreateProgramObjectARB());
2742 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2743 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2744 GL_EXTCALL(glLinkProgramARB(program_id));
2746 print_glsl_info_log(&GLINFO_LOCATION, program_id);
2748 return program_id;
2751 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2752 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2753 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2754 GLhandleARB program_id = 0;
2756 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
2757 else This->stateBlock->glsl_program = NULL;
2759 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2760 if (program_id) TRACE("Using GLSL program %u\n", program_id);
2761 GL_EXTCALL(glUseProgramObjectARB(program_id));
2762 checkGLcall("glUseProgramObjectARB");
2765 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2766 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2767 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2768 static GLhandleARB program_id = 0;
2769 static GLhandleARB loc = -1;
2771 if (!program_id) {
2772 program_id = create_glsl_blt_shader(gl_info);
2773 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2776 GL_EXTCALL(glUseProgramObjectARB(program_id));
2777 GL_EXTCALL(glUniform1iARB(loc, 0));
2780 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
2781 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2782 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2783 GL_EXTCALL(glUseProgramObjectARB(0));
2786 const shader_backend_t glsl_shader_backend = {
2787 &shader_glsl_select,
2788 &shader_glsl_select_depth_blt,
2789 &shader_glsl_load_constants,
2790 &shader_glsl_cleanup,
2791 &shader_glsl_color_correction