VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/glsl_shader.c@ 16684

Last change on this file since 16684 was 16477, checked in by vboxsync, 16 years ago

LGPL disclaimer by filemuncher

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette