VirtualBox

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

Last change on this file since 97405 was 94385, checked in by vboxsync, 3 years ago

Devices,SVGA: Fix some memory leaks in the OpenGL backend

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