VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/shaderlib/glsl_shader.c@ 53201

Last change on this file since 53201 was 53201, checked in by vboxsync, 10 years ago

Devices/Main: vmsvga updates

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

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