2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
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.
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.
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
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
33 #include "wine/port.h"
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
48 #define WINED3D_GLSL_SAMPLE_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
55 unsigned int coord_size
;
56 unsigned int resinfo_size
;
57 const char *type_part
;
59 resource_type_info
[] =
61 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
62 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
63 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
64 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
66 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
67 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
68 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
69 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
70 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
85 struct glsl_sample_function
87 struct wined3d_string_buffer
*name
;
88 unsigned int coord_mask
;
89 unsigned int deriv_mask
;
90 enum wined3d_data_type data_type
;
91 BOOL output_single_component
;
92 unsigned int offset_size
;
97 HEAP_NODE_TRAVERSE_LEFT
,
98 HEAP_NODE_TRAVERSE_RIGHT
,
102 struct constant_entry
105 unsigned int version
;
110 struct constant_entry
*entries
;
112 unsigned int *positions
;
116 /* GLSL shader private data */
117 struct shader_glsl_priv
{
118 struct wined3d_string_buffer shader_buffer
;
119 struct wined3d_string_buffer_list string_buffers
;
120 struct wine_rb_tree program_lookup
;
121 struct constant_heap vconst_heap
;
122 struct constant_heap pconst_heap
;
123 unsigned char *stack
;
124 UINT next_constant_version
;
126 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
127 const struct fragment_pipeline
*fragment_pipe
;
128 struct wine_rb_tree ffp_vertex_shaders
;
129 struct wine_rb_tree ffp_fragment_shaders
;
130 BOOL ffp_proj_control
;
131 BOOL legacy_lighting
;
134 struct glsl_vs_program
136 struct list shader_entry
;
138 GLenum vertex_color_clamp
;
139 GLint uniform_f_locations
[WINED3D_MAX_VS_CONSTS_F
];
140 GLint uniform_i_locations
[WINED3D_MAX_CONSTS_I
];
141 GLint uniform_b_locations
[WINED3D_MAX_CONSTS_B
];
142 GLint pos_fixup_location
;
144 GLint modelview_matrix_location
[MAX_VERTEX_BLENDS
];
145 GLint projection_matrix_location
;
146 GLint normal_matrix_location
;
147 GLint texture_matrix_location
[MAX_TEXTURES
];
148 GLint material_ambient_location
;
149 GLint material_diffuse_location
;
150 GLint material_specular_location
;
151 GLint material_emissive_location
;
152 GLint material_shininess_location
;
153 GLint light_ambient_location
;
168 } light_location
[MAX_ACTIVE_LIGHTS
];
169 GLint pointsize_location
;
170 GLint pointsize_min_location
;
171 GLint pointsize_max_location
;
172 GLint pointsize_c_att_location
;
173 GLint pointsize_l_att_location
;
174 GLint pointsize_q_att_location
;
175 GLint clip_planes_location
;
178 struct glsl_gs_program
180 struct list shader_entry
;
183 GLint pos_fixup_location
;
186 struct glsl_ps_program
188 struct list shader_entry
;
190 GLint uniform_f_locations
[WINED3D_MAX_PS_CONSTS_F
];
191 GLint uniform_i_locations
[WINED3D_MAX_CONSTS_I
];
192 GLint uniform_b_locations
[WINED3D_MAX_CONSTS_B
];
193 GLint bumpenv_mat_location
[MAX_TEXTURES
];
194 GLint bumpenv_lum_scale_location
[MAX_TEXTURES
];
195 GLint bumpenv_lum_offset_location
[MAX_TEXTURES
];
196 GLint tss_constant_location
[MAX_TEXTURES
];
197 GLint tex_factor_location
;
198 GLint specular_enable_location
;
199 GLint fog_color_location
;
200 GLint fog_density_location
;
201 GLint fog_end_location
;
202 GLint fog_scale_location
;
203 GLint alpha_test_ref_location
;
204 GLint ycorrection_location
;
205 GLint np2_fixup_location
;
206 GLint color_key_location
;
207 const struct ps_np2fixup_info
*np2_fixup_info
;
210 /* Struct to maintain data about a linked GLSL program */
211 struct glsl_shader_prog_link
213 struct wine_rb_entry program_lookup_entry
;
214 struct glsl_vs_program vs
;
215 struct glsl_gs_program gs
;
216 struct glsl_ps_program ps
;
218 DWORD constant_update_mask
;
219 UINT constant_version
;
222 struct glsl_program_key
229 struct shader_glsl_ctx_priv
{
230 const struct vs_compile_args
*cur_vs_args
;
231 const struct ps_compile_args
*cur_ps_args
;
232 struct ps_np2fixup_info
*cur_np2fixup_info
;
233 struct wined3d_string_buffer_list
*string_buffers
;
236 struct glsl_context_data
238 struct glsl_shader_prog_link
*glsl_program
;
241 struct glsl_ps_compiled_shader
243 struct ps_compile_args args
;
244 struct ps_np2fixup_info np2fixup
;
248 struct glsl_vs_compiled_shader
250 struct vs_compile_args args
;
254 struct glsl_gs_compiled_shader
256 struct gs_compile_args args
;
260 struct glsl_shader_private
264 struct glsl_vs_compiled_shader
*vs
;
265 struct glsl_gs_compiled_shader
*gs
;
266 struct glsl_ps_compiled_shader
*ps
;
268 UINT num_gl_shaders
, shader_array_size
;
271 struct glsl_ffp_vertex_shader
273 struct wined3d_ffp_vs_desc desc
;
275 struct list linked_programs
;
278 struct glsl_ffp_fragment_shader
280 struct ffp_frag_desc entry
;
282 struct list linked_programs
;
285 struct glsl_ffp_destroy_ctx
287 struct shader_glsl_priv
*priv
;
288 const struct wined3d_gl_info
*gl_info
;
291 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context
*ctx
);
293 static const char *debug_gl_shader_type(GLenum type
)
297 #define WINED3D_TO_STR(u) case u: return #u
298 WINED3D_TO_STR(GL_VERTEX_SHADER
);
299 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER
);
300 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER
);
301 WINED3D_TO_STR(GL_GEOMETRY_SHADER
);
302 WINED3D_TO_STR(GL_FRAGMENT_SHADER
);
303 WINED3D_TO_STR(GL_COMPUTE_SHADER
);
304 #undef WINED3D_TO_STR
306 return wine_dbg_sprintf("UNKNOWN(%#x)", type
);
310 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type
)
314 case WINED3D_SHADER_TYPE_VERTEX
:
317 case WINED3D_SHADER_TYPE_HULL
:
320 case WINED3D_SHADER_TYPE_DOMAIN
:
323 case WINED3D_SHADER_TYPE_GEOMETRY
:
326 case WINED3D_SHADER_TYPE_PIXEL
:
329 case WINED3D_SHADER_TYPE_COMPUTE
:
333 FIXME("Unhandled shader type %#x.\n", type
);
338 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info
*gl_info
,
339 const struct wined3d_shader_version
*version
)
341 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
343 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 30) && version
&& version
->major
>= 4)
349 static const char *shader_glsl_get_version_declaration(const struct wined3d_gl_info
*gl_info
,
350 const struct wined3d_shader_version
*version
)
352 unsigned int glsl_version
;
354 switch (glsl_version
= shader_glsl_get_version(gl_info
, version
))
357 return "#version 150";
359 return "#version 130";
361 return "#version 120";
363 FIXME("Unexpected GLSL version %u requested.\n", glsl_version
);
368 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer
*buffer
, const float *values
)
372 wined3d_ftoa(values
[0], str
[0]);
373 wined3d_ftoa(values
[1], str
[1]);
374 wined3d_ftoa(values
[2], str
[2]);
375 wined3d_ftoa(values
[3], str
[3]);
376 shader_addline(buffer
, "vec4(%s, %s, %s, %s)", str
[0], str
[1], str
[2], str
[3]);
379 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer
*buffer
,
380 const int *values
, unsigned int size
)
384 if (!size
|| size
> 4)
386 ERR("Invalid vector size %u.\n", size
);
391 shader_addline(buffer
, "ivec%u(", size
);
393 for (i
= 0; i
< size
; ++i
)
394 shader_addline(buffer
, i
? ", %#x" : "%#x", values
[i
]);
397 shader_addline(buffer
, ")");
400 static const char *get_info_log_line(const char **ptr
)
405 if (!(q
= strstr(p
, "\n")))
407 if (!*p
) return NULL
;
416 /* Context activation is done by the caller. */
417 void print_glsl_info_log(const struct wined3d_gl_info
*gl_info
, GLuint id
, BOOL program
)
422 if (!WARN_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
426 GL_EXTCALL(glGetProgramiv(id
, GL_INFO_LOG_LENGTH
, &length
));
428 GL_EXTCALL(glGetShaderiv(id
, GL_INFO_LOG_LENGTH
, &length
));
430 /* A size of 1 is just a null-terminated string, so the log should be bigger than
431 * that if there are errors. */
434 const char *ptr
, *line
;
436 log
= HeapAlloc(GetProcessHeap(), 0, length
);
437 /* The info log is supposed to be zero-terminated, but at least some
438 * versions of fglrx don't terminate the string properly. The reported
439 * length does include the terminator, so explicitly set it to zero
443 GL_EXTCALL(glGetProgramInfoLog(id
, length
, NULL
, log
));
445 GL_EXTCALL(glGetShaderInfoLog(id
, length
, NULL
, log
));
448 if (gl_info
->quirks
& WINED3D_QUIRK_INFO_LOG_SPAM
)
450 WARN("Info log received from GLSL shader #%u:\n", id
);
451 while ((line
= get_info_log_line(&ptr
))) WARN(" %.*s", (int)(ptr
- line
), line
);
455 FIXME("Info log received from GLSL shader #%u:\n", id
);
456 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
458 HeapFree(GetProcessHeap(), 0, log
);
462 /* Context activation is done by the caller. */
463 static void shader_glsl_compile(const struct wined3d_gl_info
*gl_info
, GLuint shader
, const char *src
)
465 const char *ptr
, *line
;
467 TRACE("Compiling shader object %u.\n", shader
);
469 if (TRACE_ON(d3d_shader
))
472 while ((line
= get_info_log_line(&ptr
))) TRACE_(d3d_shader
)(" %.*s", (int)(ptr
- line
), line
);
475 GL_EXTCALL(glShaderSource(shader
, 1, &src
, NULL
));
476 checkGLcall("glShaderSource");
477 GL_EXTCALL(glCompileShader(shader
));
478 checkGLcall("glCompileShader");
479 print_glsl_info_log(gl_info
, shader
, FALSE
);
482 /* Context activation is done by the caller. */
483 static void shader_glsl_dump_program_source(const struct wined3d_gl_info
*gl_info
, GLuint program
)
485 GLint i
, shader_count
, source_size
= -1;
489 GL_EXTCALL(glGetProgramiv(program
, GL_ATTACHED_SHADERS
, &shader_count
));
490 if (!(shaders
= wined3d_calloc(shader_count
, sizeof(*shaders
))))
492 ERR("Failed to allocate shader array memory.\n");
496 GL_EXTCALL(glGetAttachedShaders(program
, shader_count
, NULL
, shaders
));
497 for (i
= 0; i
< shader_count
; ++i
)
499 const char *ptr
, *line
;
502 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_SOURCE_LENGTH
, &tmp
));
504 if (source_size
< tmp
)
506 HeapFree(GetProcessHeap(), 0, source
);
508 source
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, tmp
);
511 ERR("Failed to allocate %d bytes for shader source.\n", tmp
);
512 HeapFree(GetProcessHeap(), 0, shaders
);
518 FIXME("Shader %u:\n", shaders
[i
]);
519 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_TYPE
, &tmp
));
520 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp
));
521 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_COMPILE_STATUS
, &tmp
));
522 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp
);
526 GL_EXTCALL(glGetShaderSource(shaders
[i
], source_size
, NULL
, source
));
527 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
531 HeapFree(GetProcessHeap(), 0, source
);
532 HeapFree(GetProcessHeap(), 0, shaders
);
535 /* Context activation is done by the caller. */
536 void shader_glsl_validate_link(const struct wined3d_gl_info
*gl_info
, GLuint program
)
540 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
543 GL_EXTCALL(glGetProgramiv(program
, GL_LINK_STATUS
, &tmp
));
546 FIXME("Program %u link status invalid.\n", program
);
547 shader_glsl_dump_program_source(gl_info
, program
);
550 print_glsl_info_log(gl_info
, program
, TRUE
);
553 /* Context activation is done by the caller. */
554 static void shader_glsl_load_samplers(const struct wined3d_gl_info
*gl_info
,
555 struct shader_glsl_priv
*priv
, const DWORD
*tex_unit_map
, GLuint program_id
)
557 unsigned int mapped_unit
;
558 struct wined3d_string_buffer
*sampler_name
= string_buffer_get(&priv
->string_buffers
);
565 enum wined3d_shader_type type
;
566 unsigned int base_idx
;
571 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
572 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
575 for (i
= 0; i
< ARRAY_SIZE(sampler_info
); ++i
)
577 prefix
= shader_glsl_get_prefix(sampler_info
[i
].type
);
579 for (j
= 0; j
< sampler_info
[i
].count
; ++j
)
581 string_buffer_sprintf(sampler_name
, "%s_sampler%u", prefix
, j
);
582 name_loc
= GL_EXTCALL(glGetUniformLocation(program_id
, sampler_name
->buffer
));
586 mapped_unit
= tex_unit_map
[sampler_info
[i
].base_idx
+ j
];
587 if (mapped_unit
== WINED3D_UNMAPPED_STAGE
|| mapped_unit
>= gl_info
->limits
.combined_samplers
)
589 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name
->buffer
, mapped_unit
);
593 TRACE("Loading sampler %s on unit %u.\n", sampler_name
->buffer
, mapped_unit
);
594 GL_EXTCALL(glUniform1i(name_loc
, mapped_unit
));
597 checkGLcall("Load sampler bindings");
598 string_buffer_release(&priv
->string_buffers
, sampler_name
);
601 static void shader_glsl_load_icb(const struct wined3d_gl_info
*gl_info
, struct shader_glsl_priv
*priv
,
602 GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
604 const struct wined3d_shader_immediate_constant_buffer
*icb
= reg_maps
->icb
;
608 struct wined3d_string_buffer
*icb_name
= string_buffer_get(&priv
->string_buffers
);
609 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
612 string_buffer_sprintf(icb_name
, "%s_icb", prefix
);
613 icb_location
= GL_EXTCALL(glGetUniformLocation(program_id
, icb_name
->buffer
));
614 GL_EXTCALL(glUniform4fv(icb_location
, icb
->vec4_count
, (const GLfloat
*)icb
->data
));
615 checkGLcall("Load immediate constant buffer");
617 string_buffer_release(&priv
->string_buffers
, icb_name
);
621 /* Context activation is done by the caller. */
622 static void shader_glsl_load_images(const struct wined3d_gl_info
*gl_info
, struct shader_glsl_priv
*priv
,
623 GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
625 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
629 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
631 if (!reg_maps
->uav_resource_info
[i
].type
)
634 string_buffer_sprintf(name
, "ps_image%u", i
);
635 location
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
639 TRACE("Loading image %s on unit %u.\n", name
->buffer
, i
);
640 GL_EXTCALL(glUniform1i(location
, i
));
642 checkGLcall("Load image bindings");
643 string_buffer_release(&priv
->string_buffers
, name
);
646 /* Context activation is done by the caller. */
647 static inline void walk_constant_heap(const struct wined3d_gl_info
*gl_info
, const struct wined3d_vec4
*constants
,
648 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
650 unsigned int start
= ~0U, end
= 0;
652 unsigned int heap_idx
= 1;
655 if (heap
->entries
[heap_idx
].version
<= version
) return;
657 idx
= heap
->entries
[heap_idx
].idx
;
658 if (constant_locations
[idx
] != -1)
660 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
662 while (stack_idx
>= 0)
664 /* Note that we fall through to the next case statement. */
665 switch(stack
[stack_idx
])
667 case HEAP_NODE_TRAVERSE_LEFT
:
669 unsigned int left_idx
= heap_idx
<< 1;
670 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
673 idx
= heap
->entries
[heap_idx
].idx
;
674 if (constant_locations
[idx
] != -1)
682 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
683 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
688 case HEAP_NODE_TRAVERSE_RIGHT
:
690 unsigned int right_idx
= (heap_idx
<< 1) + 1;
691 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
693 heap_idx
= right_idx
;
694 idx
= heap
->entries
[heap_idx
].idx
;
695 if (constant_locations
[idx
] != -1)
703 stack
[stack_idx
++] = HEAP_NODE_POP
;
704 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
716 GL_EXTCALL(glUniform4fv(constant_locations
[start
], end
- start
+ 1, &constants
[start
].x
));
717 checkGLcall("walk_constant_heap()");
720 /* Context activation is done by the caller. */
721 static inline void apply_clamped_constant(const struct wined3d_gl_info
*gl_info
,
722 GLint location
, const struct wined3d_vec4
*data
)
724 GLfloat clamped_constant
[4];
726 if (location
== -1) return;
728 clamped_constant
[0] = data
->x
< -1.0f
? -1.0f
: data
->x
> 1.0f
? 1.0f
: data
->x
;
729 clamped_constant
[1] = data
->y
< -1.0f
? -1.0f
: data
->y
> 1.0f
? 1.0f
: data
->y
;
730 clamped_constant
[2] = data
->z
< -1.0f
? -1.0f
: data
->z
> 1.0f
? 1.0f
: data
->z
;
731 clamped_constant
[3] = data
->w
< -1.0f
? -1.0f
: data
->w
> 1.0f
? 1.0f
: data
->w
;
733 GL_EXTCALL(glUniform4fv(location
, 1, clamped_constant
));
736 /* Context activation is done by the caller. */
737 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info
*gl_info
,
738 const struct wined3d_vec4
*constants
, const GLint
*constant_locations
,
739 const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
742 unsigned int heap_idx
= 1;
745 if (heap
->entries
[heap_idx
].version
<= version
) return;
747 idx
= heap
->entries
[heap_idx
].idx
;
748 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
749 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
751 while (stack_idx
>= 0)
753 /* Note that we fall through to the next case statement. */
754 switch(stack
[stack_idx
])
756 case HEAP_NODE_TRAVERSE_LEFT
:
758 unsigned int left_idx
= heap_idx
<< 1;
759 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
762 idx
= heap
->entries
[heap_idx
].idx
;
763 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
765 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
766 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
771 case HEAP_NODE_TRAVERSE_RIGHT
:
773 unsigned int right_idx
= (heap_idx
<< 1) + 1;
774 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
776 heap_idx
= right_idx
;
777 idx
= heap
->entries
[heap_idx
].idx
;
778 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
780 stack
[stack_idx
++] = HEAP_NODE_POP
;
781 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
792 checkGLcall("walk_constant_heap_clamped()");
795 /* Context activation is done by the caller. */
796 static void shader_glsl_load_constants_f(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
797 const struct wined3d_vec4
*constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
798 unsigned char *stack
, unsigned int version
)
800 const struct wined3d_shader_lconst
*lconst
;
802 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
803 if (shader
->reg_maps
.shader_version
.major
== 1
804 && shader
->reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
805 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
807 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
809 if (!shader
->load_local_constsF
)
811 TRACE("No need to load local float constants for this shader.\n");
815 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
816 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
818 GL_EXTCALL(glUniform4fv(constant_locations
[lconst
->idx
], 1, (const GLfloat
*)lconst
->value
));
820 checkGLcall("glUniform4fv()");
823 /* Context activation is done by the caller. */
824 static void shader_glsl_load_constants_i(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
825 const struct wined3d_ivec4
*constants
, const GLint locations
[WINED3D_MAX_CONSTS_I
], WORD constants_set
)
830 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
832 if (!(constants_set
& 1)) continue;
834 /* We found this uniform name in the program - go ahead and send the data */
835 GL_EXTCALL(glUniform4iv(locations
[i
], 1, &constants
[i
].x
));
838 /* Load immediate constants */
839 ptr
= list_head(&shader
->constantsI
);
842 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
843 unsigned int idx
= lconst
->idx
;
844 const GLint
*values
= (const GLint
*)lconst
->value
;
846 /* We found this uniform name in the program - go ahead and send the data */
847 GL_EXTCALL(glUniform4iv(locations
[idx
], 1, values
));
848 ptr
= list_next(&shader
->constantsI
, ptr
);
850 checkGLcall("glUniform4iv()");
853 /* Context activation is done by the caller. */
854 static void shader_glsl_load_constantsB(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
855 const GLint locations
[WINED3D_MAX_CONSTS_B
], const BOOL
*constants
, WORD constants_set
)
860 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
862 if (!(constants_set
& 1)) continue;
864 GL_EXTCALL(glUniform1iv(locations
[i
], 1, &constants
[i
]));
867 /* Load immediate constants */
868 ptr
= list_head(&shader
->constantsB
);
871 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
872 unsigned int idx
= lconst
->idx
;
873 const GLint
*values
= (const GLint
*)lconst
->value
;
875 GL_EXTCALL(glUniform1iv(locations
[idx
], 1, values
));
876 ptr
= list_next(&shader
->constantsB
, ptr
);
878 checkGLcall("glUniform1iv()");
881 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
883 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
886 /* Context activation is done by the caller (state handler). */
887 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program
*ps
,
888 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
894 np2fixup_constants
[MAX_FRAGMENT_SAMPLERS
];
895 UINT fixup
= ps
->np2_fixup_info
->active
;
898 for (i
= 0; fixup
; fixup
>>= 1, ++i
)
900 const struct wined3d_texture
*tex
= state
->textures
[i
];
901 unsigned char idx
= ps
->np2_fixup_info
->idx
[i
];
905 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
909 np2fixup_constants
[idx
].sx
= tex
->pow2_matrix
[0];
910 np2fixup_constants
[idx
].sy
= tex
->pow2_matrix
[5];
913 GL_EXTCALL(glUniform4fv(ps
->np2_fixup_location
, ps
->np2_fixup_info
->num_consts
, &np2fixup_constants
[0].sx
));
916 /* Taken and adapted from Mesa. */
917 static BOOL
invert_matrix_3d(struct wined3d_matrix
*out
, const struct wined3d_matrix
*in
)
919 float pos
, neg
, t
, det
;
920 struct wined3d_matrix temp
;
922 /* Calculate the determinant of upper left 3x3 submatrix and
923 * determine if the matrix is singular. */
925 t
= in
->_11
* in
->_22
* in
->_33
;
931 t
= in
->_21
* in
->_32
* in
->_13
;
936 t
= in
->_31
* in
->_12
* in
->_23
;
942 t
= -in
->_31
* in
->_22
* in
->_13
;
947 t
= -in
->_21
* in
->_12
* in
->_33
;
953 t
= -in
->_11
* in
->_32
* in
->_23
;
961 if (fabsf(det
) < 1e-25f
)
965 temp
._11
= (in
->_22
* in
->_33
- in
->_32
* in
->_23
) * det
;
966 temp
._12
= -(in
->_12
* in
->_33
- in
->_32
* in
->_13
) * det
;
967 temp
._13
= (in
->_12
* in
->_23
- in
->_22
* in
->_13
) * det
;
968 temp
._21
= -(in
->_21
* in
->_33
- in
->_31
* in
->_23
) * det
;
969 temp
._22
= (in
->_11
* in
->_33
- in
->_31
* in
->_13
) * det
;
970 temp
._23
= -(in
->_11
* in
->_23
- in
->_21
* in
->_13
) * det
;
971 temp
._31
= (in
->_21
* in
->_32
- in
->_31
* in
->_22
) * det
;
972 temp
._32
= -(in
->_11
* in
->_32
- in
->_31
* in
->_12
) * det
;
973 temp
._33
= (in
->_11
* in
->_22
- in
->_21
* in
->_12
) * det
;
979 static void swap_rows(float **a
, float **b
)
987 static BOOL
invert_matrix(struct wined3d_matrix
*out
, struct wined3d_matrix
*m
)
990 float m0
, m1
, m2
, m3
, s
;
991 float *r0
, *r1
, *r2
, *r3
;
1003 r0
[5] = r0
[6] = r0
[7] = 0.0f
;
1010 r1
[4] = r1
[6] = r1
[7] = 0.0f
;
1017 r2
[4] = r2
[5] = r2
[7] = 0.0f
;
1024 r3
[4] = r3
[5] = r3
[6] = 0.0f
;
1026 /* Choose pivot - or die. */
1027 if (fabsf(r3
[0]) > fabsf(r2
[0]))
1028 swap_rows(&r3
, &r2
);
1029 if (fabsf(r2
[0]) > fabsf(r1
[0]))
1030 swap_rows(&r2
, &r1
);
1031 if (fabsf(r1
[0]) > fabsf(r0
[0]))
1032 swap_rows(&r1
, &r0
);
1036 /* Eliminate first variable. */
1037 m1
= r1
[0] / r0
[0]; m2
= r2
[0] / r0
[0]; m3
= r3
[0] / r0
[0];
1038 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
1039 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
1040 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
1070 /* Choose pivot - or die. */
1071 if (fabsf(r3
[1]) > fabsf(r2
[1]))
1072 swap_rows(&r3
, &r2
);
1073 if (fabsf(r2
[1]) > fabsf(r1
[1]))
1074 swap_rows(&r2
, &r1
);
1078 /* Eliminate second variable. */
1079 m2
= r2
[1] / r1
[1]; m3
= r3
[1] / r1
[1];
1080 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
1081 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
1107 /* Choose pivot - or die. */
1108 if (fabsf(r3
[2]) > fabsf(r2
[2]))
1109 swap_rows(&r3
, &r2
);
1113 /* Eliminate third variable. */
1115 r3
[3] -= m3
* r2
[3];
1116 r3
[4] -= m3
* r2
[4];
1117 r3
[5] -= m3
* r2
[5];
1118 r3
[6] -= m3
* r2
[6];
1119 r3
[7] -= m3
* r2
[7];
1125 /* Back substitute row 3. */
1132 /* Back substitute row 2. */
1135 r2
[4] = s
* (r2
[4] - r3
[4] * m2
);
1136 r2
[5] = s
* (r2
[5] - r3
[5] * m2
);
1137 r2
[6] = s
* (r2
[6] - r3
[6] * m2
);
1138 r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
1140 r1
[4] -= r3
[4] * m1
;
1141 r1
[5] -= r3
[5] * m1
;
1142 r1
[6] -= r3
[6] * m1
;
1143 r1
[7] -= r3
[7] * m1
;
1145 r0
[4] -= r3
[4] * m0
;
1146 r0
[5] -= r3
[5] * m0
;
1147 r0
[6] -= r3
[6] * m0
;
1148 r0
[7] -= r3
[7] * m0
;
1150 /* Back substitute row 1. */
1153 r1
[4] = s
* (r1
[4] - r2
[4] * m1
);
1154 r1
[5] = s
* (r1
[5] - r2
[5] * m1
);
1155 r1
[6] = s
* (r1
[6] - r2
[6] * m1
);
1156 r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
1158 r0
[4] -= r2
[4] * m0
;
1159 r0
[5] -= r2
[5] * m0
;
1160 r0
[6] -= r2
[6] * m0
;
1161 r0
[7] -= r2
[7] * m0
;
1163 /* Back substitute row 0. */
1166 r0
[4] = s
* (r0
[4] - r1
[4] * m0
);
1167 r0
[5] = s
* (r0
[5] - r1
[5] * m0
);
1168 r0
[6] = s
* (r0
[6] - r1
[6] * m0
);
1169 r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
1191 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context
*context
,
1192 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1194 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1196 struct wined3d_matrix mv
;
1199 if (prog
->vs
.normal_matrix_location
== -1)
1202 get_modelview_matrix(context
, state
, 0, &mv
);
1203 if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_FFP_LIGHTING
)
1204 invert_matrix_3d(&mv
, &mv
);
1206 invert_matrix(&mv
, &mv
);
1207 /* Tests show that singular modelview matrices are used unchanged as normal
1208 * matrices on D3D3 and older. There seems to be no clearly consistent
1209 * behavior on newer D3D versions so always follow older ddraw behavior. */
1210 for (i
= 0; i
< 3; ++i
)
1211 for (j
= 0; j
< 3; ++j
)
1212 mat
[i
* 3 + j
] = (&mv
._11
)[j
* 4 + i
];
1214 GL_EXTCALL(glUniformMatrix3fv(prog
->vs
.normal_matrix_location
, 1, FALSE
, mat
));
1215 checkGLcall("glUniformMatrix3fv");
1218 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context
*context
,
1219 const struct wined3d_state
*state
, unsigned int tex
, struct glsl_shader_prog_link
*prog
)
1221 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1222 struct wined3d_matrix mat
;
1224 if (tex
>= MAX_TEXTURES
)
1226 if (prog
->vs
.texture_matrix_location
[tex
] == -1)
1229 get_texture_matrix(context
, state
, tex
, &mat
);
1230 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.texture_matrix_location
[tex
], 1, FALSE
, &mat
._11
));
1231 checkGLcall("glUniformMatrix4fv");
1234 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context
*context
,
1235 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1237 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1239 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1241 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, &state
->material
.specular
.r
));
1242 GL_EXTCALL(glUniform1f(prog
->vs
.material_shininess_location
, state
->material
.power
));
1246 static const float black
[] = {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1248 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, black
));
1250 GL_EXTCALL(glUniform4fv(prog
->vs
.material_ambient_location
, 1, &state
->material
.ambient
.r
));
1251 GL_EXTCALL(glUniform4fv(prog
->vs
.material_diffuse_location
, 1, &state
->material
.diffuse
.r
));
1252 GL_EXTCALL(glUniform4fv(prog
->vs
.material_emissive_location
, 1, &state
->material
.emissive
.r
));
1253 checkGLcall("setting FFP material uniforms");
1256 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context
*context
,
1257 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1259 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1260 struct wined3d_color color
;
1262 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_AMBIENT
]);
1263 GL_EXTCALL(glUniform3fv(prog
->vs
.light_ambient_location
, 1, &color
.r
));
1264 checkGLcall("glUniform3fv");
1267 static void multiply_vector_matrix(struct wined3d_vec4
*dest
, const struct wined3d_vec4
*src1
,
1268 const struct wined3d_matrix
*src2
)
1270 struct wined3d_vec4 temp
;
1272 temp
.x
= (src1
->x
* src2
->_11
) + (src1
->y
* src2
->_21
) + (src1
->z
* src2
->_31
) + (src1
->w
* src2
->_41
);
1273 temp
.y
= (src1
->x
* src2
->_12
) + (src1
->y
* src2
->_22
) + (src1
->z
* src2
->_32
) + (src1
->w
* src2
->_42
);
1274 temp
.z
= (src1
->x
* src2
->_13
) + (src1
->y
* src2
->_23
) + (src1
->z
* src2
->_33
) + (src1
->w
* src2
->_43
);
1275 temp
.w
= (src1
->x
* src2
->_14
) + (src1
->y
* src2
->_24
) + (src1
->z
* src2
->_34
) + (src1
->w
* src2
->_44
);
1280 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context
*context
,
1281 const struct wined3d_state
*state
, unsigned int light
, const struct wined3d_light_info
*light_info
,
1282 struct glsl_shader_prog_link
*prog
)
1284 const struct wined3d_matrix
*view
= &state
->transforms
[WINED3D_TS_VIEW
];
1285 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1286 struct wined3d_vec4 vec4
;
1288 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].diffuse
, 1, &light_info
->OriginalParms
.diffuse
.r
));
1289 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].specular
, 1, &light_info
->OriginalParms
.specular
.r
));
1290 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].ambient
, 1, &light_info
->OriginalParms
.ambient
.r
));
1292 switch (light_info
->OriginalParms
.type
)
1294 case WINED3D_LIGHT_POINT
:
1295 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1296 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1297 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1298 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1299 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1300 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1303 case WINED3D_LIGHT_SPOT
:
1304 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1305 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1307 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1308 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1310 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1311 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].falloff
, light_info
->OriginalParms
.falloff
));
1312 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1313 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1314 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1315 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_htheta
, cosf(light_info
->OriginalParms
.theta
/ 2.0f
)));
1316 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_hphi
, cosf(light_info
->OriginalParms
.phi
/ 2.0f
)));
1319 case WINED3D_LIGHT_DIRECTIONAL
:
1320 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1321 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1324 case WINED3D_LIGHT_PARALLELPOINT
:
1325 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1326 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1330 FIXME("Unrecognized light type %#x.\n", light_info
->OriginalParms
.type
);
1332 checkGLcall("setting FFP lights uniforms");
1335 static void shader_glsl_pointsize_uniform(const struct wined3d_context
*context
,
1336 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1338 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1342 get_pointsize_minmax(context
, state
, &min
, &max
);
1344 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_min_location
, min
));
1345 checkGLcall("glUniform1f");
1346 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_max_location
, max
));
1347 checkGLcall("glUniform1f");
1349 get_pointsize(context
, state
, &size
, att
);
1351 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_location
, size
));
1352 checkGLcall("glUniform1f");
1353 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_c_att_location
, att
[0]));
1354 checkGLcall("glUniform1f");
1355 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_l_att_location
, att
[1]));
1356 checkGLcall("glUniform1f");
1357 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_q_att_location
, att
[2]));
1358 checkGLcall("glUniform1f");
1361 static void shader_glsl_load_fog_uniform(const struct wined3d_context
*context
,
1362 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1364 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1365 struct wined3d_color color
;
1366 float start
, end
, scale
;
1373 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_FOGCOLOR
]);
1374 GL_EXTCALL(glUniform4fv(prog
->ps
.fog_color_location
, 1, &color
.r
));
1375 tmpvalue
.d
= state
->render_states
[WINED3D_RS_FOGDENSITY
];
1376 GL_EXTCALL(glUniform1f(prog
->ps
.fog_density_location
, tmpvalue
.f
));
1377 get_fog_start_end(context
, state
, &start
, &end
);
1378 scale
= 1.0f
/ (end
- start
);
1379 GL_EXTCALL(glUniform1f(prog
->ps
.fog_end_location
, end
));
1380 GL_EXTCALL(glUniform1f(prog
->ps
.fog_scale_location
, scale
));
1381 checkGLcall("fog emulation uniforms");
1384 static void shader_glsl_clip_plane_uniform(const struct wined3d_context
*context
,
1385 const struct wined3d_state
*state
, unsigned int index
, struct glsl_shader_prog_link
*prog
)
1387 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1388 struct wined3d_vec4 plane
;
1390 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1392 multiply_vector_matrix(&plane
, &state
->clip_planes
[index
], &state
->transforms
[WINED3D_TS_VIEW
]);
1394 plane
= state
->clip_planes
[index
];
1396 GL_EXTCALL(glUniform4fv(prog
->vs
.clip_planes_location
+ index
, 1, &plane
.x
));
1399 /* Context activation is done by the caller (state handler). */
1400 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program
*ps
,
1401 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
1403 struct wined3d_color float_key
[2];
1404 const struct wined3d_texture
*texture
= state
->textures
[0];
1406 wined3d_format_get_float_color_key(texture
->resource
.format
, &texture
->async
.src_blt_color_key
, float_key
);
1407 GL_EXTCALL(glUniform4fv(ps
->color_key_location
, 2, &float_key
[0].r
));
1410 /* Context activation is done by the caller (state handler). */
1411 static void shader_glsl_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1412 const struct wined3d_state
*state
)
1414 const struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
1415 const struct wined3d_shader
*vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
1416 const struct wined3d_shader
*pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
1417 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1418 struct shader_glsl_priv
*priv
= shader_priv
;
1419 float position_fixup
[4];
1422 struct glsl_shader_prog_link
*prog
= ctx_data
->glsl_program
;
1423 UINT constant_version
;
1427 /* No GLSL program set - nothing to do. */
1430 constant_version
= prog
->constant_version
;
1431 update_mask
= context
->constant_update_mask
& prog
->constant_update_mask
;
1433 if (update_mask
& WINED3D_SHADER_CONST_VS_F
)
1434 shader_glsl_load_constants_f(vshader
, gl_info
, state
->vs_consts_f
,
1435 prog
->vs
.uniform_f_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
1437 if (update_mask
& WINED3D_SHADER_CONST_VS_I
)
1438 shader_glsl_load_constants_i(vshader
, gl_info
, state
->vs_consts_i
,
1439 prog
->vs
.uniform_i_locations
, vshader
->reg_maps
.integer_constants
);
1441 if (update_mask
& WINED3D_SHADER_CONST_VS_B
)
1442 shader_glsl_load_constantsB(vshader
, gl_info
, prog
->vs
.uniform_b_locations
, state
->vs_consts_b
,
1443 vshader
->reg_maps
.boolean_constants
);
1445 if (update_mask
& WINED3D_SHADER_CONST_VS_CLIP_PLANES
)
1447 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
1448 shader_glsl_clip_plane_uniform(context
, state
, i
, prog
);
1451 if (update_mask
& WINED3D_SHADER_CONST_VS_POINTSIZE
)
1452 shader_glsl_pointsize_uniform(context
, state
, prog
);
1454 if (update_mask
& WINED3D_SHADER_CONST_POS_FIXUP
)
1456 shader_get_position_fixup(context
, state
, position_fixup
);
1457 if (state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
])
1458 GL_EXTCALL(glUniform4fv(prog
->gs
.pos_fixup_location
, 1, position_fixup
));
1460 GL_EXTCALL(glUniform4fv(prog
->vs
.pos_fixup_location
, 1, position_fixup
));
1461 checkGLcall("glUniform4fv");
1464 if (update_mask
& WINED3D_SHADER_CONST_FFP_MODELVIEW
)
1466 struct wined3d_matrix mat
;
1468 get_modelview_matrix(context
, state
, 0, &mat
);
1469 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[0], 1, FALSE
, &mat
._11
));
1470 checkGLcall("glUniformMatrix4fv");
1472 shader_glsl_ffp_vertex_normalmatrix_uniform(context
, state
, prog
);
1475 if (update_mask
& WINED3D_SHADER_CONST_FFP_VERTEXBLEND
)
1477 struct wined3d_matrix mat
;
1479 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
1481 if (prog
->vs
.modelview_matrix_location
[i
] == -1)
1484 get_modelview_matrix(context
, state
, i
, &mat
);
1485 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[i
], 1, FALSE
, &mat
._11
));
1486 checkGLcall("glUniformMatrix4fv");
1490 if (update_mask
& WINED3D_SHADER_CONST_FFP_PROJ
)
1492 struct wined3d_matrix projection
;
1494 get_projection_matrix(context
, state
, &projection
);
1495 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.projection_matrix_location
, 1, FALSE
, &projection
._11
));
1496 checkGLcall("glUniformMatrix4fv");
1499 if (update_mask
& WINED3D_SHADER_CONST_FFP_TEXMATRIX
)
1501 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1502 shader_glsl_ffp_vertex_texmatrix_uniform(context
, state
, i
, prog
);
1505 if (update_mask
& WINED3D_SHADER_CONST_FFP_MATERIAL
)
1506 shader_glsl_ffp_vertex_material_uniform(context
, state
, prog
);
1508 if (update_mask
& WINED3D_SHADER_CONST_FFP_LIGHTS
)
1510 unsigned int point_idx
, spot_idx
, directional_idx
, parallel_point_idx
;
1511 DWORD point_count
= 0;
1512 DWORD spot_count
= 0;
1513 DWORD directional_count
= 0;
1514 DWORD parallel_point_count
= 0;
1516 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
1518 if (!state
->lights
[i
])
1521 switch (state
->lights
[i
]->OriginalParms
.type
)
1523 case WINED3D_LIGHT_POINT
:
1526 case WINED3D_LIGHT_SPOT
:
1529 case WINED3D_LIGHT_DIRECTIONAL
:
1530 ++directional_count
;
1532 case WINED3D_LIGHT_PARALLELPOINT
:
1533 ++parallel_point_count
;
1536 FIXME("Unhandled light type %#x.\n", state
->lights
[i
]->OriginalParms
.type
);
1541 spot_idx
= point_idx
+ point_count
;
1542 directional_idx
= spot_idx
+ spot_count
;
1543 parallel_point_idx
= directional_idx
+ directional_count
;
1545 shader_glsl_ffp_vertex_lightambient_uniform(context
, state
, prog
);
1546 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
1548 const struct wined3d_light_info
*light_info
= state
->lights
[i
];
1554 switch (light_info
->OriginalParms
.type
)
1556 case WINED3D_LIGHT_POINT
:
1559 case WINED3D_LIGHT_SPOT
:
1562 case WINED3D_LIGHT_DIRECTIONAL
:
1563 idx
= directional_idx
++;
1565 case WINED3D_LIGHT_PARALLELPOINT
:
1566 idx
= parallel_point_idx
++;
1569 FIXME("Unhandled light type %#x.\n", light_info
->OriginalParms
.type
);
1572 shader_glsl_ffp_vertex_light_uniform(context
, state
, idx
, light_info
, prog
);
1576 if (update_mask
& WINED3D_SHADER_CONST_PS_F
)
1577 shader_glsl_load_constants_f(pshader
, gl_info
, state
->ps_consts_f
,
1578 prog
->ps
.uniform_f_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
1580 if (update_mask
& WINED3D_SHADER_CONST_PS_I
)
1581 shader_glsl_load_constants_i(pshader
, gl_info
, state
->ps_consts_i
,
1582 prog
->ps
.uniform_i_locations
, pshader
->reg_maps
.integer_constants
);
1584 if (update_mask
& WINED3D_SHADER_CONST_PS_B
)
1585 shader_glsl_load_constantsB(pshader
, gl_info
, prog
->ps
.uniform_b_locations
, state
->ps_consts_b
,
1586 pshader
->reg_maps
.boolean_constants
);
1588 if (update_mask
& WINED3D_SHADER_CONST_PS_BUMP_ENV
)
1590 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1592 if (prog
->ps
.bumpenv_mat_location
[i
] == -1)
1595 GL_EXTCALL(glUniformMatrix2fv(prog
->ps
.bumpenv_mat_location
[i
], 1, 0,
1596 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
]));
1598 if (prog
->ps
.bumpenv_lum_scale_location
[i
] != -1)
1600 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_scale_location
[i
], 1,
1601 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
]));
1602 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_offset_location
[i
], 1,
1603 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
]));
1607 checkGLcall("bump env uniforms");
1610 if (update_mask
& WINED3D_SHADER_CONST_PS_Y_CORR
)
1612 const struct wined3d_vec4 correction_params
=
1614 /* Position is relative to the framebuffer, not the viewport. */
1615 context
->render_offscreen
? 0.0f
: (float)state
->fb
->render_targets
[0]->height
,
1616 context
->render_offscreen
? 1.0f
: -1.0f
,
1621 GL_EXTCALL(glUniform4fv(prog
->ps
.ycorrection_location
, 1, &correction_params
.x
));
1624 if (update_mask
& WINED3D_SHADER_CONST_PS_NP2_FIXUP
)
1625 shader_glsl_load_np2fixup_constants(&prog
->ps
, gl_info
, state
);
1626 if (update_mask
& WINED3D_SHADER_CONST_FFP_COLOR_KEY
)
1627 shader_glsl_load_color_key_constant(&prog
->ps
, gl_info
, state
);
1629 if (update_mask
& WINED3D_SHADER_CONST_FFP_PS
)
1631 struct wined3d_color color
;
1633 if (prog
->ps
.tex_factor_location
!= -1)
1635 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_TEXTUREFACTOR
]);
1636 GL_EXTCALL(glUniform4fv(prog
->ps
.tex_factor_location
, 1, &color
.r
));
1639 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1640 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 1.0f
, 1.0f
, 1.0f
, 0.0f
));
1642 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
1644 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1646 if (prog
->ps
.tss_constant_location
[i
] == -1)
1649 wined3d_color_from_d3dcolor(&color
, state
->texture_states
[i
][WINED3D_TSS_CONSTANT
]);
1650 GL_EXTCALL(glUniform4fv(prog
->ps
.tss_constant_location
[i
], 1, &color
.r
));
1653 checkGLcall("fixed function uniforms");
1656 if (update_mask
& WINED3D_SHADER_CONST_PS_FOG
)
1657 shader_glsl_load_fog_uniform(context
, state
, prog
);
1659 if (update_mask
& WINED3D_SHADER_CONST_PS_ALPHA_TEST
)
1661 float ref
= state
->render_states
[WINED3D_RS_ALPHAREF
] / 255.0f
;
1663 GL_EXTCALL(glUniform1f(prog
->ps
.alpha_test_ref_location
, ref
));
1664 checkGLcall("alpha test emulation uniform");
1667 if (priv
->next_constant_version
== UINT_MAX
)
1669 TRACE("Max constant version reached, resetting to 0.\n");
1670 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
1671 priv
->next_constant_version
= 1;
1675 prog
->constant_version
= priv
->next_constant_version
++;
1679 static void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
, DWORD new_version
)
1681 struct constant_entry
*entries
= heap
->entries
;
1682 unsigned int *positions
= heap
->positions
;
1683 unsigned int heap_idx
, parent_idx
;
1685 if (!heap
->contained
[idx
])
1687 heap_idx
= heap
->size
++;
1688 heap
->contained
[idx
] = TRUE
;
1692 heap_idx
= positions
[idx
];
1695 while (heap_idx
> 1)
1697 parent_idx
= heap_idx
>> 1;
1699 if (new_version
<= entries
[parent_idx
].version
) break;
1701 entries
[heap_idx
] = entries
[parent_idx
];
1702 positions
[entries
[parent_idx
].idx
] = heap_idx
;
1703 heap_idx
= parent_idx
;
1706 entries
[heap_idx
].version
= new_version
;
1707 entries
[heap_idx
].idx
= idx
;
1708 positions
[idx
] = heap_idx
;
1711 static void shader_glsl_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1713 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1714 struct constant_heap
*heap
= &priv
->vconst_heap
;
1717 for (i
= start
; i
< count
+ start
; ++i
)
1719 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1723 static void shader_glsl_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1725 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1726 struct constant_heap
*heap
= &priv
->pconst_heap
;
1729 for (i
= start
; i
< count
+ start
; ++i
)
1731 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1735 static unsigned int vec4_varyings(DWORD shader_major
, const struct wined3d_gl_info
*gl_info
)
1737 unsigned int ret
= gl_info
->limits
.glsl_varyings
/ 4;
1738 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1739 if(shader_major
> 3) return ret
;
1741 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1742 if (gl_info
->quirks
& WINED3D_QUIRK_GLSL_CLIP_VARYING
) ret
-= 1;
1746 static BOOL
needs_legacy_glsl_syntax(const struct wined3d_gl_info
*gl_info
)
1748 return gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
1751 static BOOL
shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info
*gl_info
)
1753 return !needs_legacy_glsl_syntax(gl_info
) && gl_info
->supported
[ARB_EXPLICIT_ATTRIB_LOCATION
];
1756 static const char *get_attribute_keyword(const struct wined3d_gl_info
*gl_info
)
1758 return needs_legacy_glsl_syntax(gl_info
) ? "attribute" : "in";
1761 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info
*gl_info
,
1762 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1767 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1768 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "in");
1771 va_start(args
, format
);
1772 ret
= shader_vaddline(buffer
, format
, args
);
1776 if (!string_buffer_resize(buffer
, ret
))
1781 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info
*gl_info
,
1782 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1787 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1788 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "out");
1791 va_start(args
, format
);
1792 ret
= shader_vaddline(buffer
, format
, args
);
1796 if (!string_buffer_resize(buffer
, ret
))
1801 static const char *get_fragment_output(const struct wined3d_gl_info
*gl_info
)
1803 return needs_legacy_glsl_syntax(gl_info
) ? "gl_FragData" : "ps_out";
1806 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
1808 switch (primitive_type
)
1810 case WINED3D_PT_POINTLIST
:
1813 case WINED3D_PT_LINELIST
:
1816 case WINED3D_PT_LINESTRIP
:
1817 return "line_strip";
1819 case WINED3D_PT_TRIANGLELIST
:
1822 case WINED3D_PT_TRIANGLESTRIP
:
1823 return "triangle_strip";
1825 case WINED3D_PT_LINELIST_ADJ
:
1826 return "lines_adjacency";
1828 case WINED3D_PT_TRIANGLELIST_ADJ
:
1829 return "triangles_adjacency";
1832 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
1837 static BOOL
glsl_is_color_reg_read(const struct wined3d_shader
*shader
, unsigned int idx
)
1839 const struct wined3d_shader_signature
*input_signature
= &shader
->input_signature
;
1840 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1841 const BOOL
*input_reg_used
= shader
->u
.ps
.input_reg_used
;
1844 if (reg_maps
->shader_version
.major
< 3)
1845 return input_reg_used
[idx
];
1847 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1849 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
1851 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
1854 if (shader_match_semantic(input
->semantic_name
, WINED3D_DECL_USAGE_COLOR
)
1855 && input
->semantic_idx
== idx
)
1857 if (input_reg_used
[input
->register_idx
])
1866 static BOOL
glsl_is_shadow_sampler(const struct wined3d_shader
*shader
,
1867 const struct ps_compile_args
*ps_args
, unsigned int resource_idx
, unsigned int sampler_idx
)
1869 const struct wined3d_shader_version
*version
= &shader
->reg_maps
.shader_version
;
1871 if (version
->major
>= 4)
1872 return shader
->reg_maps
.sampler_comparison_mode
& (1u << sampler_idx
);
1874 return version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& (ps_args
->shadow
& (1u << resource_idx
));
1877 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer
*buffer
,
1878 const struct wined3d_gl_info
*gl_info
, const char *vector_type
, const char *scalar_type
,
1881 shader_addline(buffer
, "%s %s4 vs_in_%s%u;\n",
1882 get_attribute_keyword(gl_info
), vector_type
, scalar_type
, index
);
1883 shader_addline(buffer
, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1884 index
, scalar_type
, scalar_type
, index
);
1887 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer
*buffer
,
1888 const struct wined3d_gl_info
*gl_info
, const struct wined3d_shader_signature_element
*e
)
1890 unsigned int index
= e
->register_idx
;
1892 if (e
->sysval_semantic
== WINED3D_SV_VERTEX_ID
)
1894 shader_addline(buffer
, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1898 if (e
->sysval_semantic
== WINED3D_SV_INSTANCE_ID
)
1900 shader_addline(buffer
, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1904 if (e
->sysval_semantic
&& e
->sysval_semantic
!= WINED3D_SV_POSITION
)
1905 FIXME("Unhandled sysval semantic %#x.\n", e
->sysval_semantic
);
1907 if (shader_glsl_use_explicit_attrib_location(gl_info
))
1908 shader_addline(buffer
, "layout(location = %u) ", index
);
1910 switch (e
->component_type
)
1912 case WINED3D_TYPE_UINT
:
1913 shader_glsl_declare_typed_vertex_attribute(buffer
, gl_info
, "uvec", "uint", index
);
1915 case WINED3D_TYPE_INT
:
1916 shader_glsl_declare_typed_vertex_attribute(buffer
, gl_info
, "ivec", "int", index
);
1920 FIXME("Unhandled type %#x.\n", e
->component_type
);
1922 case WINED3D_TYPE_UNKNOWN
:
1923 case WINED3D_TYPE_FLOAT
:
1924 shader_addline(buffer
, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info
), index
);
1929 /** Generate the variable & register declarations for the GLSL output target */
1930 static void shader_generate_glsl_declarations(const struct wined3d_context
*context
,
1931 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
1932 const struct wined3d_shader_reg_maps
*reg_maps
, const struct shader_glsl_ctx_priv
*ctx_priv
)
1934 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
1935 const struct vs_compile_args
*vs_args
= ctx_priv
->cur_vs_args
;
1936 const struct ps_compile_args
*ps_args
= ctx_priv
->cur_ps_args
;
1937 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1938 const struct wined3d_shader_indexable_temp
*idx_temp_reg
;
1939 unsigned int i
, extra_constants_needed
= 0;
1940 const struct wined3d_shader_lconst
*lconst
;
1944 prefix
= shader_glsl_get_prefix(version
->type
);
1946 /* Prototype the subroutines */
1947 for (i
= 0, map
= reg_maps
->labels
; map
; map
>>= 1, ++i
)
1949 if (map
& 1) shader_addline(buffer
, "void subroutine%u();\n", i
);
1952 /* Declare the constants (aka uniforms) */
1953 if (shader
->limits
->constant_float
> 0)
1955 unsigned max_constantsF
;
1957 /* Unless the shader uses indirect addressing, always declare the
1958 * maximum array size and ignore that we need some uniforms privately.
1959 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1960 * and immediate values, still declare VC[256]. If the shader needs
1961 * more uniforms than we have it won't work in any case. If it uses
1962 * less, the compiler will figure out which uniforms are really used
1963 * and strip them out. This allows a shader to use c255 on a dx9 card,
1964 * as long as it doesn't also use all the other constants.
1966 * If the shader uses indirect addressing the compiler must assume
1967 * that all declared uniforms are used. In this case, declare only the
1968 * amount that we're assured to have.
1970 * Thus we run into problems in these two cases:
1971 * 1) The shader really uses more uniforms than supported.
1972 * 2) The shader uses indirect addressing, less constants than
1973 * supported, but uses a constant index > #supported consts. */
1974 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1976 /* No indirect addressing here. */
1977 max_constantsF
= gl_info
->limits
.glsl_ps_float_constants
;
1981 if (reg_maps
->usesrelconstF
)
1983 /* Subtract the other potential uniforms from the max
1984 * available (bools, ints, and 1 row of projection matrix).
1985 * Subtract another uniform for immediate values, which have
1986 * to be loaded via uniform by the driver as well. The shader
1987 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1988 * shader code, so one vec4 should be enough. (Unfortunately
1989 * the Nvidia driver doesn't store 128 and -128 in one float).
1991 * Writing gl_ClipVertex requires one uniform for each
1992 * clipplane as well. */
1993 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
- 3;
1994 if (vs_args
->clip_enabled
)
1995 max_constantsF
-= gl_info
->limits
.user_clip_distances
;
1996 max_constantsF
-= wined3d_popcount(reg_maps
->integer_constants
);
1997 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1998 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1999 * for now take this into account when calculating the number of available constants
2001 max_constantsF
-= wined3d_popcount(reg_maps
->boolean_constants
);
2002 /* Set by driver quirks in directx.c */
2003 max_constantsF
-= gl_info
->reserved_glsl_constants
;
2005 if (max_constantsF
< shader
->limits
->constant_float
)
2007 static unsigned int once
;
2010 ERR_(winediag
)("The hardware does not support enough uniform components to run this shader,"
2011 " it may not render correctly.\n");
2013 WARN("The hardware does not support enough uniform components to run this shader.\n");
2018 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
;
2021 max_constantsF
= min(shader
->limits
->constant_float
, max_constantsF
);
2022 shader_addline(buffer
, "uniform vec4 %s_c[%u];\n", prefix
, max_constantsF
);
2025 /* Always declare the full set of constants, the compiler can remove the
2026 * unused ones because d3d doesn't (yet) support indirect int and bool
2027 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2028 if (shader
->limits
->constant_int
> 0 && reg_maps
->integer_constants
)
2029 shader_addline(buffer
, "uniform ivec4 %s_i[%u];\n", prefix
, shader
->limits
->constant_int
);
2031 if (shader
->limits
->constant_bool
> 0 && reg_maps
->boolean_constants
)
2032 shader_addline(buffer
, "uniform bool %s_b[%u];\n", prefix
, shader
->limits
->constant_bool
);
2034 /* Declare immediate constant buffer */
2036 shader_addline(buffer
, "uniform vec4 %s_icb[%u];\n", prefix
, reg_maps
->icb
->vec4_count
);
2038 /* Declare constant buffers */
2039 for (i
= 0; i
< WINED3D_MAX_CBS
; ++i
)
2041 if (reg_maps
->cb_sizes
[i
])
2042 shader_addline(buffer
, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2043 prefix
, i
, prefix
, i
, reg_maps
->cb_sizes
[i
]);
2046 /* Declare texture samplers */
2047 for (i
= 0; i
< reg_maps
->sampler_map
.count
; ++i
)
2049 struct wined3d_shader_sampler_map_entry
*entry
;
2050 const char *sampler_type_prefix
, *sampler_type
;
2051 BOOL shadow_sampler
, tex_rect
;
2053 entry
= ®_maps
->sampler_map
.entries
[i
];
2055 if (entry
->resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
2057 ERR("Invalid resource index %u.\n", entry
->resource_idx
);
2061 switch (reg_maps
->resource_info
[entry
->resource_idx
].data_type
)
2063 case WINED3D_DATA_FLOAT
:
2064 case WINED3D_DATA_UNORM
:
2065 case WINED3D_DATA_SNORM
:
2066 sampler_type_prefix
= "";
2069 case WINED3D_DATA_INT
:
2070 sampler_type_prefix
= "i";
2073 case WINED3D_DATA_UINT
:
2074 sampler_type_prefix
= "u";
2078 sampler_type_prefix
= "";
2079 ERR("Unhandled resource data type %#x.\n", reg_maps
->resource_info
[i
].data_type
);
2083 shadow_sampler
= glsl_is_shadow_sampler(shader
, ps_args
, entry
->resource_idx
, entry
->sampler_idx
);
2084 switch (reg_maps
->resource_info
[entry
->resource_idx
].type
)
2086 case WINED3D_SHADER_RESOURCE_BUFFER
:
2087 sampler_type
= "samplerBuffer";
2090 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
2092 sampler_type
= "sampler1DShadow";
2094 sampler_type
= "sampler1D";
2097 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2098 tex_rect
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
2099 && (ps_args
->np2_fixup
& (1u << entry
->resource_idx
))
2100 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
2104 sampler_type
= "sampler2DRectShadow";
2106 sampler_type
= "sampler2DShadow";
2111 sampler_type
= "sampler2DRect";
2113 sampler_type
= "sampler2D";
2117 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2119 FIXME("Unsupported 3D shadow sampler.\n");
2120 sampler_type
= "sampler3D";
2123 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
2125 FIXME("Unsupported Cube shadow sampler.\n");
2126 sampler_type
= "samplerCube";
2129 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
2131 sampler_type
= "sampler2DArrayShadow";
2133 sampler_type
= "sampler2DArray";
2137 sampler_type
= "unsupported_sampler";
2138 FIXME("Unhandled resource type %#x.\n", reg_maps
->resource_info
[entry
->resource_idx
].type
);
2141 shader_addline(buffer
, "uniform %s%s %s_sampler%u;\n",
2142 sampler_type_prefix
, sampler_type
, prefix
, entry
->bind_idx
);
2145 /* Declare images */
2146 for (i
= 0; i
< ARRAY_SIZE(reg_maps
->uav_resource_info
); ++i
)
2148 const char *image_type_prefix
, *image_type
, *read_format
;
2150 if (!reg_maps
->uav_resource_info
[i
].type
)
2153 switch (reg_maps
->uav_resource_info
[i
].data_type
)
2155 case WINED3D_DATA_FLOAT
:
2156 case WINED3D_DATA_UNORM
:
2157 case WINED3D_DATA_SNORM
:
2158 image_type_prefix
= "";
2159 read_format
= "r32f";
2162 case WINED3D_DATA_INT
:
2163 image_type_prefix
= "i";
2164 read_format
= "r32i";
2167 case WINED3D_DATA_UINT
:
2168 image_type_prefix
= "u";
2169 read_format
= "r32ui";
2173 image_type_prefix
= "";
2175 ERR("Unhandled resource data type %#x.\n", reg_maps
->uav_resource_info
[i
].data_type
);
2179 switch (reg_maps
->uav_resource_info
[i
].type
)
2181 case WINED3D_SHADER_RESOURCE_BUFFER
:
2182 image_type
= "imageBuffer";
2185 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2186 image_type
= "image2D";
2189 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2190 image_type
= "image3D";
2193 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
2194 image_type
= "image2DArray";
2198 image_type
= "unsupported_image";
2199 FIXME("Unhandled resource type %#x.\n", reg_maps
->uav_resource_info
[i
].type
);
2203 if (reg_maps
->uav_read_mask
& (1u << i
))
2204 shader_addline(buffer
, "layout(%s) uniform %s%s %s_image%u;\n",
2205 read_format
, image_type_prefix
, image_type
, prefix
, i
);
2207 shader_addline(buffer
, "writeonly uniform %s%s %s_image%u;\n",
2208 image_type_prefix
, image_type
, prefix
, i
);
2211 /* Declare uniforms for NP2 texcoord fixup:
2212 * This is NOT done inside the loop that declares the texture samplers
2213 * since the NP2 fixup code is currently only used for the GeforceFX
2214 * series and when forcing the ARB_npot extension off. Modern cards just
2215 * skip the code anyway, so put it inside a separate loop. */
2216 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& ps_args
->np2_fixup
)
2218 struct ps_np2fixup_info
*fixup
= ctx_priv
->cur_np2fixup_info
;
2221 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2222 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2223 * samplerNP2Fixup stores texture dimensions and is updated through
2224 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2226 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2228 if (!reg_maps
->resource_info
[i
].type
|| !(ps_args
->np2_fixup
& (1u << i
)))
2231 if (reg_maps
->resource_info
[i
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_2D
)
2233 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2237 fixup
->idx
[i
] = cur
++;
2240 fixup
->num_consts
= (cur
+ 1) >> 1;
2241 fixup
->active
= ps_args
->np2_fixup
;
2242 shader_addline(buffer
, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix
, fixup
->num_consts
);
2245 /* Declare address variables */
2246 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
2248 if (map
& 1) shader_addline(buffer
, "ivec4 A%u;\n", i
);
2251 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
2253 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
2254 shader_glsl_declare_generic_vertex_attribute(buffer
, gl_info
, &shader
->input_signature
.elements
[i
]);
2256 if (vs_args
->point_size
&& !vs_args
->per_vertex_point_size
)
2258 shader_addline(buffer
, "uniform struct\n{\n");
2259 shader_addline(buffer
, " float size;\n");
2260 shader_addline(buffer
, " float size_min;\n");
2261 shader_addline(buffer
, " float size_max;\n");
2262 shader_addline(buffer
, "} ffp_point;\n");
2265 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2267 if (vs_args
->clip_enabled
)
2268 shader_addline(buffer
, "uniform vec4 clip_planes[%u];\n", gl_info
->limits
.user_clip_distances
);
2270 if (version
->major
< 3)
2272 declare_out_varying(gl_info
, buffer
, vs_args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
2273 declare_out_varying(gl_info
, buffer
, vs_args
->flatshading
, "vec4 ffp_varying_specular;\n");
2274 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
2275 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
2279 if (version
->major
< 4)
2280 shader_addline(buffer
, "void setup_vs_output(in vec4[%u]);\n", shader
->limits
->packed_output
);
2282 else if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
2284 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2286 shader_addline(buffer
, "varying in vec4 gs_in[][%u];\n", shader
->limits
->packed_input
);
2290 shader_addline(buffer
, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader
->u
.gs
.input_type
));
2291 shader_addline(buffer
, "layout(%s, max_vertices = %u) out;\n",
2292 glsl_primitive_type_from_d3d(shader
->u
.gs
.output_type
), shader
->u
.gs
.vertices_out
);
2293 shader_addline(buffer
, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader
->limits
->packed_input
);
2296 else if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2298 if (version
->major
< 3 || ps_args
->vp_mode
!= vertexshader
)
2300 shader_addline(buffer
, "uniform struct\n{\n");
2301 shader_addline(buffer
, " vec4 color;\n");
2302 shader_addline(buffer
, " float density;\n");
2303 shader_addline(buffer
, " float end;\n");
2304 shader_addline(buffer
, " float scale;\n");
2305 shader_addline(buffer
, "} ffp_fog;\n");
2307 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2309 if (glsl_is_color_reg_read(shader
, 0))
2310 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
2311 if (glsl_is_color_reg_read(shader
, 1))
2312 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
2313 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
2314 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
2318 if (glsl_is_color_reg_read(shader
, 0))
2319 declare_in_varying(gl_info
, buffer
, ps_args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
2320 if (glsl_is_color_reg_read(shader
, 1))
2321 declare_in_varying(gl_info
, buffer
, ps_args
->flatshading
, "vec4 ffp_varying_specular;\n");
2322 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
2323 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
2324 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
2328 if (version
->major
>= 3)
2330 UINT in_count
= min(vec4_varyings(version
->major
, gl_info
), shader
->limits
->packed_input
);
2332 if (ps_args
->vp_mode
== vertexshader
)
2333 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 %s_link[%u];\n", prefix
, in_count
);
2334 shader_addline(buffer
, "vec4 %s_in[%u];\n", prefix
, in_count
);
2337 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
2342 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", i
);
2344 if (reg_maps
->luminanceparams
& (1u << i
))
2346 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", i
);
2347 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", i
);
2348 extra_constants_needed
++;
2351 extra_constants_needed
++;
2354 if (ps_args
->srgb_correction
)
2356 shader_addline(buffer
, "const vec4 srgb_const0 = ");
2357 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
2358 shader_addline(buffer
, ";\n");
2359 shader_addline(buffer
, "const vec4 srgb_const1 = ");
2360 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
2361 shader_addline(buffer
, ";\n");
2363 if (reg_maps
->vpos
|| reg_maps
->usesdsy
)
2365 if (reg_maps
->usesdsy
|| !gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
2367 ++extra_constants_needed
;
2368 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
2372 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
2374 if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_PIXEL_CENTER_INTEGER
)
2375 shader_addline(buffer
, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2376 ps_args
->render_offscreen
? "" : "origin_upper_left, ");
2377 else if (!ps_args
->render_offscreen
)
2378 shader_addline(buffer
, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2380 shader_addline(buffer
, "vec4 vpos;\n");
2384 if (ps_args
->alpha_test_func
+ 1 != WINED3D_CMP_ALWAYS
)
2385 shader_addline(buffer
, "uniform float alpha_test_ref;\n");
2387 if (!needs_legacy_glsl_syntax(gl_info
))
2388 shader_addline(buffer
, "out vec4 ps_out[%u];\n", gl_info
->limits
.buffers
);
2390 if (shader
->limits
->constant_float
+ extra_constants_needed
>= gl_info
->limits
.glsl_ps_float_constants
)
2391 FIXME("Insufficient uniforms to run this shader.\n");
2394 /* Declare output register temporaries */
2395 if (shader
->limits
->packed_output
)
2396 shader_addline(buffer
, "vec4 %s_out[%u];\n", prefix
, shader
->limits
->packed_output
);
2398 /* Declare temporary variables */
2399 if (reg_maps
->temporary_count
)
2401 for (i
= 0; i
< reg_maps
->temporary_count
; ++i
)
2402 shader_addline(buffer
, "vec4 R%u;\n", i
);
2406 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
2409 shader_addline(buffer
, "vec4 R%u;\n", i
);
2413 /* Declare indexable temporary variables */
2414 LIST_FOR_EACH_ENTRY(idx_temp_reg
, ®_maps
->indexable_temps
, struct wined3d_shader_indexable_temp
, entry
)
2416 if (idx_temp_reg
->component_count
!= 4)
2417 FIXME("Ignoring component count %u.\n", idx_temp_reg
->component_count
);
2418 shader_addline(buffer
, "vec4 X%u[%u];\n", idx_temp_reg
->register_idx
, idx_temp_reg
->register_size
);
2421 /* Declare loop registers aLx */
2422 if (version
->major
< 4)
2424 for (i
= 0; i
< reg_maps
->loop_depth
; ++i
)
2426 shader_addline(buffer
, "int aL%u;\n", i
);
2427 shader_addline(buffer
, "int tmpInt%u;\n", i
);
2431 /* Temporary variables for matrix operations */
2432 shader_addline(buffer
, "vec4 tmp0;\n");
2433 shader_addline(buffer
, "vec4 tmp1;\n");
2435 if (!shader
->load_local_constsF
)
2437 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
2439 shader_addline(buffer
, "const vec4 %s_lc%u = ", prefix
, lconst
->idx
);
2440 shader_glsl_append_imm_vec4(buffer
, (const float *)lconst
->value
);
2441 shader_addline(buffer
, ";\n");
2446 /*****************************************************************************
2447 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2449 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2450 ****************************************************************************/
2453 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2454 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
);
2456 /** Used for opcode modifiers - They multiply the result by the specified amount */
2457 static const char * const shift_glsl_tab
[] = {
2459 "2.0 * ", /* 1 (x2) */
2460 "4.0 * ", /* 2 (x4) */
2461 "8.0 * ", /* 3 (x8) */
2462 "16.0 * ", /* 4 (x16) */
2463 "32.0 * ", /* 5 (x32) */
2470 "0.0625 * ", /* 12 (d16) */
2471 "0.125 * ", /* 13 (d8) */
2472 "0.25 * ", /* 14 (d4) */
2473 "0.5 * " /* 15 (d2) */
2476 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2477 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier
,
2478 const char *in_reg
, const char *in_regswizzle
, char *out_str
)
2480 switch (src_modifier
)
2482 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
2483 case WINED3DSPSM_DW
:
2484 case WINED3DSPSM_NONE
:
2485 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2487 case WINED3DSPSM_NEG
:
2488 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
2490 case WINED3DSPSM_NOT
:
2491 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
2493 case WINED3DSPSM_BIAS
:
2494 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2496 case WINED3DSPSM_BIASNEG
:
2497 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2499 case WINED3DSPSM_SIGN
:
2500 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2502 case WINED3DSPSM_SIGNNEG
:
2503 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2505 case WINED3DSPSM_COMP
:
2506 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
2508 case WINED3DSPSM_X2
:
2509 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
2511 case WINED3DSPSM_X2NEG
:
2512 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
2514 case WINED3DSPSM_ABS
:
2515 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
2517 case WINED3DSPSM_ABSNEG
:
2518 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
2521 FIXME("Unhandled modifier %u\n", src_modifier
);
2522 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2526 /** Writes the GLSL variable name that corresponds to the register that the
2527 * DX opcode parameter is trying to access */
2528 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
2529 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
2531 /* oPos, oFog and oPts in D3D */
2532 static const char * const hwrastout_reg_names
[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2534 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
2535 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
2536 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
2537 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
2538 const char *prefix
= shader_glsl_get_prefix(version
->type
);
2539 struct glsl_src_param rel_param0
, rel_param1
;
2540 char imm_str
[4][17];
2542 if (reg
->idx
[0].offset
!= ~0U && reg
->idx
[0].rel_addr
)
2543 shader_glsl_add_src_param(ins
, reg
->idx
[0].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param0
);
2544 if (reg
->idx
[1].offset
!= ~0U && reg
->idx
[1].rel_addr
)
2545 shader_glsl_add_src_param(ins
, reg
->idx
[1].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param1
);
2550 case WINED3DSPR_TEMP
:
2551 sprintf(register_name
, "R%u", reg
->idx
[0].offset
);
2554 case WINED3DSPR_INPUT
:
2555 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
2557 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2559 if (reg
->idx
[0].rel_addr
)
2560 FIXME("VS3+ input registers relative addressing.\n");
2561 if (priv
->cur_vs_args
->swizzle_map
& (1u << reg
->idx
[0].offset
))
2563 sprintf(register_name
, "%s_in%u", prefix
, reg
->idx
[0].offset
);
2567 if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
2569 if (reg
->idx
[0].rel_addr
)
2571 if (reg
->idx
[1].rel_addr
)
2572 sprintf(register_name
, "gs_in[%s + %u]%s[%s + %u]",
2573 rel_param0
.param_str
, reg
->idx
[0].offset
,
2574 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2575 rel_param1
.param_str
, reg
->idx
[1].offset
);
2577 sprintf(register_name
, "gs_in[%s + %u]%s[%u]",
2578 rel_param0
.param_str
, reg
->idx
[0].offset
,
2579 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2580 reg
->idx
[1].offset
);
2582 else if (reg
->idx
[1].rel_addr
)
2583 sprintf(register_name
, "gs_in[%u]%s[%s + %u]", reg
->idx
[0].offset
,
2584 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2585 rel_param1
.param_str
, reg
->idx
[1].offset
);
2587 sprintf(register_name
, "gs_in[%u]%s[%u]", reg
->idx
[0].offset
,
2588 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2589 reg
->idx
[1].offset
);
2593 /* pixel shaders >= 3.0 */
2594 if (version
->major
>= 3)
2596 DWORD idx
= shader
->u
.ps
.input_reg_map
[reg
->idx
[0].offset
];
2597 unsigned int in_count
= vec4_varyings(version
->major
, gl_info
);
2599 if (reg
->idx
[0].rel_addr
)
2601 /* Removing a + 0 would be an obvious optimization, but
2602 * OS X doesn't see the NOP operation there. */
2605 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2606 && shader
->u
.ps
.declared_in_count
> in_count
)
2608 sprintf(register_name
,
2609 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2610 rel_param0
.param_str
, idx
, in_count
- 1, rel_param0
.param_str
, idx
, in_count
,
2611 prefix
, rel_param0
.param_str
, idx
);
2615 sprintf(register_name
, "%s_in[%s + %u]", prefix
, rel_param0
.param_str
, idx
);
2620 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2621 && shader
->u
.ps
.declared_in_count
> in_count
)
2623 sprintf(register_name
, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2624 rel_param0
.param_str
, in_count
- 1, rel_param0
.param_str
, in_count
,
2625 prefix
, rel_param0
.param_str
);
2629 sprintf(register_name
, "%s_in[%s]", prefix
, rel_param0
.param_str
);
2635 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
2636 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
2637 else sprintf(register_name
, "%s_in[%u]", prefix
, idx
);
2642 if (!reg
->idx
[0].offset
)
2643 strcpy(register_name
, "ffp_varying_diffuse");
2645 strcpy(register_name
, "ffp_varying_specular");
2650 case WINED3DSPR_CONST
:
2652 /* Relative addressing */
2653 if (reg
->idx
[0].rel_addr
)
2655 if (wined3d_settings
.check_float_constants
)
2656 sprintf(register_name
, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2657 rel_param0
.param_str
, reg
->idx
[0].offset
,
2658 rel_param0
.param_str
, reg
->idx
[0].offset
, shader
->limits
->constant_float
,
2659 prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2660 else if (reg
->idx
[0].offset
)
2661 sprintf(register_name
, "%s_c[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2663 sprintf(register_name
, "%s_c[%s]", prefix
, rel_param0
.param_str
);
2667 if (shader_constant_is_local(shader
, reg
->idx
[0].offset
))
2668 sprintf(register_name
, "%s_lc%u", prefix
, reg
->idx
[0].offset
);
2670 sprintf(register_name
, "%s_c[%u]", prefix
, reg
->idx
[0].offset
);
2675 case WINED3DSPR_CONSTINT
:
2676 sprintf(register_name
, "%s_i[%u]", prefix
, reg
->idx
[0].offset
);
2679 case WINED3DSPR_CONSTBOOL
:
2680 sprintf(register_name
, "%s_b[%u]", prefix
, reg
->idx
[0].offset
);
2683 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
2684 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2685 sprintf(register_name
, "T%u", reg
->idx
[0].offset
);
2687 sprintf(register_name
, "A%u", reg
->idx
[0].offset
);
2690 case WINED3DSPR_LOOP
:
2691 sprintf(register_name
, "aL%u", ins
->ctx
->state
->current_loop_reg
- 1);
2694 case WINED3DSPR_SAMPLER
:
2695 sprintf(register_name
, "%s_sampler%u", prefix
, reg
->idx
[0].offset
);
2698 case WINED3DSPR_COLOROUT
:
2699 if (reg
->idx
[0].offset
>= gl_info
->limits
.buffers
)
2700 WARN("Write to render target %u, only %d supported.\n",
2701 reg
->idx
[0].offset
, gl_info
->limits
.buffers
);
2703 sprintf(register_name
, "%s[%u]", get_fragment_output(gl_info
), reg
->idx
[0].offset
);
2706 case WINED3DSPR_RASTOUT
:
2707 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
[0].offset
]);
2710 case WINED3DSPR_DEPTHOUT
:
2711 sprintf(register_name
, "gl_FragDepth");
2714 case WINED3DSPR_ATTROUT
:
2715 if (!reg
->idx
[0].offset
)
2716 sprintf(register_name
, "%s_out[8]", prefix
);
2718 sprintf(register_name
, "%s_out[9]", prefix
);
2721 case WINED3DSPR_TEXCRDOUT
:
2722 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2723 if (reg
->idx
[0].rel_addr
)
2724 FIXME("VS3 output registers relative addressing.\n");
2725 sprintf(register_name
, "%s_out[%u]", prefix
, reg
->idx
[0].offset
);
2728 case WINED3DSPR_MISCTYPE
:
2729 if (!reg
->idx
[0].offset
)
2732 sprintf(register_name
, "vpos");
2734 else if (reg
->idx
[0].offset
== 1)
2736 /* Note that gl_FrontFacing is a bool, while vFace is
2737 * a float for which the sign determines front/back */
2738 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
2742 FIXME("Unhandled misctype register %u.\n", reg
->idx
[0].offset
);
2743 sprintf(register_name
, "unrecognized_register");
2747 case WINED3DSPR_IMMCONST
:
2748 switch (reg
->immconst_type
)
2750 case WINED3D_IMMCONST_SCALAR
:
2751 switch (reg
->data_type
)
2753 case WINED3D_DATA_FLOAT
:
2754 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
2755 sprintf(register_name
, "uintBitsToFloat(%#xu)", reg
->u
.immconst_data
[0]);
2757 wined3d_ftoa(*(const float *)reg
->u
.immconst_data
, register_name
);
2759 case WINED3D_DATA_INT
:
2760 sprintf(register_name
, "%#x", reg
->u
.immconst_data
[0]);
2762 case WINED3D_DATA_RESOURCE
:
2763 case WINED3D_DATA_SAMPLER
:
2764 case WINED3D_DATA_UINT
:
2765 sprintf(register_name
, "%#xu", reg
->u
.immconst_data
[0]);
2768 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2773 case WINED3D_IMMCONST_VEC4
:
2774 switch (reg
->data_type
)
2776 case WINED3D_DATA_FLOAT
:
2777 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
2779 sprintf(register_name
, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2780 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
2781 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
2785 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[0], imm_str
[0]);
2786 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[1], imm_str
[1]);
2787 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[2], imm_str
[2]);
2788 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[3], imm_str
[3]);
2789 sprintf(register_name
, "vec4(%s, %s, %s, %s)",
2790 imm_str
[0], imm_str
[1], imm_str
[2], imm_str
[3]);
2793 case WINED3D_DATA_INT
:
2794 sprintf(register_name
, "ivec4(%#x, %#x, %#x, %#x)",
2795 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
2796 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
2798 case WINED3D_DATA_RESOURCE
:
2799 case WINED3D_DATA_SAMPLER
:
2800 case WINED3D_DATA_UINT
:
2801 sprintf(register_name
, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2802 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
2803 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
2806 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2812 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
2813 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
2817 case WINED3DSPR_CONSTBUFFER
:
2818 if (reg
->idx
[1].rel_addr
)
2819 sprintf(register_name
, "%s_cb%u[%s + %u]",
2820 prefix
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2822 sprintf(register_name
, "%s_cb%u[%u]", prefix
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2825 case WINED3DSPR_IMMCONSTBUFFER
:
2826 if (reg
->idx
[0].rel_addr
)
2827 sprintf(register_name
, "%s_icb[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2829 sprintf(register_name
, "%s_icb[%u]", prefix
, reg
->idx
[0].offset
);
2832 case WINED3DSPR_PRIMID
:
2833 sprintf(register_name
, "uint(gl_PrimitiveIDIn)");
2836 case WINED3DSPR_IDXTEMP
:
2837 if (reg
->idx
[1].rel_addr
)
2838 sprintf(register_name
, "X%u[%s + %u]", reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2840 sprintf(register_name
, "X%u[%u]", reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2843 case WINED3DSPR_LOCALTHREADINDEX
:
2844 sprintf(register_name
, "int(gl_LocalInvocationIndex)");
2847 case WINED3DSPR_THREADID
:
2848 sprintf(register_name
, "ivec3(gl_GlobalInvocationID)");
2851 case WINED3DSPR_THREADGROUPID
:
2852 sprintf(register_name
, "ivec3(gl_WorkGroupID)");
2855 case WINED3DSPR_LOCALTHREADID
:
2856 sprintf(register_name
, "ivec3(gl_LocalInvocationID)");
2860 FIXME("Unhandled register type %#x.\n", reg
->type
);
2861 sprintf(register_name
, "unrecognized_register");
2866 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
2869 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
2870 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
2871 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
2872 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
2876 /* Get the GLSL write mask for the destination register */
2877 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
2879 DWORD mask
= param
->write_mask
;
2881 if (shader_is_scalar(¶m
->reg
))
2883 mask
= WINED3DSP_WRITEMASK_0
;
2888 shader_glsl_write_mask_to_str(mask
, write_mask
);
2894 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
2895 unsigned int size
= 0;
2897 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
2898 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
2899 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
2900 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
2905 static void shader_glsl_swizzle_to_str(const DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
2907 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2908 * but addressed as "rgba". To fix this we need to swap the register's x
2909 * and z components. */
2910 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
2913 /* swizzle bits fields: wwzzyyxx */
2914 if (mask
& WINED3DSP_WRITEMASK_0
) *str
++ = swizzle_chars
[swizzle
& 0x03];
2915 if (mask
& WINED3DSP_WRITEMASK_1
) *str
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
2916 if (mask
& WINED3DSP_WRITEMASK_2
) *str
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
2917 if (mask
& WINED3DSP_WRITEMASK_3
) *str
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
2921 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
2922 BOOL fixup
, DWORD mask
, char *swizzle_str
)
2924 if (shader_is_scalar(¶m
->reg
))
2925 *swizzle_str
= '\0';
2927 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
2930 /* From a given parameter token, generate the corresponding GLSL string.
2931 * Also, return the actual register name and swizzle in case the
2932 * caller needs this information as well. */
2933 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction
*ins
,
2934 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
,
2935 enum wined3d_data_type data_type
)
2937 BOOL is_color
= FALSE
;
2938 char swizzle_str
[6];
2940 glsl_src
->reg_name
[0] = '\0';
2941 glsl_src
->param_str
[0] = '\0';
2942 swizzle_str
[0] = '\0';
2944 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
2945 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
2947 if (wined3d_src
->reg
.type
== WINED3DSPR_IMMCONST
2948 || wined3d_src
->reg
.type
== WINED3DSPR_PRIMID
2949 || wined3d_src
->reg
.type
== WINED3DSPR_LOCALTHREADINDEX
2950 || wined3d_src
->reg
.type
== WINED3DSPR_THREADID
2951 || wined3d_src
->reg
.type
== WINED3DSPR_THREADGROUPID
2952 || wined3d_src
->reg
.type
== WINED3DSPR_LOCALTHREADID
)
2954 shader_glsl_gen_modifier(wined3d_src
->modifiers
, glsl_src
->reg_name
, swizzle_str
, glsl_src
->param_str
);
2962 case WINED3D_DATA_FLOAT
:
2963 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2965 case WINED3D_DATA_INT
:
2966 sprintf(reg_name
, "floatBitsToInt(%s)", glsl_src
->reg_name
);
2968 case WINED3D_DATA_RESOURCE
:
2969 case WINED3D_DATA_SAMPLER
:
2970 case WINED3D_DATA_UINT
:
2971 sprintf(reg_name
, "floatBitsToUint(%s)", glsl_src
->reg_name
);
2974 FIXME("Unhandled data type %#x.\n", data_type
);
2975 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2979 shader_glsl_gen_modifier(wined3d_src
->modifiers
, reg_name
, swizzle_str
, glsl_src
->param_str
);
2983 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2984 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
)
2986 shader_glsl_add_src_param_ext(ins
, wined3d_src
, mask
, glsl_src
, wined3d_src
->reg
.data_type
);
2989 /* From a given parameter token, generate the corresponding GLSL string.
2990 * Also, return the actual register name and swizzle in case the
2991 * caller needs this information as well. */
2992 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
2993 const struct wined3d_shader_dst_param
*wined3d_dst
, struct glsl_dst_param
*glsl_dst
)
2995 BOOL is_color
= FALSE
;
2997 glsl_dst
->mask_str
[0] = '\0';
2998 glsl_dst
->reg_name
[0] = '\0';
3000 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
3001 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
3004 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3005 static DWORD
shader_glsl_append_dst_ext(struct wined3d_string_buffer
*buffer
,
3006 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
,
3007 enum wined3d_data_type data_type
)
3009 struct glsl_dst_param glsl_dst
;
3012 if ((mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
)))
3016 case WINED3D_DATA_FLOAT
:
3017 shader_addline(buffer
, "%s%s = %s(",
3018 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3020 case WINED3D_DATA_INT
:
3021 shader_addline(buffer
, "%s%s = %sintBitsToFloat(",
3022 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3024 case WINED3D_DATA_RESOURCE
:
3025 case WINED3D_DATA_SAMPLER
:
3026 case WINED3D_DATA_UINT
:
3027 shader_addline(buffer
, "%s%s = %suintBitsToFloat(",
3028 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3031 FIXME("Unhandled data type %#x.\n", data_type
);
3032 shader_addline(buffer
, "%s%s = %s(",
3033 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3041 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3042 static DWORD
shader_glsl_append_dst(struct wined3d_string_buffer
*buffer
, const struct wined3d_shader_instruction
*ins
)
3044 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3047 /** Process GLSL instruction modifiers */
3048 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
3050 struct glsl_dst_param dst_param
;
3053 if (!ins
->dst_count
) return;
3055 modifiers
= ins
->dst
[0].modifiers
;
3056 if (!modifiers
) return;
3058 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3060 if (modifiers
& WINED3DSPDM_SATURATE
)
3062 /* _SAT means to clamp the value of the register to between 0 and 1 */
3063 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
3064 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
3067 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
3069 FIXME("_centroid modifier not handled\n");
3072 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
3074 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3078 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op
)
3082 case WINED3D_SHADER_REL_OP_GT
: return ">";
3083 case WINED3D_SHADER_REL_OP_EQ
: return "==";
3084 case WINED3D_SHADER_REL_OP_GE
: return ">=";
3085 case WINED3D_SHADER_REL_OP_LT
: return "<";
3086 case WINED3D_SHADER_REL_OP_NE
: return "!=";
3087 case WINED3D_SHADER_REL_OP_LE
: return "<=";
3089 FIXME("Unrecognized operator %#x.\n", op
);
3094 static BOOL
shader_glsl_has_core_grad(const struct wined3d_gl_info
*gl_info
,
3095 const struct wined3d_shader_version
*version
)
3097 return shader_glsl_get_version(gl_info
, version
) >= 130 || gl_info
->supported
[EXT_GPU_SHADER4
];
3100 static void shader_glsl_get_sample_function(const struct wined3d_shader_context
*ctx
,
3101 DWORD resource_idx
, DWORD sampler_idx
, DWORD flags
, struct glsl_sample_function
*sample_function
)
3103 enum wined3d_shader_resource_type resource_type
= ctx
->reg_maps
->resource_info
[resource_idx
].type
;
3104 struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
3105 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
3106 BOOL shadow
= glsl_is_shadow_sampler(ctx
->shader
, priv
->cur_ps_args
, resource_idx
, sampler_idx
);
3107 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
3108 BOOL texrect
= ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
3109 && priv
->cur_ps_args
->np2_fixup
& (1u << resource_idx
)
3110 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
3111 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
3112 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
3113 BOOL offset
= flags
& WINED3D_GLSL_SAMPLE_OFFSET
;
3114 const char *base
= "texture", *type_part
= "", *suffix
= "";
3115 unsigned int coord_size
, deriv_size
;
3118 sample_function
->data_type
= ctx
->reg_maps
->resource_info
[resource_idx
].data_type
;
3120 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
3122 ERR("Unexpected resource type %#x.\n", resource_type
);
3123 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
3125 array
= resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3126 || resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
;
3128 /* Note that there's no such thing as a projected cube texture. */
3129 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
3132 if (needs_legacy_glsl_syntax(gl_info
))
3137 type_part
= resource_type_info
[resource_type
].type_part
;
3138 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
&& texrect
)
3139 type_part
= "2DRect";
3141 FIXME("Unhandled resource type %#x.\n", resource_type
);
3143 if (!lod
&& grad
&& !shader_glsl_has_core_grad(gl_info
, &ctx
->shader
->reg_maps
.shader_version
))
3145 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
3148 FIXME("Unsupported grad function.\n");
3152 if (flags
& WINED3D_GLSL_SAMPLE_LOAD
)
3154 static const DWORD texel_fetch_flags
= WINED3D_GLSL_SAMPLE_LOAD
| WINED3D_GLSL_SAMPLE_OFFSET
;
3155 if (flags
& ~texel_fetch_flags
)
3156 ERR("Unexpected flags %#x for texelFetch.\n", flags
& ~texel_fetch_flags
);
3158 base
= "texelFetch";
3162 sample_function
->name
= string_buffer_get(priv
->string_buffers
);
3163 string_buffer_sprintf(sample_function
->name
, "%s%s%s%s%s%s", base
, type_part
, projected
? "Proj" : "",
3164 lod
? "Lod" : grad
? "Grad" : "", offset
? "Offset" : "", suffix
);
3166 coord_size
= resource_type_info
[resource_type
].coord_size
;
3167 deriv_size
= coord_size
;
3172 sample_function
->offset_size
= offset
? deriv_size
: 0;
3173 sample_function
->coord_mask
= (1u << coord_size
) - 1;
3174 sample_function
->deriv_mask
= (1u << deriv_size
) - 1;
3175 sample_function
->output_single_component
= shadow
&& !needs_legacy_glsl_syntax(gl_info
);
3178 static void shader_glsl_release_sample_function(const struct wined3d_shader_context
*ctx
,
3179 struct glsl_sample_function
*sample_function
)
3181 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
3183 string_buffer_release(priv
->string_buffers
, sample_function
->name
);
3186 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
3187 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
3189 switch(channel_source
)
3191 case CHANNEL_SOURCE_ZERO
:
3192 strcat(arguments
, "0.0");
3195 case CHANNEL_SOURCE_ONE
:
3196 strcat(arguments
, "1.0");
3199 case CHANNEL_SOURCE_X
:
3200 strcat(arguments
, reg_name
);
3201 strcat(arguments
, ".x");
3204 case CHANNEL_SOURCE_Y
:
3205 strcat(arguments
, reg_name
);
3206 strcat(arguments
, ".y");
3209 case CHANNEL_SOURCE_Z
:
3210 strcat(arguments
, reg_name
);
3211 strcat(arguments
, ".z");
3214 case CHANNEL_SOURCE_W
:
3215 strcat(arguments
, reg_name
);
3216 strcat(arguments
, ".w");
3220 FIXME("Unhandled channel source %#x\n", channel_source
);
3221 strcat(arguments
, "undefined");
3225 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
3228 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer
*buffer
,
3229 const char *reg_name
, DWORD mask
, struct color_fixup_desc fixup
)
3231 unsigned int mask_size
, remaining
;
3232 DWORD fixup_mask
= 0;
3233 char arguments
[256];
3236 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) fixup_mask
|= WINED3DSP_WRITEMASK_0
;
3237 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) fixup_mask
|= WINED3DSP_WRITEMASK_1
;
3238 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) fixup_mask
|= WINED3DSP_WRITEMASK_2
;
3239 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) fixup_mask
|= WINED3DSP_WRITEMASK_3
;
3240 if (!(mask
&= fixup_mask
))
3243 if (is_complex_fixup(fixup
))
3245 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
3246 FIXME("Complex fixup (%#x) not supported\n",complex_fixup
);
3250 shader_glsl_write_mask_to_str(mask
, mask_str
);
3251 mask_size
= shader_glsl_get_write_mask_size(mask
);
3253 arguments
[0] = '\0';
3254 remaining
= mask_size
;
3255 if (mask
& WINED3DSP_WRITEMASK_0
)
3257 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
3258 if (--remaining
) strcat(arguments
, ", ");
3260 if (mask
& WINED3DSP_WRITEMASK_1
)
3262 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
3263 if (--remaining
) strcat(arguments
, ", ");
3265 if (mask
& WINED3DSP_WRITEMASK_2
)
3267 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
3268 if (--remaining
) strcat(arguments
, ", ");
3270 if (mask
& WINED3DSP_WRITEMASK_3
)
3272 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
3273 if (--remaining
) strcat(arguments
, ", ");
3277 shader_addline(buffer
, "%s%s = vec%u(%s);\n", reg_name
, mask_str
, mask_size
, arguments
);
3279 shader_addline(buffer
, "%s%s = %s;\n", reg_name
, mask_str
, arguments
);
3282 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
3287 shader_glsl_get_register_name(&ins
->dst
[0].reg
, reg_name
, &is_color
, ins
);
3288 shader_glsl_color_correction_ext(ins
->ctx
->buffer
, reg_name
, ins
->dst
[0].write_mask
, fixup
);
3291 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
3292 unsigned int sampler_bind_idx
, const struct glsl_sample_function
*sample_function
, DWORD swizzle
,
3293 const char *dx
, const char *dy
, const char *bias
, const struct wined3d_shader_texel_offset
*offset
,
3294 const char *coord_reg_fmt
, ...)
3296 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
3297 char dst_swizzle
[6];
3298 struct color_fixup_desc fixup
;
3299 BOOL np2_fixup
= FALSE
;
3303 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
3305 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3306 * We actually rely on it for vertex shaders and SM4+. */
3307 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& version
->major
< 4)
3309 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3310 fixup
= priv
->cur_ps_args
->color_fixup
[sampler_bind_idx
];
3312 if (priv
->cur_ps_args
->np2_fixup
& (1u << sampler_bind_idx
))
3317 fixup
= COLOR_FIXUP_IDENTITY
;
3320 shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], sample_function
->data_type
);
3322 if (sample_function
->output_single_component
)
3323 shader_addline(ins
->ctx
->buffer
, "vec4(");
3325 shader_addline(ins
->ctx
->buffer
, "%s(%s_sampler%u, ",
3326 sample_function
->name
->buffer
, shader_glsl_get_prefix(version
->type
), sampler_bind_idx
);
3330 va_start(args
, coord_reg_fmt
);
3331 ret
= shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
3335 if (!string_buffer_resize(ins
->ctx
->buffer
, ret
))
3341 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3342 const unsigned char idx
= priv
->cur_np2fixup_info
->idx
[sampler_bind_idx
];
3344 switch (shader_glsl_get_write_mask_size(sample_function
->coord_mask
))
3347 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
3348 idx
>> 1, (idx
% 2) ? "z" : "x");
3351 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
3352 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3355 shader_addline(ins
->ctx
->buffer
, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3356 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3359 shader_addline(ins
->ctx
->buffer
, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3360 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3365 shader_addline(ins
->ctx
->buffer
, ", %s, %s", dx
, dy
);
3367 shader_addline(ins
->ctx
->buffer
, ", %s", bias
);
3368 if (sample_function
->offset_size
)
3370 int offset_immdata
[4] = {offset
->u
, offset
->v
, offset
->w
};
3371 shader_addline(ins
->ctx
->buffer
, ", ");
3372 shader_glsl_append_imm_ivec(ins
->ctx
->buffer
, offset_immdata
, sample_function
->offset_size
);
3374 shader_addline(ins
->ctx
->buffer
, ")");
3376 if (sample_function
->output_single_component
)
3377 shader_addline(ins
->ctx
->buffer
, ")");
3379 shader_addline(ins
->ctx
->buffer
, "%s);\n", dst_swizzle
);
3381 if (!is_identity_fixup(fixup
))
3382 shader_glsl_color_correction(ins
, fixup
);
3385 static void shader_glsl_fixup_position(struct wined3d_string_buffer
*buffer
)
3387 /* Write the final position.
3389 * OpenGL coordinates specify the center of the pixel while D3D coords
3390 * specify the corner. The offsets are stored in z and w in
3391 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3392 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3394 shader_addline(buffer
, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3395 shader_addline(buffer
, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3397 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3400 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3401 * shaders are run before the homogeneous divide, so we have to take the w
3402 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3404 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3407 /*****************************************************************************
3408 * Begin processing individual instruction opcodes
3409 ****************************************************************************/
3411 static void shader_glsl_binop(const struct wined3d_shader_instruction
*ins
)
3413 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3414 struct glsl_src_param src0_param
;
3415 struct glsl_src_param src1_param
;
3419 /* Determine the GLSL operator to use based on the opcode */
3420 switch (ins
->handler_idx
)
3422 case WINED3DSIH_ADD
: op
= "+"; break;
3423 case WINED3DSIH_AND
: op
= "&"; break;
3424 case WINED3DSIH_DIV
: op
= "/"; break;
3425 case WINED3DSIH_IADD
: op
= "+"; break;
3426 case WINED3DSIH_ISHL
: op
= "<<"; break;
3427 case WINED3DSIH_ISHR
: op
= ">>"; break;
3428 case WINED3DSIH_MUL
: op
= "*"; break;
3429 case WINED3DSIH_OR
: op
= "|"; break;
3430 case WINED3DSIH_SUB
: op
= "-"; break;
3431 case WINED3DSIH_USHR
: op
= ">>"; break;
3432 case WINED3DSIH_XOR
: op
= "^"; break;
3434 op
= "<unhandled operator>";
3435 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3439 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3440 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3441 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3442 shader_addline(buffer
, "%s %s %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
3445 static void shader_glsl_relop(const struct wined3d_shader_instruction
*ins
)
3447 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3448 struct glsl_src_param src0_param
;
3449 struct glsl_src_param src1_param
;
3450 unsigned int mask_size
;
3454 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3455 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3456 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3457 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3461 switch (ins
->handler_idx
)
3463 case WINED3DSIH_EQ
: op
= "equal"; break;
3464 case WINED3DSIH_IEQ
: op
= "equal"; break;
3465 case WINED3DSIH_GE
: op
= "greaterThanEqual"; break;
3466 case WINED3DSIH_IGE
: op
= "greaterThanEqual"; break;
3467 case WINED3DSIH_UGE
: op
= "greaterThanEqual"; break;
3468 case WINED3DSIH_LT
: op
= "lessThan"; break;
3469 case WINED3DSIH_ILT
: op
= "lessThan"; break;
3470 case WINED3DSIH_ULT
: op
= "lessThan"; break;
3471 case WINED3DSIH_NE
: op
= "notEqual"; break;
3472 case WINED3DSIH_INE
: op
= "notEqual"; break;
3474 op
= "<unhandled operator>";
3475 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3479 shader_addline(buffer
, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3480 mask_size
, op
, src0_param
.param_str
, src1_param
.param_str
);
3484 switch (ins
->handler_idx
)
3486 case WINED3DSIH_EQ
: op
= "=="; break;
3487 case WINED3DSIH_IEQ
: op
= "=="; break;
3488 case WINED3DSIH_GE
: op
= ">="; break;
3489 case WINED3DSIH_IGE
: op
= ">="; break;
3490 case WINED3DSIH_UGE
: op
= ">="; break;
3491 case WINED3DSIH_LT
: op
= "<"; break;
3492 case WINED3DSIH_ILT
: op
= "<"; break;
3493 case WINED3DSIH_ULT
: op
= "<"; break;
3494 case WINED3DSIH_NE
: op
= "!="; break;
3495 case WINED3DSIH_INE
: op
= "!="; break;
3497 op
= "<unhandled operator>";
3498 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3502 shader_addline(buffer
, "%s %s %s ? 0xffffffffu : 0u);\n",
3503 src0_param
.param_str
, op
, src1_param
.param_str
);
3507 static void shader_glsl_unary_op(const struct wined3d_shader_instruction
*ins
)
3509 struct glsl_src_param src_param
;
3513 switch (ins
->handler_idx
)
3515 case WINED3DSIH_INEG
: op
= "-"; break;
3516 case WINED3DSIH_NOT
: op
= "~"; break;
3518 op
= "<unhandled operator>";
3519 ERR("Unhandled opcode %s.\n",
3520 debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3524 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3525 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3526 shader_addline(ins
->ctx
->buffer
, "%s%s);\n", op
, src_param
.param_str
);
3529 static void shader_glsl_imul(const struct wined3d_shader_instruction
*ins
)
3531 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3532 struct glsl_src_param src0_param
;
3533 struct glsl_src_param src1_param
;
3536 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3537 * not, we can emulate it. */
3538 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3539 FIXME("64-bit integer multiplies not implemented.\n");
3541 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3543 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3544 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3545 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3547 shader_addline(ins
->ctx
->buffer
, "%s * %s);\n",
3548 src0_param
.param_str
, src1_param
.param_str
);
3552 static void shader_glsl_udiv(const struct wined3d_shader_instruction
*ins
)
3554 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3555 struct glsl_src_param src0_param
, src1_param
;
3558 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3560 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3564 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3565 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3566 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3567 shader_addline(buffer
, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3568 dst_mask
, src0_param
.param_str
, src1_param
.param_str
);
3570 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3571 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3572 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3573 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3575 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], WINED3D_DATA_FLOAT
);
3576 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
3580 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3581 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3582 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3583 shader_addline(buffer
, "%s / %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3586 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3588 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3589 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3590 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3591 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3595 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3596 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
3598 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3599 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3600 struct glsl_src_param src0_param
;
3603 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3604 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3606 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3607 * shader versions WINED3DSIO_MOVA is used for this. */
3608 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
3609 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
3610 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
3612 /* This is a simple floor() */
3613 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3614 if (mask_size
> 1) {
3615 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
3617 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
3620 else if (ins
->handler_idx
== WINED3DSIH_MOVA
)
3622 const struct wined3d_shader_version
*version
= &ins
->ctx
->shader
->reg_maps
.shader_version
;
3623 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3625 if (shader_glsl_get_version(gl_info
, version
) >= 130 || gl_info
->supported
[EXT_GPU_SHADER4
])
3628 shader_addline(buffer
, "ivec%d(round(%s)));\n", mask_size
, src0_param
.param_str
);
3630 shader_addline(buffer
, "int(round(%s)));\n", src0_param
.param_str
);
3635 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3636 mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
3638 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3639 src0_param
.param_str
, src0_param
.param_str
);
3644 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
3648 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3649 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
3651 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3652 struct glsl_src_param src0_param
;
3653 struct glsl_src_param src1_param
;
3654 DWORD dst_write_mask
, src_write_mask
;
3655 unsigned int dst_size
;
3657 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3658 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3660 /* dp4 works on vec4, dp3 on vec3, etc. */
3661 if (ins
->handler_idx
== WINED3DSIH_DP4
)
3662 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
3663 else if (ins
->handler_idx
== WINED3DSIH_DP3
)
3664 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3666 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
3668 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
3669 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
3672 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
3674 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
3678 /* Note that this instruction has some restrictions. The destination write mask
3679 * can't contain the w component, and the source swizzles have to be .xyzw */
3680 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
3682 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3683 struct glsl_src_param src0_param
;
3684 struct glsl_src_param src1_param
;
3687 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3688 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3689 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3690 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
3691 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
3694 static void shader_glsl_cut(const struct wined3d_shader_instruction
*ins
)
3696 unsigned int stream
= ins
->handler_idx
== WINED3DSIH_CUT
? 0 : ins
->src
[0].reg
.idx
[0].offset
;
3699 shader_addline(ins
->ctx
->buffer
, "EndPrimitive();\n");
3701 FIXME("Unhandled primitive stream %u.\n", stream
);
3704 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3705 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3706 * GLSL uses the value as-is. */
3707 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
3709 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3710 struct glsl_src_param src0_param
;
3711 struct glsl_src_param src1_param
;
3712 DWORD dst_write_mask
;
3713 unsigned int dst_size
;
3715 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3716 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3718 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3719 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3723 shader_addline(buffer
, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3724 dst_size
, src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3728 shader_addline(buffer
, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3729 src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3733 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3734 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
3736 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3737 struct glsl_src_param src_param
;
3738 const char *instruction
;
3742 /* Determine the GLSL function to use based on the opcode */
3743 /* TODO: Possibly make this a table for faster lookups */
3744 switch (ins
->handler_idx
)
3746 case WINED3DSIH_ABS
: instruction
= "abs"; break;
3747 case WINED3DSIH_BFREV
: instruction
= "bitfieldReverse"; break;
3748 case WINED3DSIH_COUNTBITS
: instruction
= "bitCount"; break;
3749 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
3750 case WINED3DSIH_DSX_COARSE
: instruction
= "dFdxCoarse"; break;
3751 case WINED3DSIH_DSX_FINE
: instruction
= "dFdxFine"; break;
3752 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
3753 case WINED3DSIH_DSY_COARSE
: instruction
= "ycorrection.y * dFdyCoarse"; break;
3754 case WINED3DSIH_DSY_FINE
: instruction
= "ycorrection.y * dFdyFine"; break;
3755 case WINED3DSIH_FIRSTBIT_HI
: instruction
= "findMSB"; break;
3756 case WINED3DSIH_FIRSTBIT_LO
: instruction
= "findLSB"; break;
3757 case WINED3DSIH_FIRSTBIT_SHI
: instruction
= "findMSB"; break;
3758 case WINED3DSIH_FRC
: instruction
= "fract"; break;
3759 case WINED3DSIH_IMAX
: instruction
= "max"; break;
3760 case WINED3DSIH_IMIN
: instruction
= "min"; break;
3761 case WINED3DSIH_MAX
: instruction
= "max"; break;
3762 case WINED3DSIH_MIN
: instruction
= "min"; break;
3763 case WINED3DSIH_ROUND_NE
: instruction
= "roundEven"; break;
3764 case WINED3DSIH_ROUND_NI
: instruction
= "floor"; break;
3765 case WINED3DSIH_ROUND_PI
: instruction
= "ceil"; break;
3766 case WINED3DSIH_ROUND_Z
: instruction
= "trunc"; break;
3767 case WINED3DSIH_SQRT
: instruction
= "sqrt"; break;
3768 case WINED3DSIH_UMAX
: instruction
= "max"; break;
3769 case WINED3DSIH_UMIN
: instruction
= "min"; break;
3770 default: instruction
= "";
3771 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3775 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3777 /* In D3D bits are numbered from the most significant bit. */
3778 if (ins
->handler_idx
== WINED3DSIH_FIRSTBIT_HI
|| ins
->handler_idx
== WINED3DSIH_FIRSTBIT_SHI
)
3779 shader_addline(buffer
, "31 - ");
3780 shader_addline(buffer
, "%s(", instruction
);
3784 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3785 shader_addline(buffer
, "%s", src_param
.param_str
);
3786 for (i
= 1; i
< ins
->src_count
; ++i
)
3788 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
3789 shader_addline(buffer
, ", %s", src_param
.param_str
);
3793 shader_addline(buffer
, "));\n");
3796 static void shader_glsl_float16(const struct wined3d_shader_instruction
*ins
)
3798 struct wined3d_shader_dst_param dst
;
3799 struct glsl_src_param src
;
3804 fmt
= ins
->handler_idx
== WINED3DSIH_F16TOF32
3805 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
3808 for (i
= 0; i
< 4; ++i
)
3810 write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
3811 dst
.write_mask
= ins
->dst
[0].write_mask
& write_mask
;
3813 if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
,
3814 &dst
, dst
.reg
.data_type
)))
3817 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src
);
3818 shader_addline(ins
->ctx
->buffer
, fmt
, src
.param_str
);
3822 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction
*ins
)
3824 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3825 struct wined3d_shader_dst_param dst
;
3826 struct glsl_src_param src
[4];
3827 const char *instruction
;
3831 switch (ins
->handler_idx
)
3833 case WINED3DSIH_BFI
: instruction
= "bitfieldInsert"; break;
3834 case WINED3DSIH_UBFE
: instruction
= "bitfieldExtract"; break;
3836 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3841 for (i
= 0; i
< 4; ++i
)
3843 write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
3844 dst
.write_mask
= ins
->dst
[0].write_mask
& write_mask
;
3846 if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
,
3847 &dst
, dst
.reg
.data_type
)))
3850 for (j
= 0; j
< ins
->src_count
; ++j
)
3851 shader_glsl_add_src_param(ins
, &ins
->src
[j
], write_mask
, &src
[j
]);
3852 shader_addline(buffer
, "%s(", instruction
);
3853 for (j
= 0; j
< ins
->src_count
- 2; ++j
)
3854 shader_addline(buffer
, "%s, ", src
[ins
->src_count
- j
- 1].param_str
);
3855 shader_addline(buffer
, "%s & 0x1f, %s & 0x1f));\n", src
[1].param_str
, src
[0].param_str
);
3859 static void shader_glsl_nop(const struct wined3d_shader_instruction
*ins
) {}
3861 static void shader_glsl_nrm(const struct wined3d_shader_instruction
*ins
)
3863 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3864 struct glsl_src_param src_param
;
3865 unsigned int mask_size
;
3869 write_mask
= shader_glsl_get_write_mask(ins
->dst
, dst_mask
);
3870 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3871 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3873 shader_addline(buffer
, "tmp0.x = dot(%s, %s);\n",
3874 src_param
.param_str
, src_param
.param_str
);
3875 shader_glsl_append_dst(buffer
, ins
);
3879 shader_addline(buffer
, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3880 mask_size
, src_param
.param_str
);
3884 shader_addline(buffer
, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3885 src_param
.param_str
);
3889 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction
*ins
)
3891 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
3892 ins
->ctx
->reg_maps
->shader_version
.minor
);
3893 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3894 struct glsl_src_param src0_param
;
3895 const char *prefix
, *suffix
;
3896 unsigned int dst_size
;
3897 DWORD dst_write_mask
;
3899 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3900 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3902 if (shader_version
< WINED3D_SHADER_VERSION(4, 0))
3903 dst_write_mask
= WINED3DSP_WRITEMASK_3
;
3905 shader_glsl_add_src_param(ins
, &ins
->src
[0], dst_write_mask
, &src0_param
);
3907 switch (ins
->handler_idx
)
3909 case WINED3DSIH_EXP
:
3910 case WINED3DSIH_EXPP
:
3915 case WINED3DSIH_LOG
:
3916 case WINED3DSIH_LOGP
:
3917 prefix
= "log2(abs(";
3921 case WINED3DSIH_RCP
:
3926 case WINED3DSIH_RSQ
:
3927 prefix
= "inversesqrt(abs(";
3934 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
3938 if (dst_size
> 1 && shader_version
< WINED3D_SHADER_VERSION(4, 0))
3939 shader_addline(buffer
, "vec%u(%s%s%s));\n", dst_size
, prefix
, src0_param
.param_str
, suffix
);
3941 shader_addline(buffer
, "%s%s%s);\n", prefix
, src0_param
.param_str
, suffix
);
3944 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3945 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3946 * dst.x = 2^(floor(src))
3947 * dst.y = src - floor(src)
3948 * dst.z = 2^src (partial precision is allowed, but optional)
3950 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3951 * dst = 2^src; (partial precision is allowed, but optional)
3953 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
3955 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
3957 struct glsl_src_param src_param
;
3960 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
3962 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
3963 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
3964 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
3965 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
3967 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3968 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3969 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
3973 shader_glsl_scalar_op(ins
);
3976 static void shader_glsl_cast(const struct wined3d_shader_instruction
*ins
,
3977 const char *vector_constructor
, const char *scalar_constructor
)
3979 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3980 struct glsl_src_param src_param
;
3981 unsigned int mask_size
;
3984 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3985 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3986 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3989 shader_addline(buffer
, "%s%u(%s));\n", vector_constructor
, mask_size
, src_param
.param_str
);
3991 shader_addline(buffer
, "%s(%s));\n", scalar_constructor
, src_param
.param_str
);
3994 static void shader_glsl_to_int(const struct wined3d_shader_instruction
*ins
)
3996 shader_glsl_cast(ins
, "ivec", "int");
3999 static void shader_glsl_to_uint(const struct wined3d_shader_instruction
*ins
)
4001 shader_glsl_cast(ins
, "uvec", "uint");
4004 static void shader_glsl_to_float(const struct wined3d_shader_instruction
*ins
)
4006 shader_glsl_cast(ins
, "vec", "float");
4009 /** Process signed comparison opcodes in GLSL. */
4010 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
4012 struct glsl_src_param src0_param
;
4013 struct glsl_src_param src1_param
;
4015 unsigned int mask_size
;
4017 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4018 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4019 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4020 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4022 if (mask_size
> 1) {
4023 const char *compare
;
4025 switch(ins
->handler_idx
)
4027 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
4028 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
4029 default: compare
= "";
4030 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
4033 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
4034 src0_param
.param_str
, src1_param
.param_str
);
4036 switch(ins
->handler_idx
)
4038 case WINED3DSIH_SLT
:
4039 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4040 * to return 0.0 but step returns 1.0 because step is not < x
4041 * An alternative is a bvec compare padded with an unused second component.
4042 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4043 * issue. Playing with not() is not possible either because not() does not accept
4046 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
4047 src0_param
.param_str
, src1_param
.param_str
);
4049 case WINED3DSIH_SGE
:
4050 /* Here we can use the step() function and safe a conditional */
4051 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
4054 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
4060 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction
*ins
)
4062 const char *condition_prefix
, *condition_suffix
;
4063 struct wined3d_shader_dst_param dst
;
4064 struct glsl_src_param src0_param
;
4065 struct glsl_src_param src1_param
;
4066 struct glsl_src_param src2_param
;
4067 BOOL temp_destination
= FALSE
;
4068 DWORD cmp_channel
= 0;
4073 switch (ins
->handler_idx
)
4075 case WINED3DSIH_CMP
:
4076 condition_prefix
= "";
4077 condition_suffix
= " >= 0.0";
4080 case WINED3DSIH_CND
:
4081 condition_prefix
= "";
4082 condition_suffix
= " > 0.5";
4085 case WINED3DSIH_MOVC
:
4086 condition_prefix
= "bool(";
4087 condition_suffix
= ")";
4091 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
4092 condition_prefix
= "<unhandled prefix>";
4093 condition_suffix
= "<unhandled suffix>";
4097 if (shader_is_scalar(&ins
->dst
[0].reg
) || shader_is_scalar(&ins
->src
[0].reg
))
4099 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4100 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4101 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4102 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4104 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
4105 condition_prefix
, src0_param
.param_str
, condition_suffix
,
4106 src1_param
.param_str
, src2_param
.param_str
);
4112 /* Splitting the instruction up in multiple lines imposes a problem:
4113 * The first lines may overwrite source parameters of the following lines.
4114 * Deal with that by using a temporary destination register if needed. */
4115 if ((ins
->src
[0].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
4116 && ins
->src
[0].reg
.type
== dst
.reg
.type
)
4117 || (ins
->src
[1].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
4118 && ins
->src
[1].reg
.type
== dst
.reg
.type
)
4119 || (ins
->src
[2].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
4120 && ins
->src
[2].reg
.type
== dst
.reg
.type
))
4121 temp_destination
= TRUE
;
4123 /* Cycle through all source0 channels. */
4124 for (i
= 0; i
< 4; ++i
)
4127 /* Find the destination channels which use the current source0 channel. */
4128 for (j
= 0; j
< 4; ++j
)
4130 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
4132 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
4133 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
4136 dst
.write_mask
= ins
->dst
[0].write_mask
& write_mask
;
4138 if (temp_destination
)
4140 if (!(write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
)))
4142 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
4144 else if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
, dst
.reg
.data_type
)))
4147 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
4148 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4149 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4151 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
4152 condition_prefix
, src0_param
.param_str
, condition_suffix
,
4153 src1_param
.param_str
, src2_param
.param_str
);
4156 if (temp_destination
)
4158 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
4159 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4160 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
4164 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4165 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4166 * the compare is done per component of src0. */
4167 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
4169 struct glsl_src_param src0_param
;
4170 struct glsl_src_param src1_param
;
4171 struct glsl_src_param src2_param
;
4173 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4174 ins
->ctx
->reg_maps
->shader_version
.minor
);
4176 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
4178 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4179 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4180 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4181 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4183 if (ins
->coissue
&& ins
->dst
->write_mask
!= WINED3DSP_WRITEMASK_3
)
4184 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
4186 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
4187 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4191 shader_glsl_conditional_move(ins
);
4194 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4195 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
4197 struct glsl_src_param src0_param
;
4198 struct glsl_src_param src1_param
;
4199 struct glsl_src_param src2_param
;
4202 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4203 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4204 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4205 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4206 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
4207 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4210 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4211 Vertex shaders to GLSL codes */
4212 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
4215 int nComponents
= 0;
4216 struct wined3d_shader_dst_param tmp_dst
= {{0}};
4217 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
4218 struct wined3d_shader_instruction tmp_ins
;
4220 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
4222 /* Set constants for the temporary argument */
4223 tmp_ins
.ctx
= ins
->ctx
;
4224 tmp_ins
.dst_count
= 1;
4225 tmp_ins
.dst
= &tmp_dst
;
4226 tmp_ins
.src_count
= 2;
4227 tmp_ins
.src
= tmp_src
;
4229 switch(ins
->handler_idx
)
4231 case WINED3DSIH_M4x4
:
4233 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
4235 case WINED3DSIH_M4x3
:
4237 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
4239 case WINED3DSIH_M3x4
:
4241 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4243 case WINED3DSIH_M3x3
:
4245 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4247 case WINED3DSIH_M3x2
:
4249 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4255 tmp_dst
= ins
->dst
[0];
4256 tmp_src
[0] = ins
->src
[0];
4257 tmp_src
[1] = ins
->src
[1];
4258 for (i
= 0; i
< nComponents
; ++i
)
4260 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
4261 shader_glsl_dot(&tmp_ins
);
4262 ++tmp_src
[1].reg
.idx
[0].offset
;
4267 The LRP instruction performs a component-wise linear interpolation
4268 between the second and third operands using the first operand as the
4269 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4270 This is equivalent to mix(src2, src1, src0);
4272 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
4274 struct glsl_src_param src0_param
;
4275 struct glsl_src_param src1_param
;
4276 struct glsl_src_param src2_param
;
4279 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4281 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4282 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4283 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4285 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
4286 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
4289 /** Process the WINED3DSIO_LIT instruction in GLSL:
4290 * dst.x = dst.w = 1.0
4291 * dst.y = (src0.x > 0) ? src0.x
4292 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4293 * where src.w is clamped at +- 128
4295 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
4297 struct glsl_src_param src0_param
;
4298 struct glsl_src_param src1_param
;
4299 struct glsl_src_param src3_param
;
4302 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4303 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4305 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4306 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
4307 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
4309 /* The sdk specifies the instruction like this
4311 * if(src.x > 0.0) dst.y = src.x
4313 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4316 * (where power = src.w clamped between -128 and 128)
4318 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4319 * dst.x = 1.0 ... No further explanation needed
4320 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4321 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4322 * dst.w = 1.0. ... Nothing fancy.
4324 * So we still have one conditional in there. So do this:
4325 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4327 * 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),
4328 * 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.
4329 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4331 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4332 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4333 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4335 shader_addline(ins
->ctx
->buffer
,
4336 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4337 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4338 src0_param
.param_str
, src3_param
.param_str
, src1_param
.param_str
,
4339 src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
4342 /** Process the WINED3DSIO_DST instruction in GLSL:
4344 * dst.y = src0.x * src0.y
4348 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
4350 struct glsl_src_param src0y_param
;
4351 struct glsl_src_param src0z_param
;
4352 struct glsl_src_param src1y_param
;
4353 struct glsl_src_param src1w_param
;
4356 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4357 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4359 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
4360 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
4361 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
4362 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
4364 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4365 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
4368 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4369 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4370 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4372 * dst.x = cos(src0.?)
4373 * dst.y = sin(src0.?)
4377 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
4379 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4380 struct glsl_src_param src0_param
;
4383 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
4385 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4387 write_mask
= shader_glsl_append_dst(buffer
, ins
);
4390 case WINED3DSP_WRITEMASK_0
:
4391 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4394 case WINED3DSP_WRITEMASK_1
:
4395 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
4398 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
4399 shader_addline(buffer
, "vec2(cos(%s), sin(%s)));\n",
4400 src0_param
.param_str
, src0_param
.param_str
);
4404 ERR("Write mask should be .x, .y or .xy\n");
4411 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
4414 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
4418 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4419 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4420 shader_addline(buffer
, "tmp0%s = sin(%s);\n", dst_mask
, src0_param
.param_str
);
4422 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
4423 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4424 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4426 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
4427 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
4431 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
4432 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4433 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
4436 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
4438 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
4439 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4440 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4444 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4445 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4446 * generate invalid code
4448 static void shader_glsl_sgn(const struct wined3d_shader_instruction
*ins
)
4450 struct glsl_src_param src0_param
;
4453 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4454 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4456 shader_addline(ins
->ctx
->buffer
, "sign(%s));\n", src0_param
.param_str
);
4459 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4460 * Start a for() loop where src1.y is the initial value of aL,
4461 * increment aL by src1.z for a total of src1.x iterations.
4462 * Need to use a temporary variable for this operation.
4464 /* FIXME: I don't think nested loops will work correctly this way. */
4465 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
4467 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4468 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4469 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
4470 const struct wined3d_shader_lconst
*constant
;
4471 struct glsl_src_param src1_param
;
4472 const DWORD
*control_values
= NULL
;
4474 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
4476 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
4478 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4479 * class hardware doesn't support real varying indexing, but Microsoft
4480 * designed this feature for Shader model 2.x+. If the loop control is
4481 * known at compile time, the GLSL compiler can unroll the loop, and
4482 * replace indirect addressing with direct addressing. */
4483 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
4485 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
4487 if (constant
->idx
== ins
->src
[1].reg
.idx
[0].offset
)
4489 control_values
= constant
->value
;
4497 struct wined3d_shader_loop_control loop_control
;
4498 loop_control
.count
= control_values
[0];
4499 loop_control
.start
= control_values
[1];
4500 loop_control
.step
= (int)control_values
[2];
4502 if (loop_control
.step
> 0)
4504 shader_addline(buffer
, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4505 state
->current_loop_depth
, loop_control
.start
,
4506 state
->current_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
4507 state
->current_loop_depth
, loop_control
.step
);
4509 else if (loop_control
.step
< 0)
4511 shader_addline(buffer
, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4512 state
->current_loop_depth
, loop_control
.start
,
4513 state
->current_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
4514 state
->current_loop_depth
, loop_control
.step
);
4518 shader_addline(buffer
, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4519 state
->current_loop_depth
, loop_control
.start
, state
->current_loop_depth
,
4520 state
->current_loop_depth
, loop_control
.count
,
4521 state
->current_loop_depth
);
4526 shader_addline(buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4527 state
->current_loop_depth
, state
->current_loop_reg
,
4528 src1_param
.reg_name
, state
->current_loop_depth
, src1_param
.reg_name
,
4529 state
->current_loop_depth
, state
->current_loop_reg
, src1_param
.reg_name
);
4532 ++state
->current_loop_reg
;
4536 shader_addline(buffer
, "for (;;)\n{\n");
4539 ++state
->current_loop_depth
;
4542 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
4544 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4546 shader_addline(ins
->ctx
->buffer
, "}\n");
4548 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
4550 --state
->current_loop_depth
;
4551 --state
->current_loop_reg
;
4554 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
4556 --state
->current_loop_depth
;
4560 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
4562 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4563 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
4564 const struct wined3d_shader_lconst
*constant
;
4565 struct glsl_src_param src0_param
;
4566 const DWORD
*control_values
= NULL
;
4568 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4569 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
4571 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
4573 if (constant
->idx
== ins
->src
[0].reg
.idx
[0].offset
)
4575 control_values
= constant
->value
;
4583 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4584 state
->current_loop_depth
, state
->current_loop_depth
,
4585 control_values
[0], state
->current_loop_depth
);
4589 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4590 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4591 state
->current_loop_depth
, state
->current_loop_depth
,
4592 src0_param
.param_str
, state
->current_loop_depth
);
4595 ++state
->current_loop_depth
;
4598 static void shader_glsl_switch(const struct wined3d_shader_instruction
*ins
)
4600 struct glsl_src_param src0_param
;
4602 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4603 shader_addline(ins
->ctx
->buffer
, "switch (%s)\n{\n", src0_param
.param_str
);
4606 static void shader_glsl_case(const struct wined3d_shader_instruction
*ins
)
4608 struct glsl_src_param src0_param
;
4610 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4611 shader_addline(ins
->ctx
->buffer
, "case %s:\n", src0_param
.param_str
);
4614 static void shader_glsl_default(const struct wined3d_shader_instruction
*ins
)
4616 shader_addline(ins
->ctx
->buffer
, "default:\n");
4619 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
4621 const char *condition
= (ins
->flags
== WINED3D_SHADER_CONDITIONAL_OP_NZ
) ? "bool" : "!bool";
4622 struct glsl_src_param src0_param
;
4624 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4625 shader_addline(ins
->ctx
->buffer
, "if (%s(%s)) {\n", condition
, src0_param
.param_str
);
4628 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
4630 struct glsl_src_param src0_param
;
4631 struct glsl_src_param src1_param
;
4633 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4634 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4636 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
4637 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
4640 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
4642 shader_addline(ins
->ctx
->buffer
, "} else {\n");
4645 static void shader_glsl_emit(const struct wined3d_shader_instruction
*ins
)
4647 unsigned int stream
= ins
->handler_idx
== WINED3DSIH_EMIT
? 0 : ins
->src
[0].reg
.idx
[0].offset
;
4649 shader_addline(ins
->ctx
->buffer
, "setup_gs_output(gs_out);\n");
4650 if (!ins
->ctx
->gl_info
->supported
[ARB_CLIP_CONTROL
])
4651 shader_glsl_fixup_position(ins
->ctx
->buffer
);
4654 shader_addline(ins
->ctx
->buffer
, "EmitVertex();\n");
4656 FIXME("Unhandled primitive stream %u.\n", stream
);
4659 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
4661 shader_addline(ins
->ctx
->buffer
, "break;\n");
4664 /* FIXME: According to MSDN the compare is done per component. */
4665 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
4667 struct glsl_src_param src0_param
;
4668 struct glsl_src_param src1_param
;
4670 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4671 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4673 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
4674 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
4677 static void shader_glsl_breakp(const struct wined3d_shader_instruction
*ins
)
4679 const char *condition
= (ins
->flags
== WINED3D_SHADER_CONDITIONAL_OP_NZ
) ? "bool" : "!bool";
4680 struct glsl_src_param src_param
;
4682 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
4683 shader_addline(ins
->ctx
->buffer
, "if (%s(%s)) break;\n", condition
, src_param
.param_str
);
4686 static void shader_glsl_continue(const struct wined3d_shader_instruction
*ins
)
4688 shader_addline(ins
->ctx
->buffer
, "continue;\n");
4691 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
4693 shader_addline(ins
->ctx
->buffer
, "}\n");
4694 shader_addline(ins
->ctx
->buffer
, "void subroutine%u()\n{\n", ins
->src
[0].reg
.idx
[0].offset
);
4696 /* Subroutines appear at the end of the shader. */
4697 ins
->ctx
->state
->in_subroutine
= TRUE
;
4700 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
4702 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
[0].offset
);
4705 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
4707 struct glsl_src_param src1_param
;
4709 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4710 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n",
4711 src1_param
.param_str
, ins
->src
[0].reg
.idx
[0].offset
);
4714 static void shader_glsl_ret(const struct wined3d_shader_instruction
*ins
)
4716 const struct wined3d_shader_version
*version
= &ins
->ctx
->shader
->reg_maps
.shader_version
;
4718 if (version
->major
>= 4 && !ins
->ctx
->state
->in_subroutine
)
4720 shader_glsl_generate_shader_epilogue(ins
->ctx
);
4721 shader_addline(ins
->ctx
->buffer
, "return;\n");
4725 static void shader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
4727 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4728 ins
->ctx
->reg_maps
->shader_version
.minor
);
4729 struct glsl_sample_function sample_function
;
4730 DWORD sample_flags
= 0;
4732 DWORD mask
= 0, swizzle
;
4733 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4735 /* 1.0-1.4: Use destination register as sampler source.
4736 * 2.0+: Use provided sampler source. */
4737 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4738 resource_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4740 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4742 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4744 DWORD flags
= (priv
->cur_ps_args
->tex_transform
>> resource_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
4745 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
4746 enum wined3d_shader_resource_type resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
4748 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4749 if (flags
& WINED3D_PSARGS_PROJECTED
&& resource_type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4751 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4752 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
4754 case WINED3D_TTFF_COUNT1
:
4755 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4757 case WINED3D_TTFF_COUNT2
:
4758 mask
= WINED3DSP_WRITEMASK_1
;
4760 case WINED3D_TTFF_COUNT3
:
4761 mask
= WINED3DSP_WRITEMASK_2
;
4763 case WINED3D_TTFF_COUNT4
:
4764 case WINED3D_TTFF_DISABLE
:
4765 mask
= WINED3DSP_WRITEMASK_3
;
4770 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4772 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
4774 if (src_mod
== WINED3DSPSM_DZ
) {
4775 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4776 mask
= WINED3DSP_WRITEMASK_2
;
4777 } else if (src_mod
== WINED3DSPSM_DW
) {
4778 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4779 mask
= WINED3DSP_WRITEMASK_3
;
4784 if ((ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
4785 && ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4787 /* ps 2.0 texldp instruction always divides by the fourth component. */
4788 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4789 mask
= WINED3DSP_WRITEMASK_3
;
4793 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, resource_idx
, sample_flags
, &sample_function
);
4794 mask
|= sample_function
.coord_mask
;
4795 sample_function
.coord_mask
= mask
;
4797 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
4798 else swizzle
= ins
->src
[1].swizzle
;
4800 /* 1.0-1.3: Use destination register as coordinate source.
4801 1.4+: Use provided coordinate source register. */
4802 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4805 shader_glsl_write_mask_to_str(mask
, coord_mask
);
4806 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
, NULL
,
4807 "T%u%s", resource_idx
, coord_mask
);
4811 struct glsl_src_param coord_param
;
4812 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
4813 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
4815 struct glsl_src_param bias
;
4816 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
4817 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
4818 NULL
, "%s", coord_param
.param_str
);
4820 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
, NULL
,
4821 "%s", coord_param
.param_str
);
4824 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4827 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
4829 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4830 struct glsl_src_param coord_param
, dx_param
, dy_param
;
4831 struct glsl_sample_function sample_function
;
4833 DWORD swizzle
= ins
->src
[1].swizzle
;
4835 if (!shader_glsl_has_core_grad(gl_info
, &ins
->ctx
->shader
->reg_maps
.shader_version
)
4836 && !gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
4838 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4839 shader_glsl_tex(ins
);
4843 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4845 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, WINED3D_GLSL_SAMPLE_GRAD
, &sample_function
);
4846 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4847 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.deriv_mask
, &dx_param
);
4848 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.deriv_mask
, &dy_param
);
4850 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
,
4851 NULL
, NULL
, "%s", coord_param
.param_str
);
4852 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4855 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
4857 const struct wined3d_shader_version
*shader_version
= &ins
->ctx
->reg_maps
->shader_version
;
4858 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4859 struct glsl_src_param coord_param
, lod_param
;
4860 struct glsl_sample_function sample_function
;
4861 DWORD swizzle
= ins
->src
[1].swizzle
;
4864 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4866 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, WINED3D_GLSL_SAMPLE_LOD
, &sample_function
);
4867 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4869 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
4871 if (shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& !shader_glsl_has_core_grad(gl_info
, shader_version
)
4872 && !gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
4874 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4875 * However, the NVIDIA drivers allow them in fragment shaders as well,
4876 * even without the appropriate extension. */
4877 WARN("Using %s in fragment shader.\n", sample_function
.name
->buffer
);
4879 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
, NULL
,
4880 "%s", coord_param
.param_str
);
4881 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4884 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map
*sampler_map
,
4885 unsigned int resource_idx
, unsigned int sampler_idx
)
4887 struct wined3d_shader_sampler_map_entry
*entries
= sampler_map
->entries
;
4890 for (i
= 0; i
< sampler_map
->count
; ++i
)
4892 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
4893 return entries
[i
].bind_idx
;
4896 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx
, sampler_idx
);
4901 static void shader_glsl_atomic(const struct wined3d_shader_instruction
*ins
)
4903 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
4904 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
4905 struct glsl_src_param image_coord_param
, image_data_param
;
4906 enum wined3d_shader_resource_type resource_type
;
4907 enum wined3d_data_type data_type
;
4908 unsigned int uav_idx
;
4912 switch (ins
->handler_idx
)
4914 case WINED3DSIH_ATOMIC_IADD
: op
= "imageAtomicAdd"; break;
4916 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
4920 uav_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4921 resource_type
= reg_maps
->uav_resource_info
[uav_idx
].type
;
4922 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
4924 ERR("Unexpected resource type %#x.\n", resource_type
);
4927 data_type
= reg_maps
->uav_resource_info
[uav_idx
].data_type
;
4928 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
4930 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &image_coord_param
);
4931 shader_glsl_add_src_param_ext(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &image_data_param
, data_type
);
4932 shader_addline(ins
->ctx
->buffer
, "%s(%s_image%u, %s, %s);\n",
4933 op
, shader_glsl_get_prefix(version
->type
), uav_idx
,
4934 image_coord_param
.param_str
, image_data_param
.param_str
);
4937 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction
*ins
)
4939 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
4940 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
4941 enum wined3d_shader_resource_type resource_type
;
4942 struct glsl_src_param image_coord_param
;
4943 enum wined3d_data_type data_type
;
4944 DWORD coord_mask
, write_mask
;
4945 unsigned int uav_idx
;
4946 char dst_swizzle
[6];
4948 uav_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4949 if (uav_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
4951 ERR("Invalid UAV index %u.\n", uav_idx
);
4954 resource_type
= reg_maps
->uav_resource_info
[uav_idx
].type
;
4955 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
4957 ERR("Unexpected resource type %#x.\n", resource_type
);
4958 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
4960 data_type
= reg_maps
->uav_resource_info
[uav_idx
].data_type
;
4961 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
4963 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], data_type
);
4964 shader_glsl_get_swizzle(&ins
->src
[1], FALSE
, write_mask
, dst_swizzle
);
4966 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &image_coord_param
);
4967 shader_addline(ins
->ctx
->buffer
, "imageLoad(%s_image%u, %s)%s);\n",
4968 shader_glsl_get_prefix(version
->type
), uav_idx
, image_coord_param
.param_str
, dst_swizzle
);
4971 static void shader_glsl_store_uav(const struct wined3d_shader_instruction
*ins
)
4973 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
4974 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
4975 struct glsl_src_param image_coord_param
, image_data_param
;
4976 enum wined3d_shader_resource_type resource_type
;
4977 enum wined3d_data_type data_type
;
4978 unsigned int uav_idx
;
4981 uav_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4982 if (uav_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
4984 ERR("Invalid UAV index %u.\n", uav_idx
);
4987 resource_type
= reg_maps
->uav_resource_info
[uav_idx
].type
;
4988 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
4990 ERR("Unexpected resource type %#x.\n", resource_type
);
4993 data_type
= reg_maps
->uav_resource_info
[uav_idx
].data_type
;
4994 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
4996 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &image_coord_param
);
4997 shader_glsl_add_src_param_ext(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &image_data_param
, data_type
);
4998 shader_addline(ins
->ctx
->buffer
, "imageStore(%s_image%u, %s, %s);\n",
4999 shader_glsl_get_prefix(version
->type
), uav_idx
,
5000 image_coord_param
.param_str
, image_data_param
.param_str
);
5003 static void shader_glsl_resinfo(const struct wined3d_shader_instruction
*ins
)
5005 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
5006 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
5007 enum wined3d_shader_resource_type resource_type
;
5008 enum wined3d_shader_register_type reg_type
;
5009 unsigned int resource_idx
, bind_idx
, i
;
5010 enum wined3d_data_type dst_data_type
;
5011 struct glsl_src_param lod_param
;
5012 char dst_swizzle
[6];
5015 dst_data_type
= ins
->dst
[0].reg
.data_type
;
5016 if (ins
->flags
== WINED3DSI_RESINFO_UINT
)
5017 dst_data_type
= WINED3D_DATA_UINT
;
5018 else if (ins
->flags
)
5019 FIXME("Unhandled flags %#x.\n", ins
->flags
);
5021 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], dst_data_type
);
5022 shader_glsl_get_swizzle(&ins
->src
[1], FALSE
, write_mask
, dst_swizzle
);
5024 reg_type
= ins
->src
[1].reg
.type
;
5025 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5026 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &lod_param
);
5027 if (reg_type
== WINED3DSPR_RESOURCE
)
5029 resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
5030 bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
5031 resource_idx
, WINED3D_SAMPLER_DEFAULT
);
5035 resource_type
= ins
->ctx
->reg_maps
->uav_resource_info
[resource_idx
].type
;
5036 bind_idx
= resource_idx
;
5039 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
5041 ERR("Unexpected resource type %#x.\n", resource_type
);
5042 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
5045 if (dst_data_type
== WINED3D_DATA_UINT
)
5046 shader_addline(ins
->ctx
->buffer
, "uvec4(");
5048 shader_addline(ins
->ctx
->buffer
, "vec4(");
5050 if (reg_type
== WINED3DSPR_RESOURCE
)
5052 shader_addline(ins
->ctx
->buffer
, "textureSize(%s_sampler%u, %s), ",
5053 shader_glsl_get_prefix(version
->type
), bind_idx
, lod_param
.param_str
);
5055 for (i
= 0; i
< 3 - resource_type_info
[resource_type
].resinfo_size
; ++i
)
5056 shader_addline(ins
->ctx
->buffer
, "0, ");
5058 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
5060 shader_addline(ins
->ctx
->buffer
, "textureQueryLevels(%s_sampler%u)",
5061 shader_glsl_get_prefix(version
->type
), bind_idx
);
5065 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5066 shader_addline(ins
->ctx
->buffer
, "1");
5071 shader_addline(ins
->ctx
->buffer
, "imageSize(%s_image%u), ",
5072 shader_glsl_get_prefix(version
->type
), bind_idx
);
5074 for (i
= 0; i
< 3 - resource_type_info
[resource_type
].resinfo_size
; ++i
)
5075 shader_addline(ins
->ctx
->buffer
, "0, ");
5077 /* For UAVs the returned miplevel count is always 1. */
5078 shader_addline(ins
->ctx
->buffer
, "1");
5081 shader_addline(ins
->ctx
->buffer
, ")%s);\n", dst_swizzle
);
5084 /* FIXME: The current implementation does not handle multisample textures correctly. */
5085 static void shader_glsl_ld(const struct wined3d_shader_instruction
*ins
)
5087 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5088 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
5089 struct glsl_src_param coord_param
, lod_param
;
5090 struct glsl_sample_function sample_function
;
5091 DWORD flags
= WINED3D_GLSL_SAMPLE_LOAD
;
5094 if (wined3d_shader_instruction_has_texel_offset(ins
))
5095 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
5097 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5098 sampler_idx
= WINED3D_SAMPLER_DEFAULT
;
5100 if (resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
5102 ERR("Invalid resource index %u.\n", resource_idx
);
5105 has_lod_param
= reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_BUFFER
;
5107 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
5108 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
5109 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
5110 sampler_bind_idx
= shader_glsl_find_sampler(®_maps
->sampler_map
, resource_idx
, sampler_idx
);
5111 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, ins
->src
[1].swizzle
,
5112 NULL
, NULL
, has_lod_param
? lod_param
.param_str
: NULL
, &ins
->texel_offset
,
5113 "%s", coord_param
.param_str
);
5114 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5117 static void shader_glsl_sample(const struct wined3d_shader_instruction
*ins
)
5119 const char *lod_param_str
= NULL
, *dx_param_str
= NULL
, *dy_param_str
= NULL
;
5120 struct glsl_src_param coord_param
, lod_param
, dx_param
, dy_param
;
5121 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
5122 struct glsl_sample_function sample_function
;
5125 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_GRAD
)
5126 flags
|= WINED3D_GLSL_SAMPLE_GRAD
;
5127 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_LOD
)
5128 flags
|= WINED3D_GLSL_SAMPLE_LOD
;
5129 if (wined3d_shader_instruction_has_texel_offset(ins
))
5130 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
5132 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5133 sampler_idx
= ins
->src
[2].reg
.idx
[0].offset
;
5135 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
5136 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
5138 switch (ins
->handler_idx
)
5140 case WINED3DSIH_SAMPLE
:
5142 case WINED3DSIH_SAMPLE_B
:
5143 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &lod_param
);
5144 lod_param_str
= lod_param
.param_str
;
5146 case WINED3DSIH_SAMPLE_GRAD
:
5147 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.deriv_mask
, &dx_param
);
5148 shader_glsl_add_src_param(ins
, &ins
->src
[4], sample_function
.deriv_mask
, &dy_param
);
5149 dx_param_str
= dx_param
.param_str
;
5150 dy_param_str
= dy_param
.param_str
;
5152 case WINED3DSIH_SAMPLE_LOD
:
5153 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &lod_param
);
5154 lod_param_str
= lod_param
.param_str
;
5157 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
5161 sampler_bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
, resource_idx
, sampler_idx
);
5162 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, ins
->src
[1].swizzle
,
5163 dx_param_str
, dy_param_str
, lod_param_str
, &ins
->texel_offset
, "%s", coord_param
.param_str
);
5164 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5167 static void shader_glsl_sample_c(const struct wined3d_shader_instruction
*ins
)
5169 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
5170 struct glsl_src_param coord_param
, compare_param
;
5171 struct glsl_sample_function sample_function
;
5172 const char *lod_param
= NULL
;
5176 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_C_LZ
)
5179 flags
|= WINED3D_GLSL_SAMPLE_LOD
;
5182 if (wined3d_shader_instruction_has_texel_offset(ins
))
5183 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
5185 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5186 sampler_idx
= ins
->src
[2].reg
.idx
[0].offset
;
5188 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
5189 coord_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
5190 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
>> 1, &coord_param
);
5191 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &compare_param
);
5192 sampler_bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
, resource_idx
, sampler_idx
);
5193 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
,
5194 NULL
, NULL
, lod_param
, &ins
->texel_offset
, "vec%u(%s, %s)",
5195 coord_size
, coord_param
.param_str
, compare_param
.param_str
);
5196 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5199 static void shader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
5201 /* FIXME: Make this work for more than just 2D textures */
5202 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5203 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5205 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
5209 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
5210 shader_addline(buffer
, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5211 ins
->dst
[0].reg
.idx
[0].offset
, dst_mask
);
5215 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
5216 DWORD reg
= ins
->src
[0].reg
.idx
[0].offset
;
5217 char dst_swizzle
[6];
5219 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
5221 if (src_mod
== WINED3DSPSM_DZ
|| src_mod
== WINED3DSPSM_DW
)
5223 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
5224 struct glsl_src_param div_param
;
5225 DWORD src_writemask
= src_mod
== WINED3DSPSM_DZ
? WINED3DSP_WRITEMASK_2
: WINED3DSP_WRITEMASK_3
;
5227 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_writemask
, &div_param
);
5230 shader_addline(buffer
, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
5232 shader_addline(buffer
, "ffp_texcoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
5236 shader_addline(buffer
, "ffp_texcoord[%u]%s);\n", reg
, dst_swizzle
);
5241 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5242 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5243 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5244 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
5246 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5247 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5248 struct glsl_sample_function sample_function
;
5249 struct glsl_src_param src0_param
;
5252 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5254 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5255 * scalar, and projected sampling would require 4.
5257 * It is a dependent read - not valid with conditional NP2 textures
5259 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5260 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
5265 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
5266 NULL
, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
5270 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
5271 NULL
, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
5275 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
5276 NULL
, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
5280 FIXME("Unexpected mask size %u\n", mask_size
);
5283 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5286 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5287 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5288 static void shader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
5290 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5291 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
5292 struct glsl_src_param src0_param
;
5294 unsigned int mask_size
;
5296 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5297 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
5298 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5300 if (mask_size
> 1) {
5301 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
5303 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
5307 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5308 * Calculate the depth as dst.x / dst.y */
5309 static void shader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
5311 struct glsl_dst_param dst_param
;
5313 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
5315 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5316 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5317 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5318 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5321 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5322 dst_param
.reg_name
, dst_param
.reg_name
);
5325 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5326 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5327 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5328 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5330 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
5332 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5333 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
5334 struct glsl_src_param src0_param
;
5336 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5338 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
5339 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5342 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5343 * Calculate the 1st of a 2-row matrix multiplication. */
5344 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
5346 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5347 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5348 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5349 struct glsl_src_param src0_param
;
5351 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5352 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5355 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5356 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5357 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
5359 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5360 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5361 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5362 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5363 struct glsl_src_param src0_param
;
5365 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5366 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx
->current_row
, reg
, src0_param
.param_str
);
5367 tex_mx
->texcoord_w
[tex_mx
->current_row
++] = reg
;
5370 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
5372 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5373 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5374 struct glsl_sample_function sample_function
;
5375 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5376 struct glsl_src_param src0_param
;
5378 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5379 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5381 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5383 /* Sample the texture using the calculated coordinates */
5384 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
, "tmp0.xy");
5385 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5388 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5389 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5390 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
5392 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5393 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5394 struct glsl_sample_function sample_function
;
5395 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5396 struct glsl_src_param src0_param
;
5398 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5399 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5401 /* Dependent read, not valid with conditional NP2 */
5402 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5404 /* Sample the texture using the calculated coordinates */
5405 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
, "tmp0.xyz");
5406 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5408 tex_mx
->current_row
= 0;
5411 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5412 * Perform the 3rd row of a 3x3 matrix multiply */
5413 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
5415 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5416 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5417 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5418 struct glsl_src_param src0_param
;
5421 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5423 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5424 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
5425 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
5427 tex_mx
->current_row
= 0;
5430 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5431 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5432 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
5434 struct glsl_src_param src0_param
;
5435 struct glsl_src_param src1_param
;
5436 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5437 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5438 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5439 struct glsl_sample_function sample_function
;
5440 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5443 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5444 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
5446 /* Perform the last matrix multiply operation */
5447 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5448 /* Reflection calculation */
5449 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
5451 /* Dependent read, not valid with conditional NP2 */
5452 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5453 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
5455 /* Sample the texture */
5456 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
5457 NULL
, NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
5458 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5460 tex_mx
->current_row
= 0;
5463 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5464 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5465 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
5467 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5468 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5469 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5470 struct glsl_sample_function sample_function
;
5471 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5472 struct glsl_src_param src0_param
;
5475 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5477 /* Perform the last matrix multiply operation */
5478 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
5480 /* Construct the eye-ray vector from w coordinates */
5481 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5482 tex_mx
->texcoord_w
[0], tex_mx
->texcoord_w
[1], reg
);
5483 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5485 /* Dependent read, not valid with conditional NP2 */
5486 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5487 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
5489 /* Sample the texture using the calculated coordinates */
5490 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
5491 NULL
, NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
5492 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5494 tex_mx
->current_row
= 0;
5497 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5498 * Apply a fake bump map transform.
5499 * texbem is pshader <= 1.3 only, this saves a few version checks
5501 static void shader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
5503 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5504 struct glsl_sample_function sample_function
;
5505 struct glsl_src_param coord_param
;
5511 sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5512 flags
= (priv
->cur_ps_args
->tex_transform
>> sampler_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
5513 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
5515 /* Dependent read, not valid with conditional NP2 */
5516 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5517 mask
= sample_function
.coord_mask
;
5519 shader_glsl_write_mask_to_str(mask
, coord_mask
);
5521 /* With projected textures, texbem only divides the static texture coord,
5522 * not the displacement, so we can't let GL handle this. */
5523 if (flags
& WINED3D_PSARGS_PROJECTED
)
5526 char coord_div_mask
[3];
5527 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
5529 case WINED3D_TTFF_COUNT1
:
5530 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5532 case WINED3D_TTFF_COUNT2
:
5533 div_mask
= WINED3DSP_WRITEMASK_1
;
5535 case WINED3D_TTFF_COUNT3
:
5536 div_mask
= WINED3DSP_WRITEMASK_2
;
5538 case WINED3D_TTFF_COUNT4
:
5539 case WINED3D_TTFF_DISABLE
:
5540 div_mask
= WINED3DSP_WRITEMASK_3
;
5543 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
5544 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
5547 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
5549 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
5550 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
5551 coord_param
.param_str
, coord_mask
);
5553 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
5555 struct glsl_src_param luminance_param
;
5556 struct glsl_dst_param dst_param
;
5558 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
5559 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
5561 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
5562 dst_param
.reg_name
, dst_param
.mask_str
,
5563 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
5565 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5568 static void shader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
5570 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5571 struct glsl_src_param src0_param
, src1_param
;
5573 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
5574 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
5576 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5577 shader_addline(ins
->ctx
->buffer
, "%s + bumpenv_mat%u * %s);\n",
5578 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
5581 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5582 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5583 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
5585 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5586 struct glsl_sample_function sample_function
;
5587 struct glsl_src_param src0_param
;
5589 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
5591 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5592 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
5593 "%s.wx", src0_param
.reg_name
);
5594 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5597 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5598 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5599 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
5601 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5602 struct glsl_sample_function sample_function
;
5603 struct glsl_src_param src0_param
;
5605 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
5607 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5608 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
5609 "%s.yz", src0_param
.reg_name
);
5610 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5613 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5614 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5615 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
5617 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5618 struct glsl_sample_function sample_function
;
5619 struct glsl_src_param src0_param
;
5621 /* Dependent read, not valid with conditional NP2 */
5622 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5623 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
5625 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
5626 "%s", src0_param
.param_str
);
5627 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5630 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5631 * If any of the first 3 components are < 0, discard this pixel */
5632 static void shader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
5634 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 4)
5636 struct glsl_src_param src_param
;
5638 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
5639 shader_addline(ins
->ctx
->buffer
, "if (bool(%s)) discard;\n", src_param
.param_str
);
5643 struct glsl_dst_param dst_param
;
5645 /* The argument is a destination parameter, and no writemasks are allowed */
5646 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
5648 /* 2.0 shaders compare all 4 components in texkill. */
5649 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
5650 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
5651 /* 1.x shaders only compare the first 3 components, probably due to
5652 * the nature of the texkill instruction as a tex* instruction, and
5653 * phase, which kills all .w components. Even if all 4 components are
5654 * defined, only the first 3 are used. */
5656 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
5660 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5661 * dst = dot2(src0, src1) + src2 */
5662 static void shader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
5664 struct glsl_src_param src0_param
;
5665 struct glsl_src_param src1_param
;
5666 struct glsl_src_param src2_param
;
5668 unsigned int mask_size
;
5670 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5671 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
5673 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
5674 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
5675 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
5677 if (mask_size
> 1) {
5678 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
5679 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
5681 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
5682 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
5686 static void shader_glsl_input_pack(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
5687 const struct wined3d_shader_signature
*input_signature
,
5688 const struct wined3d_shader_reg_maps
*reg_maps
,
5689 const struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
5693 for (i
= 0; i
< input_signature
->element_count
; ++i
)
5695 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
5696 const char *semantic_name
;
5701 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
5704 semantic_name
= input
->semantic_name
;
5705 semantic_idx
= input
->semantic_idx
;
5706 shader_glsl_write_mask_to_str(input
->mask
, reg_mask
);
5708 if (args
->vp_mode
== vertexshader
)
5710 if (input
->sysval_semantic
== WINED3D_SV_POSITION
&& !semantic_idx
)
5712 shader_addline(buffer
, "ps_in[%u]%s = vpos%s;\n",
5713 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5715 else if (args
->pointsprite
&& shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
5717 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input
->register_idx
);
5719 else if (input
->sysval_semantic
== WINED3D_SV_IS_FRONT_FACE
)
5721 shader_addline(buffer
, "ps_in[%u] = vec4("
5722 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
5723 input
->register_idx
);
5727 if (input
->sysval_semantic
)
5728 FIXME("Unhandled sysval semantic %#x.\n", input
->sysval_semantic
);
5729 shader_addline(buffer
, "ps_in[%u]%s = ps_link[%u]%s;\n",
5730 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
5731 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
);
5734 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
5736 if (args
->pointsprite
)
5737 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5738 shader
->u
.ps
.input_reg_map
[input
->register_idx
]);
5739 else if (args
->vp_mode
== pretransformed
&& args
->texcoords_initialized
& (1u << semantic_idx
))
5740 shader_addline(buffer
, "ps_in[%u]%s = %s[%u]%s;\n",
5741 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
5742 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
5743 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx
, reg_mask
);
5745 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5746 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5748 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
))
5751 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5752 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5753 else if (semantic_idx
== 1)
5754 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5755 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5757 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5758 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5762 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5763 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5768 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
)
5770 struct glsl_program_key key
;
5772 key
.vs_id
= entry
->vs
.id
;
5773 key
.gs_id
= entry
->gs
.id
;
5774 key
.ps_id
= entry
->ps
.id
;
5776 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
5778 ERR("Failed to insert program entry.\n");
5782 static struct glsl_shader_prog_link
*get_glsl_program_entry(const struct shader_glsl_priv
*priv
,
5783 GLuint vs_id
, GLuint gs_id
, GLuint ps_id
)
5785 struct wine_rb_entry
*entry
;
5786 struct glsl_program_key key
;
5792 entry
= wine_rb_get(&priv
->program_lookup
, &key
);
5793 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
5796 /* Context activation is done by the caller. */
5797 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
5798 struct glsl_shader_prog_link
*entry
)
5800 wine_rb_remove(&priv
->program_lookup
, &entry
->program_lookup_entry
);
5802 GL_EXTCALL(glDeleteProgram(entry
->id
));
5804 list_remove(&entry
->vs
.shader_entry
);
5806 list_remove(&entry
->gs
.shader_entry
);
5808 list_remove(&entry
->ps
.shader_entry
);
5809 HeapFree(GetProcessHeap(), 0, entry
);
5812 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv
*priv
,
5813 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
5814 const struct wined3d_shader_signature
*input_signature
,
5815 const struct wined3d_shader_reg_maps
*reg_maps_in
,
5816 const struct wined3d_shader_signature
*output_signature
,
5817 const struct wined3d_shader_reg_maps
*reg_maps_out
, const char *out_array_name
)
5819 struct wined3d_string_buffer
*destination
= string_buffer_get(&priv
->string_buffers
);
5820 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5821 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5822 unsigned int in_count
= vec4_varyings(3, gl_info
);
5823 unsigned int max_varyings
= legacy_context
? in_count
+ 2 : in_count
;
5824 DWORD in_idx
, *set
= NULL
;
5828 set
= wined3d_calloc(max_varyings
, sizeof(*set
));
5830 for (i
= 0; i
< input_signature
->element_count
; ++i
)
5832 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
5834 if (!(reg_maps_in
->input_registers
& (1u << input
->register_idx
)))
5837 in_idx
= map
[input
->register_idx
];
5838 /* Declared, but not read register */
5841 if (in_idx
>= max_varyings
)
5843 FIXME("More input varyings declared than supported, expect issues.\n");
5847 if (in_idx
== in_count
)
5848 string_buffer_sprintf(destination
, "gl_FrontColor");
5849 else if (in_idx
== in_count
+ 1)
5850 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
5852 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, in_idx
);
5857 for (j
= 0; j
< output_signature
->element_count
; ++j
)
5859 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[j
];
5862 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
))
5863 || input
->semantic_idx
!= output
->semantic_idx
5864 || strcmp(input
->semantic_name
, output
->semantic_name
)
5865 || !(mask
= input
->mask
& output
->mask
))
5868 if (set
[in_idx
] == ~0u)
5870 set
[in_idx
] |= mask
& reg_maps_out
->u
.output_registers_mask
[output
->register_idx
];
5871 shader_glsl_write_mask_to_str(mask
, reg_mask
);
5873 shader_addline(buffer
, "%s%s = shader_out[%u]%s;\n",
5874 destination
->buffer
, reg_mask
, output
->register_idx
, reg_mask
);
5878 for (i
= 0; i
< max_varyings
; ++i
)
5882 if (!set
[i
] || set
[i
] == WINED3DSP_WRITEMASK_ALL
)
5889 if (!(set
[i
] & WINED3DSP_WRITEMASK_0
))
5890 reg_mask
[size
++] = 'x';
5891 if (!(set
[i
] & WINED3DSP_WRITEMASK_1
))
5892 reg_mask
[size
++] = 'y';
5893 if (!(set
[i
] & WINED3DSP_WRITEMASK_2
))
5894 reg_mask
[size
++] = 'z';
5895 if (!(set
[i
] & WINED3DSP_WRITEMASK_3
))
5896 reg_mask
[size
++] = 'w';
5897 reg_mask
[size
] = '\0';
5900 string_buffer_sprintf(destination
, "gl_FrontColor");
5901 else if (i
== in_count
+ 1)
5902 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
5904 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, i
);
5907 shader_addline(buffer
, "%s.%s = 0.0;\n", destination
->buffer
, reg_mask
);
5909 shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
->buffer
, reg_mask
, size
);
5912 HeapFree(GetProcessHeap(), 0, set
);
5913 string_buffer_release(&priv
->string_buffers
, destination
);
5916 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv
*priv
,
5917 unsigned int input_count
, const struct wined3d_shader_signature
*output_signature
,
5918 const struct wined3d_shader_reg_maps
*reg_maps_out
, const char *out_array_name
)
5920 struct wined3d_string_buffer
*destination
= string_buffer_get(&priv
->string_buffers
);
5921 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5925 for (i
= 0; i
< output_signature
->element_count
; ++i
)
5927 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[i
];
5929 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
)))
5932 if (output
->register_idx
>= input_count
)
5935 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, output
->register_idx
);
5937 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
5939 shader_addline(buffer
, "%s%s = shader_out[%u]%s;\n",
5940 destination
->buffer
, reg_mask
, output
->register_idx
, reg_mask
);
5943 string_buffer_release(&priv
->string_buffers
, destination
);
5946 /* Context activation is done by the caller. */
5947 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv
*priv
,
5948 const struct wined3d_shader
*vs
, unsigned int input_count
,
5949 const struct wined3d_gl_info
*gl_info
)
5951 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5952 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5955 shader_addline(buffer
, "varying out vec4 gs_in[%u];\n", input_count
);
5957 shader_addline(buffer
, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count
);
5958 shader_addline(buffer
, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs
->limits
->packed_output
);
5960 shader_glsl_setup_sm4_shader_output(priv
, input_count
, &vs
->output_signature
, &vs
->reg_maps
,
5961 legacy_context
? "gs_in" : "gs_in.gs_in");
5963 shader_addline(buffer
, "}\n");
5966 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv
*priv
,
5967 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
5968 const struct wined3d_shader_signature
*input_signature
,
5969 const struct wined3d_shader_reg_maps
*reg_maps_in
, unsigned int input_count
,
5970 const struct wined3d_shader_signature
*output_signature
,
5971 const struct wined3d_shader_reg_maps
*reg_maps_out
, BOOL per_vertex_point_size
)
5973 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5974 const char *semantic_name
;
5979 /* First, sort out position and point size system values. */
5980 for (i
= 0; i
< output_signature
->element_count
; ++i
)
5982 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[i
];
5984 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
)))
5987 semantic_name
= output
->semantic_name
;
5988 semantic_idx
= output
->semantic_idx
;
5989 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
5991 if (output
->sysval_semantic
== WINED3D_SV_POSITION
&& !semantic_idx
)
5993 shader_addline(buffer
, "gl_Position%s = shader_out[%u]%s;\n",
5994 reg_mask
, output
->register_idx
, reg_mask
);
5996 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
5998 shader_addline(buffer
, "gl_PointSize = clamp(shader_out[%u].%c, "
5999 "ffp_point.size_min, ffp_point.size_max);\n", output
->register_idx
, reg_mask
[1]);
6001 else if (output
->sysval_semantic
)
6003 FIXME("Unhandled sysval semantic %#x.\n", output
->sysval_semantic
);
6007 /* Then, setup the pixel shader input. */
6008 if (reg_maps_out
->shader_version
.major
< 4)
6009 shader_glsl_setup_vs3_output(priv
, gl_info
, map
, input_signature
, reg_maps_in
,
6010 output_signature
, reg_maps_out
, "ps_link");
6012 shader_glsl_setup_sm4_shader_output(priv
, input_count
, output_signature
, reg_maps_out
, "ps_link");
6015 /* Context activation is done by the caller. */
6016 static GLuint
shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv
*priv
,
6017 const struct wined3d_shader
*vs
, const struct wined3d_shader
*ps
,
6018 BOOL per_vertex_point_size
, BOOL flatshading
, const struct wined3d_gl_info
*gl_info
)
6020 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6022 DWORD ps_major
= ps
? ps
->reg_maps
.shader_version
.major
: 0;
6024 const char *semantic_name
;
6027 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6029 string_buffer_clear(buffer
);
6031 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, &vs
->reg_maps
.shader_version
));
6033 if (per_vertex_point_size
)
6035 shader_addline(buffer
, "uniform struct\n{\n");
6036 shader_addline(buffer
, " float size_min;\n");
6037 shader_addline(buffer
, " float size_max;\n");
6038 shader_addline(buffer
, "} ffp_point;\n");
6043 DWORD colors_written_mask
[2] = {0};
6044 DWORD texcoords_written_mask
[MAX_TEXTURES
] = {0};
6046 if (!legacy_context
)
6048 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_diffuse;\n");
6049 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_specular;\n");
6050 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
6051 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
6054 shader_addline(buffer
, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs
->limits
->packed_output
);
6056 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
6058 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
6061 if (!(vs
->reg_maps
.output_registers
& (1u << output
->register_idx
)))
6064 semantic_name
= output
->semantic_name
;
6065 semantic_idx
= output
->semantic_idx
;
6066 write_mask
= output
->mask
;
6067 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
6069 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
) && semantic_idx
< 2)
6072 shader_addline(buffer
, "gl_Front%sColor%s = shader_out[%u]%s;\n",
6073 semantic_idx
? "Secondary" : "", reg_mask
, output
->register_idx
, reg_mask
);
6075 shader_addline(buffer
, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
6076 semantic_idx
? "specular" : "diffuse", reg_mask
, output
->register_idx
, reg_mask
);
6078 colors_written_mask
[semantic_idx
] = write_mask
;
6080 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
6082 shader_addline(buffer
, "gl_Position%s = shader_out[%u]%s;\n",
6083 reg_mask
, output
->register_idx
, reg_mask
);
6085 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
6087 if (semantic_idx
< MAX_TEXTURES
)
6089 shader_addline(buffer
, "%s[%u]%s = shader_out[%u]%s;\n",
6090 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord",
6091 semantic_idx
, reg_mask
, output
->register_idx
, reg_mask
);
6092 texcoords_written_mask
[semantic_idx
] = write_mask
;
6095 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
6097 shader_addline(buffer
, "gl_PointSize = clamp(shader_out[%u].%c, "
6098 "ffp_point.size_min, ffp_point.size_max);\n", output
->register_idx
, reg_mask
[1]);
6100 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_FOG
))
6102 shader_addline(buffer
, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6103 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6104 output
->register_idx
, reg_mask
[1]);
6108 for (i
= 0; i
< 2; ++i
)
6110 if (colors_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
6112 shader_glsl_write_mask_to_str(~colors_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
6114 shader_addline(buffer
, "%s%s = vec4(1.0)%s;\n",
6115 legacy_context
? "gl_FrontColor" : "ffp_varying_diffuse",
6116 reg_mask
, reg_mask
);
6118 shader_addline(buffer
, "%s%s = vec4(0.0)%s;\n",
6119 legacy_context
? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6120 reg_mask
, reg_mask
);
6123 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6125 if (ps
&& !(ps
->reg_maps
.texcoord
& (1u << i
)))
6128 if (texcoords_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
6130 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
6131 && !texcoords_written_mask
[i
])
6134 shader_glsl_write_mask_to_str(~texcoords_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
6135 shader_addline(buffer
, "%s[%u]%s = vec4(0.0)%s;\n",
6136 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
, reg_mask
, reg_mask
);
6142 UINT in_count
= min(vec4_varyings(ps_major
, gl_info
), ps
->limits
->packed_input
);
6144 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", in_count
);
6145 shader_addline(buffer
, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs
->limits
->packed_output
);
6146 shader_glsl_setup_sm3_rasterizer_input(priv
, gl_info
, ps
->u
.ps
.input_reg_map
, &ps
->input_signature
,
6147 &ps
->reg_maps
, 0, &vs
->output_signature
, &vs
->reg_maps
, per_vertex_point_size
);
6150 shader_addline(buffer
, "}\n");
6152 ret
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
6153 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6154 shader_glsl_compile(gl_info
, ret
, buffer
->buffer
);
6159 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv
*priv
,
6160 const struct wined3d_shader
*shader
, unsigned int input_count
,
6161 const struct wined3d_gl_info
*gl_info
)
6163 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6166 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info
), input_count
));
6168 shader_addline(buffer
, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6169 shader_glsl_get_prefix(shader
->reg_maps
.shader_version
.type
), shader
->limits
->packed_output
);
6171 shader_glsl_setup_sm3_rasterizer_input(priv
, gl_info
, NULL
, NULL
,
6172 NULL
, input_count
, &shader
->output_signature
, &shader
->reg_maps
, FALSE
);
6174 shader_addline(buffer
, "}\n");
6177 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer
*buffer
,
6178 const struct wined3d_gl_info
*gl_info
)
6180 const char *output
= get_fragment_output(gl_info
);
6182 shader_addline(buffer
, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output
);
6183 shader_addline(buffer
, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6184 shader_addline(buffer
, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output
);
6185 shader_addline(buffer
, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output
);
6186 shader_addline(buffer
, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output
);
6187 shader_addline(buffer
, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output
, output
);
6190 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer
*buffer
,
6191 const struct wined3d_gl_info
*gl_info
, enum wined3d_ffp_ps_fog_mode mode
)
6193 const char *output
= get_fragment_output(gl_info
);
6197 case WINED3D_FFP_PS_FOG_OFF
:
6200 case WINED3D_FFP_PS_FOG_LINEAR
:
6201 shader_addline(buffer
, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6204 case WINED3D_FFP_PS_FOG_EXP
:
6205 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6208 case WINED3D_FFP_PS_FOG_EXP2
:
6209 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6210 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6214 ERR("Invalid fog mode %#x.\n", mode
);
6218 shader_addline(buffer
, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6222 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer
*buffer
,
6223 const struct wined3d_gl_info
*gl_info
, enum wined3d_cmp_func alpha_func
)
6225 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6226 * flipping all the operators here, just negate the comparison below. */
6227 static const char * const comparison_operator
[] =
6229 "", /* WINED3D_CMP_NEVER */
6230 "<", /* WINED3D_CMP_LESS */
6231 "==", /* WINED3D_CMP_EQUAL */
6232 "<=", /* WINED3D_CMP_LESSEQUAL */
6233 ">", /* WINED3D_CMP_GREATER */
6234 "!=", /* WINED3D_CMP_NOTEQUAL */
6235 ">=", /* WINED3D_CMP_GREATEREQUAL */
6236 "" /* WINED3D_CMP_ALWAYS */
6239 if (alpha_func
== WINED3D_CMP_ALWAYS
)
6242 if (alpha_func
!= WINED3D_CMP_NEVER
)
6243 shader_addline(buffer
, "if (!(%s[0].a %s alpha_test_ref))\n",
6244 get_fragment_output(gl_info
), comparison_operator
[alpha_func
- WINED3D_CMP_NEVER
]);
6245 shader_addline(buffer
, " discard;\n");
6248 static void shader_glsl_enable_extensions(struct wined3d_string_buffer
*buffer
,
6249 const struct wined3d_gl_info
*gl_info
)
6251 if (gl_info
->supported
[ARB_GPU_SHADER5
])
6252 shader_addline(buffer
, "#extension GL_ARB_gpu_shader5 : enable\n");
6253 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
6254 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
6255 if (gl_info
->supported
[ARB_SHADER_IMAGE_LOAD_STORE
])
6256 shader_addline(buffer
, "#extension GL_ARB_shader_image_load_store : enable\n");
6257 if (gl_info
->supported
[ARB_SHADER_IMAGE_SIZE
])
6258 shader_addline(buffer
, "#extension GL_ARB_shader_image_size : enable\n");
6259 if (gl_info
->supported
[ARB_SHADING_LANGUAGE_PACKING
])
6260 shader_addline(buffer
, "#extension GL_ARB_shading_language_packing : enable\n");
6261 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
6262 shader_addline(buffer
, "#extension GL_ARB_texture_query_levels : enable\n");
6263 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
6264 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
6265 if (gl_info
->supported
[EXT_GPU_SHADER4
])
6266 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
6267 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
6268 shader_addline(buffer
, "#extension GL_EXT_texture_array : enable\n");
6271 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info
*gl_info
,
6272 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
6273 const struct ps_compile_args
*args
)
6275 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6277 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6278 if (reg_maps
->shader_version
.major
< 2)
6279 shader_addline(buffer
, "%s[0] = R0;\n", get_fragment_output(gl_info
));
6281 if (args
->srgb_correction
)
6282 shader_glsl_generate_srgb_write_correction(buffer
, gl_info
);
6284 /* SM < 3 does not replace the fog stage. */
6285 if (reg_maps
->shader_version
.major
< 3)
6286 shader_glsl_generate_fog_code(buffer
, gl_info
, args
->fog
);
6288 shader_glsl_generate_alpha_test(buffer
, gl_info
, args
->alpha_test_func
+ 1);
6291 /* Context activation is done by the caller. */
6292 static GLuint
shader_glsl_generate_pshader(const struct wined3d_context
*context
,
6293 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
6294 const struct wined3d_shader
*shader
,
6295 const struct ps_compile_args
*args
, struct ps_np2fixup_info
*np2fixup_info
)
6297 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6298 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6299 const DWORD
*function
= shader
->function
;
6300 struct shader_glsl_ctx_priv priv_ctx
;
6301 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6303 /* Create the hw GLSL shader object and assign it as the shader->prgId */
6304 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
6306 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
6307 priv_ctx
.cur_ps_args
= args
;
6308 priv_ctx
.cur_np2fixup_info
= np2fixup_info
;
6309 priv_ctx
.string_buffers
= string_buffers
;
6311 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, ®_maps
->shader_version
));
6313 shader_glsl_enable_extensions(buffer
, gl_info
);
6314 if (gl_info
->supported
[ARB_DERIVATIVE_CONTROL
])
6315 shader_addline(buffer
, "#extension GL_ARB_derivative_control : enable\n");
6316 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
6317 shader_addline(buffer
, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6318 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
6319 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
6320 /* The spec says that it doesn't have to be explicitly enabled, but the
6321 * nvidia drivers write a warning if we don't do so. */
6322 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
6323 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
6325 /* Base Declarations */
6326 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
6328 shader_addline(buffer
, "void main()\n{\n");
6330 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6331 * add approximately 0.5. This causes off-by-one problems as spotted by
6332 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6333 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6334 * causes precision troubles when we just subtract 0.5.
6336 * To deal with that, just floor() the position. This will eliminate the
6337 * fraction on all cards.
6339 * TODO: Test how this behaves with multisampling.
6341 * An advantage of floor is that it works even if the driver doesn't add
6342 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6343 * to return in gl_FragCoord, even though coordinates specify the pixel
6344 * centers instead of the pixel corners. This code will behave correctly
6345 * on drivers that returns integer values. */
6348 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
6349 shader_addline(buffer
, "vpos = gl_FragCoord;\n");
6350 else if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_PIXEL_CENTER_INTEGER
)
6351 shader_addline(buffer
,
6352 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6354 shader_addline(buffer
,
6355 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6358 if (reg_maps
->shader_version
.major
< 3 || args
->vp_mode
!= vertexshader
)
6361 WORD map
= reg_maps
->texcoord
;
6365 if (glsl_is_color_reg_read(shader
, 0))
6366 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
6367 if (glsl_is_color_reg_read(shader
, 1))
6368 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
6371 for (i
= 0; map
; map
>>= 1, ++i
)
6375 if (args
->pointsprite
)
6376 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i
);
6377 else if (args
->texcoords_initialized
& (1u << i
))
6378 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n", i
,
6379 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
);
6381 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", i
);
6382 shader_addline(buffer
, "vec4 T%u = ffp_texcoord[%u];\n", i
, i
);
6387 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6390 /* Pack 3.0 inputs */
6391 if (reg_maps
->shader_version
.major
>= 3)
6392 shader_glsl_input_pack(shader
, buffer
, &shader
->input_signature
, reg_maps
, args
, gl_info
);
6394 /* Base Shader Body */
6395 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
6397 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6398 if (reg_maps
->shader_version
.major
< 4)
6399 shader_glsl_generate_ps_epilogue(gl_info
, buffer
, shader
, args
);
6401 shader_addline(buffer
, "}\n");
6403 TRACE("Compiling shader object %u.\n", shader_id
);
6404 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6409 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info
*gl_info
,
6410 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
6411 const struct vs_compile_args
*args
)
6413 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6414 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6417 /* Unpack outputs. */
6418 shader_addline(buffer
, "setup_vs_output(vs_out);\n");
6420 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6421 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6422 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6423 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6425 if (reg_maps
->shader_version
.major
< 3)
6427 if (args
->fog_src
== VS_FOG_Z
)
6428 shader_addline(buffer
, "%s = gl_Position.z;\n",
6429 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6430 else if (!reg_maps
->fog
)
6431 shader_addline(buffer
, "%s = 0.0;\n",
6432 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6435 /* We always store the clipplanes without y inversion. */
6436 if (args
->clip_enabled
)
6439 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
6441 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
6442 shader_addline(buffer
, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i
, i
);
6445 if (args
->point_size
&& !args
->per_vertex_point_size
)
6446 shader_addline(buffer
, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6448 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
6449 shader_glsl_fixup_position(buffer
);
6452 /* Context activation is done by the caller. */
6453 static GLuint
shader_glsl_generate_vshader(const struct wined3d_context
*context
,
6454 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
, const struct vs_compile_args
*args
)
6456 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
6457 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6458 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6459 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6460 const DWORD
*function
= shader
->function
;
6461 struct shader_glsl_ctx_priv priv_ctx
;
6463 /* Create the hw GLSL shader program and assign it as the shader->prgId */
6464 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
6466 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, ®_maps
->shader_version
));
6468 shader_glsl_enable_extensions(buffer
, gl_info
);
6469 if (gl_info
->supported
[ARB_DRAW_INSTANCED
])
6470 shader_addline(buffer
, "#extension GL_ARB_draw_instanced : enable\n");
6471 if (gl_info
->supported
[ARB_EXPLICIT_ATTRIB_LOCATION
])
6472 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
6474 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
6475 priv_ctx
.cur_vs_args
= args
;
6476 priv_ctx
.string_buffers
= string_buffers
;
6478 /* Base Declarations */
6479 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
6481 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
6482 shader_addline(buffer
, "uniform vec4 pos_fixup;\n");
6484 if (reg_maps
->shader_version
.major
>= 4)
6486 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
6487 shader_glsl_generate_sm4_rasterizer_input_setup(priv
, shader
, args
->next_shader_input_count
, gl_info
);
6488 else if (args
->next_shader_type
== WINED3D_SHADER_TYPE_GEOMETRY
)
6489 shader_glsl_generate_vs_gs_setup(priv
, shader
, args
->next_shader_input_count
, gl_info
);
6492 shader_addline(buffer
, "void main()\n{\n");
6494 /* Base Shader Body */
6495 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
6497 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6498 if (reg_maps
->shader_version
.major
< 4)
6499 shader_glsl_generate_vs_epilogue(gl_info
, buffer
, shader
, args
);
6501 shader_addline(buffer
, "}\n");
6503 TRACE("Compiling shader object %u.\n", shader_id
);
6504 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6509 /* Context activation is done by the caller. */
6510 static GLuint
shader_glsl_generate_geometry_shader(const struct wined3d_context
*context
,
6511 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
, const struct gs_compile_args
*args
)
6513 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
6514 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6515 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6516 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6517 const DWORD
*function
= shader
->function
;
6518 struct shader_glsl_ctx_priv priv_ctx
;
6521 shader_id
= GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER
));
6523 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, ®_maps
->shader_version
));
6525 shader_glsl_enable_extensions(buffer
, gl_info
);
6526 if (gl_info
->supported
[ARB_GEOMETRY_SHADER4
])
6527 shader_addline(buffer
, "#extension GL_ARB_geometry_shader4 : enable\n");
6529 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
6530 priv_ctx
.string_buffers
= string_buffers
;
6531 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
6532 if (!gl_info
->supported
[ARB_CLIP_CONTROL
])
6533 shader_addline(buffer
, "uniform vec4 pos_fixup;\n");
6534 shader_glsl_generate_sm4_rasterizer_input_setup(priv
, shader
, args
->ps_input_count
, gl_info
);
6535 shader_addline(buffer
, "void main()\n{\n");
6536 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
6537 shader_addline(buffer
, "}\n");
6539 TRACE("Compiling shader object %u.\n", shader_id
);
6540 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6545 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context
*ctx
)
6547 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
6548 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
6549 const struct wined3d_shader
*shader
= ctx
->shader
;
6551 switch (shader
->reg_maps
.shader_version
.type
)
6553 case WINED3D_SHADER_TYPE_PIXEL
:
6554 shader_glsl_generate_ps_epilogue(gl_info
, ctx
->buffer
, shader
, priv
->cur_ps_args
);
6556 case WINED3D_SHADER_TYPE_VERTEX
:
6557 shader_glsl_generate_vs_epilogue(gl_info
, ctx
->buffer
, shader
, priv
->cur_vs_args
);
6559 case WINED3D_SHADER_TYPE_GEOMETRY
:
6562 FIXME("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
6567 static GLuint
find_glsl_pshader(const struct wined3d_context
*context
,
6568 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
6569 struct wined3d_shader
*shader
,
6570 const struct ps_compile_args
*args
, const struct ps_np2fixup_info
**np2fixup_info
)
6572 struct glsl_ps_compiled_shader
*gl_shaders
, *new_array
;
6573 struct glsl_shader_private
*shader_data
;
6574 struct ps_np2fixup_info
*np2fixup
;
6579 if (!shader
->backend_data
)
6581 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
6582 if (!shader
->backend_data
)
6584 ERR("Failed to allocate backend data.\n");
6588 shader_data
= shader
->backend_data
;
6589 gl_shaders
= shader_data
->gl_shaders
.ps
;
6591 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6592 * so a linear search is more performant than a hashmap or a binary search
6593 * (cache coherency etc)
6595 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
6597 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
6599 if (args
->np2_fixup
)
6600 *np2fixup_info
= &gl_shaders
[i
].np2fixup
;
6601 return gl_shaders
[i
].id
;
6605 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
6606 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
6607 if (shader_data
->num_gl_shaders
)
6609 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
6610 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
,
6611 new_size
* sizeof(*gl_shaders
));
6615 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
6620 ERR("Out of memory\n");
6623 shader_data
->gl_shaders
.ps
= new_array
;
6624 shader_data
->shader_array_size
= new_size
;
6625 gl_shaders
= new_array
;
6628 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
6630 np2fixup
= &gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
;
6631 memset(np2fixup
, 0, sizeof(*np2fixup
));
6632 *np2fixup_info
= args
->np2_fixup
? np2fixup
: NULL
;
6634 pixelshader_update_resource_types(shader
, args
->tex_types
);
6636 string_buffer_clear(buffer
);
6637 ret
= shader_glsl_generate_pshader(context
, buffer
, string_buffers
, shader
, args
, np2fixup
);
6638 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
6643 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
6644 const DWORD use_map
)
6646 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
6647 if((stored
->clip_enabled
) != new->clip_enabled
) return FALSE
;
6648 if (stored
->point_size
!= new->point_size
)
6650 if (stored
->per_vertex_point_size
!= new->per_vertex_point_size
)
6652 if (stored
->flatshading
!= new->flatshading
)
6654 if (stored
->next_shader_type
!= new->next_shader_type
)
6656 if (stored
->next_shader_input_count
!= new->next_shader_input_count
)
6658 return stored
->fog_src
== new->fog_src
;
6661 static GLuint
find_glsl_vshader(const struct wined3d_context
*context
, struct shader_glsl_priv
*priv
,
6662 struct wined3d_shader
*shader
, const struct vs_compile_args
*args
)
6666 DWORD use_map
= context
->stream_info
.use_map
;
6667 struct glsl_vs_compiled_shader
*gl_shaders
, *new_array
;
6668 struct glsl_shader_private
*shader_data
;
6671 if (!shader
->backend_data
)
6673 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
6674 if (!shader
->backend_data
)
6676 ERR("Failed to allocate backend data.\n");
6680 shader_data
= shader
->backend_data
;
6681 gl_shaders
= shader_data
->gl_shaders
.vs
;
6683 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6684 * so a linear search is more performant than a hashmap or a binary search
6685 * (cache coherency etc)
6687 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
6689 if (vs_args_equal(&gl_shaders
[i
].args
, args
, use_map
))
6690 return gl_shaders
[i
].id
;
6693 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
6695 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
6696 if (shader_data
->num_gl_shaders
)
6698 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
6699 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
,
6700 new_size
* sizeof(*gl_shaders
));
6704 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
6709 ERR("Out of memory\n");
6712 shader_data
->gl_shaders
.vs
= new_array
;
6713 shader_data
->shader_array_size
= new_size
;
6714 gl_shaders
= new_array
;
6717 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
6719 string_buffer_clear(&priv
->shader_buffer
);
6720 ret
= shader_glsl_generate_vshader(context
, priv
, shader
, args
);
6721 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
6726 static GLuint
find_glsl_geometry_shader(const struct wined3d_context
*context
,
6727 struct shader_glsl_priv
*priv
, struct wined3d_shader
*shader
, const struct gs_compile_args
*args
)
6729 struct glsl_gs_compiled_shader
*gl_shaders
, *new_array
;
6730 struct glsl_shader_private
*shader_data
;
6731 unsigned int i
, new_size
;
6734 if (!shader
->backend_data
)
6736 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
6738 ERR("Failed to allocate backend data.\n");
6742 shader_data
= shader
->backend_data
;
6743 gl_shaders
= shader_data
->gl_shaders
.gs
;
6745 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
6747 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
6748 return gl_shaders
[i
].id
;
6751 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
6753 if (shader_data
->num_gl_shaders
)
6755 new_size
= shader_data
->shader_array_size
+ 1;
6756 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
,
6757 new_size
* sizeof(*new_array
));
6761 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
6767 ERR("Failed to allocate GL shaders array.\n");
6770 shader_data
->gl_shaders
.gs
= new_array
;
6771 shader_data
->shader_array_size
= new_size
;
6772 gl_shaders
= new_array
;
6774 string_buffer_clear(&priv
->shader_buffer
);
6775 ret
= shader_glsl_generate_geometry_shader(context
, priv
, shader
, args
);
6776 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
6777 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
6782 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs
, const char *material
)
6786 case WINED3D_MCS_MATERIAL
:
6788 case WINED3D_MCS_COLOR1
:
6789 return "ffp_attrib_diffuse";
6790 case WINED3D_MCS_COLOR2
:
6791 return "ffp_attrib_specular";
6793 ERR("Invalid material color source %#x.\n", mcs
);
6798 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer
*buffer
,
6799 const struct wined3d_ffp_vs_settings
*settings
, BOOL legacy_lighting
)
6801 const char *diffuse
, *specular
, *emissive
, *ambient
;
6802 unsigned int i
, idx
;
6804 if (!settings
->lighting
)
6806 shader_addline(buffer
, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6807 shader_addline(buffer
, "ffp_varying_specular = ffp_attrib_specular;\n");
6811 shader_addline(buffer
, "vec3 ambient = ffp_light_ambient;\n");
6812 shader_addline(buffer
, "vec3 diffuse = vec3(0.0);\n");
6813 shader_addline(buffer
, "vec4 specular = vec4(0.0);\n");
6814 shader_addline(buffer
, "vec3 dir, dst;\n");
6815 shader_addline(buffer
, "float att, t;\n");
6817 ambient
= shader_glsl_ffp_mcs(settings
->ambient_source
, "ffp_material.ambient");
6818 diffuse
= shader_glsl_ffp_mcs(settings
->diffuse_source
, "ffp_material.diffuse");
6819 specular
= shader_glsl_ffp_mcs(settings
->specular_source
, "ffp_material.specular");
6820 emissive
= shader_glsl_ffp_mcs(settings
->emissive_source
, "ffp_material.emissive");
6823 for (i
= 0; i
< settings
->point_light_count
; ++i
, ++idx
)
6825 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx
);
6826 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
6827 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
6828 shader_addline(buffer
, "dst.x = 1.0;\n");
6829 if (legacy_lighting
)
6831 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx
, idx
);
6832 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
6836 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", idx
);
6838 shader_addline(buffer
, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6839 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx
, idx
, idx
);
6840 if (!legacy_lighting
)
6841 shader_addline(buffer
, "att = 1.0 / att;\n");
6842 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx
);
6843 if (!settings
->normal
)
6845 if (!legacy_lighting
)
6846 shader_addline(buffer
, "}\n");
6849 shader_addline(buffer
, "dir = normalize(dir);\n");
6850 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6851 " * ffp_light[%u].diffuse.xyz) * att;\n", idx
);
6852 if (settings
->localviewer
)
6853 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6855 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6856 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6857 " * ffp_light[%u].specular) * att;\n", idx
);
6858 if (!legacy_lighting
)
6859 shader_addline(buffer
, "}\n");
6862 for (i
= 0; i
< settings
->spot_light_count
; ++i
, ++idx
)
6864 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx
);
6865 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
6866 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
6867 shader_addline(buffer
, "dst.x = 1.0;\n");
6868 if (legacy_lighting
)
6870 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx
, idx
);
6871 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
6875 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", idx
);
6877 shader_addline(buffer
, "dir = normalize(dir);\n");
6878 shader_addline(buffer
, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx
);
6879 shader_addline(buffer
, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx
);
6880 shader_addline(buffer
, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx
);
6881 shader_addline(buffer
, "else att = pow((t - ffp_light[%u].cos_hphi)"
6882 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
6883 idx
, idx
, idx
, idx
);
6884 if (legacy_lighting
)
6885 shader_addline(buffer
, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6886 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6889 shader_addline(buffer
, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6890 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6892 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx
);
6893 if (!settings
->normal
)
6895 if (!legacy_lighting
)
6896 shader_addline(buffer
, "}\n");
6899 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6900 " * ffp_light[%u].diffuse.xyz) * att;\n", idx
);
6901 if (settings
->localviewer
)
6902 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6904 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6905 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6906 " * ffp_light[%u].specular) * att;\n", idx
);
6907 if (!legacy_lighting
)
6908 shader_addline(buffer
, "}\n");
6911 for (i
= 0; i
< settings
->directional_light_count
; ++i
, ++idx
)
6913 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", idx
);
6914 if (!settings
->normal
)
6916 shader_addline(buffer
, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx
);
6917 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6918 " * ffp_light[%u].diffuse.xyz;\n", idx
);
6919 /* TODO: In the non-local viewer case the halfvector is constant
6920 * and could be precomputed and stored in a uniform. */
6921 if (settings
->localviewer
)
6922 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6924 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6925 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6926 " * ffp_light[%u].specular;\n", idx
);
6929 for (i
= 0; i
< settings
->parallel_point_light_count
; ++i
, ++idx
)
6931 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", idx
);
6932 if (!settings
->normal
)
6934 shader_addline(buffer
, "dir = normalize(ffp_light[%u].position.xyz);\n", idx
);
6935 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6936 " * ffp_light[%u].diffuse.xyz;\n", idx
);
6937 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6938 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6939 " * ffp_light[%u].specular;\n", idx
);
6942 shader_addline(buffer
, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6943 ambient
, diffuse
, emissive
);
6944 shader_addline(buffer
, "ffp_varying_diffuse.w = %s.w;\n", diffuse
);
6945 shader_addline(buffer
, "ffp_varying_specular = %s * specular;\n", specular
);
6948 /* Context activation is done by the caller. */
6949 static GLuint
shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
6950 const struct wined3d_ffp_vs_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
6952 static const struct attrib_info
6955 const char name
[24];
6959 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6960 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6961 /* TODO: Indexed vertex blending */
6962 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6963 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6964 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6965 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6966 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6968 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6969 BOOL legacy_lighting
= priv
->legacy_lighting
;
6972 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6973 BOOL output_legacy_fogcoord
= legacy_context
;
6975 string_buffer_clear(buffer
);
6977 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, NULL
));
6979 if (shader_glsl_use_explicit_attrib_location(gl_info
))
6980 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
6982 for (i
= 0; i
< WINED3D_FFP_ATTRIBS_COUNT
; ++i
)
6984 const char *type
= i
< ARRAY_SIZE(attrib_info
) ? attrib_info
[i
].type
: "vec4";
6986 if (shader_glsl_use_explicit_attrib_location(gl_info
))
6987 shader_addline(buffer
, "layout(location = %u) ", i
);
6988 shader_addline(buffer
, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info
), type
, i
);
6990 shader_addline(buffer
, "\n");
6992 shader_addline(buffer
, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS
);
6993 shader_addline(buffer
, "uniform mat4 ffp_projection_matrix;\n");
6994 shader_addline(buffer
, "uniform mat3 ffp_normal_matrix;\n");
6995 shader_addline(buffer
, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES
);
6997 shader_addline(buffer
, "uniform struct\n{\n");
6998 shader_addline(buffer
, " vec4 emissive;\n");
6999 shader_addline(buffer
, " vec4 ambient;\n");
7000 shader_addline(buffer
, " vec4 diffuse;\n");
7001 shader_addline(buffer
, " vec4 specular;\n");
7002 shader_addline(buffer
, " float shininess;\n");
7003 shader_addline(buffer
, "} ffp_material;\n");
7005 shader_addline(buffer
, "uniform vec3 ffp_light_ambient;\n");
7006 shader_addline(buffer
, "uniform struct\n{\n");
7007 shader_addline(buffer
, " vec4 diffuse;\n");
7008 shader_addline(buffer
, " vec4 specular;\n");
7009 shader_addline(buffer
, " vec4 ambient;\n");
7010 shader_addline(buffer
, " vec4 position;\n");
7011 shader_addline(buffer
, " vec3 direction;\n");
7012 shader_addline(buffer
, " float range;\n");
7013 shader_addline(buffer
, " float falloff;\n");
7014 shader_addline(buffer
, " float c_att;\n");
7015 shader_addline(buffer
, " float l_att;\n");
7016 shader_addline(buffer
, " float q_att;\n");
7017 shader_addline(buffer
, " float cos_htheta;\n");
7018 shader_addline(buffer
, " float cos_hphi;\n");
7019 shader_addline(buffer
, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS
);
7021 if (settings
->point_size
)
7023 shader_addline(buffer
, "uniform struct\n{\n");
7024 shader_addline(buffer
, " float size;\n");
7025 shader_addline(buffer
, " float size_min;\n");
7026 shader_addline(buffer
, " float size_max;\n");
7027 shader_addline(buffer
, " float c_att;\n");
7028 shader_addline(buffer
, " float l_att;\n");
7029 shader_addline(buffer
, " float q_att;\n");
7030 shader_addline(buffer
, "} ffp_point;\n");
7035 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
7036 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
7037 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7038 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
7042 if (settings
->clipping
)
7043 shader_addline(buffer
, "uniform vec4 clip_planes[%u];\n", gl_info
->limits
.user_clip_distances
);
7045 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
7046 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
7047 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7048 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
7051 shader_addline(buffer
, "\nvoid main()\n{\n");
7052 shader_addline(buffer
, "float m;\n");
7053 shader_addline(buffer
, "vec3 r;\n");
7055 for (i
= 0; i
< ARRAY_SIZE(attrib_info
); ++i
)
7057 if (attrib_info
[i
].name
[0])
7058 shader_addline(buffer
, "%s %s = vs_in%u%s;\n", attrib_info
[i
].type
, attrib_info
[i
].name
,
7059 i
, settings
->swizzle_map
& (1u << i
) ? ".zyxw" : "");
7061 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7063 unsigned int coord_idx
= settings
->texgen
[i
] & 0x0000ffff;
7064 if ((settings
->texgen
[i
] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
7065 && settings
->texcoords
& (1u << i
))
7066 shader_addline(buffer
, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i
, coord_idx
+ WINED3D_FFP_TEXCOORD0
);
7069 shader_addline(buffer
, "ffp_attrib_blendweight[%u] = 1.0;\n", settings
->vertexblends
);
7071 if (settings
->transformed
)
7073 shader_addline(buffer
, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
7074 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7075 shader_addline(buffer
, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
7079 for (i
= 0; i
< settings
->vertexblends
; ++i
)
7080 shader_addline(buffer
, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings
->vertexblends
, i
);
7082 shader_addline(buffer
, "vec4 ec_pos = vec4(0.0);\n");
7083 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
7084 shader_addline(buffer
, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i
, i
);
7086 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7087 if (settings
->clipping
)
7089 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
7090 shader_addline(buffer
, "gl_ClipVertex = ec_pos;\n");
7092 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
7093 shader_addline(buffer
, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i
, i
);
7095 shader_addline(buffer
, "ec_pos /= ec_pos.w;\n");
7098 shader_addline(buffer
, "vec3 normal = vec3(0.0);\n");
7099 if (settings
->normal
)
7101 if (!settings
->vertexblends
)
7103 shader_addline(buffer
, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7107 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
7108 shader_addline(buffer
, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i
, i
);
7111 if (settings
->normalize
)
7112 shader_addline(buffer
, "normal = normalize(normal);\n");
7115 shader_glsl_ffp_vertex_lighting(buffer
, settings
, legacy_lighting
);
7118 shader_addline(buffer
, "gl_FrontColor = ffp_varying_diffuse;\n");
7119 shader_addline(buffer
, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7123 shader_addline(buffer
, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7124 shader_addline(buffer
, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7127 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7129 BOOL output_legacy_texcoord
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
7131 switch (settings
->texgen
[i
] & 0xffff0000)
7133 case WINED3DTSS_TCI_PASSTHRU
:
7134 if (settings
->texcoords
& (1u << i
))
7135 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7137 else if (gl_info
->limits
.glsl_varyings
>= wined3d_max_compat_varyings(gl_info
))
7138 shader_addline(buffer
, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i
);
7140 output_legacy_texcoord
= FALSE
;
7143 case WINED3DTSS_TCI_CAMERASPACENORMAL
:
7144 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i
, i
);
7147 case WINED3DTSS_TCI_CAMERASPACEPOSITION
:
7148 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i
, i
);
7151 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
:
7152 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7153 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i
, i
);
7156 case WINED3DTSS_TCI_SPHEREMAP
:
7157 shader_addline(buffer
, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7158 shader_addline(buffer
, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7159 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7160 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i
, i
);
7164 ERR("Unhandled texgen %#x.\n", settings
->texgen
[i
]);
7167 if (output_legacy_texcoord
)
7168 shader_addline(buffer
, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i
, i
);
7171 switch (settings
->fog_mode
)
7173 case WINED3D_FFP_VS_FOG_OFF
:
7174 output_legacy_fogcoord
= FALSE
;
7177 case WINED3D_FFP_VS_FOG_FOGCOORD
:
7178 shader_addline(buffer
, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7181 case WINED3D_FFP_VS_FOG_RANGE
:
7182 shader_addline(buffer
, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7185 case WINED3D_FFP_VS_FOG_DEPTH
:
7186 if (settings
->ortho_fog
)
7188 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
7189 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z;\n");
7191 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7192 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7194 else if (settings
->transformed
)
7196 shader_addline(buffer
, "ffp_varying_fogcoord = ec_pos.z;\n");
7200 shader_addline(buffer
, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7205 ERR("Unhandled fog mode %#x.\n", settings
->fog_mode
);
7208 if (output_legacy_fogcoord
)
7209 shader_addline(buffer
, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7211 if (settings
->point_size
)
7213 shader_addline(buffer
, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7214 " + ffp_point.l_att * length(ec_pos.xyz)"
7215 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7216 settings
->per_vertex_point_size
? "ffp_attrib_psize" : "ffp_point.size");
7217 shader_addline(buffer
, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7220 shader_addline(buffer
, "}\n");
7222 shader_obj
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
7223 shader_glsl_compile(gl_info
, shader_obj
, buffer
->buffer
);
7228 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer
*buffer
,
7229 DWORD argnum
, unsigned int stage
, DWORD arg
)
7233 if (arg
== ARG_UNUSED
)
7234 return "<unused arg>";
7236 switch (arg
& WINED3DTA_SELECTMASK
)
7238 case WINED3DTA_DIFFUSE
:
7239 ret
= "ffp_varying_diffuse";
7242 case WINED3DTA_CURRENT
:
7246 case WINED3DTA_TEXTURE
:
7249 case 0: ret
= "tex0"; break;
7250 case 1: ret
= "tex1"; break;
7251 case 2: ret
= "tex2"; break;
7252 case 3: ret
= "tex3"; break;
7253 case 4: ret
= "tex4"; break;
7254 case 5: ret
= "tex5"; break;
7255 case 6: ret
= "tex6"; break;
7256 case 7: ret
= "tex7"; break;
7258 ret
= "<invalid texture>";
7263 case WINED3DTA_TFACTOR
:
7267 case WINED3DTA_SPECULAR
:
7268 ret
= "ffp_varying_specular";
7271 case WINED3DTA_TEMP
:
7275 case WINED3DTA_CONSTANT
:
7278 case 0: ret
= "tss_const0"; break;
7279 case 1: ret
= "tss_const1"; break;
7280 case 2: ret
= "tss_const2"; break;
7281 case 3: ret
= "tss_const3"; break;
7282 case 4: ret
= "tss_const4"; break;
7283 case 5: ret
= "tss_const5"; break;
7284 case 6: ret
= "tss_const6"; break;
7285 case 7: ret
= "tss_const7"; break;
7287 ret
= "<invalid constant>";
7293 return "<unhandled arg>";
7296 if (arg
& WINED3DTA_COMPLEMENT
)
7298 shader_addline(buffer
, "arg%u = vec4(1.0) - %s;\n", argnum
, ret
);
7301 else if (argnum
== 1)
7303 else if (argnum
== 2)
7307 if (arg
& WINED3DTA_ALPHAREPLICATE
)
7309 shader_addline(buffer
, "arg%u = vec4(%s.w);\n", argnum
, ret
);
7312 else if (argnum
== 1)
7314 else if (argnum
== 2)
7321 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer
*buffer
, unsigned int stage
, BOOL color
,
7322 BOOL alpha
, DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
)
7324 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
7334 dstreg
= "temp_reg";
7338 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, dw_arg0
);
7339 arg1
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 1, stage
, dw_arg1
);
7340 arg2
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 2, stage
, dw_arg2
);
7344 case WINED3D_TOP_DISABLE
:
7347 case WINED3D_TOP_SELECT_ARG1
:
7348 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
);
7351 case WINED3D_TOP_SELECT_ARG2
:
7352 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg2
, dstmask
);
7355 case WINED3D_TOP_MODULATE
:
7356 shader_addline(buffer
, "%s%s = %s%s * %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7359 case WINED3D_TOP_MODULATE_4X
:
7360 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7361 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7364 case WINED3D_TOP_MODULATE_2X
:
7365 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7366 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7369 case WINED3D_TOP_ADD
:
7370 shader_addline(buffer
, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7371 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7374 case WINED3D_TOP_ADD_SIGNED
:
7375 shader_addline(buffer
, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7376 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7379 case WINED3D_TOP_ADD_SIGNED_2X
:
7380 shader_addline(buffer
, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7381 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7384 case WINED3D_TOP_SUBTRACT
:
7385 shader_addline(buffer
, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7386 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7389 case WINED3D_TOP_ADD_SMOOTH
:
7390 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7391 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
, dstmask
);
7394 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA
:
7395 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
7396 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7397 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
7400 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
7401 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
7402 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7403 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
7406 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
7407 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
7408 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7409 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
7412 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
7413 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
7414 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7415 dstreg
, dstmask
, arg2
, dstmask
, arg0
, arg1
, dstmask
);
7418 case WINED3D_TOP_BLEND_CURRENT_ALPHA
:
7419 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
7420 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7421 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
7424 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR
:
7425 shader_addline(buffer
, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
7426 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
7429 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA
:
7430 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
7431 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
7434 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR
:
7435 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7436 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
7438 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA
:
7439 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
7440 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
7443 case WINED3D_TOP_BUMPENVMAP
:
7444 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
7445 /* These are handled in the first pass, nothing to do. */
7448 case WINED3D_TOP_DOTPRODUCT3
:
7449 shader_addline(buffer
, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
7450 dstreg
, dstmask
, arg1
, arg2
, dstmask
);
7453 case WINED3D_TOP_MULTIPLY_ADD
:
7454 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
7455 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg0
, dstmask
);
7458 case WINED3D_TOP_LERP
:
7459 /* MSDN isn't quite right here. */
7460 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s%s);\n",
7461 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
, dstmask
);
7465 FIXME("Unhandled operation %#x.\n", op
);
7470 /* Context activation is done by the caller. */
7471 static GLuint
shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
7472 const struct ffp_frag_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
7474 struct wined3d_string_buffer
*tex_reg_name
= string_buffer_get(&priv
->string_buffers
);
7475 enum wined3d_cmp_func alpha_test_func
= settings
->alpha_test_func
+ 1;
7476 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
7477 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
7478 BYTE lum_map
= 0, bump_map
= 0, tex_map
= 0, tss_const_map
= 0;
7479 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
7480 UINT lowest_disabled_stage
;
7482 DWORD arg0
, arg1
, arg2
;
7485 string_buffer_clear(buffer
);
7487 /* Find out which textures are read */
7488 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
7490 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
7493 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
7494 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
7495 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
7497 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
7498 || (stage
== 0 && settings
->color_key_enabled
))
7499 tex_map
|= 1u << stage
;
7500 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
7501 tfactor_used
= TRUE
;
7502 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
7503 tempreg_used
= TRUE
;
7504 if (settings
->op
[stage
].dst
== tempreg
)
7505 tempreg_used
= TRUE
;
7506 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
7507 tss_const_map
|= 1u << stage
;
7509 switch (settings
->op
[stage
].cop
)
7511 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
7512 lum_map
|= 1u << stage
;
7514 case WINED3D_TOP_BUMPENVMAP
:
7515 bump_map
|= 1u << stage
;
7517 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
7518 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
7519 tex_map
|= 1u << stage
;
7522 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
7523 tfactor_used
= TRUE
;
7530 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
7533 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
7534 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
7535 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
7537 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
)
7538 tex_map
|= 1u << stage
;
7539 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
7540 tfactor_used
= TRUE
;
7541 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
7542 tempreg_used
= TRUE
;
7543 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
7544 tss_const_map
|= 1u << stage
;
7546 lowest_disabled_stage
= stage
;
7548 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, NULL
));
7550 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
7551 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
7553 if (!needs_legacy_glsl_syntax(gl_info
))
7554 shader_addline(buffer
, "out vec4 ps_out[1];\n");
7556 shader_addline(buffer
, "vec4 tmp0, tmp1;\n");
7557 shader_addline(buffer
, "vec4 ret;\n");
7558 if (tempreg_used
|| settings
->sRGB_write
)
7559 shader_addline(buffer
, "vec4 temp_reg = vec4(0.0);\n");
7560 shader_addline(buffer
, "vec4 arg0, arg1, arg2;\n");
7562 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
7564 if (tss_const_map
& (1u << stage
))
7565 shader_addline(buffer
, "uniform vec4 tss_const%u;\n", stage
);
7567 if (!(tex_map
& (1u << stage
)))
7570 switch (settings
->op
[stage
].tex_type
)
7572 case WINED3D_GL_RES_TYPE_TEX_1D
:
7573 shader_addline(buffer
, "uniform sampler1D ps_sampler%u;\n", stage
);
7575 case WINED3D_GL_RES_TYPE_TEX_2D
:
7576 shader_addline(buffer
, "uniform sampler2D ps_sampler%u;\n", stage
);
7578 case WINED3D_GL_RES_TYPE_TEX_3D
:
7579 shader_addline(buffer
, "uniform sampler3D ps_sampler%u;\n", stage
);
7581 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
7582 shader_addline(buffer
, "uniform samplerCube ps_sampler%u;\n", stage
);
7584 case WINED3D_GL_RES_TYPE_TEX_RECT
:
7585 shader_addline(buffer
, "uniform sampler2DRect ps_sampler%u;\n", stage
);
7588 FIXME("Unhandled sampler type %#x.\n", settings
->op
[stage
].tex_type
);
7592 shader_addline(buffer
, "vec4 tex%u;\n", stage
);
7594 if (!(bump_map
& (1u << stage
)))
7596 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", stage
);
7598 if (!(lum_map
& (1u << stage
)))
7600 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", stage
);
7601 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", stage
);
7604 shader_addline(buffer
, "uniform vec4 tex_factor;\n");
7605 if (settings
->color_key_enabled
)
7606 shader_addline(buffer
, "uniform vec4 color_key[2];\n");
7607 shader_addline(buffer
, "uniform vec4 specular_enable;\n");
7609 if (settings
->sRGB_write
)
7611 shader_addline(buffer
, "const vec4 srgb_const0 = ");
7612 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
7613 shader_addline(buffer
, ";\n");
7614 shader_addline(buffer
, "const vec4 srgb_const1 = ");
7615 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
7616 shader_addline(buffer
, ";\n");
7619 shader_addline(buffer
, "uniform struct\n{\n");
7620 shader_addline(buffer
, " vec4 color;\n");
7621 shader_addline(buffer
, " float density;\n");
7622 shader_addline(buffer
, " float end;\n");
7623 shader_addline(buffer
, " float scale;\n");
7624 shader_addline(buffer
, "} ffp_fog;\n");
7626 if (alpha_test_func
!= WINED3D_CMP_ALWAYS
)
7627 shader_addline(buffer
, "uniform float alpha_test_ref;\n");
7631 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
7632 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
7633 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7634 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
7635 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
7639 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
7640 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
7641 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7642 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
7643 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
7646 shader_addline(buffer
, "void main()\n{\n");
7650 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
7651 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
7654 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
7656 if (tex_map
& (1u << stage
))
7658 if (settings
->pointsprite
)
7659 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage
);
7660 else if (settings
->texcoords_initialized
& (1u << stage
))
7661 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n",
7662 stage
, legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", stage
);
7664 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", stage
);
7668 if (legacy_context
&& settings
->fog
!= WINED3D_FFP_PS_FOG_OFF
)
7669 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7671 if (lowest_disabled_stage
< 7 && settings
->emul_clipplanes
)
7672 shader_addline(buffer
, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
7674 /* Generate texture sampling instructions */
7675 for (stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3D_TOP_DISABLE
; ++stage
)
7677 const char *texture_function
, *coord_mask
;
7680 if (!(tex_map
& (1u << stage
)))
7683 if (settings
->op
[stage
].projected
== proj_none
)
7687 else if (settings
->op
[stage
].projected
== proj_count4
7688 || settings
->op
[stage
].projected
== proj_count3
)
7694 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
7698 if (settings
->op
[stage
].tex_type
== WINED3D_GL_RES_TYPE_TEX_CUBE
)
7701 switch (settings
->op
[stage
].tex_type
)
7703 case WINED3D_GL_RES_TYPE_TEX_1D
:
7706 texture_function
= "texture1DProj";
7711 texture_function
= "texture1D";
7715 case WINED3D_GL_RES_TYPE_TEX_2D
:
7718 texture_function
= "texture2DProj";
7723 texture_function
= "texture2D";
7727 case WINED3D_GL_RES_TYPE_TEX_3D
:
7730 texture_function
= "texture3DProj";
7731 coord_mask
= "xyzw";
7735 texture_function
= "texture3D";
7739 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
7740 texture_function
= "textureCube";
7743 case WINED3D_GL_RES_TYPE_TEX_RECT
:
7746 texture_function
= "texture2DRectProj";
7751 texture_function
= "texture2DRect";
7756 FIXME("Unhandled texture type %#x.\n", settings
->op
[stage
].tex_type
);
7757 texture_function
= "";
7758 coord_mask
= "xyzw";
7761 if (!needs_legacy_glsl_syntax(gl_info
))
7762 texture_function
= proj
? "textureProj" : "texture";
7765 && (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP
7766 || settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
))
7768 shader_addline(buffer
, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage
- 1, stage
- 1);
7770 /* With projective textures, texbem only divides the static
7771 * texture coord, not the displacement, so multiply the
7772 * displacement with the dividing parameter before passing it to
7774 if (settings
->op
[stage
].projected
!= proj_none
)
7776 if (settings
->op
[stage
].projected
== proj_count4
)
7778 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7780 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].ww;\n", stage
);
7784 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7786 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].zz;\n", stage
);
7791 shader_addline(buffer
, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage
);
7794 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7795 stage
, texture_function
, stage
, coord_mask
);
7797 if (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
7798 shader_addline(buffer
, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7799 stage
, stage
- 1, stage
- 1, stage
- 1);
7801 else if (settings
->op
[stage
].projected
== proj_count3
)
7803 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7804 stage
, texture_function
, stage
, stage
);
7808 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7809 stage
, texture_function
, stage
, stage
, coord_mask
);
7812 string_buffer_sprintf(tex_reg_name
, "tex%u", stage
);
7813 shader_glsl_color_correction_ext(buffer
, tex_reg_name
->buffer
, WINED3DSP_WRITEMASK_ALL
,
7814 settings
->op
[stage
].color_fixup
);
7817 if (settings
->color_key_enabled
)
7819 shader_addline(buffer
, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7820 shader_addline(buffer
, " discard;\n");
7823 shader_addline(buffer
, "ret = ffp_varying_diffuse;\n");
7825 /* Generate the main shader */
7826 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
7830 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
7833 if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
7834 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
7835 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
7836 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
7837 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
7838 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
7839 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
7840 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
7841 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
7842 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
7843 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
7844 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
7846 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
7847 && settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
7848 && settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
7849 && settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
7851 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
7853 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
7854 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
7855 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
7859 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
7860 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
7861 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
7863 else if (settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP
7864 && settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
7866 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
7867 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
7868 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
7869 shader_glsl_ffp_fragment_op(buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
7870 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
7871 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
7875 shader_addline(buffer
, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
7876 get_fragment_output(gl_info
));
7878 if (settings
->sRGB_write
)
7879 shader_glsl_generate_srgb_write_correction(buffer
, gl_info
);
7881 shader_glsl_generate_fog_code(buffer
, gl_info
, settings
->fog
);
7883 shader_glsl_generate_alpha_test(buffer
, gl_info
, alpha_test_func
);
7885 shader_addline(buffer
, "}\n");
7887 shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
7888 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7890 string_buffer_release(&priv
->string_buffers
, tex_reg_name
);
7894 static struct glsl_ffp_vertex_shader
*shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
7895 const struct wined3d_gl_info
*gl_info
, const struct wined3d_ffp_vs_settings
*settings
)
7897 struct glsl_ffp_vertex_shader
*shader
;
7898 const struct wine_rb_entry
*entry
;
7900 if ((entry
= wine_rb_get(&priv
->ffp_vertex_shaders
, settings
)))
7901 return WINE_RB_ENTRY_VALUE(entry
, struct glsl_ffp_vertex_shader
, desc
.entry
);
7903 if (!(shader
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader
))))
7906 shader
->desc
.settings
= *settings
;
7907 shader
->id
= shader_glsl_generate_ffp_vertex_shader(priv
, settings
, gl_info
);
7908 list_init(&shader
->linked_programs
);
7909 if (wine_rb_put(&priv
->ffp_vertex_shaders
, &shader
->desc
.settings
, &shader
->desc
.entry
) == -1)
7910 ERR("Failed to insert ffp vertex shader.\n");
7915 static struct glsl_ffp_fragment_shader
*shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
7916 const struct wined3d_gl_info
*gl_info
, const struct ffp_frag_settings
*args
)
7918 struct glsl_ffp_fragment_shader
*glsl_desc
;
7919 const struct ffp_frag_desc
*desc
;
7921 if ((desc
= find_ffp_frag_shader(&priv
->ffp_fragment_shaders
, args
)))
7922 return CONTAINING_RECORD(desc
, struct glsl_ffp_fragment_shader
, entry
);
7924 if (!(glsl_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc
))))
7927 glsl_desc
->entry
.settings
= *args
;
7928 glsl_desc
->id
= shader_glsl_generate_ffp_fragment_shader(priv
, args
, gl_info
);
7929 list_init(&glsl_desc
->linked_programs
);
7930 add_ffp_frag_shader(&priv
->ffp_fragment_shaders
, &glsl_desc
->entry
);
7936 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
7937 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_vs_program
*vs
, unsigned int vs_c_count
)
7940 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
7942 for (i
= 0; i
< vs_c_count
; ++i
)
7944 string_buffer_sprintf(name
, "vs_c[%u]", i
);
7945 vs
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7947 memset(&vs
->uniform_f_locations
[vs_c_count
], 0xff, (WINED3D_MAX_VS_CONSTS_F
- vs_c_count
) * sizeof(GLuint
));
7949 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
7951 string_buffer_sprintf(name
, "vs_i[%u]", i
);
7952 vs
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7955 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
7957 string_buffer_sprintf(name
, "vs_b[%u]", i
);
7958 vs
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7961 vs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "pos_fixup"));
7963 for (i
= 0; i
< MAX_VERTEX_BLENDS
; ++i
)
7965 string_buffer_sprintf(name
, "ffp_modelview_matrix[%u]", i
);
7966 vs
->modelview_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7968 vs
->projection_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_projection_matrix"));
7969 vs
->normal_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_normal_matrix"));
7970 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7972 string_buffer_sprintf(name
, "ffp_texture_matrix[%u]", i
);
7973 vs
->texture_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7975 vs
->material_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.ambient"));
7976 vs
->material_diffuse_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.diffuse"));
7977 vs
->material_specular_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.specular"));
7978 vs
->material_emissive_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.emissive"));
7979 vs
->material_shininess_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.shininess"));
7980 vs
->light_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_light_ambient"));
7981 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
7983 string_buffer_sprintf(name
, "ffp_light[%u].diffuse", i
);
7984 vs
->light_location
[i
].diffuse
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7985 string_buffer_sprintf(name
, "ffp_light[%u].specular", i
);
7986 vs
->light_location
[i
].specular
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7987 string_buffer_sprintf(name
, "ffp_light[%u].ambient", i
);
7988 vs
->light_location
[i
].ambient
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7989 string_buffer_sprintf(name
, "ffp_light[%u].position", i
);
7990 vs
->light_location
[i
].position
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7991 string_buffer_sprintf(name
, "ffp_light[%u].direction", i
);
7992 vs
->light_location
[i
].direction
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7993 string_buffer_sprintf(name
, "ffp_light[%u].range", i
);
7994 vs
->light_location
[i
].range
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7995 string_buffer_sprintf(name
, "ffp_light[%u].falloff", i
);
7996 vs
->light_location
[i
].falloff
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7997 string_buffer_sprintf(name
, "ffp_light[%u].c_att", i
);
7998 vs
->light_location
[i
].c_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7999 string_buffer_sprintf(name
, "ffp_light[%u].l_att", i
);
8000 vs
->light_location
[i
].l_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8001 string_buffer_sprintf(name
, "ffp_light[%u].q_att", i
);
8002 vs
->light_location
[i
].q_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8003 string_buffer_sprintf(name
, "ffp_light[%u].cos_htheta", i
);
8004 vs
->light_location
[i
].cos_htheta
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8005 string_buffer_sprintf(name
, "ffp_light[%u].cos_hphi", i
);
8006 vs
->light_location
[i
].cos_hphi
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8008 vs
->pointsize_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size"));
8009 vs
->pointsize_min_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_min"));
8010 vs
->pointsize_max_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_max"));
8011 vs
->pointsize_c_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.c_att"));
8012 vs
->pointsize_l_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.l_att"));
8013 vs
->pointsize_q_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.q_att"));
8014 vs
->clip_planes_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "clip_planes"));
8016 string_buffer_release(&priv
->string_buffers
, name
);
8019 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
8020 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_gs_program
*gs
)
8022 gs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "pos_fixup"));
8025 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info
*gl_info
,
8026 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_ps_program
*ps
, unsigned int ps_c_count
)
8029 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
8031 for (i
= 0; i
< ps_c_count
; ++i
)
8033 string_buffer_sprintf(name
, "ps_c[%u]", i
);
8034 ps
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8036 memset(&ps
->uniform_f_locations
[ps_c_count
], 0xff, (WINED3D_MAX_PS_CONSTS_F
- ps_c_count
) * sizeof(GLuint
));
8038 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
8040 string_buffer_sprintf(name
, "ps_i[%u]", i
);
8041 ps
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8044 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
8046 string_buffer_sprintf(name
, "ps_b[%u]", i
);
8047 ps
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8050 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
8052 string_buffer_sprintf(name
, "bumpenv_mat%u", i
);
8053 ps
->bumpenv_mat_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8054 string_buffer_sprintf(name
, "bumpenv_lum_scale%u", i
);
8055 ps
->bumpenv_lum_scale_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8056 string_buffer_sprintf(name
, "bumpenv_lum_offset%u", i
);
8057 ps
->bumpenv_lum_offset_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8058 string_buffer_sprintf(name
, "tss_const%u", i
);
8059 ps
->tss_constant_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8062 ps
->tex_factor_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "tex_factor"));
8063 ps
->specular_enable_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "specular_enable"));
8065 ps
->fog_color_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.color"));
8066 ps
->fog_density_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.density"));
8067 ps
->fog_end_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.end"));
8068 ps
->fog_scale_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.scale"));
8070 ps
->alpha_test_ref_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "alpha_test_ref"));
8072 ps
->np2_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ps_samplerNP2Fixup"));
8073 ps
->ycorrection_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ycorrection"));
8074 ps
->color_key_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "color_key"));
8076 string_buffer_release(&priv
->string_buffers
, name
);
8079 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info
*gl_info
,
8080 struct shader_glsl_priv
*priv
, GLuint program_id
,
8081 const struct wined3d_shader_reg_maps
*reg_maps
)
8083 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
8084 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
8085 unsigned int i
, base
, count
;
8088 wined3d_gl_limits_get_uniform_block_range(&gl_info
->limits
, reg_maps
->shader_version
.type
, &base
, &count
);
8089 for (i
= 0; i
< count
; ++i
)
8091 if (!reg_maps
->cb_sizes
[i
])
8094 string_buffer_sprintf(name
, "block_%s_cb%u", prefix
, i
);
8095 block_idx
= GL_EXTCALL(glGetUniformBlockIndex(program_id
, name
->buffer
));
8096 GL_EXTCALL(glUniformBlockBinding(program_id
, block_idx
, base
+ i
));
8098 checkGLcall("glUniformBlockBinding");
8099 string_buffer_release(&priv
->string_buffers
, name
);
8102 /* Context activation is done by the caller. */
8103 static void set_glsl_shader_program(const struct wined3d_context
*context
, const struct wined3d_state
*state
,
8104 struct shader_glsl_priv
*priv
, struct glsl_context_data
*ctx_data
)
8106 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8107 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
8108 const struct ps_np2fixup_info
*np2fixup_info
= NULL
;
8109 struct glsl_shader_prog_link
*entry
= NULL
;
8110 struct wined3d_shader
*vshader
= NULL
;
8111 struct wined3d_shader
*gshader
= NULL
;
8112 struct wined3d_shader
*pshader
= NULL
;
8114 GLuint reorder_shader_id
= 0;
8119 struct list
*ps_list
, *vs_list
;
8121 struct wined3d_string_buffer
*tmp_name
;
8123 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_VERTEX
)) && ctx_data
->glsl_program
)
8125 vs_id
= ctx_data
->glsl_program
->vs
.id
;
8126 vs_list
= &ctx_data
->glsl_program
->vs
.shader_entry
;
8130 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
8131 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
8133 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_GEOMETRY
))
8134 && ctx_data
->glsl_program
->gs
.id
)
8136 gs_id
= ctx_data
->glsl_program
->gs
.id
;
8140 struct gs_compile_args args
;
8142 find_gs_compile_args(state
, gshader
, &args
);
8143 gs_id
= find_glsl_geometry_shader(context
, priv
, gshader
, &args
);
8147 else if (use_vs(state
))
8149 struct vs_compile_args vs_compile_args
;
8151 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
8152 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
8154 find_vs_compile_args(state
, vshader
, context
->stream_info
.swizzle_map
, &vs_compile_args
, d3d_info
);
8155 vs_id
= find_glsl_vshader(context
, priv
, vshader
, &vs_compile_args
);
8156 vs_list
= &vshader
->linked_programs
;
8158 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_GEOMETRY
))
8159 && ctx_data
->glsl_program
->gs
.id
)
8161 gs_id
= ctx_data
->glsl_program
->gs
.id
;
8165 struct gs_compile_args gs_compile_args
;
8167 find_gs_compile_args(state
, gshader
, &gs_compile_args
);
8168 gs_id
= find_glsl_geometry_shader(context
, priv
, gshader
, &gs_compile_args
);
8171 else if (priv
->vertex_pipe
== &glsl_vertex_pipe
)
8173 struct glsl_ffp_vertex_shader
*ffp_shader
;
8174 struct wined3d_ffp_vs_settings settings
;
8176 wined3d_ffp_get_vs_settings(context
, state
, &settings
);
8177 ffp_shader
= shader_glsl_find_ffp_vertex_shader(priv
, gl_info
, &settings
);
8178 vs_id
= ffp_shader
->id
;
8179 vs_list
= &ffp_shader
->linked_programs
;
8182 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_PIXEL
)) && ctx_data
->glsl_program
)
8184 ps_id
= ctx_data
->glsl_program
->ps
.id
;
8185 ps_list
= &ctx_data
->glsl_program
->ps
.shader_entry
;
8188 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
8190 else if (use_ps(state
))
8192 struct ps_compile_args ps_compile_args
;
8193 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
8194 find_ps_compile_args(state
, pshader
, context
->stream_info
.position_transformed
, &ps_compile_args
, context
);
8195 ps_id
= find_glsl_pshader(context
, &priv
->shader_buffer
, &priv
->string_buffers
,
8196 pshader
, &ps_compile_args
, &np2fixup_info
);
8197 ps_list
= &pshader
->linked_programs
;
8199 else if (priv
->fragment_pipe
== &glsl_fragment_pipe
)
8201 struct glsl_ffp_fragment_shader
*ffp_shader
;
8202 struct ffp_frag_settings settings
;
8204 gen_ffp_frag_op(context
, state
, &settings
, FALSE
);
8205 ffp_shader
= shader_glsl_find_ffp_fragment_shader(priv
, gl_info
, &settings
);
8206 ps_id
= ffp_shader
->id
;
8207 ps_list
= &ffp_shader
->linked_programs
;
8210 if ((!vs_id
&& !gs_id
&& !ps_id
) || (entry
= get_glsl_program_entry(priv
, vs_id
, gs_id
, ps_id
)))
8212 ctx_data
->glsl_program
= entry
;
8216 /* If we get to this point, then no matching program exists, so we create one */
8217 program_id
= GL_EXTCALL(glCreateProgram());
8218 TRACE("Created new GLSL shader program %u.\n", program_id
);
8220 /* Create the entry */
8221 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
8222 entry
->id
= program_id
;
8223 entry
->vs
.id
= vs_id
;
8224 entry
->gs
.id
= gs_id
;
8225 entry
->ps
.id
= ps_id
;
8226 entry
->constant_version
= 0;
8227 entry
->ps
.np2_fixup_info
= np2fixup_info
;
8228 /* Add the hash table entry */
8229 add_glsl_program_entry(priv
, entry
);
8231 /* Set the current program */
8232 ctx_data
->glsl_program
= entry
;
8234 /* Attach GLSL vshader */
8237 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id
, program_id
);
8238 GL_EXTCALL(glAttachShader(program_id
, vs_id
));
8239 checkGLcall("glAttachShader");
8241 list_add_head(vs_list
, &entry
->vs
.shader_entry
);
8246 attribs_map
= vshader
->reg_maps
.input_registers
;
8247 if (vshader
->reg_maps
.shader_version
.major
< 4)
8249 reorder_shader_id
= shader_glsl_generate_vs3_rasterizer_input_setup(priv
, vshader
, pshader
,
8250 state
->gl_primitive_type
== GL_POINTS
&& vshader
->reg_maps
.point_size
,
8251 d3d_info
->emulated_flatshading
8252 && state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
, gl_info
);
8253 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id
, program_id
);
8254 GL_EXTCALL(glAttachShader(program_id
, reorder_shader_id
));
8255 checkGLcall("glAttachShader");
8256 /* Flag the reorder function for deletion, it will be freed
8257 * automatically when the program is destroyed. */
8258 GL_EXTCALL(glDeleteShader(reorder_shader_id
));
8263 attribs_map
= (1u << WINED3D_FFP_ATTRIBS_COUNT
) - 1;
8266 if (!shader_glsl_use_explicit_attrib_location(gl_info
))
8268 /* Bind vertex attributes to a corresponding index number to match
8269 * the same index numbers as ARB_vertex_programs (makes loading
8270 * vertex attributes simpler). With this method, we can use the
8271 * exact same code to load the attributes later for both ARB and
8274 * We have to do this here because we need to know the Program ID
8275 * in order to make the bindings work, and it has to be done prior
8276 * to linking the GLSL program. */
8277 tmp_name
= string_buffer_get(&priv
->string_buffers
);
8278 for (i
= 0; attribs_map
; attribs_map
>>= 1, ++i
)
8280 if (!(attribs_map
& 1))
8283 string_buffer_sprintf(tmp_name
, "vs_in%u", i
);
8284 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
8285 if (vshader
&& vshader
->reg_maps
.shader_version
.major
>= 4)
8287 string_buffer_sprintf(tmp_name
, "vs_in_uint%u", i
);
8288 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
8289 string_buffer_sprintf(tmp_name
, "vs_in_int%u", i
);
8290 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
8293 checkGLcall("glBindAttribLocation");
8294 string_buffer_release(&priv
->string_buffers
, tmp_name
);
8299 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id
, program_id
);
8300 GL_EXTCALL(glAttachShader(program_id
, gs_id
));
8301 checkGLcall("glAttachShader");
8303 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
8305 TRACE("input type %s, output type %s, vertices out %u.\n",
8306 debug_d3dprimitivetype(gshader
->u
.gs
.input_type
),
8307 debug_d3dprimitivetype(gshader
->u
.gs
.output_type
),
8308 gshader
->u
.gs
.vertices_out
);
8309 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_INPUT_TYPE_ARB
,
8310 gl_primitive_type_from_d3d(gshader
->u
.gs
.input_type
)));
8311 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_OUTPUT_TYPE_ARB
,
8312 gl_primitive_type_from_d3d(gshader
->u
.gs
.output_type
)));
8313 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_VERTICES_OUT_ARB
,
8314 gshader
->u
.gs
.vertices_out
));
8315 checkGLcall("glProgramParameteriARB");
8318 list_add_head(&gshader
->linked_programs
, &entry
->gs
.shader_entry
);
8321 /* Attach GLSL pshader */
8324 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id
, program_id
);
8325 GL_EXTCALL(glAttachShader(program_id
, ps_id
));
8326 checkGLcall("glAttachShader");
8328 list_add_head(ps_list
, &entry
->ps
.shader_entry
);
8331 /* Link the program */
8332 TRACE("Linking GLSL shader program %u.\n", program_id
);
8333 GL_EXTCALL(glLinkProgram(program_id
));
8334 shader_glsl_validate_link(gl_info
, program_id
);
8336 shader_glsl_init_vs_uniform_locations(gl_info
, priv
, program_id
, &entry
->vs
,
8337 vshader
? vshader
->limits
->constant_float
: 0);
8338 shader_glsl_init_gs_uniform_locations(gl_info
, priv
, program_id
, &entry
->gs
);
8339 shader_glsl_init_ps_uniform_locations(gl_info
, priv
, program_id
, &entry
->ps
,
8340 pshader
? pshader
->limits
->constant_float
: 0);
8341 checkGLcall("Find glsl program uniform locations");
8343 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
8345 if (pshader
&& pshader
->reg_maps
.shader_version
.major
>= 3
8346 && pshader
->u
.ps
.declared_in_count
> vec4_varyings(3, gl_info
))
8348 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id
);
8349 entry
->vs
.vertex_color_clamp
= GL_FALSE
;
8353 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
8358 /* With core profile we never change vertex_color_clamp from
8359 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
8360 * glClampColorARB(). */
8361 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
8364 /* Set the shader to allow uniform loading on it */
8365 GL_EXTCALL(glUseProgram(program_id
));
8366 checkGLcall("glUseProgram");
8368 /* Texture unit mapping is set up to be the same each time the shader
8369 * program is used so we can hardcode the sampler uniform values. */
8370 shader_glsl_load_samplers(gl_info
, priv
, context
->tex_unit_map
, program_id
);
8372 entry
->constant_update_mask
= 0;
8375 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
8376 if (vshader
->reg_maps
.integer_constants
)
8377 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_I
;
8378 if (vshader
->reg_maps
.boolean_constants
)
8379 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_B
;
8380 if (entry
->vs
.pos_fixup_location
!= -1)
8381 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
8383 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &vshader
->reg_maps
);
8384 shader_glsl_load_icb(gl_info
, priv
, program_id
, &vshader
->reg_maps
);
8388 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
8389 | WINED3D_SHADER_CONST_FFP_PROJ
;
8391 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
8393 if (entry
->vs
.modelview_matrix_location
[i
] != -1)
8395 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
8400 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
8402 if (entry
->vs
.texture_matrix_location
[i
] != -1)
8404 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
8408 if (entry
->vs
.material_ambient_location
!= -1 || entry
->vs
.material_diffuse_location
!= -1
8409 || entry
->vs
.material_specular_location
!= -1
8410 || entry
->vs
.material_emissive_location
!= -1
8411 || entry
->vs
.material_shininess_location
!= -1)
8412 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
8413 if (entry
->vs
.light_ambient_location
!= -1)
8414 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
8416 if (entry
->vs
.clip_planes_location
!= -1)
8417 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
8418 if (entry
->vs
.pointsize_min_location
!= -1)
8419 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
8423 if (entry
->gs
.pos_fixup_location
!= -1)
8424 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
8425 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &gshader
->reg_maps
);
8426 shader_glsl_load_icb(gl_info
, priv
, program_id
, &gshader
->reg_maps
);
8433 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
8434 if (pshader
->reg_maps
.integer_constants
)
8435 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_I
;
8436 if (pshader
->reg_maps
.boolean_constants
)
8437 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_B
;
8438 if (entry
->ps
.ycorrection_location
!= -1)
8439 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
8441 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &pshader
->reg_maps
);
8442 shader_glsl_load_icb(gl_info
, priv
, program_id
, &pshader
->reg_maps
);
8443 shader_glsl_load_images(gl_info
, priv
, program_id
, &pshader
->reg_maps
);
8447 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
8450 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
8452 if (entry
->ps
.bumpenv_mat_location
[i
] != -1)
8454 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_BUMP_ENV
;
8459 if (entry
->ps
.fog_color_location
!= -1)
8460 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
8461 if (entry
->ps
.alpha_test_ref_location
!= -1)
8462 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_ALPHA_TEST
;
8463 if (entry
->ps
.np2_fixup_location
!= -1)
8464 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_NP2_FIXUP
;
8465 if (entry
->ps
.color_key_location
!= -1)
8466 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
8470 /* Context activation is done by the caller. */
8471 static void shader_glsl_select(void *shader_priv
, struct wined3d_context
*context
,
8472 const struct wined3d_state
*state
)
8474 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
8475 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8476 struct shader_glsl_priv
*priv
= shader_priv
;
8477 GLuint program_id
= 0, prev_id
= 0;
8478 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
8480 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
8481 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
8483 if (ctx_data
->glsl_program
)
8485 prev_id
= ctx_data
->glsl_program
->id
;
8486 old_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
8491 old_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
8494 set_glsl_shader_program(context
, state
, priv
, ctx_data
);
8496 if (ctx_data
->glsl_program
)
8498 program_id
= ctx_data
->glsl_program
->id
;
8499 current_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
8504 current_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
8507 if (old_vertex_color_clamp
!= current_vertex_color_clamp
)
8509 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
8511 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
8512 checkGLcall("glClampColorARB");
8516 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
8520 TRACE("Using GLSL program %u.\n", program_id
);
8522 if (prev_id
!= program_id
)
8524 GL_EXTCALL(glUseProgram(program_id
));
8525 checkGLcall("glUseProgram");
8528 context
->constant_update_mask
|= ctx_data
->glsl_program
->constant_update_mask
;
8532 /* Context activation is done by the caller. */
8533 static void shader_glsl_select_compute(void *shader_priv
, struct wined3d_context
*context
,
8534 const struct wined3d_state
*state
)
8536 FIXME("Compute pipeline not supported yet.\n");
8539 /* "context" is not necessarily the currently active context. */
8540 static void shader_glsl_invalidate_current_program(struct wined3d_context
*context
)
8542 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
8544 ctx_data
->glsl_program
= NULL
;
8545 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
8546 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
8547 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
8548 | (1u << WINED3D_SHADER_TYPE_HULL
)
8549 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
8550 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
8553 /* Context activation is done by the caller. */
8554 static void shader_glsl_disable(void *shader_priv
, struct wined3d_context
*context
)
8556 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8557 struct shader_glsl_priv
*priv
= shader_priv
;
8559 shader_glsl_invalidate_current_program(context
);
8560 GL_EXTCALL(glUseProgram(0));
8561 checkGLcall("glUseProgram");
8563 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
8564 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
8566 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
8568 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, GL_FIXED_ONLY_ARB
));
8569 checkGLcall("glClampColorARB");
8573 static void shader_glsl_invalidate_contexts_program(struct wined3d_device
*device
,
8574 const struct glsl_shader_prog_link
*program
)
8576 const struct glsl_context_data
*ctx_data
;
8577 struct wined3d_context
*context
;
8580 for (i
= 0; i
< device
->context_count
; ++i
)
8582 context
= device
->contexts
[i
];
8583 ctx_data
= context
->shader_backend_data
;
8585 if (ctx_data
->glsl_program
== program
)
8586 shader_glsl_invalidate_current_program(context
);
8590 static void shader_glsl_destroy(struct wined3d_shader
*shader
)
8592 struct glsl_shader_private
*shader_data
= shader
->backend_data
;
8593 struct wined3d_device
*device
= shader
->device
;
8594 struct shader_glsl_priv
*priv
= device
->shader_priv
;
8595 const struct wined3d_gl_info
*gl_info
;
8596 const struct list
*linked_programs
;
8597 struct wined3d_context
*context
;
8599 if (!shader_data
|| !shader_data
->num_gl_shaders
)
8601 HeapFree(GetProcessHeap(), 0, shader_data
);
8602 shader
->backend_data
= NULL
;
8606 context
= context_acquire(device
, NULL
);
8607 gl_info
= context
->gl_info
;
8609 TRACE("Deleting linked programs.\n");
8610 linked_programs
= &shader
->linked_programs
;
8611 if (linked_programs
->next
)
8613 struct glsl_shader_prog_link
*entry
, *entry2
;
8616 switch (shader
->reg_maps
.shader_version
.type
)
8618 case WINED3D_SHADER_TYPE_PIXEL
:
8620 struct glsl_ps_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.ps
;
8622 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8624 TRACE("Deleting pixel shader %u.\n", gl_shaders
[i
].id
);
8625 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
8626 checkGLcall("glDeleteShader");
8628 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
);
8630 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
8631 struct glsl_shader_prog_link
, ps
.shader_entry
)
8633 shader_glsl_invalidate_contexts_program(device
, entry
);
8634 delete_glsl_program_entry(priv
, gl_info
, entry
);
8640 case WINED3D_SHADER_TYPE_VERTEX
:
8642 struct glsl_vs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.vs
;
8644 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8646 TRACE("Deleting vertex shader %u.\n", gl_shaders
[i
].id
);
8647 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
8648 checkGLcall("glDeleteShader");
8650 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
);
8652 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
8653 struct glsl_shader_prog_link
, vs
.shader_entry
)
8655 shader_glsl_invalidate_contexts_program(device
, entry
);
8656 delete_glsl_program_entry(priv
, gl_info
, entry
);
8662 case WINED3D_SHADER_TYPE_GEOMETRY
:
8664 struct glsl_gs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.gs
;
8666 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8668 TRACE("Deleting geometry shader %u.\n", gl_shaders
[i
].id
);
8669 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
8670 checkGLcall("glDeleteShader");
8672 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
);
8674 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
8675 struct glsl_shader_prog_link
, gs
.shader_entry
)
8677 shader_glsl_invalidate_contexts_program(device
, entry
);
8678 delete_glsl_program_entry(priv
, gl_info
, entry
);
8685 ERR("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
8690 HeapFree(GetProcessHeap(), 0, shader
->backend_data
);
8691 shader
->backend_data
= NULL
;
8693 context_release(context
);
8696 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
8698 const struct glsl_program_key
*k
= key
;
8699 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
8700 const struct glsl_shader_prog_link
, program_lookup_entry
);
8702 if (k
->vs_id
> prog
->vs
.id
) return 1;
8703 else if (k
->vs_id
< prog
->vs
.id
) return -1;
8705 if (k
->gs_id
> prog
->gs
.id
) return 1;
8706 else if (k
->gs_id
< prog
->gs
.id
) return -1;
8708 if (k
->ps_id
> prog
->ps
.id
) return 1;
8709 else if (k
->ps_id
< prog
->ps
.id
) return -1;
8714 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
8716 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
)
8717 + constant_count
* sizeof(*heap
->contained
)
8718 + constant_count
* sizeof(*heap
->positions
);
8719 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
8723 ERR("Failed to allocate memory\n");
8727 heap
->entries
= mem
;
8728 heap
->entries
[1].version
= 0;
8729 heap
->contained
= (BOOL
*)(heap
->entries
+ constant_count
+ 1);
8730 memset(heap
->contained
, 0, constant_count
* sizeof(*heap
->contained
));
8731 heap
->positions
= (unsigned int *)(heap
->contained
+ constant_count
);
8737 static void constant_heap_free(struct constant_heap
*heap
)
8739 HeapFree(GetProcessHeap(), 0, heap
->entries
);
8742 static HRESULT
shader_glsl_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
8743 const struct fragment_pipeline
*fragment_pipe
)
8745 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
8746 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
8747 SIZE_T stack_size
= wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F
, WINED3D_MAX_PS_CONSTS_F
)) + 1;
8748 struct fragment_caps fragment_caps
;
8749 void *vertex_priv
, *fragment_priv
;
8751 string_buffer_list_init(&priv
->string_buffers
);
8753 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&glsl_shader_backend
, priv
)))
8755 ERR("Failed to initialize vertex pipe.\n");
8756 HeapFree(GetProcessHeap(), 0, priv
);
8760 if (!(fragment_priv
= fragment_pipe
->alloc_private(&glsl_shader_backend
, priv
)))
8762 ERR("Failed to initialize fragment pipe.\n");
8763 vertex_pipe
->vp_free(device
);
8764 HeapFree(GetProcessHeap(), 0, priv
);
8768 if (!string_buffer_init(&priv
->shader_buffer
))
8770 ERR("Failed to initialize shader buffer.\n");
8774 if (!(priv
->stack
= wined3d_calloc(stack_size
, sizeof(*priv
->stack
))))
8776 ERR("Failed to allocate memory.\n");
8780 if (!constant_heap_init(&priv
->vconst_heap
, WINED3D_MAX_VS_CONSTS_F
))
8782 ERR("Failed to initialize vertex shader constant heap\n");
8786 if (!constant_heap_init(&priv
->pconst_heap
, WINED3D_MAX_PS_CONSTS_F
))
8788 ERR("Failed to initialize pixel shader constant heap\n");
8792 wine_rb_init(&priv
->program_lookup
, glsl_program_key_compare
);
8794 priv
->next_constant_version
= 1;
8795 priv
->vertex_pipe
= vertex_pipe
;
8796 priv
->fragment_pipe
= fragment_pipe
;
8797 fragment_pipe
->get_caps(gl_info
, &fragment_caps
);
8798 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
8799 priv
->legacy_lighting
= device
->wined3d
->flags
& WINED3D_LEGACY_FFP_LIGHTING
;
8801 device
->vertex_priv
= vertex_priv
;
8802 device
->fragment_priv
= fragment_priv
;
8803 device
->shader_priv
= priv
;
8808 constant_heap_free(&priv
->pconst_heap
);
8809 constant_heap_free(&priv
->vconst_heap
);
8810 HeapFree(GetProcessHeap(), 0, priv
->stack
);
8811 string_buffer_free(&priv
->shader_buffer
);
8812 fragment_pipe
->free_private(device
);
8813 vertex_pipe
->vp_free(device
);
8814 HeapFree(GetProcessHeap(), 0, priv
);
8815 return E_OUTOFMEMORY
;
8818 /* Context activation is done by the caller. */
8819 static void shader_glsl_free(struct wined3d_device
*device
)
8821 struct shader_glsl_priv
*priv
= device
->shader_priv
;
8823 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
8824 constant_heap_free(&priv
->pconst_heap
);
8825 constant_heap_free(&priv
->vconst_heap
);
8826 HeapFree(GetProcessHeap(), 0, priv
->stack
);
8827 string_buffer_list_cleanup(&priv
->string_buffers
);
8828 string_buffer_free(&priv
->shader_buffer
);
8829 priv
->fragment_pipe
->free_private(device
);
8830 priv
->vertex_pipe
->vp_free(device
);
8832 HeapFree(GetProcessHeap(), 0, device
->shader_priv
);
8833 device
->shader_priv
= NULL
;
8836 static BOOL
shader_glsl_allocate_context_data(struct wined3d_context
*context
)
8838 return !!(context
->shader_backend_data
= HeapAlloc(GetProcessHeap(),
8839 HEAP_ZERO_MEMORY
, sizeof(struct glsl_context_data
)));
8842 static void shader_glsl_free_context_data(struct wined3d_context
*context
)
8844 HeapFree(GetProcessHeap(), 0, context
->shader_backend_data
);
8847 static void shader_glsl_init_context_state(struct wined3d_context
*context
)
8849 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8851 gl_info
->gl_ops
.gl
.p_glEnable(GL_PROGRAM_POINT_SIZE
);
8852 checkGLcall("GL_PROGRAM_POINT_SIZE");
8855 static void shader_glsl_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
8859 /* FIXME: Check for the specific extensions required for SM5 support
8860 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8861 * soon as we introduce them, adjusting the GL / GLSL version checks
8863 if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(4, 30) && gl_info
->supported
[WINED3D_GL_VERSION_4_3
]
8864 && gl_info
->supported
[ARB_COMPUTE_SHADER
]
8865 && gl_info
->supported
[ARB_DERIVATIVE_CONTROL
]
8866 && gl_info
->supported
[ARB_GPU_SHADER5
]
8867 && gl_info
->supported
[ARB_SHADER_IMAGE_LOAD_STORE
]
8868 && gl_info
->supported
[ARB_SHADER_IMAGE_SIZE
]
8869 && gl_info
->supported
[ARB_SHADING_LANGUAGE_PACKING
])
8871 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 50) && gl_info
->supported
[WINED3D_GL_VERSION_3_2
]
8872 && gl_info
->supported
[ARB_SHADER_BIT_ENCODING
] && gl_info
->supported
[ARB_SAMPLER_OBJECTS
]
8873 && gl_info
->supported
[ARB_TEXTURE_SWIZZLE
])
8875 /* Support for texldd and texldl instructions in pixel shaders is required
8877 else if (shader_glsl_has_core_grad(gl_info
, NULL
) || gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
8881 TRACE("Shader model %u.\n", shader_model
);
8883 caps
->vs_version
= min(wined3d_settings
.max_sm_vs
, shader_model
);
8884 caps
->hs_version
= min(wined3d_settings
.max_sm_hs
, shader_model
);
8885 caps
->ds_version
= min(wined3d_settings
.max_sm_ds
, shader_model
);
8886 caps
->gs_version
= min(wined3d_settings
.max_sm_gs
, shader_model
);
8887 caps
->ps_version
= min(wined3d_settings
.max_sm_ps
, shader_model
);
8888 caps
->cs_version
= min(wined3d_settings
.max_sm_cs
, shader_model
);
8890 caps
->vs_uniform_count
= min(WINED3D_MAX_VS_CONSTS_F
, gl_info
->limits
.glsl_vs_float_constants
);
8891 caps
->ps_uniform_count
= min(WINED3D_MAX_PS_CONSTS_F
, gl_info
->limits
.glsl_ps_float_constants
);
8892 caps
->varying_count
= gl_info
->limits
.glsl_varyings
;
8894 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8895 * Direct3D minimum requirement.
8897 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8898 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8900 * The problem is that the refrast clamps temporary results in the shader to
8901 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8902 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8903 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8904 * offer a way to query this.
8906 if (shader_model
>= 4)
8907 caps
->ps_1x_max_value
= FLT_MAX
;
8909 caps
->ps_1x_max_value
= 1024.0f
;
8911 /* Ideally we'd only set caps like sRGB writes here if supported by both
8912 * the shader backend and the fragment pipe, but we can get called before
8913 * shader_glsl_alloc(). */
8914 caps
->wined3d_caps
= WINED3D_SHADER_CAP_VS_CLIPPING
8915 | WINED3D_SHADER_CAP_SRGB_WRITE
;
8918 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
8920 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
8922 TRACE("Checking support for fixup:\n");
8923 dump_color_fixup_desc(fixup
);
8926 /* We support everything except YUV conversions. */
8927 if (!is_complex_fixup(fixup
))
8933 TRACE("[FAILED]\n");
8937 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
8939 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
8940 /* WINED3DSIH_ADD */ shader_glsl_binop
,
8941 /* WINED3DSIH_AND */ shader_glsl_binop
,
8942 /* WINED3DSIH_ATOMIC_AND */ NULL
,
8943 /* WINED3DSIH_ATOMIC_CMP_STORE */ NULL
,
8944 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic
,
8945 /* WINED3DSIH_ATOMIC_IMAX */ NULL
,
8946 /* WINED3DSIH_ATOMIC_IMIN */ NULL
,
8947 /* WINED3DSIH_ATOMIC_OR */ NULL
,
8948 /* WINED3DSIH_ATOMIC_UMAX */ NULL
,
8949 /* WINED3DSIH_ATOMIC_UMIN */ NULL
,
8950 /* WINED3DSIH_ATOMIC_XOR */ NULL
,
8951 /* WINED3DSIH_BEM */ shader_glsl_bem
,
8952 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op
,
8953 /* WINED3DSIH_BFREV */ shader_glsl_map2gl
,
8954 /* WINED3DSIH_BREAK */ shader_glsl_break
,
8955 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
8956 /* WINED3DSIH_BREAKP */ shader_glsl_breakp
,
8957 /* WINED3DSIH_BUFINFO */ NULL
,
8958 /* WINED3DSIH_CALL */ shader_glsl_call
,
8959 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
8960 /* WINED3DSIH_CASE */ shader_glsl_case
,
8961 /* WINED3DSIH_CMP */ shader_glsl_conditional_move
,
8962 /* WINED3DSIH_CND */ shader_glsl_cnd
,
8963 /* WINED3DSIH_CONTINUE */ shader_glsl_continue
,
8964 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl
,
8965 /* WINED3DSIH_CRS */ shader_glsl_cross
,
8966 /* WINED3DSIH_CUT */ shader_glsl_cut
,
8967 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut
,
8968 /* WINED3DSIH_DCL */ shader_glsl_nop
,
8969 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop
,
8970 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL
,
8971 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL
,
8972 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop
,
8973 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL
,
8974 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL
,
8975 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop
,
8976 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop
,
8977 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop
,
8978 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL
,
8979 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop
,
8980 /* WINED3DSIH_DCL_INPUT_PS */ NULL
,
8981 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL
,
8982 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL
,
8983 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop
,
8984 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop
,
8985 /* WINED3DSIH_DCL_INTERFACE */ NULL
,
8986 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop
,
8987 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL
,
8988 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop
,
8989 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop
,
8990 /* WINED3DSIH_DCL_RESOURCE_RAW */ NULL
,
8991 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL
,
8992 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop
,
8993 /* WINED3DSIH_DCL_STREAM */ NULL
,
8994 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop
,
8995 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL
,
8996 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL
,
8997 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL
,
8998 /* WINED3DSIH_DCL_TGSM_RAW */ NULL
,
8999 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL
,
9000 /* WINED3DSIH_DCL_THREAD_GROUP */ NULL
,
9001 /* WINED3DSIH_DCL_UAV_RAW */ NULL
,
9002 /* WINED3DSIH_DCL_UAV_STRUCTURED */ NULL
,
9003 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop
,
9004 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop
,
9005 /* WINED3DSIH_DEF */ shader_glsl_nop
,
9006 /* WINED3DSIH_DEFAULT */ shader_glsl_default
,
9007 /* WINED3DSIH_DEFB */ shader_glsl_nop
,
9008 /* WINED3DSIH_DEFI */ shader_glsl_nop
,
9009 /* WINED3DSIH_DIV */ shader_glsl_binop
,
9010 /* WINED3DSIH_DP2 */ shader_glsl_dot
,
9011 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add
,
9012 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
9013 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
9014 /* WINED3DSIH_DST */ shader_glsl_dst
,
9015 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
9016 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl
,
9017 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl
,
9018 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
9019 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl
,
9020 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl
,
9021 /* WINED3DSIH_ELSE */ shader_glsl_else
,
9022 /* WINED3DSIH_EMIT */ shader_glsl_emit
,
9023 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit
,
9024 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
9025 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
9026 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
9027 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end
,
9028 /* WINED3DSIH_EQ */ shader_glsl_relop
,
9029 /* WINED3DSIH_EXP */ shader_glsl_scalar_op
,
9030 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
9031 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16
,
9032 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16
,
9033 /* WINED3DSIH_FCALL */ NULL
,
9034 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl
,
9035 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl
,
9036 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl
,
9037 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
9038 /* WINED3DSIH_FTOI */ shader_glsl_to_int
,
9039 /* WINED3DSIH_FTOU */ shader_glsl_to_uint
,
9040 /* WINED3DSIH_GATHER4 */ NULL
,
9041 /* WINED3DSIH_GATHER4_C */ NULL
,
9042 /* WINED3DSIH_GE */ shader_glsl_relop
,
9043 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL
,
9044 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop
,
9045 /* WINED3DSIH_HS_FORK_PHASE */ NULL
,
9046 /* WINED3DSIH_HS_JOIN_PHASE */ NULL
,
9047 /* WINED3DSIH_IADD */ shader_glsl_binop
,
9048 /* WINED3DSIH_IEQ */ shader_glsl_relop
,
9049 /* WINED3DSIH_IF */ shader_glsl_if
,
9050 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
9051 /* WINED3DSIH_IGE */ shader_glsl_relop
,
9052 /* WINED3DSIH_ILT */ shader_glsl_relop
,
9053 /* WINED3DSIH_IMAD */ shader_glsl_mad
,
9054 /* WINED3DSIH_IMAX */ shader_glsl_map2gl
,
9055 /* WINED3DSIH_IMIN */ shader_glsl_map2gl
,
9056 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL
,
9057 /* WINED3DSIH_IMM_ATOMIC_AND */ NULL
,
9058 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ NULL
,
9059 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL
,
9060 /* WINED3DSIH_IMM_ATOMIC_EXCH */ NULL
,
9061 /* WINED3DSIH_IMM_ATOMIC_IADD */ NULL
,
9062 /* WINED3DSIH_IMM_ATOMIC_OR */ NULL
,
9063 /* WINED3DSIH_IMM_ATOMIC_UMAX */ NULL
,
9064 /* WINED3DSIH_IMM_ATOMIC_UMIN */ NULL
,
9065 /* WINED3DSIH_IMM_ATOMIC_XOR */ NULL
,
9066 /* WINED3DSIH_IMUL */ shader_glsl_imul
,
9067 /* WINED3DSIH_INE */ shader_glsl_relop
,
9068 /* WINED3DSIH_INEG */ shader_glsl_unary_op
,
9069 /* WINED3DSIH_ISHL */ shader_glsl_binop
,
9070 /* WINED3DSIH_ISHR */ shader_glsl_binop
,
9071 /* WINED3DSIH_ITOF */ shader_glsl_to_float
,
9072 /* WINED3DSIH_LABEL */ shader_glsl_label
,
9073 /* WINED3DSIH_LD */ shader_glsl_ld
,
9074 /* WINED3DSIH_LD2DMS */ NULL
,
9075 /* WINED3DSIH_LD_RAW */ NULL
,
9076 /* WINED3DSIH_LD_STRUCTURED */ NULL
,
9077 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav
,
9078 /* WINED3DSIH_LIT */ shader_glsl_lit
,
9079 /* WINED3DSIH_LOD */ NULL
,
9080 /* WINED3DSIH_LOG */ shader_glsl_scalar_op
,
9081 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op
,
9082 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
9083 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
9084 /* WINED3DSIH_LT */ shader_glsl_relop
,
9085 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
9086 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
9087 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
9088 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
9089 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
9090 /* WINED3DSIH_MAD */ shader_glsl_mad
,
9091 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
9092 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
9093 /* WINED3DSIH_MOV */ shader_glsl_mov
,
9094 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
9095 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move
,
9096 /* WINED3DSIH_MUL */ shader_glsl_binop
,
9097 /* WINED3DSIH_NE */ shader_glsl_relop
,
9098 /* WINED3DSIH_NOP */ shader_glsl_nop
,
9099 /* WINED3DSIH_NOT */ shader_glsl_unary_op
,
9100 /* WINED3DSIH_NRM */ shader_glsl_nrm
,
9101 /* WINED3DSIH_OR */ shader_glsl_binop
,
9102 /* WINED3DSIH_PHASE */ shader_glsl_nop
,
9103 /* WINED3DSIH_POW */ shader_glsl_pow
,
9104 /* WINED3DSIH_RCP */ shader_glsl_scalar_op
,
9105 /* WINED3DSIH_REP */ shader_glsl_rep
,
9106 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo
,
9107 /* WINED3DSIH_RET */ shader_glsl_ret
,
9108 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl
,
9109 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl
,
9110 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl
,
9111 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl
,
9112 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op
,
9113 /* WINED3DSIH_SAMPLE */ shader_glsl_sample
,
9114 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample
,
9115 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c
,
9116 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c
,
9117 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample
,
9118 /* WINED3DSIH_SAMPLE_INFO */ NULL
,
9119 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample
,
9120 /* WINED3DSIH_SAMPLE_POS */ NULL
,
9121 /* WINED3DSIH_SETP */ NULL
,
9122 /* WINED3DSIH_SGE */ shader_glsl_compare
,
9123 /* WINED3DSIH_SGN */ shader_glsl_sgn
,
9124 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
9125 /* WINED3DSIH_SLT */ shader_glsl_compare
,
9126 /* WINED3DSIH_SQRT */ shader_glsl_map2gl
,
9127 /* WINED3DSIH_STORE_RAW */ NULL
,
9128 /* WINED3DSIH_STORE_STRUCTURED */ NULL
,
9129 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav
,
9130 /* WINED3DSIH_SUB */ shader_glsl_binop
,
9131 /* WINED3DSIH_SWAPC */ NULL
,
9132 /* WINED3DSIH_SWITCH */ shader_glsl_switch
,
9133 /* WINED3DSIH_SYNC */ NULL
,
9134 /* WINED3DSIH_TEX */ shader_glsl_tex
,
9135 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem
,
9136 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem
,
9137 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord
,
9138 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth
,
9139 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3
,
9140 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex
,
9141 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill
,
9142 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
9143 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
9144 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth
,
9145 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad
,
9146 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex
,
9147 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3
,
9148 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
9149 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad
,
9150 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec
,
9151 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex
,
9152 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec
,
9153 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar
,
9154 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb
,
9155 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb
,
9156 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op
,
9157 /* WINED3DSIH_UDIV */ shader_glsl_udiv
,
9158 /* WINED3DSIH_UGE */ shader_glsl_relop
,
9159 /* WINED3DSIH_ULT */ shader_glsl_relop
,
9160 /* WINED3DSIH_UMAX */ shader_glsl_map2gl
,
9161 /* WINED3DSIH_UMIN */ shader_glsl_map2gl
,
9162 /* WINED3DSIH_USHR */ shader_glsl_binop
,
9163 /* WINED3DSIH_UTOF */ shader_glsl_to_float
,
9164 /* WINED3DSIH_XOR */ shader_glsl_binop
,
9167 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
9168 SHADER_HANDLER hw_fct
;
9170 /* Select handler */
9171 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
9173 /* Unhandled opcode */
9176 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
9181 shader_glsl_add_instruction_modifiers(ins
);
9184 static BOOL
shader_glsl_has_ffp_proj_control(void *shader_priv
)
9186 struct shader_glsl_priv
*priv
= shader_priv
;
9188 return priv
->ffp_proj_control
;
9191 const struct wined3d_shader_backend_ops glsl_shader_backend
=
9193 shader_glsl_handle_instruction
,
9195 shader_glsl_select_compute
,
9196 shader_glsl_disable
,
9197 shader_glsl_update_float_vertex_constants
,
9198 shader_glsl_update_float_pixel_constants
,
9199 shader_glsl_load_constants
,
9200 shader_glsl_destroy
,
9203 shader_glsl_allocate_context_data
,
9204 shader_glsl_free_context_data
,
9205 shader_glsl_init_context_state
,
9206 shader_glsl_get_caps
,
9207 shader_glsl_color_fixup_supported
,
9208 shader_glsl_has_ffp_proj_control
,
9211 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
) {}
9213 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info
*gl_info
, struct wined3d_vertex_caps
*caps
)
9215 caps
->xyzrhw
= TRUE
;
9216 caps
->emulated_flatshading
= !gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
9217 caps
->ffp_generic_attributes
= TRUE
;
9218 caps
->max_active_lights
= MAX_ACTIVE_LIGHTS
;
9219 caps
->max_vertex_blend_matrices
= MAX_VERTEX_BLENDS
;
9220 caps
->max_vertex_blend_matrix_index
= 0;
9221 caps
->vertex_processing_caps
= WINED3DVTXPCAPS_TEXGEN
9222 | WINED3DVTXPCAPS_MATERIALSOURCE7
9223 | WINED3DVTXPCAPS_VERTEXFOG
9224 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9225 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9226 | WINED3DVTXPCAPS_LOCALVIEWER
9227 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP
;
9228 caps
->fvf_caps
= WINED3DFVFCAPS_PSIZE
| 8; /* 8 texture coordinates. */
9229 caps
->max_user_clip_planes
= gl_info
->limits
.user_clip_distances
;
9230 caps
->raster_caps
= WINED3DPRASTERCAPS_FOGRANGE
;
9233 static DWORD
glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
9235 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
9236 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
9240 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
9242 struct shader_glsl_priv
*priv
;
9244 if (shader_backend
== &glsl_shader_backend
)
9247 wine_rb_init(&priv
->ffp_vertex_shaders
, wined3d_ffp_vertex_program_key_compare
);
9251 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9256 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry
*entry
, void *context
)
9258 struct glsl_ffp_vertex_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
9259 struct glsl_ffp_vertex_shader
, desc
.entry
);
9260 struct glsl_shader_prog_link
*program
, *program2
;
9261 struct glsl_ffp_destroy_ctx
*ctx
= context
;
9263 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
9264 struct glsl_shader_prog_link
, vs
.shader_entry
)
9266 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
9268 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
9269 HeapFree(GetProcessHeap(), 0, shader
);
9272 /* Context activation is done by the caller. */
9273 static void glsl_vertex_pipe_vp_free(struct wined3d_device
*device
)
9275 struct shader_glsl_priv
*priv
= device
->vertex_priv
;
9276 struct glsl_ffp_destroy_ctx ctx
;
9279 ctx
.gl_info
= &device
->adapter
->gl_info
;
9280 wine_rb_destroy(&priv
->ffp_vertex_shaders
, shader_glsl_free_ffp_vertex_shader
, &ctx
);
9283 static void glsl_vertex_pipe_nop(struct wined3d_context
*context
,
9284 const struct wined3d_state
*state
, DWORD state_id
) {}
9286 static void glsl_vertex_pipe_shader(struct wined3d_context
*context
,
9287 const struct wined3d_state
*state
, DWORD state_id
)
9289 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9292 static void glsl_vertex_pipe_vdecl(struct wined3d_context
*context
,
9293 const struct wined3d_state
*state
, DWORD state_id
)
9295 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9296 BOOL normal
= !!(context
->stream_info
.use_map
& (1u << WINED3D_FFP_NORMAL
));
9297 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
9298 BOOL transformed
= context
->stream_info
.position_transformed
;
9299 BOOL wasrhw
= context
->last_was_rhw
;
9302 context
->last_was_rhw
= transformed
;
9304 /* If the vertex declaration contains a transformed position attribute,
9305 * the draw uses the fixed function vertex pipeline regardless of any
9306 * vertex shader set by the application. */
9307 if (transformed
!= wasrhw
9308 || context
->stream_info
.swizzle_map
!= context
->last_swizzle_map
)
9309 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9311 context
->last_swizzle_map
= context
->stream_info
.swizzle_map
;
9315 if (context
->last_was_vshader
)
9318 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
9319 clipplane(context
, state
, STATE_CLIPPLANE(i
));
9321 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
9324 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
9326 /* Because of settings->texcoords, we have to regenerate the vertex
9327 * shader on a vdecl change if there aren't enough varyings to just
9328 * always output all the texture coordinates. */
9329 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
9330 || normal
!= context
->last_was_normal
)
9331 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9334 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.major
== 1
9335 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.minor
<= 3)
9336 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9340 if (!context
->last_was_vshader
)
9342 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
9344 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
9345 clipplane(context
, state
, STATE_CLIPPLANE(i
));
9347 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
9351 context
->last_was_vshader
= use_vs(state
);
9352 context
->last_was_normal
= normal
;
9355 static void glsl_vertex_pipe_vs(struct wined3d_context
*context
,
9356 const struct wined3d_state
*state
, DWORD state_id
)
9358 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9359 /* Different vertex shaders potentially require a different vertex attributes setup. */
9360 if (!isStateDirty(context
, STATE_VDECL
))
9361 context_apply_state(context
, state
, STATE_VDECL
);
9364 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context
*context
,
9365 const struct wined3d_state
*state
, DWORD state_id
)
9367 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]
9368 && state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.shader_version
.major
>= 4)
9369 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9372 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context
*context
,
9373 const struct wined3d_state
*state
, DWORD state_id
)
9375 if (state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
])
9376 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_GEOMETRY
;
9377 else if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]
9378 && state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.shader_version
.major
>= 4)
9379 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9382 static void glsl_vertex_pipe_world(struct wined3d_context
*context
,
9383 const struct wined3d_state
*state
, DWORD state_id
)
9385 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
;
9388 static void glsl_vertex_pipe_vertexblend(struct wined3d_context
*context
,
9389 const struct wined3d_state
*state
, DWORD state_id
)
9391 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
9394 static void glsl_vertex_pipe_view(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
9396 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9399 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
9400 | WINED3D_SHADER_CONST_FFP_LIGHTS
9401 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
9403 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
9405 for (k
= 0; k
< gl_info
->limits
.user_clip_distances
; ++k
)
9407 if (!isStateDirty(context
, STATE_CLIPPLANE(k
)))
9408 clipplane(context
, state
, STATE_CLIPPLANE(k
));
9413 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
9417 static void glsl_vertex_pipe_projection(struct wined3d_context
*context
,
9418 const struct wined3d_state
*state
, DWORD state_id
)
9420 /* Table fog behavior depends on the projection matrix. */
9421 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
9422 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] != WINED3D_FOG_NONE
)
9423 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9424 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PROJ
;
9427 static void glsl_vertex_pipe_viewport(struct wined3d_context
*context
,
9428 const struct wined3d_state
*state
, DWORD state_id
)
9430 if (!isStateDirty(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
)))
9431 glsl_vertex_pipe_projection(context
, state
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
9432 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
))
9433 && state
->render_states
[WINED3D_RS_POINTSCALEENABLE
])
9434 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
9435 context
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
9438 static void glsl_vertex_pipe_texmatrix(struct wined3d_context
*context
,
9439 const struct wined3d_state
*state
, DWORD state_id
)
9441 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
9444 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context
*context
,
9445 const struct wined3d_state
*state
, DWORD state_id
)
9447 DWORD sampler
= state_id
- STATE_SAMPLER(0);
9448 const struct wined3d_texture
*texture
= state
->textures
[sampler
];
9454 if (sampler
>= MAX_TEXTURES
)
9457 if ((np2
= !(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
9458 || context
->lastWasPow2Texture
& (1u << sampler
))
9461 context
->lastWasPow2Texture
|= 1u << sampler
;
9463 context
->lastWasPow2Texture
&= ~(1u << sampler
);
9465 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
9469 static void glsl_vertex_pipe_material(struct wined3d_context
*context
,
9470 const struct wined3d_state
*state
, DWORD state_id
)
9472 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
9475 static void glsl_vertex_pipe_light(struct wined3d_context
*context
,
9476 const struct wined3d_state
*state
, DWORD state_id
)
9478 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
9481 static void glsl_vertex_pipe_pointsize(struct wined3d_context
*context
,
9482 const struct wined3d_state
*state
, DWORD state_id
)
9484 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
9487 static void glsl_vertex_pipe_pointscale(struct wined3d_context
*context
,
9488 const struct wined3d_state
*state
, DWORD state_id
)
9491 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
9494 static void glsl_vertex_pointsprite_core(struct wined3d_context
*context
,
9495 const struct wined3d_state
*state
, DWORD state_id
)
9497 static unsigned int once
;
9499 if (state
->gl_primitive_type
== GL_POINTS
&& !state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
] && !once
++)
9500 FIXME("Non-point sprite points not supported in core profile.\n");
9503 static void glsl_vertex_pipe_shademode(struct wined3d_context
*context
,
9504 const struct wined3d_state
*state
, DWORD state_id
)
9506 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9509 static void glsl_vertex_pipe_clip_plane(struct wined3d_context
*context
,
9510 const struct wined3d_state
*state
, DWORD state_id
)
9512 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9513 UINT index
= state_id
- STATE_CLIPPLANE(0);
9515 if (index
>= gl_info
->limits
.user_clip_distances
)
9518 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
9521 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states
[] =
9523 {STATE_VDECL
, {STATE_VDECL
, glsl_vertex_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
9524 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_vertex_pipe_vs
}, WINED3D_GL_EXT_NONE
},
9525 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY
), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY
), glsl_vertex_pipe_geometry_shader
}, WINED3D_GL_EXT_NONE
},
9526 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_vertex_pipe_pixel_shader
}, WINED3D_GL_EXT_NONE
},
9527 {STATE_MATERIAL
, {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9528 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_vertex_pipe_material
}, WINED3D_GL_EXT_NONE
},
9530 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9531 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9532 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9533 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9534 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9535 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9536 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9537 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9538 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9539 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9540 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9541 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9542 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9543 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9544 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9545 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9546 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9547 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9548 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9549 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9550 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9551 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9552 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9553 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9554 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9555 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9556 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9557 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9558 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9559 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9560 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9561 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9562 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9563 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9564 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9565 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9566 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9567 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9568 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9569 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9570 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9571 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9572 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9573 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9574 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9575 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9576 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9577 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9578 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9579 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9580 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9581 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9582 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9583 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9584 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9585 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9586 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9587 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9588 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9589 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9590 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9591 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9592 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9593 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9595 {STATE_LIGHT_TYPE
, {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9596 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9597 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9598 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9599 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9600 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9601 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9602 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9603 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9605 {STATE_VIEWPORT
, {STATE_VIEWPORT
, glsl_vertex_pipe_viewport
}, WINED3D_GL_EXT_NONE
},
9606 /* Transform states */
9607 {STATE_TRANSFORM(WINED3D_TS_VIEW
), {STATE_TRANSFORM(WINED3D_TS_VIEW
), glsl_vertex_pipe_view
}, WINED3D_GL_EXT_NONE
},
9608 {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), glsl_vertex_pipe_projection
}, WINED3D_GL_EXT_NONE
},
9609 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9610 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9611 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9612 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9613 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9614 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9615 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9616 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9617 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world
}, WINED3D_GL_EXT_NONE
},
9618 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
9619 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
9620 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
9621 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9622 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9623 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9624 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9625 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9626 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9627 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9628 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9629 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9630 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9631 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9632 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9633 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9634 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9635 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9636 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9638 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
9639 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9640 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9641 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9642 {STATE_RENDER(WINED3D_RS_CLIPPING
), {STATE_RENDER(WINED3D_RS_CLIPPING
), state_clipping
}, WINED3D_GL_EXT_NONE
},
9643 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE
), {STATE_RENDER(WINED3D_RS_CLIPPING
), NULL
}, WINED3D_GL_EXT_NONE
},
9644 {STATE_RENDER(WINED3D_RS_LIGHTING
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9645 {STATE_RENDER(WINED3D_RS_AMBIENT
), {STATE_RENDER(WINED3D_RS_AMBIENT
), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9646 {STATE_RENDER(WINED3D_RS_COLORVERTEX
), {STATE_RENDER(WINED3D_RS_COLORVERTEX
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
9647 {STATE_RENDER(WINED3D_RS_LOCALVIEWER
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9648 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9649 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9650 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9651 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9652 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9653 {STATE_RENDER(WINED3D_RS_VERTEXBLEND
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9654 {STATE_RENDER(WINED3D_RS_POINTSIZE
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
9655 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), glsl_vertex_pipe_pointsize
}, WINED3D_GL_EXT_NONE
},
9656 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite
}, ARB_POINT_SPRITE
},
9657 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite_w
}, WINED3D_GL_LEGACY_CONTEXT
},
9658 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_vertex_pointsprite_core
}, WINED3D_GL_EXT_NONE
},
9659 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), glsl_vertex_pipe_pointscale
}, WINED3D_GL_EXT_NONE
},
9660 {STATE_RENDER(WINED3D_RS_POINTSCALE_A
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9661 {STATE_RENDER(WINED3D_RS_POINTSCALE_B
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9662 {STATE_RENDER(WINED3D_RS_POINTSCALE_C
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9663 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
9664 {STATE_RENDER(WINED3D_RS_TWEENFACTOR
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9665 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9666 /* NP2 texture matrix fixups. They are not needed if
9667 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
9668 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
9670 {STATE_SAMPLER(0), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9671 {STATE_SAMPLER(0), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9672 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9673 {STATE_SAMPLER(1), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9674 {STATE_SAMPLER(1), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9675 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9676 {STATE_SAMPLER(2), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9677 {STATE_SAMPLER(2), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9678 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9679 {STATE_SAMPLER(3), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9680 {STATE_SAMPLER(3), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9681 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9682 {STATE_SAMPLER(4), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9683 {STATE_SAMPLER(4), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9684 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9685 {STATE_SAMPLER(5), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9686 {STATE_SAMPLER(5), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9687 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9688 {STATE_SAMPLER(6), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9689 {STATE_SAMPLER(6), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9690 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9691 {STATE_SAMPLER(7), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9692 {STATE_SAMPLER(7), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9693 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9694 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
9695 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_nop
}, WINED3D_GL_LEGACY_CONTEXT
},
9696 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_shademode
}, WINED3D_GL_EXT_NONE
},
9697 {0 /* Terminate */, {0, NULL
}, WINED3D_GL_EXT_NONE
},
9701 * - Implement vertex tweening. */
9702 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe
=
9704 glsl_vertex_pipe_vp_enable
,
9705 glsl_vertex_pipe_vp_get_caps
,
9706 glsl_vertex_pipe_vp_get_emul_mask
,
9707 glsl_vertex_pipe_vp_alloc
,
9708 glsl_vertex_pipe_vp_free
,
9709 glsl_vertex_pipe_vp_states
,
9712 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
)
9714 /* Nothing to do. */
9717 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info
*gl_info
, struct fragment_caps
*caps
)
9719 caps
->wined3d_caps
= WINED3D_FRAGMENT_CAP_PROJ_CONTROL
9720 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
9721 | WINED3D_FRAGMENT_CAP_COLOR_KEY
;
9722 caps
->PrimitiveMiscCaps
= WINED3DPMISCCAPS_TSSARGTEMP
9723 | WINED3DPMISCCAPS_PERSTAGECONSTANT
;
9724 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
9725 | WINED3DTEXOPCAPS_SELECTARG1
9726 | WINED3DTEXOPCAPS_SELECTARG2
9727 | WINED3DTEXOPCAPS_MODULATE4X
9728 | WINED3DTEXOPCAPS_MODULATE2X
9729 | WINED3DTEXOPCAPS_MODULATE
9730 | WINED3DTEXOPCAPS_ADDSIGNED2X
9731 | WINED3DTEXOPCAPS_ADDSIGNED
9732 | WINED3DTEXOPCAPS_ADD
9733 | WINED3DTEXOPCAPS_SUBTRACT
9734 | WINED3DTEXOPCAPS_ADDSMOOTH
9735 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
9736 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
9737 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
9738 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
9739 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
9740 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
9741 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
9742 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
9743 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
9744 | WINED3DTEXOPCAPS_DOTPRODUCT3
9745 | WINED3DTEXOPCAPS_MULTIPLYADD
9746 | WINED3DTEXOPCAPS_LERP
9747 | WINED3DTEXOPCAPS_BUMPENVMAP
9748 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
9749 caps
->MaxTextureBlendStages
= MAX_TEXTURES
;
9750 caps
->MaxSimultaneousTextures
= min(gl_info
->limits
.fragment_samplers
, MAX_TEXTURES
);
9753 static DWORD
glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
9755 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
9756 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
9760 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
9762 struct shader_glsl_priv
*priv
;
9764 if (shader_backend
== &glsl_shader_backend
)
9767 wine_rb_init(&priv
->ffp_fragment_shaders
, wined3d_ffp_frag_program_key_compare
);
9771 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
9776 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry
*entry
, void *context
)
9778 struct glsl_ffp_fragment_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
9779 struct glsl_ffp_fragment_shader
, entry
.entry
);
9780 struct glsl_shader_prog_link
*program
, *program2
;
9781 struct glsl_ffp_destroy_ctx
*ctx
= context
;
9783 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
9784 struct glsl_shader_prog_link
, ps
.shader_entry
)
9786 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
9788 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
9789 HeapFree(GetProcessHeap(), 0, shader
);
9792 /* Context activation is done by the caller. */
9793 static void glsl_fragment_pipe_free(struct wined3d_device
*device
)
9795 struct shader_glsl_priv
*priv
= device
->fragment_priv
;
9796 struct glsl_ffp_destroy_ctx ctx
;
9799 ctx
.gl_info
= &device
->adapter
->gl_info
;
9800 wine_rb_destroy(&priv
->ffp_fragment_shaders
, shader_glsl_free_ffp_fragment_shader
, &ctx
);
9803 static void glsl_fragment_pipe_shader(struct wined3d_context
*context
,
9804 const struct wined3d_state
*state
, DWORD state_id
)
9806 context
->last_was_pshader
= use_ps(state
);
9808 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9811 static void glsl_fragment_pipe_fogparams(struct wined3d_context
*context
,
9812 const struct wined3d_state
*state
, DWORD state_id
)
9814 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
9817 static void glsl_fragment_pipe_fog(struct wined3d_context
*context
,
9818 const struct wined3d_state
*state
, DWORD state_id
)
9820 BOOL use_vshader
= use_vs(state
);
9821 enum fogsource new_source
;
9822 DWORD fogstart
= state
->render_states
[WINED3D_RS_FOGSTART
];
9823 DWORD fogend
= state
->render_states
[WINED3D_RS_FOGEND
];
9825 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9827 if (!state
->render_states
[WINED3D_RS_FOGENABLE
])
9830 if (state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
9833 new_source
= FOGSOURCE_VS
;
9834 else if (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
|| context
->stream_info
.position_transformed
)
9835 new_source
= FOGSOURCE_COORD
;
9837 new_source
= FOGSOURCE_FFP
;
9841 new_source
= FOGSOURCE_FFP
;
9844 if (new_source
!= context
->fog_source
|| fogstart
== fogend
)
9846 context
->fog_source
= new_source
;
9847 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
9851 static void glsl_fragment_pipe_vdecl(struct wined3d_context
*context
,
9852 const struct wined3d_state
*state
, DWORD state_id
)
9854 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9855 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
9856 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9858 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
)))
9859 glsl_fragment_pipe_fog(context
, state
, state_id
);
9862 static void glsl_fragment_pipe_vs(struct wined3d_context
*context
,
9863 const struct wined3d_state
*state
, DWORD state_id
)
9865 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9866 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
9867 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9870 static void glsl_fragment_pipe_tex_transform(struct wined3d_context
*context
,
9871 const struct wined3d_state
*state
, DWORD state_id
)
9873 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9876 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context
*context
,
9877 const struct wined3d_state
*state
, DWORD state_id
)
9879 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
9882 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context
*context
,
9883 const struct wined3d_state
*state
, DWORD state_id
)
9885 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9886 GLint func
= wined3d_gl_compare_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
]);
9887 float ref
= state
->render_states
[WINED3D_RS_ALPHAREF
] / 255.0f
;
9891 gl_info
->gl_ops
.gl
.p_glAlphaFunc(func
, ref
);
9892 checkGLcall("glAlphaFunc");
9896 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context
*context
,
9897 const struct wined3d_state
*state
, DWORD state_id
)
9899 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9902 static void glsl_fragment_pipe_alpha_test(struct wined3d_context
*context
,
9903 const struct wined3d_state
*state
, DWORD state_id
)
9905 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9907 if (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
])
9909 gl_info
->gl_ops
.gl
.p_glEnable(GL_ALPHA_TEST
);
9910 checkGLcall("glEnable(GL_ALPHA_TEST)");
9914 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
9915 checkGLcall("glDisable(GL_ALPHA_TEST)");
9919 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context
*context
,
9920 const struct wined3d_state
*state
, DWORD state_id
)
9922 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_ALPHA_TEST
;
9925 static void glsl_fragment_pipe_color_key(struct wined3d_context
*context
,
9926 const struct wined3d_state
*state
, DWORD state_id
)
9928 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
9931 static void glsl_fragment_pipe_shademode(struct wined3d_context
*context
,
9932 const struct wined3d_state
*state
, DWORD state_id
)
9934 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9937 static const struct StateEntryTemplate glsl_fragment_pipe_state_template
[] =
9939 {STATE_VDECL
, {STATE_VDECL
, glsl_fragment_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
9940 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_fragment_pipe_vs
}, WINED3D_GL_EXT_NONE
},
9941 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
9942 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9943 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9944 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9945 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9946 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9947 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9948 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9949 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9950 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9951 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9952 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9953 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9954 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9955 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9956 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9957 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9958 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9959 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9960 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9961 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9962 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9963 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9964 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9965 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9966 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9967 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9968 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9969 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9970 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9971 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9972 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9973 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9974 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9975 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9976 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9977 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9978 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9979 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9980 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9981 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9982 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9983 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9984 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9985 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9986 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9987 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9988 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9989 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9990 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9991 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9992 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9993 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9994 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9995 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9996 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9997 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9998 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9999 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10000 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10001 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10002 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10003 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10004 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10005 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10006 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10007 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10008 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10009 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10010 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10011 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10012 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10013 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10014 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
10015 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), glsl_fragment_pipe_alpha_test_func
}, WINED3D_GL_LEGACY_CONTEXT
},
10016 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10017 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), NULL
}, WINED3D_GL_LEGACY_CONTEXT
},
10018 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHAREF
), glsl_fragment_pipe_core_alpha_test_ref
}, WINED3D_GL_EXT_NONE
},
10019 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_alpha_test
}, WINED3D_GL_LEGACY_CONTEXT
},
10020 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_core_alpha_test
}, WINED3D_GL_EXT_NONE
},
10021 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10022 {STATE_COLOR_KEY
, { STATE_COLOR_KEY
, glsl_fragment_pipe_color_key
}, WINED3D_GL_EXT_NONE
},
10023 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_fragment_pipe_fog
}, WINED3D_GL_EXT_NONE
},
10024 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10025 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10026 {STATE_RENDER(WINED3D_RS_FOGSTART
), {STATE_RENDER(WINED3D_RS_FOGSTART
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
10027 {STATE_RENDER(WINED3D_RS_FOGEND
), {STATE_RENDER(WINED3D_RS_FOGSTART
), NULL
}, WINED3D_GL_EXT_NONE
},
10028 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), state_srgbwrite
}, ARB_FRAMEBUFFER_SRGB
},
10029 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10030 {STATE_RENDER(WINED3D_RS_FOGCOLOR
), {STATE_RENDER(WINED3D_RS_FOGCOLOR
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
10031 {STATE_RENDER(WINED3D_RS_FOGDENSITY
), {STATE_RENDER(WINED3D_RS_FOGDENSITY
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
10032 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, ARB_POINT_SPRITE
},
10033 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, WINED3D_GL_VERSION_2_0
},
10034 {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
10035 {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
10036 {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
10037 {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
10038 {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
10039 {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
10040 {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
10041 {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
10042 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10043 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10044 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10045 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10046 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10047 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10048 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10049 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10050 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10051 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
10052 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), state_shademode
}, WINED3D_GL_LEGACY_CONTEXT
},
10053 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_fragment_pipe_shademode
}, WINED3D_GL_EXT_NONE
},
10054 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE
},
10057 static BOOL
glsl_fragment_pipe_alloc_context_data(struct wined3d_context
*context
)
10062 static void glsl_fragment_pipe_free_context_data(struct wined3d_context
*context
)
10066 const struct fragment_pipeline glsl_fragment_pipe
=
10068 glsl_fragment_pipe_enable
,
10069 glsl_fragment_pipe_get_caps
,
10070 glsl_fragment_pipe_get_emul_mask
,
10071 glsl_fragment_pipe_alloc
,
10072 glsl_fragment_pipe_free
,
10073 glsl_fragment_pipe_alloc_context_data
,
10074 glsl_fragment_pipe_free_context_data
,
10075 shader_glsl_color_fixup_supported
,
10076 glsl_fragment_pipe_state_template
,