VirtualBox

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

Last change on this file since 40647 was 40480, checked in by vboxsync, 13 years ago

wined3d: build log bugfix, max_uniforms tmp workaround for win8

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

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