VirtualBox

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

Last change on this file since 83626 was 83626, checked in by vboxsync, 5 years ago

VMSVGA: consider only allowed output reg names, bugref:9668

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