VirtualBox

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

Last change on this file since 20817 was 20612, checked in by vboxsync, 16 years ago

crOpenGL: update wine to 1.1.23

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