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 */
71 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
86 struct glsl_sample_function
88 struct wined3d_string_buffer
*name
;
89 unsigned int coord_mask
;
90 unsigned int deriv_mask
;
91 enum wined3d_data_type data_type
;
92 BOOL output_single_component
;
93 unsigned int offset_size
;
98 HEAP_NODE_TRAVERSE_LEFT
,
99 HEAP_NODE_TRAVERSE_RIGHT
,
103 struct constant_entry
106 unsigned int version
;
111 struct constant_entry
*entries
;
113 unsigned int *positions
;
117 /* GLSL shader private data */
118 struct shader_glsl_priv
{
119 struct wined3d_string_buffer shader_buffer
;
120 struct wined3d_string_buffer_list string_buffers
;
121 struct wine_rb_tree program_lookup
;
122 struct constant_heap vconst_heap
;
123 struct constant_heap pconst_heap
;
124 unsigned char *stack
;
125 UINT next_constant_version
;
127 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
128 const struct fragment_pipeline
*fragment_pipe
;
129 struct wine_rb_tree ffp_vertex_shaders
;
130 struct wine_rb_tree ffp_fragment_shaders
;
131 BOOL ffp_proj_control
;
132 BOOL legacy_lighting
;
135 struct glsl_vs_program
137 struct list shader_entry
;
139 GLenum vertex_color_clamp
;
140 GLint uniform_f_locations
[WINED3D_MAX_VS_CONSTS_F
];
141 GLint uniform_i_locations
[WINED3D_MAX_CONSTS_I
];
142 GLint uniform_b_locations
[WINED3D_MAX_CONSTS_B
];
143 GLint pos_fixup_location
;
145 GLint modelview_matrix_location
[MAX_VERTEX_BLENDS
];
146 GLint projection_matrix_location
;
147 GLint normal_matrix_location
;
148 GLint texture_matrix_location
[MAX_TEXTURES
];
149 GLint material_ambient_location
;
150 GLint material_diffuse_location
;
151 GLint material_specular_location
;
152 GLint material_emissive_location
;
153 GLint material_shininess_location
;
154 GLint light_ambient_location
;
169 } light_location
[MAX_ACTIVE_LIGHTS
];
170 GLint pointsize_location
;
171 GLint pointsize_min_location
;
172 GLint pointsize_max_location
;
173 GLint pointsize_c_att_location
;
174 GLint pointsize_l_att_location
;
175 GLint pointsize_q_att_location
;
176 GLint clip_planes_location
;
179 struct glsl_gs_program
181 struct list shader_entry
;
184 GLint pos_fixup_location
;
187 struct glsl_ps_program
189 struct list shader_entry
;
191 GLint uniform_f_locations
[WINED3D_MAX_PS_CONSTS_F
];
192 GLint uniform_i_locations
[WINED3D_MAX_CONSTS_I
];
193 GLint uniform_b_locations
[WINED3D_MAX_CONSTS_B
];
194 GLint bumpenv_mat_location
[MAX_TEXTURES
];
195 GLint bumpenv_lum_scale_location
[MAX_TEXTURES
];
196 GLint bumpenv_lum_offset_location
[MAX_TEXTURES
];
197 GLint tss_constant_location
[MAX_TEXTURES
];
198 GLint tex_factor_location
;
199 GLint specular_enable_location
;
200 GLint fog_color_location
;
201 GLint fog_density_location
;
202 GLint fog_end_location
;
203 GLint fog_scale_location
;
204 GLint alpha_test_ref_location
;
205 GLint ycorrection_location
;
206 GLint np2_fixup_location
;
207 GLint color_key_location
;
208 const struct ps_np2fixup_info
*np2_fixup_info
;
211 struct glsl_cs_program
213 struct list shader_entry
;
217 /* Struct to maintain data about a linked GLSL program */
218 struct glsl_shader_prog_link
220 struct wine_rb_entry program_lookup_entry
;
221 struct glsl_vs_program vs
;
222 struct glsl_gs_program gs
;
223 struct glsl_ps_program ps
;
224 struct glsl_cs_program cs
;
226 DWORD constant_update_mask
;
227 UINT constant_version
;
230 struct glsl_program_key
238 struct shader_glsl_ctx_priv
{
239 const struct vs_compile_args
*cur_vs_args
;
240 const struct ps_compile_args
*cur_ps_args
;
241 struct ps_np2fixup_info
*cur_np2fixup_info
;
242 struct wined3d_string_buffer_list
*string_buffers
;
245 struct glsl_context_data
247 struct glsl_shader_prog_link
*glsl_program
;
248 GLenum vertex_color_clamp
;
251 struct glsl_ps_compiled_shader
253 struct ps_compile_args args
;
254 struct ps_np2fixup_info np2fixup
;
258 struct glsl_vs_compiled_shader
260 struct vs_compile_args args
;
264 struct glsl_gs_compiled_shader
266 struct gs_compile_args args
;
270 struct glsl_cs_compiled_shader
275 struct glsl_shader_private
279 struct glsl_vs_compiled_shader
*vs
;
280 struct glsl_gs_compiled_shader
*gs
;
281 struct glsl_ps_compiled_shader
*ps
;
282 struct glsl_cs_compiled_shader
*cs
;
284 UINT num_gl_shaders
, shader_array_size
;
287 struct glsl_ffp_vertex_shader
289 struct wined3d_ffp_vs_desc desc
;
291 struct list linked_programs
;
294 struct glsl_ffp_fragment_shader
296 struct ffp_frag_desc entry
;
298 struct list linked_programs
;
301 struct glsl_ffp_destroy_ctx
303 struct shader_glsl_priv
*priv
;
304 const struct wined3d_gl_info
*gl_info
;
307 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context
*ctx
);
309 static const char *debug_gl_shader_type(GLenum type
)
313 #define WINED3D_TO_STR(u) case u: return #u
314 WINED3D_TO_STR(GL_VERTEX_SHADER
);
315 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER
);
316 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER
);
317 WINED3D_TO_STR(GL_GEOMETRY_SHADER
);
318 WINED3D_TO_STR(GL_FRAGMENT_SHADER
);
319 WINED3D_TO_STR(GL_COMPUTE_SHADER
);
320 #undef WINED3D_TO_STR
322 return wine_dbg_sprintf("UNKNOWN(%#x)", type
);
326 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type
)
330 case WINED3D_SHADER_TYPE_VERTEX
:
333 case WINED3D_SHADER_TYPE_HULL
:
336 case WINED3D_SHADER_TYPE_DOMAIN
:
339 case WINED3D_SHADER_TYPE_GEOMETRY
:
342 case WINED3D_SHADER_TYPE_PIXEL
:
345 case WINED3D_SHADER_TYPE_COMPUTE
:
349 FIXME("Unhandled shader type %#x.\n", type
);
354 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info
*gl_info
,
355 const struct wined3d_shader_version
*version
)
357 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
358 || (version
&& version
->type
== WINED3D_SHADER_TYPE_COMPUTE
))
360 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 30) && version
&& version
->major
>= 4)
366 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer
*buffer
,
367 const struct wined3d_gl_info
*gl_info
, const struct wined3d_shader_version
*version
)
369 unsigned int glsl_version
= shader_glsl_get_version(gl_info
, version
);
370 if (glsl_version
>= 150 && gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
371 shader_addline(buffer
, "#version %u compatibility\n", glsl_version
);
373 shader_addline(buffer
, "#version %u\n", glsl_version
);
376 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer
*buffer
, const float *values
)
380 wined3d_ftoa(values
[0], str
[0]);
381 wined3d_ftoa(values
[1], str
[1]);
382 wined3d_ftoa(values
[2], str
[2]);
383 wined3d_ftoa(values
[3], str
[3]);
384 shader_addline(buffer
, "vec4(%s, %s, %s, %s)", str
[0], str
[1], str
[2], str
[3]);
387 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer
*buffer
,
388 const int *values
, unsigned int size
)
392 if (!size
|| size
> 4)
394 ERR("Invalid vector size %u.\n", size
);
399 shader_addline(buffer
, "ivec%u(", size
);
401 for (i
= 0; i
< size
; ++i
)
402 shader_addline(buffer
, i
? ", %#x" : "%#x", values
[i
]);
405 shader_addline(buffer
, ")");
408 static const char *get_info_log_line(const char **ptr
)
413 if (!(q
= strstr(p
, "\n")))
415 if (!*p
) return NULL
;
424 /* Context activation is done by the caller. */
425 void print_glsl_info_log(const struct wined3d_gl_info
*gl_info
, GLuint id
, BOOL program
)
430 if (!WARN_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
434 GL_EXTCALL(glGetProgramiv(id
, GL_INFO_LOG_LENGTH
, &length
));
436 GL_EXTCALL(glGetShaderiv(id
, GL_INFO_LOG_LENGTH
, &length
));
438 /* A size of 1 is just a null-terminated string, so the log should be bigger than
439 * that if there are errors. */
442 const char *ptr
, *line
;
444 log
= HeapAlloc(GetProcessHeap(), 0, length
);
445 /* The info log is supposed to be zero-terminated, but at least some
446 * versions of fglrx don't terminate the string properly. The reported
447 * length does include the terminator, so explicitly set it to zero
451 GL_EXTCALL(glGetProgramInfoLog(id
, length
, NULL
, log
));
453 GL_EXTCALL(glGetShaderInfoLog(id
, length
, NULL
, log
));
456 if (gl_info
->quirks
& WINED3D_QUIRK_INFO_LOG_SPAM
)
458 WARN("Info log received from GLSL shader #%u:\n", id
);
459 while ((line
= get_info_log_line(&ptr
))) WARN(" %.*s", (int)(ptr
- line
), line
);
463 FIXME("Info log received from GLSL shader #%u:\n", id
);
464 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
466 HeapFree(GetProcessHeap(), 0, log
);
470 /* Context activation is done by the caller. */
471 static void shader_glsl_compile(const struct wined3d_gl_info
*gl_info
, GLuint shader
, const char *src
)
473 const char *ptr
, *line
;
475 TRACE("Compiling shader object %u.\n", shader
);
477 if (TRACE_ON(d3d_shader
))
480 while ((line
= get_info_log_line(&ptr
))) TRACE_(d3d_shader
)(" %.*s", (int)(ptr
- line
), line
);
483 GL_EXTCALL(glShaderSource(shader
, 1, &src
, NULL
));
484 checkGLcall("glShaderSource");
485 GL_EXTCALL(glCompileShader(shader
));
486 checkGLcall("glCompileShader");
487 print_glsl_info_log(gl_info
, shader
, FALSE
);
490 /* Context activation is done by the caller. */
491 static void shader_glsl_dump_program_source(const struct wined3d_gl_info
*gl_info
, GLuint program
)
493 GLint i
, shader_count
, source_size
= -1;
497 GL_EXTCALL(glGetProgramiv(program
, GL_ATTACHED_SHADERS
, &shader_count
));
498 if (!(shaders
= wined3d_calloc(shader_count
, sizeof(*shaders
))))
500 ERR("Failed to allocate shader array memory.\n");
504 GL_EXTCALL(glGetAttachedShaders(program
, shader_count
, NULL
, shaders
));
505 for (i
= 0; i
< shader_count
; ++i
)
507 const char *ptr
, *line
;
510 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_SOURCE_LENGTH
, &tmp
));
512 if (source_size
< tmp
)
514 HeapFree(GetProcessHeap(), 0, source
);
516 source
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, tmp
);
519 ERR("Failed to allocate %d bytes for shader source.\n", tmp
);
520 HeapFree(GetProcessHeap(), 0, shaders
);
526 FIXME("Shader %u:\n", shaders
[i
]);
527 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_TYPE
, &tmp
));
528 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp
));
529 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_COMPILE_STATUS
, &tmp
));
530 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp
);
534 GL_EXTCALL(glGetShaderSource(shaders
[i
], source_size
, NULL
, source
));
535 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
539 HeapFree(GetProcessHeap(), 0, source
);
540 HeapFree(GetProcessHeap(), 0, shaders
);
543 /* Context activation is done by the caller. */
544 void shader_glsl_validate_link(const struct wined3d_gl_info
*gl_info
, GLuint program
)
548 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
551 GL_EXTCALL(glGetProgramiv(program
, GL_LINK_STATUS
, &tmp
));
554 FIXME("Program %u link status invalid.\n", program
);
555 shader_glsl_dump_program_source(gl_info
, program
);
558 print_glsl_info_log(gl_info
, program
, TRUE
);
561 static BOOL
shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info
*gl_info
)
563 return !gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_SHADING_LANGUAGE_420PACK
];
566 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info
*gl_info
,
567 struct shader_glsl_priv
*priv
, GLuint program_id
,
568 const struct wined3d_shader_reg_maps
*reg_maps
)
570 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
571 struct wined3d_string_buffer
*name
;
572 unsigned int i
, base
, count
;
575 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
578 name
= string_buffer_get(&priv
->string_buffers
);
579 wined3d_gl_limits_get_uniform_block_range(&gl_info
->limits
, reg_maps
->shader_version
.type
, &base
, &count
);
580 for (i
= 0; i
< count
; ++i
)
582 if (!reg_maps
->cb_sizes
[i
])
585 string_buffer_sprintf(name
, "block_%s_cb%u", prefix
, i
);
586 block_idx
= GL_EXTCALL(glGetUniformBlockIndex(program_id
, name
->buffer
));
587 GL_EXTCALL(glUniformBlockBinding(program_id
, block_idx
, base
+ i
));
589 checkGLcall("glUniformBlockBinding");
590 string_buffer_release(&priv
->string_buffers
, name
);
593 /* Context activation is done by the caller. */
594 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info
*gl_info
,
595 struct shader_glsl_priv
*priv
, GLuint program_id
, const char *prefix
,
596 unsigned int base
, unsigned int count
, const DWORD
*tex_unit_map
)
598 struct wined3d_string_buffer
*sampler_name
= string_buffer_get(&priv
->string_buffers
);
599 unsigned int i
, mapped_unit
;
602 for (i
= 0; i
< count
; ++i
)
604 string_buffer_sprintf(sampler_name
, "%s_sampler%u", prefix
, i
);
605 name_loc
= GL_EXTCALL(glGetUniformLocation(program_id
, sampler_name
->buffer
));
609 mapped_unit
= tex_unit_map
? tex_unit_map
[base
+ i
] : base
+ i
;
610 if (mapped_unit
== WINED3D_UNMAPPED_STAGE
|| mapped_unit
>= gl_info
->limits
.combined_samplers
)
612 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name
->buffer
, mapped_unit
);
616 TRACE("Loading sampler %s on unit %u.\n", sampler_name
->buffer
, mapped_unit
);
617 GL_EXTCALL(glUniform1i(name_loc
, mapped_unit
));
619 checkGLcall("Load sampler bindings");
620 string_buffer_release(&priv
->string_buffers
, sampler_name
);
623 /* Context activation is done by the caller. */
624 static void shader_glsl_load_samplers(const struct wined3d_context
*context
,
625 struct shader_glsl_priv
*priv
, GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
627 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
628 const char *prefix
= shader_glsl_get_prefix(version
->type
);
629 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
630 const DWORD
*tex_unit_map
;
631 unsigned int base
, count
;
633 if (reg_maps
->shader_version
.major
>= 4)
636 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, version
->type
, &base
, &count
);
640 tex_unit_map
= context
->tex_unit_map
;
641 switch (reg_maps
->shader_version
.type
)
643 case WINED3D_SHADER_TYPE_PIXEL
:
645 count
= MAX_FRAGMENT_SAMPLERS
;
647 case WINED3D_SHADER_TYPE_VERTEX
:
648 base
= MAX_FRAGMENT_SAMPLERS
;
649 count
= MAX_VERTEX_SAMPLERS
;
652 ERR("Unhandled shader type %#x.\n", reg_maps
->shader_version
.type
);
657 shader_glsl_load_samplers_range(gl_info
, priv
, program_id
, prefix
, base
, count
, tex_unit_map
);
660 static void shader_glsl_load_icb(const struct wined3d_gl_info
*gl_info
, struct shader_glsl_priv
*priv
,
661 GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
663 const struct wined3d_shader_immediate_constant_buffer
*icb
= reg_maps
->icb
;
667 struct wined3d_string_buffer
*icb_name
= string_buffer_get(&priv
->string_buffers
);
668 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
671 string_buffer_sprintf(icb_name
, "%s_icb", prefix
);
672 icb_location
= GL_EXTCALL(glGetUniformLocation(program_id
, icb_name
->buffer
));
673 GL_EXTCALL(glUniform4fv(icb_location
, icb
->vec4_count
, (const GLfloat
*)icb
->data
));
674 checkGLcall("Load immediate constant buffer");
676 string_buffer_release(&priv
->string_buffers
, icb_name
);
680 /* Context activation is done by the caller. */
681 static void shader_glsl_load_images(const struct wined3d_gl_info
*gl_info
, struct shader_glsl_priv
*priv
,
682 GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
684 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
685 struct wined3d_string_buffer
*name
;
689 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
692 name
= string_buffer_get(&priv
->string_buffers
);
693 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
695 if (!reg_maps
->uav_resource_info
[i
].type
)
698 string_buffer_sprintf(name
, "%s_image%u", prefix
, i
);
699 location
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
703 TRACE("Loading image %s on unit %u.\n", name
->buffer
, i
);
704 GL_EXTCALL(glUniform1i(location
, i
));
706 checkGLcall("Load image bindings");
707 string_buffer_release(&priv
->string_buffers
, name
);
710 /* Context activation is done by the caller. */
711 static void shader_glsl_load_program_resources(const struct wined3d_context
*context
,
712 struct shader_glsl_priv
*priv
, GLuint program_id
, const struct wined3d_shader
*shader
)
714 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
716 shader_glsl_init_uniform_block_bindings(context
->gl_info
, priv
, program_id
, reg_maps
);
717 shader_glsl_load_icb(context
->gl_info
, priv
, program_id
, reg_maps
);
718 /* Texture unit mapping is set up to be the same each time the shader
719 * program is used so we can hardcode the sampler uniform values. */
720 shader_glsl_load_samplers(context
, priv
, program_id
, reg_maps
);
723 /* Context activation is done by the caller. */
724 static inline void walk_constant_heap(const struct wined3d_gl_info
*gl_info
, const struct wined3d_vec4
*constants
,
725 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
727 unsigned int start
= ~0U, end
= 0;
729 unsigned int heap_idx
= 1;
732 if (heap
->entries
[heap_idx
].version
<= version
) return;
734 idx
= heap
->entries
[heap_idx
].idx
;
735 if (constant_locations
[idx
] != -1)
737 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
739 while (stack_idx
>= 0)
741 /* Note that we fall through to the next case statement. */
742 switch(stack
[stack_idx
])
744 case HEAP_NODE_TRAVERSE_LEFT
:
746 unsigned int left_idx
= heap_idx
<< 1;
747 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
750 idx
= heap
->entries
[heap_idx
].idx
;
751 if (constant_locations
[idx
] != -1)
759 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
760 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
765 case HEAP_NODE_TRAVERSE_RIGHT
:
767 unsigned int right_idx
= (heap_idx
<< 1) + 1;
768 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
770 heap_idx
= right_idx
;
771 idx
= heap
->entries
[heap_idx
].idx
;
772 if (constant_locations
[idx
] != -1)
780 stack
[stack_idx
++] = HEAP_NODE_POP
;
781 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
793 GL_EXTCALL(glUniform4fv(constant_locations
[start
], end
- start
+ 1, &constants
[start
].x
));
794 checkGLcall("walk_constant_heap()");
797 /* Context activation is done by the caller. */
798 static inline void apply_clamped_constant(const struct wined3d_gl_info
*gl_info
,
799 GLint location
, const struct wined3d_vec4
*data
)
801 GLfloat clamped_constant
[4];
803 if (location
== -1) return;
805 clamped_constant
[0] = data
->x
< -1.0f
? -1.0f
: data
->x
> 1.0f
? 1.0f
: data
->x
;
806 clamped_constant
[1] = data
->y
< -1.0f
? -1.0f
: data
->y
> 1.0f
? 1.0f
: data
->y
;
807 clamped_constant
[2] = data
->z
< -1.0f
? -1.0f
: data
->z
> 1.0f
? 1.0f
: data
->z
;
808 clamped_constant
[3] = data
->w
< -1.0f
? -1.0f
: data
->w
> 1.0f
? 1.0f
: data
->w
;
810 GL_EXTCALL(glUniform4fv(location
, 1, clamped_constant
));
813 /* Context activation is done by the caller. */
814 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info
*gl_info
,
815 const struct wined3d_vec4
*constants
, const GLint
*constant_locations
,
816 const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
819 unsigned int heap_idx
= 1;
822 if (heap
->entries
[heap_idx
].version
<= version
) return;
824 idx
= heap
->entries
[heap_idx
].idx
;
825 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
826 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
828 while (stack_idx
>= 0)
830 /* Note that we fall through to the next case statement. */
831 switch(stack
[stack_idx
])
833 case HEAP_NODE_TRAVERSE_LEFT
:
835 unsigned int left_idx
= heap_idx
<< 1;
836 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
839 idx
= heap
->entries
[heap_idx
].idx
;
840 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
842 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
843 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
848 case HEAP_NODE_TRAVERSE_RIGHT
:
850 unsigned int right_idx
= (heap_idx
<< 1) + 1;
851 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
853 heap_idx
= right_idx
;
854 idx
= heap
->entries
[heap_idx
].idx
;
855 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
857 stack
[stack_idx
++] = HEAP_NODE_POP
;
858 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
869 checkGLcall("walk_constant_heap_clamped()");
872 /* Context activation is done by the caller. */
873 static void shader_glsl_load_constants_f(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
874 const struct wined3d_vec4
*constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
875 unsigned char *stack
, unsigned int version
)
877 const struct wined3d_shader_lconst
*lconst
;
879 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
880 if (shader
->reg_maps
.shader_version
.major
== 1
881 && shader
->reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
882 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
884 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
886 if (!shader
->load_local_constsF
)
888 TRACE("No need to load local float constants for this shader.\n");
892 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
893 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
895 GL_EXTCALL(glUniform4fv(constant_locations
[lconst
->idx
], 1, (const GLfloat
*)lconst
->value
));
897 checkGLcall("glUniform4fv()");
900 /* Context activation is done by the caller. */
901 static void shader_glsl_load_constants_i(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
902 const struct wined3d_ivec4
*constants
, const GLint locations
[WINED3D_MAX_CONSTS_I
], WORD constants_set
)
907 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
909 if (!(constants_set
& 1)) continue;
911 /* We found this uniform name in the program - go ahead and send the data */
912 GL_EXTCALL(glUniform4iv(locations
[i
], 1, &constants
[i
].x
));
915 /* Load immediate constants */
916 ptr
= list_head(&shader
->constantsI
);
919 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
920 unsigned int idx
= lconst
->idx
;
921 const GLint
*values
= (const GLint
*)lconst
->value
;
923 /* We found this uniform name in the program - go ahead and send the data */
924 GL_EXTCALL(glUniform4iv(locations
[idx
], 1, values
));
925 ptr
= list_next(&shader
->constantsI
, ptr
);
927 checkGLcall("glUniform4iv()");
930 /* Context activation is done by the caller. */
931 static void shader_glsl_load_constantsB(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
932 const GLint locations
[WINED3D_MAX_CONSTS_B
], const BOOL
*constants
, WORD constants_set
)
937 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
939 if (!(constants_set
& 1)) continue;
941 GL_EXTCALL(glUniform1iv(locations
[i
], 1, &constants
[i
]));
944 /* Load immediate constants */
945 ptr
= list_head(&shader
->constantsB
);
948 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
949 unsigned int idx
= lconst
->idx
;
950 const GLint
*values
= (const GLint
*)lconst
->value
;
952 GL_EXTCALL(glUniform1iv(locations
[idx
], 1, values
));
953 ptr
= list_next(&shader
->constantsB
, ptr
);
955 checkGLcall("glUniform1iv()");
958 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
960 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
963 /* Context activation is done by the caller (state handler). */
964 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program
*ps
,
965 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
971 np2fixup_constants
[MAX_FRAGMENT_SAMPLERS
];
972 UINT fixup
= ps
->np2_fixup_info
->active
;
975 for (i
= 0; fixup
; fixup
>>= 1, ++i
)
977 const struct wined3d_texture
*tex
= state
->textures
[i
];
978 unsigned char idx
= ps
->np2_fixup_info
->idx
[i
];
982 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
986 np2fixup_constants
[idx
].sx
= tex
->pow2_matrix
[0];
987 np2fixup_constants
[idx
].sy
= tex
->pow2_matrix
[5];
990 GL_EXTCALL(glUniform4fv(ps
->np2_fixup_location
, ps
->np2_fixup_info
->num_consts
, &np2fixup_constants
[0].sx
));
993 /* Taken and adapted from Mesa. */
994 static BOOL
invert_matrix_3d(struct wined3d_matrix
*out
, const struct wined3d_matrix
*in
)
996 float pos
, neg
, t
, det
;
997 struct wined3d_matrix temp
;
999 /* Calculate the determinant of upper left 3x3 submatrix and
1000 * determine if the matrix is singular. */
1002 t
= in
->_11
* in
->_22
* in
->_33
;
1008 t
= in
->_21
* in
->_32
* in
->_13
;
1013 t
= in
->_31
* in
->_12
* in
->_23
;
1019 t
= -in
->_31
* in
->_22
* in
->_13
;
1024 t
= -in
->_21
* in
->_12
* in
->_33
;
1030 t
= -in
->_11
* in
->_32
* in
->_23
;
1038 if (fabsf(det
) < 1e-25f
)
1042 temp
._11
= (in
->_22
* in
->_33
- in
->_32
* in
->_23
) * det
;
1043 temp
._12
= -(in
->_12
* in
->_33
- in
->_32
* in
->_13
) * det
;
1044 temp
._13
= (in
->_12
* in
->_23
- in
->_22
* in
->_13
) * det
;
1045 temp
._21
= -(in
->_21
* in
->_33
- in
->_31
* in
->_23
) * det
;
1046 temp
._22
= (in
->_11
* in
->_33
- in
->_31
* in
->_13
) * det
;
1047 temp
._23
= -(in
->_11
* in
->_23
- in
->_21
* in
->_13
) * det
;
1048 temp
._31
= (in
->_21
* in
->_32
- in
->_31
* in
->_22
) * det
;
1049 temp
._32
= -(in
->_11
* in
->_32
- in
->_31
* in
->_12
) * det
;
1050 temp
._33
= (in
->_11
* in
->_22
- in
->_21
* in
->_12
) * det
;
1056 static void swap_rows(float **a
, float **b
)
1064 static BOOL
invert_matrix(struct wined3d_matrix
*out
, struct wined3d_matrix
*m
)
1067 float m0
, m1
, m2
, m3
, s
;
1068 float *r0
, *r1
, *r2
, *r3
;
1080 r0
[5] = r0
[6] = r0
[7] = 0.0f
;
1087 r1
[4] = r1
[6] = r1
[7] = 0.0f
;
1094 r2
[4] = r2
[5] = r2
[7] = 0.0f
;
1101 r3
[4] = r3
[5] = r3
[6] = 0.0f
;
1103 /* Choose pivot - or die. */
1104 if (fabsf(r3
[0]) > fabsf(r2
[0]))
1105 swap_rows(&r3
, &r2
);
1106 if (fabsf(r2
[0]) > fabsf(r1
[0]))
1107 swap_rows(&r2
, &r1
);
1108 if (fabsf(r1
[0]) > fabsf(r0
[0]))
1109 swap_rows(&r1
, &r0
);
1113 /* Eliminate first variable. */
1114 m1
= r1
[0] / r0
[0]; m2
= r2
[0] / r0
[0]; m3
= r3
[0] / r0
[0];
1115 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
1116 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
1117 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
1147 /* Choose pivot - or die. */
1148 if (fabsf(r3
[1]) > fabsf(r2
[1]))
1149 swap_rows(&r3
, &r2
);
1150 if (fabsf(r2
[1]) > fabsf(r1
[1]))
1151 swap_rows(&r2
, &r1
);
1155 /* Eliminate second variable. */
1156 m2
= r2
[1] / r1
[1]; m3
= r3
[1] / r1
[1];
1157 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
1158 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
1184 /* Choose pivot - or die. */
1185 if (fabsf(r3
[2]) > fabsf(r2
[2]))
1186 swap_rows(&r3
, &r2
);
1190 /* Eliminate third variable. */
1192 r3
[3] -= m3
* r2
[3];
1193 r3
[4] -= m3
* r2
[4];
1194 r3
[5] -= m3
* r2
[5];
1195 r3
[6] -= m3
* r2
[6];
1196 r3
[7] -= m3
* r2
[7];
1202 /* Back substitute row 3. */
1209 /* Back substitute row 2. */
1212 r2
[4] = s
* (r2
[4] - r3
[4] * m2
);
1213 r2
[5] = s
* (r2
[5] - r3
[5] * m2
);
1214 r2
[6] = s
* (r2
[6] - r3
[6] * m2
);
1215 r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
1217 r1
[4] -= r3
[4] * m1
;
1218 r1
[5] -= r3
[5] * m1
;
1219 r1
[6] -= r3
[6] * m1
;
1220 r1
[7] -= r3
[7] * m1
;
1222 r0
[4] -= r3
[4] * m0
;
1223 r0
[5] -= r3
[5] * m0
;
1224 r0
[6] -= r3
[6] * m0
;
1225 r0
[7] -= r3
[7] * m0
;
1227 /* Back substitute row 1. */
1230 r1
[4] = s
* (r1
[4] - r2
[4] * m1
);
1231 r1
[5] = s
* (r1
[5] - r2
[5] * m1
);
1232 r1
[6] = s
* (r1
[6] - r2
[6] * m1
);
1233 r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
1235 r0
[4] -= r2
[4] * m0
;
1236 r0
[5] -= r2
[5] * m0
;
1237 r0
[6] -= r2
[6] * m0
;
1238 r0
[7] -= r2
[7] * m0
;
1240 /* Back substitute row 0. */
1243 r0
[4] = s
* (r0
[4] - r1
[4] * m0
);
1244 r0
[5] = s
* (r0
[5] - r1
[5] * m0
);
1245 r0
[6] = s
* (r0
[6] - r1
[6] * m0
);
1246 r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
1268 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context
*context
,
1269 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1271 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1273 struct wined3d_matrix mv
;
1276 if (prog
->vs
.normal_matrix_location
== -1)
1279 get_modelview_matrix(context
, state
, 0, &mv
);
1280 if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_FFP_LIGHTING
)
1281 invert_matrix_3d(&mv
, &mv
);
1283 invert_matrix(&mv
, &mv
);
1284 /* Tests show that singular modelview matrices are used unchanged as normal
1285 * matrices on D3D3 and older. There seems to be no clearly consistent
1286 * behavior on newer D3D versions so always follow older ddraw behavior. */
1287 for (i
= 0; i
< 3; ++i
)
1288 for (j
= 0; j
< 3; ++j
)
1289 mat
[i
* 3 + j
] = (&mv
._11
)[j
* 4 + i
];
1291 GL_EXTCALL(glUniformMatrix3fv(prog
->vs
.normal_matrix_location
, 1, FALSE
, mat
));
1292 checkGLcall("glUniformMatrix3fv");
1295 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context
*context
,
1296 const struct wined3d_state
*state
, unsigned int tex
, struct glsl_shader_prog_link
*prog
)
1298 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1299 struct wined3d_matrix mat
;
1301 if (tex
>= MAX_TEXTURES
)
1303 if (prog
->vs
.texture_matrix_location
[tex
] == -1)
1306 get_texture_matrix(context
, state
, tex
, &mat
);
1307 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.texture_matrix_location
[tex
], 1, FALSE
, &mat
._11
));
1308 checkGLcall("glUniformMatrix4fv");
1311 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context
*context
,
1312 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1314 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1316 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1318 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, &state
->material
.specular
.r
));
1319 GL_EXTCALL(glUniform1f(prog
->vs
.material_shininess_location
, state
->material
.power
));
1323 static const float black
[] = {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1325 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, black
));
1327 GL_EXTCALL(glUniform4fv(prog
->vs
.material_ambient_location
, 1, &state
->material
.ambient
.r
));
1328 GL_EXTCALL(glUniform4fv(prog
->vs
.material_diffuse_location
, 1, &state
->material
.diffuse
.r
));
1329 GL_EXTCALL(glUniform4fv(prog
->vs
.material_emissive_location
, 1, &state
->material
.emissive
.r
));
1330 checkGLcall("setting FFP material uniforms");
1333 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context
*context
,
1334 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1336 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1337 struct wined3d_color color
;
1339 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_AMBIENT
]);
1340 GL_EXTCALL(glUniform3fv(prog
->vs
.light_ambient_location
, 1, &color
.r
));
1341 checkGLcall("glUniform3fv");
1344 static void multiply_vector_matrix(struct wined3d_vec4
*dest
, const struct wined3d_vec4
*src1
,
1345 const struct wined3d_matrix
*src2
)
1347 struct wined3d_vec4 temp
;
1349 temp
.x
= (src1
->x
* src2
->_11
) + (src1
->y
* src2
->_21
) + (src1
->z
* src2
->_31
) + (src1
->w
* src2
->_41
);
1350 temp
.y
= (src1
->x
* src2
->_12
) + (src1
->y
* src2
->_22
) + (src1
->z
* src2
->_32
) + (src1
->w
* src2
->_42
);
1351 temp
.z
= (src1
->x
* src2
->_13
) + (src1
->y
* src2
->_23
) + (src1
->z
* src2
->_33
) + (src1
->w
* src2
->_43
);
1352 temp
.w
= (src1
->x
* src2
->_14
) + (src1
->y
* src2
->_24
) + (src1
->z
* src2
->_34
) + (src1
->w
* src2
->_44
);
1357 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context
*context
,
1358 const struct wined3d_state
*state
, unsigned int light
, const struct wined3d_light_info
*light_info
,
1359 struct glsl_shader_prog_link
*prog
)
1361 const struct wined3d_matrix
*view
= &state
->transforms
[WINED3D_TS_VIEW
];
1362 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1363 struct wined3d_vec4 vec4
;
1365 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].diffuse
, 1, &light_info
->OriginalParms
.diffuse
.r
));
1366 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].specular
, 1, &light_info
->OriginalParms
.specular
.r
));
1367 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].ambient
, 1, &light_info
->OriginalParms
.ambient
.r
));
1369 switch (light_info
->OriginalParms
.type
)
1371 case WINED3D_LIGHT_POINT
:
1372 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1373 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1374 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1375 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1376 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1377 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1380 case WINED3D_LIGHT_SPOT
:
1381 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1382 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1384 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1385 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1387 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1388 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].falloff
, light_info
->OriginalParms
.falloff
));
1389 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1390 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1391 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1392 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_htheta
, cosf(light_info
->OriginalParms
.theta
/ 2.0f
)));
1393 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_hphi
, cosf(light_info
->OriginalParms
.phi
/ 2.0f
)));
1396 case WINED3D_LIGHT_DIRECTIONAL
:
1397 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1398 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1401 case WINED3D_LIGHT_PARALLELPOINT
:
1402 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1403 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1407 FIXME("Unrecognized light type %#x.\n", light_info
->OriginalParms
.type
);
1409 checkGLcall("setting FFP lights uniforms");
1412 static void shader_glsl_pointsize_uniform(const struct wined3d_context
*context
,
1413 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1415 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1419 get_pointsize_minmax(context
, state
, &min
, &max
);
1421 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_min_location
, min
));
1422 checkGLcall("glUniform1f");
1423 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_max_location
, max
));
1424 checkGLcall("glUniform1f");
1426 get_pointsize(context
, state
, &size
, att
);
1428 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_location
, size
));
1429 checkGLcall("glUniform1f");
1430 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_c_att_location
, att
[0]));
1431 checkGLcall("glUniform1f");
1432 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_l_att_location
, att
[1]));
1433 checkGLcall("glUniform1f");
1434 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_q_att_location
, att
[2]));
1435 checkGLcall("glUniform1f");
1438 static void shader_glsl_load_fog_uniform(const struct wined3d_context
*context
,
1439 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1441 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1442 struct wined3d_color color
;
1443 float start
, end
, scale
;
1450 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_FOGCOLOR
]);
1451 GL_EXTCALL(glUniform4fv(prog
->ps
.fog_color_location
, 1, &color
.r
));
1452 tmpvalue
.d
= state
->render_states
[WINED3D_RS_FOGDENSITY
];
1453 GL_EXTCALL(glUniform1f(prog
->ps
.fog_density_location
, tmpvalue
.f
));
1454 get_fog_start_end(context
, state
, &start
, &end
);
1455 scale
= 1.0f
/ (end
- start
);
1456 GL_EXTCALL(glUniform1f(prog
->ps
.fog_end_location
, end
));
1457 GL_EXTCALL(glUniform1f(prog
->ps
.fog_scale_location
, scale
));
1458 checkGLcall("fog emulation uniforms");
1461 static void shader_glsl_clip_plane_uniform(const struct wined3d_context
*context
,
1462 const struct wined3d_state
*state
, unsigned int index
, struct glsl_shader_prog_link
*prog
)
1464 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1465 struct wined3d_vec4 plane
;
1467 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1469 multiply_vector_matrix(&plane
, &state
->clip_planes
[index
], &state
->transforms
[WINED3D_TS_VIEW
]);
1471 plane
= state
->clip_planes
[index
];
1473 GL_EXTCALL(glUniform4fv(prog
->vs
.clip_planes_location
+ index
, 1, &plane
.x
));
1476 /* Context activation is done by the caller (state handler). */
1477 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program
*ps
,
1478 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
1480 struct wined3d_color float_key
[2];
1481 const struct wined3d_texture
*texture
= state
->textures
[0];
1483 wined3d_format_get_float_color_key(texture
->resource
.format
, &texture
->async
.src_blt_color_key
, float_key
);
1484 GL_EXTCALL(glUniform4fv(ps
->color_key_location
, 2, &float_key
[0].r
));
1487 /* Context activation is done by the caller (state handler). */
1488 static void shader_glsl_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1489 const struct wined3d_state
*state
)
1491 const struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
1492 const struct wined3d_shader
*vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
1493 const struct wined3d_shader
*pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
1494 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1495 struct shader_glsl_priv
*priv
= shader_priv
;
1496 float position_fixup
[4];
1499 struct glsl_shader_prog_link
*prog
= ctx_data
->glsl_program
;
1500 UINT constant_version
;
1504 /* No GLSL program set - nothing to do. */
1507 constant_version
= prog
->constant_version
;
1508 update_mask
= context
->constant_update_mask
& prog
->constant_update_mask
;
1510 if (update_mask
& WINED3D_SHADER_CONST_VS_F
)
1511 shader_glsl_load_constants_f(vshader
, gl_info
, state
->vs_consts_f
,
1512 prog
->vs
.uniform_f_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
1514 if (update_mask
& WINED3D_SHADER_CONST_VS_I
)
1515 shader_glsl_load_constants_i(vshader
, gl_info
, state
->vs_consts_i
,
1516 prog
->vs
.uniform_i_locations
, vshader
->reg_maps
.integer_constants
);
1518 if (update_mask
& WINED3D_SHADER_CONST_VS_B
)
1519 shader_glsl_load_constantsB(vshader
, gl_info
, prog
->vs
.uniform_b_locations
, state
->vs_consts_b
,
1520 vshader
->reg_maps
.boolean_constants
);
1522 if (update_mask
& WINED3D_SHADER_CONST_VS_CLIP_PLANES
)
1524 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
1525 shader_glsl_clip_plane_uniform(context
, state
, i
, prog
);
1528 if (update_mask
& WINED3D_SHADER_CONST_VS_POINTSIZE
)
1529 shader_glsl_pointsize_uniform(context
, state
, prog
);
1531 if (update_mask
& WINED3D_SHADER_CONST_POS_FIXUP
)
1533 shader_get_position_fixup(context
, state
, position_fixup
);
1534 if (state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
])
1535 GL_EXTCALL(glUniform4fv(prog
->gs
.pos_fixup_location
, 1, position_fixup
));
1537 GL_EXTCALL(glUniform4fv(prog
->vs
.pos_fixup_location
, 1, position_fixup
));
1538 checkGLcall("glUniform4fv");
1541 if (update_mask
& WINED3D_SHADER_CONST_FFP_MODELVIEW
)
1543 struct wined3d_matrix mat
;
1545 get_modelview_matrix(context
, state
, 0, &mat
);
1546 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[0], 1, FALSE
, &mat
._11
));
1547 checkGLcall("glUniformMatrix4fv");
1549 shader_glsl_ffp_vertex_normalmatrix_uniform(context
, state
, prog
);
1552 if (update_mask
& WINED3D_SHADER_CONST_FFP_VERTEXBLEND
)
1554 struct wined3d_matrix mat
;
1556 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
1558 if (prog
->vs
.modelview_matrix_location
[i
] == -1)
1561 get_modelview_matrix(context
, state
, i
, &mat
);
1562 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[i
], 1, FALSE
, &mat
._11
));
1563 checkGLcall("glUniformMatrix4fv");
1567 if (update_mask
& WINED3D_SHADER_CONST_FFP_PROJ
)
1569 struct wined3d_matrix projection
;
1571 get_projection_matrix(context
, state
, &projection
);
1572 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.projection_matrix_location
, 1, FALSE
, &projection
._11
));
1573 checkGLcall("glUniformMatrix4fv");
1576 if (update_mask
& WINED3D_SHADER_CONST_FFP_TEXMATRIX
)
1578 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1579 shader_glsl_ffp_vertex_texmatrix_uniform(context
, state
, i
, prog
);
1582 if (update_mask
& WINED3D_SHADER_CONST_FFP_MATERIAL
)
1583 shader_glsl_ffp_vertex_material_uniform(context
, state
, prog
);
1585 if (update_mask
& WINED3D_SHADER_CONST_FFP_LIGHTS
)
1587 unsigned int point_idx
, spot_idx
, directional_idx
, parallel_point_idx
;
1588 DWORD point_count
= 0;
1589 DWORD spot_count
= 0;
1590 DWORD directional_count
= 0;
1591 DWORD parallel_point_count
= 0;
1593 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
1595 if (!state
->lights
[i
])
1598 switch (state
->lights
[i
]->OriginalParms
.type
)
1600 case WINED3D_LIGHT_POINT
:
1603 case WINED3D_LIGHT_SPOT
:
1606 case WINED3D_LIGHT_DIRECTIONAL
:
1607 ++directional_count
;
1609 case WINED3D_LIGHT_PARALLELPOINT
:
1610 ++parallel_point_count
;
1613 FIXME("Unhandled light type %#x.\n", state
->lights
[i
]->OriginalParms
.type
);
1618 spot_idx
= point_idx
+ point_count
;
1619 directional_idx
= spot_idx
+ spot_count
;
1620 parallel_point_idx
= directional_idx
+ directional_count
;
1622 shader_glsl_ffp_vertex_lightambient_uniform(context
, state
, prog
);
1623 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
1625 const struct wined3d_light_info
*light_info
= state
->lights
[i
];
1631 switch (light_info
->OriginalParms
.type
)
1633 case WINED3D_LIGHT_POINT
:
1636 case WINED3D_LIGHT_SPOT
:
1639 case WINED3D_LIGHT_DIRECTIONAL
:
1640 idx
= directional_idx
++;
1642 case WINED3D_LIGHT_PARALLELPOINT
:
1643 idx
= parallel_point_idx
++;
1646 FIXME("Unhandled light type %#x.\n", light_info
->OriginalParms
.type
);
1649 shader_glsl_ffp_vertex_light_uniform(context
, state
, idx
, light_info
, prog
);
1653 if (update_mask
& WINED3D_SHADER_CONST_PS_F
)
1654 shader_glsl_load_constants_f(pshader
, gl_info
, state
->ps_consts_f
,
1655 prog
->ps
.uniform_f_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
1657 if (update_mask
& WINED3D_SHADER_CONST_PS_I
)
1658 shader_glsl_load_constants_i(pshader
, gl_info
, state
->ps_consts_i
,
1659 prog
->ps
.uniform_i_locations
, pshader
->reg_maps
.integer_constants
);
1661 if (update_mask
& WINED3D_SHADER_CONST_PS_B
)
1662 shader_glsl_load_constantsB(pshader
, gl_info
, prog
->ps
.uniform_b_locations
, state
->ps_consts_b
,
1663 pshader
->reg_maps
.boolean_constants
);
1665 if (update_mask
& WINED3D_SHADER_CONST_PS_BUMP_ENV
)
1667 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1669 if (prog
->ps
.bumpenv_mat_location
[i
] == -1)
1672 GL_EXTCALL(glUniformMatrix2fv(prog
->ps
.bumpenv_mat_location
[i
], 1, 0,
1673 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
]));
1675 if (prog
->ps
.bumpenv_lum_scale_location
[i
] != -1)
1677 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_scale_location
[i
], 1,
1678 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
]));
1679 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_offset_location
[i
], 1,
1680 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
]));
1684 checkGLcall("bump env uniforms");
1687 if (update_mask
& WINED3D_SHADER_CONST_PS_Y_CORR
)
1689 const struct wined3d_vec4 correction_params
=
1691 /* Position is relative to the framebuffer, not the viewport. */
1692 context
->render_offscreen
? 0.0f
: (float)state
->fb
->render_targets
[0]->height
,
1693 context
->render_offscreen
? 1.0f
: -1.0f
,
1698 GL_EXTCALL(glUniform4fv(prog
->ps
.ycorrection_location
, 1, &correction_params
.x
));
1701 if (update_mask
& WINED3D_SHADER_CONST_PS_NP2_FIXUP
)
1702 shader_glsl_load_np2fixup_constants(&prog
->ps
, gl_info
, state
);
1703 if (update_mask
& WINED3D_SHADER_CONST_FFP_COLOR_KEY
)
1704 shader_glsl_load_color_key_constant(&prog
->ps
, gl_info
, state
);
1706 if (update_mask
& WINED3D_SHADER_CONST_FFP_PS
)
1708 struct wined3d_color color
;
1710 if (prog
->ps
.tex_factor_location
!= -1)
1712 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_TEXTUREFACTOR
]);
1713 GL_EXTCALL(glUniform4fv(prog
->ps
.tex_factor_location
, 1, &color
.r
));
1716 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1717 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 1.0f
, 1.0f
, 1.0f
, 0.0f
));
1719 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
1721 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1723 if (prog
->ps
.tss_constant_location
[i
] == -1)
1726 wined3d_color_from_d3dcolor(&color
, state
->texture_states
[i
][WINED3D_TSS_CONSTANT
]);
1727 GL_EXTCALL(glUniform4fv(prog
->ps
.tss_constant_location
[i
], 1, &color
.r
));
1730 checkGLcall("fixed function uniforms");
1733 if (update_mask
& WINED3D_SHADER_CONST_PS_FOG
)
1734 shader_glsl_load_fog_uniform(context
, state
, prog
);
1736 if (update_mask
& WINED3D_SHADER_CONST_PS_ALPHA_TEST
)
1738 float ref
= state
->render_states
[WINED3D_RS_ALPHAREF
] / 255.0f
;
1740 GL_EXTCALL(glUniform1f(prog
->ps
.alpha_test_ref_location
, ref
));
1741 checkGLcall("alpha test emulation uniform");
1744 if (priv
->next_constant_version
== UINT_MAX
)
1746 TRACE("Max constant version reached, resetting to 0.\n");
1747 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
1748 priv
->next_constant_version
= 1;
1752 prog
->constant_version
= priv
->next_constant_version
++;
1756 static void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
, DWORD new_version
)
1758 struct constant_entry
*entries
= heap
->entries
;
1759 unsigned int *positions
= heap
->positions
;
1760 unsigned int heap_idx
, parent_idx
;
1762 if (!heap
->contained
[idx
])
1764 heap_idx
= heap
->size
++;
1765 heap
->contained
[idx
] = TRUE
;
1769 heap_idx
= positions
[idx
];
1772 while (heap_idx
> 1)
1774 parent_idx
= heap_idx
>> 1;
1776 if (new_version
<= entries
[parent_idx
].version
) break;
1778 entries
[heap_idx
] = entries
[parent_idx
];
1779 positions
[entries
[parent_idx
].idx
] = heap_idx
;
1780 heap_idx
= parent_idx
;
1783 entries
[heap_idx
].version
= new_version
;
1784 entries
[heap_idx
].idx
= idx
;
1785 positions
[idx
] = heap_idx
;
1788 static void shader_glsl_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1790 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1791 struct constant_heap
*heap
= &priv
->vconst_heap
;
1794 for (i
= start
; i
< count
+ start
; ++i
)
1796 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1800 static void shader_glsl_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1802 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1803 struct constant_heap
*heap
= &priv
->pconst_heap
;
1806 for (i
= start
; i
< count
+ start
; ++i
)
1808 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1812 static unsigned int vec4_varyings(DWORD shader_major
, const struct wined3d_gl_info
*gl_info
)
1814 unsigned int ret
= gl_info
->limits
.glsl_varyings
/ 4;
1815 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1816 if(shader_major
> 3) return ret
;
1818 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1819 if (gl_info
->quirks
& WINED3D_QUIRK_GLSL_CLIP_VARYING
) ret
-= 1;
1823 static BOOL
needs_legacy_glsl_syntax(const struct wined3d_gl_info
*gl_info
)
1825 return gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
1828 static BOOL
shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info
*gl_info
)
1830 return !needs_legacy_glsl_syntax(gl_info
) && gl_info
->supported
[ARB_EXPLICIT_ATTRIB_LOCATION
];
1833 static const char *get_attribute_keyword(const struct wined3d_gl_info
*gl_info
)
1835 return needs_legacy_glsl_syntax(gl_info
) ? "attribute" : "in";
1838 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info
*gl_info
,
1839 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1844 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1845 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "in");
1848 va_start(args
, format
);
1849 ret
= shader_vaddline(buffer
, format
, args
);
1853 if (!string_buffer_resize(buffer
, ret
))
1858 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info
*gl_info
,
1859 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1864 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1865 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "out");
1868 va_start(args
, format
);
1869 ret
= shader_vaddline(buffer
, format
, args
);
1873 if (!string_buffer_resize(buffer
, ret
))
1878 static const char *get_fragment_output(const struct wined3d_gl_info
*gl_info
)
1880 return needs_legacy_glsl_syntax(gl_info
) ? "gl_FragData" : "ps_out";
1883 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
1885 switch (primitive_type
)
1887 case WINED3D_PT_POINTLIST
:
1890 case WINED3D_PT_LINELIST
:
1893 case WINED3D_PT_LINESTRIP
:
1894 return "line_strip";
1896 case WINED3D_PT_TRIANGLELIST
:
1899 case WINED3D_PT_TRIANGLESTRIP
:
1900 return "triangle_strip";
1902 case WINED3D_PT_LINELIST_ADJ
:
1903 return "lines_adjacency";
1905 case WINED3D_PT_TRIANGLELIST_ADJ
:
1906 return "triangles_adjacency";
1909 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
1914 static BOOL
glsl_is_color_reg_read(const struct wined3d_shader
*shader
, unsigned int idx
)
1916 const struct wined3d_shader_signature
*input_signature
= &shader
->input_signature
;
1917 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1918 DWORD input_reg_used
= shader
->u
.ps
.input_reg_used
;
1921 if (reg_maps
->shader_version
.major
< 3)
1922 return input_reg_used
& (1u << idx
);
1924 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1926 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
1928 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
1931 if (shader_match_semantic(input
->semantic_name
, WINED3D_DECL_USAGE_COLOR
)
1932 && input
->semantic_idx
== idx
)
1933 return input_reg_used
& (1u << input
->register_idx
);
1938 static BOOL
glsl_is_shadow_sampler(const struct wined3d_shader
*shader
,
1939 const struct ps_compile_args
*ps_args
, unsigned int resource_idx
, unsigned int sampler_idx
)
1941 const struct wined3d_shader_version
*version
= &shader
->reg_maps
.shader_version
;
1943 if (version
->major
>= 4)
1944 return shader
->reg_maps
.sampler_comparison_mode
& (1u << sampler_idx
);
1946 return version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& (ps_args
->shadow
& (1u << resource_idx
));
1949 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer
*buffer
,
1950 const struct wined3d_gl_info
*gl_info
, const char *vector_type
, const char *scalar_type
,
1953 shader_addline(buffer
, "%s %s4 vs_in_%s%u;\n",
1954 get_attribute_keyword(gl_info
), vector_type
, scalar_type
, index
);
1955 shader_addline(buffer
, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1956 index
, scalar_type
, scalar_type
, index
);
1959 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer
*buffer
,
1960 const struct wined3d_gl_info
*gl_info
, const struct wined3d_shader_signature_element
*e
)
1962 unsigned int index
= e
->register_idx
;
1964 if (e
->sysval_semantic
== WINED3D_SV_VERTEX_ID
)
1966 shader_addline(buffer
, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1970 if (e
->sysval_semantic
== WINED3D_SV_INSTANCE_ID
)
1972 shader_addline(buffer
, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1976 if (e
->sysval_semantic
&& e
->sysval_semantic
!= WINED3D_SV_POSITION
)
1977 FIXME("Unhandled sysval semantic %#x.\n", e
->sysval_semantic
);
1979 if (shader_glsl_use_explicit_attrib_location(gl_info
))
1980 shader_addline(buffer
, "layout(location = %u) ", index
);
1982 switch (e
->component_type
)
1984 case WINED3D_TYPE_UINT
:
1985 shader_glsl_declare_typed_vertex_attribute(buffer
, gl_info
, "uvec", "uint", index
);
1987 case WINED3D_TYPE_INT
:
1988 shader_glsl_declare_typed_vertex_attribute(buffer
, gl_info
, "ivec", "int", index
);
1992 FIXME("Unhandled type %#x.\n", e
->component_type
);
1994 case WINED3D_TYPE_UNKNOWN
:
1995 case WINED3D_TYPE_FLOAT
:
1996 shader_addline(buffer
, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info
), index
);
2001 /** Generate the variable & register declarations for the GLSL output target */
2002 static void shader_generate_glsl_declarations(const struct wined3d_context
*context
,
2003 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
2004 const struct wined3d_shader_reg_maps
*reg_maps
, const struct shader_glsl_ctx_priv
*ctx_priv
)
2006 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
2007 const struct vs_compile_args
*vs_args
= ctx_priv
->cur_vs_args
;
2008 const struct ps_compile_args
*ps_args
= ctx_priv
->cur_ps_args
;
2009 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2010 const struct wined3d_shader_indexable_temp
*idx_temp_reg
;
2011 unsigned int uniform_block_base
, uniform_block_count
;
2012 unsigned int i
, extra_constants_needed
= 0;
2013 const struct wined3d_shader_lconst
*lconst
;
2017 prefix
= shader_glsl_get_prefix(version
->type
);
2019 /* Prototype the subroutines */
2020 for (i
= 0, map
= reg_maps
->labels
; map
; map
>>= 1, ++i
)
2022 if (map
& 1) shader_addline(buffer
, "void subroutine%u();\n", i
);
2025 /* Declare the constants (aka uniforms) */
2026 if (shader
->limits
->constant_float
> 0)
2028 unsigned max_constantsF
;
2030 /* Unless the shader uses indirect addressing, always declare the
2031 * maximum array size and ignore that we need some uniforms privately.
2032 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2033 * and immediate values, still declare VC[256]. If the shader needs
2034 * more uniforms than we have it won't work in any case. If it uses
2035 * less, the compiler will figure out which uniforms are really used
2036 * and strip them out. This allows a shader to use c255 on a dx9 card,
2037 * as long as it doesn't also use all the other constants.
2039 * If the shader uses indirect addressing the compiler must assume
2040 * that all declared uniforms are used. In this case, declare only the
2041 * amount that we're assured to have.
2043 * Thus we run into problems in these two cases:
2044 * 1) The shader really uses more uniforms than supported.
2045 * 2) The shader uses indirect addressing, less constants than
2046 * supported, but uses a constant index > #supported consts. */
2047 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2049 /* No indirect addressing here. */
2050 max_constantsF
= gl_info
->limits
.glsl_ps_float_constants
;
2054 if (reg_maps
->usesrelconstF
)
2056 /* Subtract the other potential uniforms from the max
2057 * available (bools, ints, and 1 row of projection matrix).
2058 * Subtract another uniform for immediate values, which have
2059 * to be loaded via uniform by the driver as well. The shader
2060 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2061 * shader code, so one vec4 should be enough. (Unfortunately
2062 * the Nvidia driver doesn't store 128 and -128 in one float).
2064 * Writing gl_ClipVertex requires one uniform for each
2065 * clipplane as well. */
2066 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
- 3;
2067 if (vs_args
->clip_enabled
)
2068 max_constantsF
-= gl_info
->limits
.user_clip_distances
;
2069 max_constantsF
-= wined3d_popcount(reg_maps
->integer_constants
);
2070 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2071 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2072 * for now take this into account when calculating the number of available constants
2074 max_constantsF
-= wined3d_popcount(reg_maps
->boolean_constants
);
2075 /* Set by driver quirks in directx.c */
2076 max_constantsF
-= gl_info
->reserved_glsl_constants
;
2078 if (max_constantsF
< shader
->limits
->constant_float
)
2080 static unsigned int once
;
2083 ERR_(winediag
)("The hardware does not support enough uniform components to run this shader,"
2084 " it may not render correctly.\n");
2086 WARN("The hardware does not support enough uniform components to run this shader.\n");
2091 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
;
2094 max_constantsF
= min(shader
->limits
->constant_float
, max_constantsF
);
2095 shader_addline(buffer
, "uniform vec4 %s_c[%u];\n", prefix
, max_constantsF
);
2098 /* Always declare the full set of constants, the compiler can remove the
2099 * unused ones because d3d doesn't (yet) support indirect int and bool
2100 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2101 if (shader
->limits
->constant_int
> 0 && reg_maps
->integer_constants
)
2102 shader_addline(buffer
, "uniform ivec4 %s_i[%u];\n", prefix
, shader
->limits
->constant_int
);
2104 if (shader
->limits
->constant_bool
> 0 && reg_maps
->boolean_constants
)
2105 shader_addline(buffer
, "uniform bool %s_b[%u];\n", prefix
, shader
->limits
->constant_bool
);
2107 /* Declare immediate constant buffer */
2109 shader_addline(buffer
, "uniform vec4 %s_icb[%u];\n", prefix
, reg_maps
->icb
->vec4_count
);
2111 /* Declare constant buffers */
2112 wined3d_gl_limits_get_uniform_block_range(&gl_info
->limits
, version
->type
,
2113 &uniform_block_base
, &uniform_block_count
);
2114 for (i
= 0; i
< min(uniform_block_count
, WINED3D_MAX_CBS
); ++i
)
2116 if (reg_maps
->cb_sizes
[i
])
2118 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
2119 shader_addline(buffer
, "layout(binding = %u)\n", uniform_block_base
+ i
);
2120 shader_addline(buffer
, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2121 prefix
, i
, prefix
, i
, reg_maps
->cb_sizes
[i
]);
2125 /* Declare texture samplers */
2126 for (i
= 0; i
< reg_maps
->sampler_map
.count
; ++i
)
2128 struct wined3d_shader_sampler_map_entry
*entry
;
2129 const char *sampler_type_prefix
, *sampler_type
;
2130 BOOL shadow_sampler
, tex_rect
;
2132 entry
= ®_maps
->sampler_map
.entries
[i
];
2134 if (entry
->resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
2136 ERR("Invalid resource index %u.\n", entry
->resource_idx
);
2140 switch (reg_maps
->resource_info
[entry
->resource_idx
].data_type
)
2142 case WINED3D_DATA_FLOAT
:
2143 case WINED3D_DATA_UNORM
:
2144 case WINED3D_DATA_SNORM
:
2145 sampler_type_prefix
= "";
2148 case WINED3D_DATA_INT
:
2149 sampler_type_prefix
= "i";
2152 case WINED3D_DATA_UINT
:
2153 sampler_type_prefix
= "u";
2157 sampler_type_prefix
= "";
2158 ERR("Unhandled resource data type %#x.\n", reg_maps
->resource_info
[i
].data_type
);
2162 shadow_sampler
= glsl_is_shadow_sampler(shader
, ps_args
, entry
->resource_idx
, entry
->sampler_idx
);
2163 switch (reg_maps
->resource_info
[entry
->resource_idx
].type
)
2165 case WINED3D_SHADER_RESOURCE_BUFFER
:
2166 sampler_type
= "samplerBuffer";
2169 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
2171 sampler_type
= "sampler1DShadow";
2173 sampler_type
= "sampler1D";
2176 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2177 tex_rect
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
2178 && (ps_args
->np2_fixup
& (1u << entry
->resource_idx
))
2179 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
2183 sampler_type
= "sampler2DRectShadow";
2185 sampler_type
= "sampler2DShadow";
2190 sampler_type
= "sampler2DRect";
2192 sampler_type
= "sampler2D";
2196 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2198 FIXME("Unsupported 3D shadow sampler.\n");
2199 sampler_type
= "sampler3D";
2202 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
2204 FIXME("Unsupported Cube shadow sampler.\n");
2205 sampler_type
= "samplerCube";
2208 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
2210 sampler_type
= "sampler2DArrayShadow";
2212 sampler_type
= "sampler2DArray";
2215 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY
:
2217 FIXME("Unsupported Cube array shadow sampler.\n");
2218 sampler_type
= "samplerCubeArray";
2222 sampler_type
= "unsupported_sampler";
2223 FIXME("Unhandled resource type %#x.\n", reg_maps
->resource_info
[entry
->resource_idx
].type
);
2226 shader_addline(buffer
, "uniform %s%s %s_sampler%u;\n",
2227 sampler_type_prefix
, sampler_type
, prefix
, entry
->bind_idx
);
2230 /* Declare images */
2231 for (i
= 0; i
< ARRAY_SIZE(reg_maps
->uav_resource_info
); ++i
)
2233 const char *image_type_prefix
, *image_type
, *read_format
;
2235 if (!reg_maps
->uav_resource_info
[i
].type
)
2238 switch (reg_maps
->uav_resource_info
[i
].data_type
)
2240 case WINED3D_DATA_FLOAT
:
2241 case WINED3D_DATA_UNORM
:
2242 case WINED3D_DATA_SNORM
:
2243 image_type_prefix
= "";
2244 read_format
= "r32f";
2247 case WINED3D_DATA_INT
:
2248 image_type_prefix
= "i";
2249 read_format
= "r32i";
2252 case WINED3D_DATA_UINT
:
2253 image_type_prefix
= "u";
2254 read_format
= "r32ui";
2258 image_type_prefix
= "";
2260 ERR("Unhandled resource data type %#x.\n", reg_maps
->uav_resource_info
[i
].data_type
);
2264 switch (reg_maps
->uav_resource_info
[i
].type
)
2266 case WINED3D_SHADER_RESOURCE_BUFFER
:
2267 image_type
= "imageBuffer";
2270 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2271 image_type
= "image2D";
2274 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2275 image_type
= "image3D";
2278 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
2279 image_type
= "image2DArray";
2283 image_type
= "unsupported_image";
2284 FIXME("Unhandled resource type %#x.\n", reg_maps
->uav_resource_info
[i
].type
);
2288 if (shader_glsl_use_layout_binding_qualifier(gl_info
))
2289 shader_addline(buffer
, "layout(binding = %u)\n", i
);
2290 if (reg_maps
->uav_read_mask
& (1u << i
))
2291 shader_addline(buffer
, "layout(%s) uniform %s%s %s_image%u;\n",
2292 read_format
, image_type_prefix
, image_type
, prefix
, i
);
2294 shader_addline(buffer
, "writeonly uniform %s%s %s_image%u;\n",
2295 image_type_prefix
, image_type
, prefix
, i
);
2297 if (reg_maps
->uav_counter_mask
& (1u << i
))
2298 shader_addline(buffer
, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2302 /* Declare uniforms for NP2 texcoord fixup:
2303 * This is NOT done inside the loop that declares the texture samplers
2304 * since the NP2 fixup code is currently only used for the GeforceFX
2305 * series and when forcing the ARB_npot extension off. Modern cards just
2306 * skip the code anyway, so put it inside a separate loop. */
2307 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& ps_args
->np2_fixup
)
2309 struct ps_np2fixup_info
*fixup
= ctx_priv
->cur_np2fixup_info
;
2312 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2313 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2314 * samplerNP2Fixup stores texture dimensions and is updated through
2315 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2317 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2319 if (!reg_maps
->resource_info
[i
].type
|| !(ps_args
->np2_fixup
& (1u << i
)))
2322 if (reg_maps
->resource_info
[i
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_2D
)
2324 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2328 fixup
->idx
[i
] = cur
++;
2331 fixup
->num_consts
= (cur
+ 1) >> 1;
2332 fixup
->active
= ps_args
->np2_fixup
;
2333 shader_addline(buffer
, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix
, fixup
->num_consts
);
2336 /* Declare address variables */
2337 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
2339 if (map
& 1) shader_addline(buffer
, "ivec4 A%u;\n", i
);
2342 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
2344 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
2345 shader_glsl_declare_generic_vertex_attribute(buffer
, gl_info
, &shader
->input_signature
.elements
[i
]);
2347 if (vs_args
->point_size
&& !vs_args
->per_vertex_point_size
)
2349 shader_addline(buffer
, "uniform struct\n{\n");
2350 shader_addline(buffer
, " float size;\n");
2351 shader_addline(buffer
, " float size_min;\n");
2352 shader_addline(buffer
, " float size_max;\n");
2353 shader_addline(buffer
, "} ffp_point;\n");
2356 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2358 if (vs_args
->clip_enabled
)
2359 shader_addline(buffer
, "uniform vec4 clip_planes[%u];\n", gl_info
->limits
.user_clip_distances
);
2361 if (version
->major
< 3)
2363 declare_out_varying(gl_info
, buffer
, vs_args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
2364 declare_out_varying(gl_info
, buffer
, vs_args
->flatshading
, "vec4 ffp_varying_specular;\n");
2365 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
2366 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
2370 if (version
->major
< 4)
2371 shader_addline(buffer
, "void setup_vs_output(in vec4[%u]);\n", shader
->limits
->packed_output
);
2373 else if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
2375 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2377 shader_addline(buffer
, "varying in vec4 gs_in[][%u];\n", shader
->limits
->packed_input
);
2381 shader_addline(buffer
, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader
->u
.gs
.input_type
));
2382 shader_addline(buffer
, "layout(%s, max_vertices = %u) out;\n",
2383 glsl_primitive_type_from_d3d(shader
->u
.gs
.output_type
), shader
->u
.gs
.vertices_out
);
2384 shader_addline(buffer
, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader
->limits
->packed_input
);
2387 else if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2389 if (version
->major
< 3 || ps_args
->vp_mode
!= vertexshader
)
2391 shader_addline(buffer
, "uniform struct\n{\n");
2392 shader_addline(buffer
, " vec4 color;\n");
2393 shader_addline(buffer
, " float density;\n");
2394 shader_addline(buffer
, " float end;\n");
2395 shader_addline(buffer
, " float scale;\n");
2396 shader_addline(buffer
, "} ffp_fog;\n");
2398 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2400 if (glsl_is_color_reg_read(shader
, 0))
2401 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
2402 if (glsl_is_color_reg_read(shader
, 1))
2403 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
2404 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
2405 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
2409 if (glsl_is_color_reg_read(shader
, 0))
2410 declare_in_varying(gl_info
, buffer
, ps_args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
2411 if (glsl_is_color_reg_read(shader
, 1))
2412 declare_in_varying(gl_info
, buffer
, ps_args
->flatshading
, "vec4 ffp_varying_specular;\n");
2413 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
2414 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
2415 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
2419 if (version
->major
>= 3)
2421 UINT in_count
= min(vec4_varyings(version
->major
, gl_info
), shader
->limits
->packed_input
);
2423 if (ps_args
->vp_mode
== vertexshader
)
2424 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 %s_link[%u];\n", prefix
, in_count
);
2425 shader_addline(buffer
, "vec4 %s_in[%u];\n", prefix
, in_count
);
2428 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
2433 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", i
);
2435 if (reg_maps
->luminanceparams
& (1u << i
))
2437 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", i
);
2438 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", i
);
2439 extra_constants_needed
++;
2442 extra_constants_needed
++;
2445 if (ps_args
->srgb_correction
)
2447 shader_addline(buffer
, "const vec4 srgb_const0 = ");
2448 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
2449 shader_addline(buffer
, ";\n");
2450 shader_addline(buffer
, "const vec4 srgb_const1 = ");
2451 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
2452 shader_addline(buffer
, ";\n");
2454 if (reg_maps
->vpos
|| reg_maps
->usesdsy
)
2456 if (reg_maps
->usesdsy
|| !gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
2458 ++extra_constants_needed
;
2459 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
2463 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
2465 if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_PIXEL_CENTER_INTEGER
)
2466 shader_addline(buffer
, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2467 ps_args
->render_offscreen
? "" : "origin_upper_left, ");
2468 else if (!ps_args
->render_offscreen
)
2469 shader_addline(buffer
, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2471 shader_addline(buffer
, "vec4 vpos;\n");
2475 if (ps_args
->alpha_test_func
+ 1 != WINED3D_CMP_ALWAYS
)
2476 shader_addline(buffer
, "uniform float alpha_test_ref;\n");
2478 if (!needs_legacy_glsl_syntax(gl_info
))
2479 shader_addline(buffer
, "out vec4 ps_out[%u];\n", gl_info
->limits
.buffers
);
2481 if (shader
->limits
->constant_float
+ extra_constants_needed
>= gl_info
->limits
.glsl_ps_float_constants
)
2482 FIXME("Insufficient uniforms to run this shader.\n");
2485 /* Declare output register temporaries */
2486 if (shader
->limits
->packed_output
)
2487 shader_addline(buffer
, "vec4 %s_out[%u];\n", prefix
, shader
->limits
->packed_output
);
2489 /* Declare temporary variables */
2490 if (reg_maps
->temporary_count
)
2492 for (i
= 0; i
< reg_maps
->temporary_count
; ++i
)
2493 shader_addline(buffer
, "vec4 R%u;\n", i
);
2497 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
2500 shader_addline(buffer
, "vec4 R%u;\n", i
);
2504 /* Declare indexable temporary variables */
2505 LIST_FOR_EACH_ENTRY(idx_temp_reg
, ®_maps
->indexable_temps
, struct wined3d_shader_indexable_temp
, entry
)
2507 if (idx_temp_reg
->component_count
!= 4)
2508 FIXME("Ignoring component count %u.\n", idx_temp_reg
->component_count
);
2509 shader_addline(buffer
, "vec4 X%u[%u];\n", idx_temp_reg
->register_idx
, idx_temp_reg
->register_size
);
2512 /* Declare loop registers aLx */
2513 if (version
->major
< 4)
2515 for (i
= 0; i
< reg_maps
->loop_depth
; ++i
)
2517 shader_addline(buffer
, "int aL%u;\n", i
);
2518 shader_addline(buffer
, "int tmpInt%u;\n", i
);
2522 /* Temporary variables for matrix operations */
2523 shader_addline(buffer
, "vec4 tmp0;\n");
2524 shader_addline(buffer
, "vec4 tmp1;\n");
2526 if (!shader
->load_local_constsF
)
2528 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
2530 shader_addline(buffer
, "const vec4 %s_lc%u = ", prefix
, lconst
->idx
);
2531 shader_glsl_append_imm_vec4(buffer
, (const float *)lconst
->value
);
2532 shader_addline(buffer
, ";\n");
2537 /*****************************************************************************
2538 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2540 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2541 ****************************************************************************/
2544 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2545 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
);
2547 /** Used for opcode modifiers - They multiply the result by the specified amount */
2548 static const char * const shift_glsl_tab
[] = {
2550 "2.0 * ", /* 1 (x2) */
2551 "4.0 * ", /* 2 (x4) */
2552 "8.0 * ", /* 3 (x8) */
2553 "16.0 * ", /* 4 (x16) */
2554 "32.0 * ", /* 5 (x32) */
2561 "0.0625 * ", /* 12 (d16) */
2562 "0.125 * ", /* 13 (d8) */
2563 "0.25 * ", /* 14 (d4) */
2564 "0.5 * " /* 15 (d2) */
2567 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2568 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier
,
2569 const char *in_reg
, const char *in_regswizzle
, char *out_str
)
2571 switch (src_modifier
)
2573 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
2574 case WINED3DSPSM_DW
:
2575 case WINED3DSPSM_NONE
:
2576 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2578 case WINED3DSPSM_NEG
:
2579 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
2581 case WINED3DSPSM_NOT
:
2582 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
2584 case WINED3DSPSM_BIAS
:
2585 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2587 case WINED3DSPSM_BIASNEG
:
2588 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2590 case WINED3DSPSM_SIGN
:
2591 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2593 case WINED3DSPSM_SIGNNEG
:
2594 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2596 case WINED3DSPSM_COMP
:
2597 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
2599 case WINED3DSPSM_X2
:
2600 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
2602 case WINED3DSPSM_X2NEG
:
2603 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
2605 case WINED3DSPSM_ABS
:
2606 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
2608 case WINED3DSPSM_ABSNEG
:
2609 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
2612 FIXME("Unhandled modifier %u\n", src_modifier
);
2613 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2617 /** Writes the GLSL variable name that corresponds to the register that the
2618 * DX opcode parameter is trying to access */
2619 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
2620 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
2622 /* oPos, oFog and oPts in D3D */
2623 static const char * const hwrastout_reg_names
[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2625 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
2626 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
2627 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
2628 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
2629 const char *prefix
= shader_glsl_get_prefix(version
->type
);
2630 struct glsl_src_param rel_param0
, rel_param1
;
2631 char imm_str
[4][17];
2633 if (reg
->idx
[0].offset
!= ~0U && reg
->idx
[0].rel_addr
)
2634 shader_glsl_add_src_param(ins
, reg
->idx
[0].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param0
);
2635 if (reg
->idx
[1].offset
!= ~0U && reg
->idx
[1].rel_addr
)
2636 shader_glsl_add_src_param(ins
, reg
->idx
[1].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param1
);
2641 case WINED3DSPR_TEMP
:
2642 sprintf(register_name
, "R%u", reg
->idx
[0].offset
);
2645 case WINED3DSPR_INPUT
:
2646 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
2648 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2650 if (reg
->idx
[0].rel_addr
)
2651 FIXME("VS3+ input registers relative addressing.\n");
2652 if (priv
->cur_vs_args
->swizzle_map
& (1u << reg
->idx
[0].offset
))
2654 sprintf(register_name
, "%s_in%u", prefix
, reg
->idx
[0].offset
);
2658 if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
2660 if (reg
->idx
[0].rel_addr
)
2662 if (reg
->idx
[1].rel_addr
)
2663 sprintf(register_name
, "gs_in[%s + %u]%s[%s + %u]",
2664 rel_param0
.param_str
, reg
->idx
[0].offset
,
2665 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2666 rel_param1
.param_str
, reg
->idx
[1].offset
);
2668 sprintf(register_name
, "gs_in[%s + %u]%s[%u]",
2669 rel_param0
.param_str
, reg
->idx
[0].offset
,
2670 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2671 reg
->idx
[1].offset
);
2673 else if (reg
->idx
[1].rel_addr
)
2674 sprintf(register_name
, "gs_in[%u]%s[%s + %u]", reg
->idx
[0].offset
,
2675 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2676 rel_param1
.param_str
, reg
->idx
[1].offset
);
2678 sprintf(register_name
, "gs_in[%u]%s[%u]", reg
->idx
[0].offset
,
2679 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2680 reg
->idx
[1].offset
);
2684 /* pixel shaders >= 3.0 */
2685 if (version
->major
>= 3)
2687 DWORD idx
= shader
->u
.ps
.input_reg_map
[reg
->idx
[0].offset
];
2688 unsigned int in_count
= vec4_varyings(version
->major
, gl_info
);
2690 if (reg
->idx
[0].rel_addr
)
2692 /* Removing a + 0 would be an obvious optimization, but
2693 * OS X doesn't see the NOP operation there. */
2696 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2697 && shader
->u
.ps
.declared_in_count
> in_count
)
2699 sprintf(register_name
,
2700 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2701 rel_param0
.param_str
, idx
, in_count
- 1, rel_param0
.param_str
, idx
, in_count
,
2702 prefix
, rel_param0
.param_str
, idx
);
2706 sprintf(register_name
, "%s_in[%s + %u]", prefix
, rel_param0
.param_str
, idx
);
2711 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2712 && shader
->u
.ps
.declared_in_count
> in_count
)
2714 sprintf(register_name
, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2715 rel_param0
.param_str
, in_count
- 1, rel_param0
.param_str
, in_count
,
2716 prefix
, rel_param0
.param_str
);
2720 sprintf(register_name
, "%s_in[%s]", prefix
, rel_param0
.param_str
);
2726 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
2727 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
2728 else sprintf(register_name
, "%s_in[%u]", prefix
, idx
);
2733 if (!reg
->idx
[0].offset
)
2734 strcpy(register_name
, "ffp_varying_diffuse");
2736 strcpy(register_name
, "ffp_varying_specular");
2741 case WINED3DSPR_CONST
:
2743 /* Relative addressing */
2744 if (reg
->idx
[0].rel_addr
)
2746 if (wined3d_settings
.check_float_constants
)
2747 sprintf(register_name
, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2748 rel_param0
.param_str
, reg
->idx
[0].offset
,
2749 rel_param0
.param_str
, reg
->idx
[0].offset
, shader
->limits
->constant_float
,
2750 prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2751 else if (reg
->idx
[0].offset
)
2752 sprintf(register_name
, "%s_c[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2754 sprintf(register_name
, "%s_c[%s]", prefix
, rel_param0
.param_str
);
2758 if (shader_constant_is_local(shader
, reg
->idx
[0].offset
))
2759 sprintf(register_name
, "%s_lc%u", prefix
, reg
->idx
[0].offset
);
2761 sprintf(register_name
, "%s_c[%u]", prefix
, reg
->idx
[0].offset
);
2766 case WINED3DSPR_CONSTINT
:
2767 sprintf(register_name
, "%s_i[%u]", prefix
, reg
->idx
[0].offset
);
2770 case WINED3DSPR_CONSTBOOL
:
2771 sprintf(register_name
, "%s_b[%u]", prefix
, reg
->idx
[0].offset
);
2774 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
2775 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2776 sprintf(register_name
, "T%u", reg
->idx
[0].offset
);
2778 sprintf(register_name
, "A%u", reg
->idx
[0].offset
);
2781 case WINED3DSPR_LOOP
:
2782 sprintf(register_name
, "aL%u", ins
->ctx
->state
->current_loop_reg
- 1);
2785 case WINED3DSPR_SAMPLER
:
2786 sprintf(register_name
, "%s_sampler%u", prefix
, reg
->idx
[0].offset
);
2789 case WINED3DSPR_COLOROUT
:
2790 if (reg
->idx
[0].offset
>= gl_info
->limits
.buffers
)
2791 WARN("Write to render target %u, only %d supported.\n",
2792 reg
->idx
[0].offset
, gl_info
->limits
.buffers
);
2794 sprintf(register_name
, "%s[%u]", get_fragment_output(gl_info
), reg
->idx
[0].offset
);
2797 case WINED3DSPR_RASTOUT
:
2798 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
[0].offset
]);
2801 case WINED3DSPR_DEPTHOUT
:
2802 sprintf(register_name
, "gl_FragDepth");
2805 case WINED3DSPR_ATTROUT
:
2806 if (!reg
->idx
[0].offset
)
2807 sprintf(register_name
, "%s_out[8]", prefix
);
2809 sprintf(register_name
, "%s_out[9]", prefix
);
2812 case WINED3DSPR_TEXCRDOUT
:
2813 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2814 if (reg
->idx
[0].rel_addr
)
2815 FIXME("VS3 output registers relative addressing.\n");
2816 sprintf(register_name
, "%s_out[%u]", prefix
, reg
->idx
[0].offset
);
2819 case WINED3DSPR_MISCTYPE
:
2820 if (!reg
->idx
[0].offset
)
2823 sprintf(register_name
, "vpos");
2825 else if (reg
->idx
[0].offset
== 1)
2827 /* Note that gl_FrontFacing is a bool, while vFace is
2828 * a float for which the sign determines front/back */
2829 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
2833 FIXME("Unhandled misctype register %u.\n", reg
->idx
[0].offset
);
2834 sprintf(register_name
, "unrecognized_register");
2838 case WINED3DSPR_IMMCONST
:
2839 switch (reg
->immconst_type
)
2841 case WINED3D_IMMCONST_SCALAR
:
2842 switch (reg
->data_type
)
2844 case WINED3D_DATA_FLOAT
:
2845 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
2846 sprintf(register_name
, "uintBitsToFloat(%#xu)", reg
->u
.immconst_data
[0]);
2848 wined3d_ftoa(*(const float *)reg
->u
.immconst_data
, register_name
);
2850 case WINED3D_DATA_INT
:
2851 sprintf(register_name
, "%#x", reg
->u
.immconst_data
[0]);
2853 case WINED3D_DATA_RESOURCE
:
2854 case WINED3D_DATA_SAMPLER
:
2855 case WINED3D_DATA_UINT
:
2856 sprintf(register_name
, "%#xu", reg
->u
.immconst_data
[0]);
2859 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2864 case WINED3D_IMMCONST_VEC4
:
2865 switch (reg
->data_type
)
2867 case WINED3D_DATA_FLOAT
:
2868 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
2870 sprintf(register_name
, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2871 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
2872 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
2876 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[0], imm_str
[0]);
2877 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[1], imm_str
[1]);
2878 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[2], imm_str
[2]);
2879 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[3], imm_str
[3]);
2880 sprintf(register_name
, "vec4(%s, %s, %s, %s)",
2881 imm_str
[0], imm_str
[1], imm_str
[2], imm_str
[3]);
2884 case WINED3D_DATA_INT
:
2885 sprintf(register_name
, "ivec4(%#x, %#x, %#x, %#x)",
2886 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
2887 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
2889 case WINED3D_DATA_RESOURCE
:
2890 case WINED3D_DATA_SAMPLER
:
2891 case WINED3D_DATA_UINT
:
2892 sprintf(register_name
, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2893 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
2894 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
2897 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2903 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
2904 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
2908 case WINED3DSPR_CONSTBUFFER
:
2909 if (reg
->idx
[1].rel_addr
)
2910 sprintf(register_name
, "%s_cb%u[%s + %u]",
2911 prefix
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2913 sprintf(register_name
, "%s_cb%u[%u]", prefix
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2916 case WINED3DSPR_IMMCONSTBUFFER
:
2917 if (reg
->idx
[0].rel_addr
)
2918 sprintf(register_name
, "%s_icb[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2920 sprintf(register_name
, "%s_icb[%u]", prefix
, reg
->idx
[0].offset
);
2923 case WINED3DSPR_PRIMID
:
2924 sprintf(register_name
, "uint(gl_PrimitiveIDIn)");
2927 case WINED3DSPR_IDXTEMP
:
2928 if (reg
->idx
[1].rel_addr
)
2929 sprintf(register_name
, "X%u[%s + %u]", reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2931 sprintf(register_name
, "X%u[%u]", reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2934 case WINED3DSPR_LOCALTHREADINDEX
:
2935 if (gl_info
->supported
[ARB_SHADING_LANGUAGE_420PACK
])
2936 sprintf(register_name
, "int(gl_LocalInvocationIndex)");
2938 sprintf(register_name
, "ivec2(gl_LocalInvocationIndex, 0)");
2941 case WINED3DSPR_THREADID
:
2942 sprintf(register_name
, "ivec3(gl_GlobalInvocationID)");
2945 case WINED3DSPR_THREADGROUPID
:
2946 sprintf(register_name
, "ivec3(gl_WorkGroupID)");
2949 case WINED3DSPR_LOCALTHREADID
:
2950 sprintf(register_name
, "ivec3(gl_LocalInvocationID)");
2954 FIXME("Unhandled register type %#x.\n", reg
->type
);
2955 sprintf(register_name
, "unrecognized_register");
2960 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
2963 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
2964 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
2965 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
2966 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
2970 /* Get the GLSL write mask for the destination register */
2971 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
2973 DWORD mask
= param
->write_mask
;
2975 if (shader_is_scalar(¶m
->reg
))
2977 mask
= WINED3DSP_WRITEMASK_0
;
2982 shader_glsl_write_mask_to_str(mask
, write_mask
);
2988 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
)
2990 unsigned int size
= 0;
2992 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
2993 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
2994 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
2995 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
3000 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle
,
3001 unsigned int component_idx
)
3003 /* swizzle bits fields: wwzzyyxx */
3004 return (swizzle
>> (2 * component_idx
)) & 0x3;
3007 static void shader_glsl_swizzle_to_str(DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
3009 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3010 * but addressed as "rgba". To fix this we need to swap the register's x
3011 * and z components. */
3012 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
3016 for (i
= 0; i
< 4; ++i
)
3018 if (mask
& (WINED3DSP_WRITEMASK_0
<< i
))
3019 *str
++ = swizzle_chars
[shader_glsl_swizzle_get_component(swizzle
, i
)];
3024 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
3025 BOOL fixup
, DWORD mask
, char *swizzle_str
)
3027 if (shader_is_scalar(¶m
->reg
))
3028 *swizzle_str
= '\0';
3030 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
3033 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer
*dst_param
, const char *src_param
,
3034 enum wined3d_data_type dst_data_type
, enum wined3d_data_type src_data_type
)
3036 if (dst_data_type
== src_data_type
)
3038 string_buffer_sprintf(dst_param
, "%s", src_param
);
3042 if (src_data_type
== WINED3D_DATA_FLOAT
)
3044 switch (dst_data_type
)
3046 case WINED3D_DATA_INT
:
3047 string_buffer_sprintf(dst_param
, "floatBitsToInt(%s)", src_param
);
3049 case WINED3D_DATA_RESOURCE
:
3050 case WINED3D_DATA_SAMPLER
:
3051 case WINED3D_DATA_UINT
:
3052 string_buffer_sprintf(dst_param
, "floatBitsToUint(%s)", src_param
);
3059 if (src_data_type
== WINED3D_DATA_UINT
&& dst_data_type
== WINED3D_DATA_FLOAT
)
3061 string_buffer_sprintf(dst_param
, "uintBitsToFloat(%s)", src_param
);
3065 if (src_data_type
== WINED3D_DATA_INT
&& dst_data_type
== WINED3D_DATA_FLOAT
)
3067 string_buffer_sprintf(dst_param
, "intBitsToFloat(%s)", src_param
);
3071 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type
, dst_data_type
);
3072 string_buffer_sprintf(dst_param
, "%s", src_param
);
3075 /* From a given parameter token, generate the corresponding GLSL string.
3076 * Also, return the actual register name and swizzle in case the
3077 * caller needs this information as well. */
3078 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction
*ins
,
3079 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
,
3080 enum wined3d_data_type data_type
)
3082 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3083 struct wined3d_string_buffer
*reg_name
= string_buffer_get(priv
->string_buffers
);
3084 enum wined3d_data_type param_data_type
;
3085 BOOL is_color
= FALSE
;
3086 char swizzle_str
[6];
3088 glsl_src
->reg_name
[0] = '\0';
3089 glsl_src
->param_str
[0] = '\0';
3090 swizzle_str
[0] = '\0';
3092 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
3093 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
3095 switch (wined3d_src
->reg
.type
)
3097 case WINED3DSPR_IMMCONST
:
3098 param_data_type
= data_type
;
3100 case WINED3DSPR_PRIMID
:
3101 param_data_type
= WINED3D_DATA_UINT
;
3103 case WINED3DSPR_LOCALTHREADINDEX
:
3104 case WINED3DSPR_THREADID
:
3105 case WINED3DSPR_THREADGROUPID
:
3106 case WINED3DSPR_LOCALTHREADID
:
3107 param_data_type
= WINED3D_DATA_INT
;
3110 param_data_type
= WINED3D_DATA_FLOAT
;
3114 shader_glsl_sprintf_cast(reg_name
, glsl_src
->reg_name
, data_type
, param_data_type
);
3115 shader_glsl_gen_modifier(wined3d_src
->modifiers
, reg_name
->buffer
, swizzle_str
, glsl_src
->param_str
);
3117 string_buffer_release(priv
->string_buffers
, reg_name
);
3120 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
3121 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
)
3123 shader_glsl_add_src_param_ext(ins
, wined3d_src
, mask
, glsl_src
, wined3d_src
->reg
.data_type
);
3126 /* From a given parameter token, generate the corresponding GLSL string.
3127 * Also, return the actual register name and swizzle in case the
3128 * caller needs this information as well. */
3129 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
3130 const struct wined3d_shader_dst_param
*wined3d_dst
, struct glsl_dst_param
*glsl_dst
)
3132 BOOL is_color
= FALSE
;
3134 glsl_dst
->mask_str
[0] = '\0';
3135 glsl_dst
->reg_name
[0] = '\0';
3137 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
3138 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
3141 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3142 static DWORD
shader_glsl_append_dst_ext(struct wined3d_string_buffer
*buffer
,
3143 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
,
3144 enum wined3d_data_type data_type
)
3146 struct glsl_dst_param glsl_dst
;
3149 if ((mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
)))
3153 case WINED3D_DATA_FLOAT
:
3154 shader_addline(buffer
, "%s%s = %s(",
3155 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3157 case WINED3D_DATA_INT
:
3158 shader_addline(buffer
, "%s%s = %sintBitsToFloat(",
3159 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3161 case WINED3D_DATA_RESOURCE
:
3162 case WINED3D_DATA_SAMPLER
:
3163 case WINED3D_DATA_UINT
:
3164 shader_addline(buffer
, "%s%s = %suintBitsToFloat(",
3165 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3168 FIXME("Unhandled data type %#x.\n", data_type
);
3169 shader_addline(buffer
, "%s%s = %s(",
3170 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
3178 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3179 static DWORD
shader_glsl_append_dst(struct wined3d_string_buffer
*buffer
, const struct wined3d_shader_instruction
*ins
)
3181 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3184 /** Process GLSL instruction modifiers */
3185 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
3187 struct glsl_dst_param dst_param
;
3190 if (!ins
->dst_count
) return;
3192 modifiers
= ins
->dst
[0].modifiers
;
3193 if (!modifiers
) return;
3195 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3197 if (modifiers
& WINED3DSPDM_SATURATE
)
3199 /* _SAT means to clamp the value of the register to between 0 and 1 */
3200 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
3201 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
3204 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
3206 FIXME("_centroid modifier not handled\n");
3209 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
3211 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3215 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op
)
3219 case WINED3D_SHADER_REL_OP_GT
: return ">";
3220 case WINED3D_SHADER_REL_OP_EQ
: return "==";
3221 case WINED3D_SHADER_REL_OP_GE
: return ">=";
3222 case WINED3D_SHADER_REL_OP_LT
: return "<";
3223 case WINED3D_SHADER_REL_OP_NE
: return "!=";
3224 case WINED3D_SHADER_REL_OP_LE
: return "<=";
3226 FIXME("Unrecognized operator %#x.\n", op
);
3231 static BOOL
shader_glsl_has_core_grad(const struct wined3d_gl_info
*gl_info
,
3232 const struct wined3d_shader_version
*version
)
3234 return shader_glsl_get_version(gl_info
, version
) >= 130 || gl_info
->supported
[EXT_GPU_SHADER4
];
3237 static void shader_glsl_get_sample_function(const struct wined3d_shader_context
*ctx
,
3238 DWORD resource_idx
, DWORD sampler_idx
, DWORD flags
, struct glsl_sample_function
*sample_function
)
3240 enum wined3d_shader_resource_type resource_type
= ctx
->reg_maps
->resource_info
[resource_idx
].type
;
3241 struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
3242 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
3243 BOOL shadow
= glsl_is_shadow_sampler(ctx
->shader
, priv
->cur_ps_args
, resource_idx
, sampler_idx
);
3244 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
3245 BOOL texrect
= ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
3246 && priv
->cur_ps_args
->np2_fixup
& (1u << resource_idx
)
3247 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
3248 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
3249 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
3250 BOOL offset
= flags
& WINED3D_GLSL_SAMPLE_OFFSET
;
3251 const char *base
= "texture", *type_part
= "", *suffix
= "";
3252 unsigned int coord_size
, deriv_size
;
3255 sample_function
->data_type
= ctx
->reg_maps
->resource_info
[resource_idx
].data_type
;
3257 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
3259 ERR("Unexpected resource type %#x.\n", resource_type
);
3260 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
3262 array
= resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3263 || resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
;
3265 /* Note that there's no such thing as a projected cube texture. */
3266 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
3269 if (needs_legacy_glsl_syntax(gl_info
))
3274 type_part
= resource_type_info
[resource_type
].type_part
;
3275 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
&& texrect
)
3276 type_part
= "2DRect";
3277 if (!type_part
[0] && resource_type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY
)
3278 FIXME("Unhandled resource type %#x.\n", resource_type
);
3280 if (!lod
&& grad
&& !shader_glsl_has_core_grad(gl_info
, &ctx
->shader
->reg_maps
.shader_version
))
3282 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
3285 FIXME("Unsupported grad function.\n");
3289 if (flags
& WINED3D_GLSL_SAMPLE_LOAD
)
3291 static const DWORD texel_fetch_flags
= WINED3D_GLSL_SAMPLE_LOAD
| WINED3D_GLSL_SAMPLE_OFFSET
;
3292 if (flags
& ~texel_fetch_flags
)
3293 ERR("Unexpected flags %#x for texelFetch.\n", flags
& ~texel_fetch_flags
);
3295 base
= "texelFetch";
3299 sample_function
->name
= string_buffer_get(priv
->string_buffers
);
3300 string_buffer_sprintf(sample_function
->name
, "%s%s%s%s%s%s", base
, type_part
, projected
? "Proj" : "",
3301 lod
? "Lod" : grad
? "Grad" : "", offset
? "Offset" : "", suffix
);
3303 coord_size
= resource_type_info
[resource_type
].coord_size
;
3304 deriv_size
= coord_size
;
3309 sample_function
->offset_size
= offset
? deriv_size
: 0;
3310 sample_function
->coord_mask
= (1u << coord_size
) - 1;
3311 sample_function
->deriv_mask
= (1u << deriv_size
) - 1;
3312 sample_function
->output_single_component
= shadow
&& !needs_legacy_glsl_syntax(gl_info
);
3315 static void shader_glsl_release_sample_function(const struct wined3d_shader_context
*ctx
,
3316 struct glsl_sample_function
*sample_function
)
3318 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
3320 string_buffer_release(priv
->string_buffers
, sample_function
->name
);
3323 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
3324 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
3326 switch(channel_source
)
3328 case CHANNEL_SOURCE_ZERO
:
3329 strcat(arguments
, "0.0");
3332 case CHANNEL_SOURCE_ONE
:
3333 strcat(arguments
, "1.0");
3336 case CHANNEL_SOURCE_X
:
3337 strcat(arguments
, reg_name
);
3338 strcat(arguments
, ".x");
3341 case CHANNEL_SOURCE_Y
:
3342 strcat(arguments
, reg_name
);
3343 strcat(arguments
, ".y");
3346 case CHANNEL_SOURCE_Z
:
3347 strcat(arguments
, reg_name
);
3348 strcat(arguments
, ".z");
3351 case CHANNEL_SOURCE_W
:
3352 strcat(arguments
, reg_name
);
3353 strcat(arguments
, ".w");
3357 FIXME("Unhandled channel source %#x\n", channel_source
);
3358 strcat(arguments
, "undefined");
3362 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
3365 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer
*buffer
,
3366 const char *reg_name
, DWORD mask
, struct color_fixup_desc fixup
)
3368 unsigned int mask_size
, remaining
;
3369 DWORD fixup_mask
= 0;
3370 char arguments
[256];
3373 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) fixup_mask
|= WINED3DSP_WRITEMASK_0
;
3374 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) fixup_mask
|= WINED3DSP_WRITEMASK_1
;
3375 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) fixup_mask
|= WINED3DSP_WRITEMASK_2
;
3376 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) fixup_mask
|= WINED3DSP_WRITEMASK_3
;
3377 if (!(mask
&= fixup_mask
))
3380 if (is_complex_fixup(fixup
))
3382 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
3383 FIXME("Complex fixup (%#x) not supported\n",complex_fixup
);
3387 shader_glsl_write_mask_to_str(mask
, mask_str
);
3388 mask_size
= shader_glsl_get_write_mask_size(mask
);
3390 arguments
[0] = '\0';
3391 remaining
= mask_size
;
3392 if (mask
& WINED3DSP_WRITEMASK_0
)
3394 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
3395 if (--remaining
) strcat(arguments
, ", ");
3397 if (mask
& WINED3DSP_WRITEMASK_1
)
3399 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
3400 if (--remaining
) strcat(arguments
, ", ");
3402 if (mask
& WINED3DSP_WRITEMASK_2
)
3404 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
3405 if (--remaining
) strcat(arguments
, ", ");
3407 if (mask
& WINED3DSP_WRITEMASK_3
)
3409 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
3410 if (--remaining
) strcat(arguments
, ", ");
3414 shader_addline(buffer
, "%s%s = vec%u(%s);\n", reg_name
, mask_str
, mask_size
, arguments
);
3416 shader_addline(buffer
, "%s%s = %s;\n", reg_name
, mask_str
, arguments
);
3419 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
3424 shader_glsl_get_register_name(&ins
->dst
[0].reg
, reg_name
, &is_color
, ins
);
3425 shader_glsl_color_correction_ext(ins
->ctx
->buffer
, reg_name
, ins
->dst
[0].write_mask
, fixup
);
3428 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
3429 unsigned int sampler_bind_idx
, const struct glsl_sample_function
*sample_function
, DWORD swizzle
,
3430 const char *dx
, const char *dy
, const char *bias
, const struct wined3d_shader_texel_offset
*offset
,
3431 const char *coord_reg_fmt
, ...)
3433 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
3434 char dst_swizzle
[6];
3435 struct color_fixup_desc fixup
;
3436 BOOL np2_fixup
= FALSE
;
3440 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
3442 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3443 * We actually rely on it for vertex shaders and SM4+. */
3444 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& version
->major
< 4)
3446 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3447 fixup
= priv
->cur_ps_args
->color_fixup
[sampler_bind_idx
];
3449 if (priv
->cur_ps_args
->np2_fixup
& (1u << sampler_bind_idx
))
3454 fixup
= COLOR_FIXUP_IDENTITY
;
3457 shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], sample_function
->data_type
);
3459 if (sample_function
->output_single_component
)
3460 shader_addline(ins
->ctx
->buffer
, "vec4(");
3462 shader_addline(ins
->ctx
->buffer
, "%s(%s_sampler%u, ",
3463 sample_function
->name
->buffer
, shader_glsl_get_prefix(version
->type
), sampler_bind_idx
);
3467 va_start(args
, coord_reg_fmt
);
3468 ret
= shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
3472 if (!string_buffer_resize(ins
->ctx
->buffer
, ret
))
3478 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3479 const unsigned char idx
= priv
->cur_np2fixup_info
->idx
[sampler_bind_idx
];
3481 switch (shader_glsl_get_write_mask_size(sample_function
->coord_mask
))
3484 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
3485 idx
>> 1, (idx
% 2) ? "z" : "x");
3488 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
3489 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3492 shader_addline(ins
->ctx
->buffer
, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3493 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3496 shader_addline(ins
->ctx
->buffer
, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3497 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3502 shader_addline(ins
->ctx
->buffer
, ", %s, %s", dx
, dy
);
3504 shader_addline(ins
->ctx
->buffer
, ", %s", bias
);
3505 if (sample_function
->offset_size
)
3507 int offset_immdata
[4] = {offset
->u
, offset
->v
, offset
->w
};
3508 shader_addline(ins
->ctx
->buffer
, ", ");
3509 shader_glsl_append_imm_ivec(ins
->ctx
->buffer
, offset_immdata
, sample_function
->offset_size
);
3511 shader_addline(ins
->ctx
->buffer
, ")");
3513 if (sample_function
->output_single_component
)
3514 shader_addline(ins
->ctx
->buffer
, ")");
3516 shader_addline(ins
->ctx
->buffer
, "%s);\n", dst_swizzle
);
3518 if (!is_identity_fixup(fixup
))
3519 shader_glsl_color_correction(ins
, fixup
);
3522 static void shader_glsl_fixup_position(struct wined3d_string_buffer
*buffer
)
3524 /* Write the final position.
3526 * OpenGL coordinates specify the center of the pixel while D3D coords
3527 * specify the corner. The offsets are stored in z and w in
3528 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3529 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3531 shader_addline(buffer
, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3532 shader_addline(buffer
, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3534 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3537 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3538 * shaders are run before the homogeneous divide, so we have to take the w
3539 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3541 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3544 /*****************************************************************************
3545 * Begin processing individual instruction opcodes
3546 ****************************************************************************/
3548 static void shader_glsl_binop(const struct wined3d_shader_instruction
*ins
)
3550 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3551 struct glsl_src_param src0_param
;
3552 struct glsl_src_param src1_param
;
3556 /* Determine the GLSL operator to use based on the opcode */
3557 switch (ins
->handler_idx
)
3559 case WINED3DSIH_ADD
: op
= "+"; break;
3560 case WINED3DSIH_AND
: op
= "&"; break;
3561 case WINED3DSIH_DIV
: op
= "/"; break;
3562 case WINED3DSIH_IADD
: op
= "+"; break;
3563 case WINED3DSIH_ISHL
: op
= "<<"; break;
3564 case WINED3DSIH_ISHR
: op
= ">>"; break;
3565 case WINED3DSIH_MUL
: op
= "*"; break;
3566 case WINED3DSIH_OR
: op
= "|"; break;
3567 case WINED3DSIH_SUB
: op
= "-"; break;
3568 case WINED3DSIH_USHR
: op
= ">>"; break;
3569 case WINED3DSIH_XOR
: op
= "^"; break;
3571 op
= "<unhandled operator>";
3572 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3576 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3577 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3578 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3579 shader_addline(buffer
, "%s %s %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
3582 static void shader_glsl_relop(const struct wined3d_shader_instruction
*ins
)
3584 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3585 struct glsl_src_param src0_param
;
3586 struct glsl_src_param src1_param
;
3587 unsigned int mask_size
;
3591 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3592 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3593 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3594 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3598 switch (ins
->handler_idx
)
3600 case WINED3DSIH_EQ
: op
= "equal"; break;
3601 case WINED3DSIH_IEQ
: op
= "equal"; break;
3602 case WINED3DSIH_GE
: op
= "greaterThanEqual"; break;
3603 case WINED3DSIH_IGE
: op
= "greaterThanEqual"; break;
3604 case WINED3DSIH_UGE
: op
= "greaterThanEqual"; break;
3605 case WINED3DSIH_LT
: op
= "lessThan"; break;
3606 case WINED3DSIH_ILT
: op
= "lessThan"; break;
3607 case WINED3DSIH_ULT
: op
= "lessThan"; break;
3608 case WINED3DSIH_NE
: op
= "notEqual"; break;
3609 case WINED3DSIH_INE
: op
= "notEqual"; break;
3611 op
= "<unhandled operator>";
3612 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3616 shader_addline(buffer
, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3617 mask_size
, op
, src0_param
.param_str
, src1_param
.param_str
);
3621 switch (ins
->handler_idx
)
3623 case WINED3DSIH_EQ
: op
= "=="; break;
3624 case WINED3DSIH_IEQ
: op
= "=="; break;
3625 case WINED3DSIH_GE
: op
= ">="; break;
3626 case WINED3DSIH_IGE
: op
= ">="; break;
3627 case WINED3DSIH_UGE
: op
= ">="; break;
3628 case WINED3DSIH_LT
: op
= "<"; break;
3629 case WINED3DSIH_ILT
: op
= "<"; break;
3630 case WINED3DSIH_ULT
: op
= "<"; break;
3631 case WINED3DSIH_NE
: op
= "!="; break;
3632 case WINED3DSIH_INE
: op
= "!="; break;
3634 op
= "<unhandled operator>";
3635 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3639 shader_addline(buffer
, "%s %s %s ? 0xffffffffu : 0u);\n",
3640 src0_param
.param_str
, op
, src1_param
.param_str
);
3644 static void shader_glsl_unary_op(const struct wined3d_shader_instruction
*ins
)
3646 struct glsl_src_param src_param
;
3650 switch (ins
->handler_idx
)
3652 case WINED3DSIH_INEG
: op
= "-"; break;
3653 case WINED3DSIH_NOT
: op
= "~"; break;
3655 op
= "<unhandled operator>";
3656 ERR("Unhandled opcode %s.\n",
3657 debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3661 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3662 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3663 shader_addline(ins
->ctx
->buffer
, "%s%s);\n", op
, src_param
.param_str
);
3666 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction
*ins
)
3668 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3669 struct glsl_src_param src0_param
;
3670 struct glsl_src_param src1_param
;
3673 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3674 * If not, we can emulate it. */
3675 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3676 FIXME("64-bit integer multiplies not implemented.\n");
3678 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3680 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3681 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3682 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3684 shader_addline(ins
->ctx
->buffer
, "%s * %s);\n",
3685 src0_param
.param_str
, src1_param
.param_str
);
3689 static void shader_glsl_udiv(const struct wined3d_shader_instruction
*ins
)
3691 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3692 struct glsl_src_param src0_param
, src1_param
;
3695 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3697 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3701 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3702 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3703 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3704 shader_addline(buffer
, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3705 dst_mask
, src0_param
.param_str
, src1_param
.param_str
);
3707 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3708 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3709 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3710 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3712 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], WINED3D_DATA_FLOAT
);
3713 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
3717 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3718 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3719 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3720 shader_addline(buffer
, "%s / %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3723 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3725 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3726 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3727 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3728 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3732 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3733 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
3735 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3736 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3737 struct glsl_src_param src0_param
;
3740 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3741 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3743 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3744 * shader versions WINED3DSIO_MOVA is used for this. */
3745 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
3746 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
3747 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
3749 /* This is a simple floor() */
3750 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3751 if (mask_size
> 1) {
3752 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
3754 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
3757 else if (ins
->handler_idx
== WINED3DSIH_MOVA
)
3759 const struct wined3d_shader_version
*version
= &ins
->ctx
->shader
->reg_maps
.shader_version
;
3760 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3762 if (shader_glsl_get_version(gl_info
, version
) >= 130 || gl_info
->supported
[EXT_GPU_SHADER4
])
3765 shader_addline(buffer
, "ivec%d(round(%s)));\n", mask_size
, src0_param
.param_str
);
3767 shader_addline(buffer
, "int(round(%s)));\n", src0_param
.param_str
);
3772 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3773 mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
3775 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3776 src0_param
.param_str
, src0_param
.param_str
);
3781 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
3785 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3786 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
3788 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3789 struct glsl_src_param src0_param
;
3790 struct glsl_src_param src1_param
;
3791 DWORD dst_write_mask
, src_write_mask
;
3792 unsigned int dst_size
;
3794 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3795 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3797 /* dp4 works on vec4, dp3 on vec3, etc. */
3798 if (ins
->handler_idx
== WINED3DSIH_DP4
)
3799 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
3800 else if (ins
->handler_idx
== WINED3DSIH_DP3
)
3801 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3803 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
3805 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
3806 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
3809 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
3811 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
3815 /* Note that this instruction has some restrictions. The destination write mask
3816 * can't contain the w component, and the source swizzles have to be .xyzw */
3817 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
3819 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3820 struct glsl_src_param src0_param
;
3821 struct glsl_src_param src1_param
;
3824 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3825 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3826 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3827 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
3828 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
3831 static void shader_glsl_cut(const struct wined3d_shader_instruction
*ins
)
3833 unsigned int stream
= ins
->handler_idx
== WINED3DSIH_CUT
? 0 : ins
->src
[0].reg
.idx
[0].offset
;
3836 shader_addline(ins
->ctx
->buffer
, "EndPrimitive();\n");
3838 FIXME("Unhandled primitive stream %u.\n", stream
);
3841 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3842 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3843 * GLSL uses the value as-is. */
3844 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
3846 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3847 struct glsl_src_param src0_param
;
3848 struct glsl_src_param src1_param
;
3849 DWORD dst_write_mask
;
3850 unsigned int dst_size
;
3852 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3853 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3855 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3856 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3860 shader_addline(buffer
, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3861 dst_size
, src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3865 shader_addline(buffer
, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3866 src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3870 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3871 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
3873 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3874 struct glsl_src_param src_param
;
3875 const char *instruction
;
3879 /* Determine the GLSL function to use based on the opcode */
3880 /* TODO: Possibly make this a table for faster lookups */
3881 switch (ins
->handler_idx
)
3883 case WINED3DSIH_ABS
: instruction
= "abs"; break;
3884 case WINED3DSIH_BFREV
: instruction
= "bitfieldReverse"; break;
3885 case WINED3DSIH_COUNTBITS
: instruction
= "bitCount"; break;
3886 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
3887 case WINED3DSIH_DSX_COARSE
: instruction
= "dFdxCoarse"; break;
3888 case WINED3DSIH_DSX_FINE
: instruction
= "dFdxFine"; break;
3889 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
3890 case WINED3DSIH_DSY_COARSE
: instruction
= "ycorrection.y * dFdyCoarse"; break;
3891 case WINED3DSIH_DSY_FINE
: instruction
= "ycorrection.y * dFdyFine"; break;
3892 case WINED3DSIH_FIRSTBIT_HI
: instruction
= "findMSB"; break;
3893 case WINED3DSIH_FIRSTBIT_LO
: instruction
= "findLSB"; break;
3894 case WINED3DSIH_FIRSTBIT_SHI
: instruction
= "findMSB"; break;
3895 case WINED3DSIH_FRC
: instruction
= "fract"; break;
3896 case WINED3DSIH_IMAX
: instruction
= "max"; break;
3897 case WINED3DSIH_IMIN
: instruction
= "min"; break;
3898 case WINED3DSIH_MAX
: instruction
= "max"; break;
3899 case WINED3DSIH_MIN
: instruction
= "min"; break;
3900 case WINED3DSIH_ROUND_NE
: instruction
= "roundEven"; break;
3901 case WINED3DSIH_ROUND_NI
: instruction
= "floor"; break;
3902 case WINED3DSIH_ROUND_PI
: instruction
= "ceil"; break;
3903 case WINED3DSIH_ROUND_Z
: instruction
= "trunc"; break;
3904 case WINED3DSIH_SQRT
: instruction
= "sqrt"; break;
3905 case WINED3DSIH_UMAX
: instruction
= "max"; break;
3906 case WINED3DSIH_UMIN
: instruction
= "min"; break;
3907 default: instruction
= "";
3908 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3912 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3914 /* In D3D bits are numbered from the most significant bit. */
3915 if (ins
->handler_idx
== WINED3DSIH_FIRSTBIT_HI
|| ins
->handler_idx
== WINED3DSIH_FIRSTBIT_SHI
)
3916 shader_addline(buffer
, "31 - ");
3917 shader_addline(buffer
, "%s(", instruction
);
3921 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3922 shader_addline(buffer
, "%s", src_param
.param_str
);
3923 for (i
= 1; i
< ins
->src_count
; ++i
)
3925 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
3926 shader_addline(buffer
, ", %s", src_param
.param_str
);
3930 shader_addline(buffer
, "));\n");
3933 static void shader_glsl_float16(const struct wined3d_shader_instruction
*ins
)
3935 struct wined3d_shader_dst_param dst
;
3936 struct glsl_src_param src
;
3941 fmt
= ins
->handler_idx
== WINED3DSIH_F16TOF32
3942 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
3945 for (i
= 0; i
< 4; ++i
)
3947 dst
.write_mask
= ins
->dst
[0].write_mask
& (WINED3DSP_WRITEMASK_0
<< i
);
3948 if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
,
3949 &dst
, dst
.reg
.data_type
)))
3952 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src
);
3953 shader_addline(ins
->ctx
->buffer
, fmt
, src
.param_str
);
3957 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction
*ins
)
3959 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3960 struct wined3d_shader_dst_param dst
;
3961 struct glsl_src_param src
[4];
3962 const char *instruction
;
3966 switch (ins
->handler_idx
)
3968 case WINED3DSIH_BFI
: instruction
= "bitfieldInsert"; break;
3969 case WINED3DSIH_UBFE
: instruction
= "bitfieldExtract"; break;
3971 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3976 for (i
= 0; i
< 4; ++i
)
3978 dst
.write_mask
= ins
->dst
[0].write_mask
& (WINED3DSP_WRITEMASK_0
<< i
);
3979 if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
,
3980 &dst
, dst
.reg
.data_type
)))
3983 for (j
= 0; j
< ins
->src_count
; ++j
)
3984 shader_glsl_add_src_param(ins
, &ins
->src
[j
], write_mask
, &src
[j
]);
3985 shader_addline(buffer
, "%s(", instruction
);
3986 for (j
= 0; j
< ins
->src_count
- 2; ++j
)
3987 shader_addline(buffer
, "%s, ", src
[ins
->src_count
- j
- 1].param_str
);
3988 shader_addline(buffer
, "%s & 0x1f, %s & 0x1f));\n", src
[1].param_str
, src
[0].param_str
);
3992 static void shader_glsl_nop(const struct wined3d_shader_instruction
*ins
) {}
3994 static void shader_glsl_nrm(const struct wined3d_shader_instruction
*ins
)
3996 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3997 struct glsl_src_param src_param
;
3998 unsigned int mask_size
;
4002 write_mask
= shader_glsl_get_write_mask(ins
->dst
, dst_mask
);
4003 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4004 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
4006 shader_addline(buffer
, "tmp0.x = dot(%s, %s);\n",
4007 src_param
.param_str
, src_param
.param_str
);
4008 shader_glsl_append_dst(buffer
, ins
);
4012 shader_addline(buffer
, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4013 mask_size
, src_param
.param_str
);
4017 shader_addline(buffer
, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4018 src_param
.param_str
);
4022 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction
*ins
)
4024 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4025 ins
->ctx
->reg_maps
->shader_version
.minor
);
4026 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4027 struct glsl_src_param src0_param
;
4028 const char *prefix
, *suffix
;
4029 unsigned int dst_size
;
4030 DWORD dst_write_mask
;
4032 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
4033 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
4035 if (shader_version
< WINED3D_SHADER_VERSION(4, 0))
4036 dst_write_mask
= WINED3DSP_WRITEMASK_3
;
4038 shader_glsl_add_src_param(ins
, &ins
->src
[0], dst_write_mask
, &src0_param
);
4040 switch (ins
->handler_idx
)
4042 case WINED3DSIH_EXP
:
4043 case WINED3DSIH_EXPP
:
4048 case WINED3DSIH_LOG
:
4049 case WINED3DSIH_LOGP
:
4050 prefix
= "log2(abs(";
4054 case WINED3DSIH_RCP
:
4059 case WINED3DSIH_RSQ
:
4060 prefix
= "inversesqrt(abs(";
4067 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
4071 if (dst_size
> 1 && shader_version
< WINED3D_SHADER_VERSION(4, 0))
4072 shader_addline(buffer
, "vec%u(%s%s%s));\n", dst_size
, prefix
, src0_param
.param_str
, suffix
);
4074 shader_addline(buffer
, "%s%s%s);\n", prefix
, src0_param
.param_str
, suffix
);
4077 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4078 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4079 * dst.x = 2^(floor(src))
4080 * dst.y = src - floor(src)
4081 * dst.z = 2^src (partial precision is allowed, but optional)
4083 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4084 * dst = 2^src; (partial precision is allowed, but optional)
4086 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
4088 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
4090 struct glsl_src_param src_param
;
4093 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
4095 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
4096 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
4097 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
4098 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
4100 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4101 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4102 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
4106 shader_glsl_scalar_op(ins
);
4109 static void shader_glsl_cast(const struct wined3d_shader_instruction
*ins
,
4110 const char *vector_constructor
, const char *scalar_constructor
)
4112 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4113 struct glsl_src_param src_param
;
4114 unsigned int mask_size
;
4117 write_mask
= shader_glsl_append_dst(buffer
, ins
);
4118 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4119 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
4122 shader_addline(buffer
, "%s%u(%s));\n", vector_constructor
, mask_size
, src_param
.param_str
);
4124 shader_addline(buffer
, "%s(%s));\n", scalar_constructor
, src_param
.param_str
);
4127 static void shader_glsl_to_int(const struct wined3d_shader_instruction
*ins
)
4129 shader_glsl_cast(ins
, "ivec", "int");
4132 static void shader_glsl_to_uint(const struct wined3d_shader_instruction
*ins
)
4134 shader_glsl_cast(ins
, "uvec", "uint");
4137 static void shader_glsl_to_float(const struct wined3d_shader_instruction
*ins
)
4139 shader_glsl_cast(ins
, "vec", "float");
4142 /** Process signed comparison opcodes in GLSL. */
4143 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
4145 struct glsl_src_param src0_param
;
4146 struct glsl_src_param src1_param
;
4148 unsigned int mask_size
;
4150 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4151 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4152 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4153 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4155 if (mask_size
> 1) {
4156 const char *compare
;
4158 switch(ins
->handler_idx
)
4160 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
4161 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
4162 default: compare
= "";
4163 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
4166 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
4167 src0_param
.param_str
, src1_param
.param_str
);
4169 switch(ins
->handler_idx
)
4171 case WINED3DSIH_SLT
:
4172 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4173 * to return 0.0 but step returns 1.0 because step is not < x
4174 * An alternative is a bvec compare padded with an unused second component.
4175 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4176 * issue. Playing with not() is not possible either because not() does not accept
4179 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
4180 src0_param
.param_str
, src1_param
.param_str
);
4182 case WINED3DSIH_SGE
:
4183 /* Here we can use the step() function and safe a conditional */
4184 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
4187 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
4193 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction
*ins
)
4195 const char *condition_prefix
, *condition_suffix
;
4196 struct wined3d_shader_dst_param dst
;
4197 struct glsl_src_param src0_param
;
4198 struct glsl_src_param src1_param
;
4199 struct glsl_src_param src2_param
;
4200 BOOL temp_destination
= FALSE
;
4201 DWORD cmp_channel
= 0;
4206 switch (ins
->handler_idx
)
4208 case WINED3DSIH_CMP
:
4209 condition_prefix
= "";
4210 condition_suffix
= " >= 0.0";
4213 case WINED3DSIH_CND
:
4214 condition_prefix
= "";
4215 condition_suffix
= " > 0.5";
4218 case WINED3DSIH_MOVC
:
4219 condition_prefix
= "bool(";
4220 condition_suffix
= ")";
4224 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
4225 condition_prefix
= "<unhandled prefix>";
4226 condition_suffix
= "<unhandled suffix>";
4230 if (shader_is_scalar(&ins
->dst
[0].reg
) || shader_is_scalar(&ins
->src
[0].reg
))
4232 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4233 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4234 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4235 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4237 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
4238 condition_prefix
, src0_param
.param_str
, condition_suffix
,
4239 src1_param
.param_str
, src2_param
.param_str
);
4245 /* Splitting the instruction up in multiple lines imposes a problem:
4246 * The first lines may overwrite source parameters of the following lines.
4247 * Deal with that by using a temporary destination register if needed. */
4248 if ((ins
->src
[0].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
4249 && ins
->src
[0].reg
.type
== dst
.reg
.type
)
4250 || (ins
->src
[1].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
4251 && ins
->src
[1].reg
.type
== dst
.reg
.type
)
4252 || (ins
->src
[2].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
4253 && ins
->src
[2].reg
.type
== dst
.reg
.type
))
4254 temp_destination
= TRUE
;
4256 /* Cycle through all source0 channels. */
4257 for (i
= 0; i
< 4; ++i
)
4260 /* Find the destination channels which use the current source0 channel. */
4261 for (j
= 0; j
< 4; ++j
)
4263 if (shader_glsl_swizzle_get_component(ins
->src
[0].swizzle
, j
) == i
)
4265 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
4266 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
4269 dst
.write_mask
= ins
->dst
[0].write_mask
& write_mask
;
4271 if (temp_destination
)
4273 if (!(write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
)))
4275 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
4277 else if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
, dst
.reg
.data_type
)))
4280 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
4281 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4282 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4284 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
4285 condition_prefix
, src0_param
.param_str
, condition_suffix
,
4286 src1_param
.param_str
, src2_param
.param_str
);
4289 if (temp_destination
)
4291 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
4292 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4293 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
4297 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4298 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4299 * the compare is done per component of src0. */
4300 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
4302 struct glsl_src_param src0_param
;
4303 struct glsl_src_param src1_param
;
4304 struct glsl_src_param src2_param
;
4306 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4307 ins
->ctx
->reg_maps
->shader_version
.minor
);
4309 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
4311 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4312 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4313 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4314 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4316 if (ins
->coissue
&& ins
->dst
->write_mask
!= WINED3DSP_WRITEMASK_3
)
4317 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
4319 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
4320 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4324 shader_glsl_conditional_move(ins
);
4327 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4328 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
4330 struct glsl_src_param src0_param
;
4331 struct glsl_src_param src1_param
;
4332 struct glsl_src_param src2_param
;
4335 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4336 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4337 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4338 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4339 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
4340 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4343 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4344 Vertex shaders to GLSL codes */
4345 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
4348 int nComponents
= 0;
4349 struct wined3d_shader_dst_param tmp_dst
= {{0}};
4350 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
4351 struct wined3d_shader_instruction tmp_ins
;
4353 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
4355 /* Set constants for the temporary argument */
4356 tmp_ins
.ctx
= ins
->ctx
;
4357 tmp_ins
.dst_count
= 1;
4358 tmp_ins
.dst
= &tmp_dst
;
4359 tmp_ins
.src_count
= 2;
4360 tmp_ins
.src
= tmp_src
;
4362 switch(ins
->handler_idx
)
4364 case WINED3DSIH_M4x4
:
4366 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
4368 case WINED3DSIH_M4x3
:
4370 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
4372 case WINED3DSIH_M3x4
:
4374 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4376 case WINED3DSIH_M3x3
:
4378 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4380 case WINED3DSIH_M3x2
:
4382 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4388 tmp_dst
= ins
->dst
[0];
4389 tmp_src
[0] = ins
->src
[0];
4390 tmp_src
[1] = ins
->src
[1];
4391 for (i
= 0; i
< nComponents
; ++i
)
4393 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
4394 shader_glsl_dot(&tmp_ins
);
4395 ++tmp_src
[1].reg
.idx
[0].offset
;
4400 The LRP instruction performs a component-wise linear interpolation
4401 between the second and third operands using the first operand as the
4402 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4403 This is equivalent to mix(src2, src1, src0);
4405 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
4407 struct glsl_src_param src0_param
;
4408 struct glsl_src_param src1_param
;
4409 struct glsl_src_param src2_param
;
4412 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4414 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4415 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4416 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4418 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
4419 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
4422 /** Process the WINED3DSIO_LIT instruction in GLSL:
4423 * dst.x = dst.w = 1.0
4424 * dst.y = (src0.x > 0) ? src0.x
4425 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4426 * where src.w is clamped at +- 128
4428 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
4430 struct glsl_src_param src0_param
;
4431 struct glsl_src_param src1_param
;
4432 struct glsl_src_param src3_param
;
4435 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4436 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4438 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4439 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
4440 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
4442 /* The sdk specifies the instruction like this
4444 * if(src.x > 0.0) dst.y = src.x
4446 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4449 * (where power = src.w clamped between -128 and 128)
4451 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4452 * dst.x = 1.0 ... No further explanation needed
4453 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4454 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4455 * dst.w = 1.0. ... Nothing fancy.
4457 * So we still have one conditional in there. So do this:
4458 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4460 * 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),
4461 * 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.
4462 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4464 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4465 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4466 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4468 shader_addline(ins
->ctx
->buffer
,
4469 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4470 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4471 src0_param
.param_str
, src3_param
.param_str
, src1_param
.param_str
,
4472 src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
4475 /** Process the WINED3DSIO_DST instruction in GLSL:
4477 * dst.y = src0.x * src0.y
4481 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
4483 struct glsl_src_param src0y_param
;
4484 struct glsl_src_param src0z_param
;
4485 struct glsl_src_param src1y_param
;
4486 struct glsl_src_param src1w_param
;
4489 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4490 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4492 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
4493 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
4494 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
4495 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
4497 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4498 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
4501 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4502 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4503 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4505 * dst.x = cos(src0.?)
4506 * dst.y = sin(src0.?)
4510 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
4512 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4513 struct glsl_src_param src0_param
;
4516 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
4518 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4520 write_mask
= shader_glsl_append_dst(buffer
, ins
);
4523 case WINED3DSP_WRITEMASK_0
:
4524 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4527 case WINED3DSP_WRITEMASK_1
:
4528 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
4531 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
4532 shader_addline(buffer
, "vec2(cos(%s), sin(%s)));\n",
4533 src0_param
.param_str
, src0_param
.param_str
);
4537 ERR("Write mask should be .x, .y or .xy\n");
4544 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
4547 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
4551 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4552 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4553 shader_addline(buffer
, "tmp0%s = sin(%s);\n", dst_mask
, src0_param
.param_str
);
4555 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
4556 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4557 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4559 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
4560 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
4564 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
4565 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4566 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
4569 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
4571 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
4572 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4573 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4577 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4578 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4579 * generate invalid code
4581 static void shader_glsl_sgn(const struct wined3d_shader_instruction
*ins
)
4583 struct glsl_src_param src0_param
;
4586 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4587 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4589 shader_addline(ins
->ctx
->buffer
, "sign(%s));\n", src0_param
.param_str
);
4592 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4593 * Start a for() loop where src1.y is the initial value of aL,
4594 * increment aL by src1.z for a total of src1.x iterations.
4595 * Need to use a temporary variable for this operation.
4597 /* FIXME: I don't think nested loops will work correctly this way. */
4598 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
4600 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4601 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4602 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
4603 const struct wined3d_shader_lconst
*constant
;
4604 struct glsl_src_param src1_param
;
4605 const DWORD
*control_values
= NULL
;
4607 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
4609 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
4611 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4612 * class hardware doesn't support real varying indexing, but Microsoft
4613 * designed this feature for Shader model 2.x+. If the loop control is
4614 * known at compile time, the GLSL compiler can unroll the loop, and
4615 * replace indirect addressing with direct addressing. */
4616 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
4618 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
4620 if (constant
->idx
== ins
->src
[1].reg
.idx
[0].offset
)
4622 control_values
= constant
->value
;
4630 struct wined3d_shader_loop_control loop_control
;
4631 loop_control
.count
= control_values
[0];
4632 loop_control
.start
= control_values
[1];
4633 loop_control
.step
= (int)control_values
[2];
4635 if (loop_control
.step
> 0)
4637 shader_addline(buffer
, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4638 state
->current_loop_depth
, loop_control
.start
,
4639 state
->current_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
4640 state
->current_loop_depth
, loop_control
.step
);
4642 else if (loop_control
.step
< 0)
4644 shader_addline(buffer
, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4645 state
->current_loop_depth
, loop_control
.start
,
4646 state
->current_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
4647 state
->current_loop_depth
, loop_control
.step
);
4651 shader_addline(buffer
, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4652 state
->current_loop_depth
, loop_control
.start
, state
->current_loop_depth
,
4653 state
->current_loop_depth
, loop_control
.count
,
4654 state
->current_loop_depth
);
4659 shader_addline(buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4660 state
->current_loop_depth
, state
->current_loop_reg
,
4661 src1_param
.reg_name
, state
->current_loop_depth
, src1_param
.reg_name
,
4662 state
->current_loop_depth
, state
->current_loop_reg
, src1_param
.reg_name
);
4665 ++state
->current_loop_reg
;
4669 shader_addline(buffer
, "for (;;)\n{\n");
4672 ++state
->current_loop_depth
;
4675 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
4677 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4679 shader_addline(ins
->ctx
->buffer
, "}\n");
4681 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
4683 --state
->current_loop_depth
;
4684 --state
->current_loop_reg
;
4687 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
4689 --state
->current_loop_depth
;
4693 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
4695 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4696 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
4697 const struct wined3d_shader_lconst
*constant
;
4698 struct glsl_src_param src0_param
;
4699 const DWORD
*control_values
= NULL
;
4701 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4702 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
4704 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
4706 if (constant
->idx
== ins
->src
[0].reg
.idx
[0].offset
)
4708 control_values
= constant
->value
;
4716 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4717 state
->current_loop_depth
, state
->current_loop_depth
,
4718 control_values
[0], state
->current_loop_depth
);
4722 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4723 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4724 state
->current_loop_depth
, state
->current_loop_depth
,
4725 src0_param
.param_str
, state
->current_loop_depth
);
4728 ++state
->current_loop_depth
;
4731 static void shader_glsl_switch(const struct wined3d_shader_instruction
*ins
)
4733 struct glsl_src_param src0_param
;
4735 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4736 shader_addline(ins
->ctx
->buffer
, "switch (%s)\n{\n", src0_param
.param_str
);
4739 static void shader_glsl_case(const struct wined3d_shader_instruction
*ins
)
4741 struct glsl_src_param src0_param
;
4743 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4744 shader_addline(ins
->ctx
->buffer
, "case %s:\n", src0_param
.param_str
);
4747 static void shader_glsl_default(const struct wined3d_shader_instruction
*ins
)
4749 shader_addline(ins
->ctx
->buffer
, "default:\n");
4752 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
4754 const char *condition
= (ins
->flags
== WINED3D_SHADER_CONDITIONAL_OP_NZ
) ? "bool" : "!bool";
4755 struct glsl_src_param src0_param
;
4757 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4758 shader_addline(ins
->ctx
->buffer
, "if (%s(%s)) {\n", condition
, src0_param
.param_str
);
4761 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
4763 struct glsl_src_param src0_param
;
4764 struct glsl_src_param src1_param
;
4766 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4767 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4769 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
4770 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
4773 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
4775 shader_addline(ins
->ctx
->buffer
, "} else {\n");
4778 static void shader_glsl_emit(const struct wined3d_shader_instruction
*ins
)
4780 unsigned int stream
= ins
->handler_idx
== WINED3DSIH_EMIT
? 0 : ins
->src
[0].reg
.idx
[0].offset
;
4782 shader_addline(ins
->ctx
->buffer
, "setup_gs_output(gs_out);\n");
4783 if (!ins
->ctx
->gl_info
->supported
[ARB_CLIP_CONTROL
])
4784 shader_glsl_fixup_position(ins
->ctx
->buffer
);
4787 shader_addline(ins
->ctx
->buffer
, "EmitVertex();\n");
4789 FIXME("Unhandled primitive stream %u.\n", stream
);
4792 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
4794 shader_addline(ins
->ctx
->buffer
, "break;\n");
4797 /* FIXME: According to MSDN the compare is done per component. */
4798 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
4800 struct glsl_src_param src0_param
;
4801 struct glsl_src_param src1_param
;
4803 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4804 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4806 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
4807 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
4810 static void shader_glsl_breakp(const struct wined3d_shader_instruction
*ins
)
4812 const char *condition
= (ins
->flags
== WINED3D_SHADER_CONDITIONAL_OP_NZ
) ? "bool" : "!bool";
4813 struct glsl_src_param src_param
;
4815 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
4816 shader_addline(ins
->ctx
->buffer
, "if (%s(%s)) break;\n", condition
, src_param
.param_str
);
4819 static void shader_glsl_continue(const struct wined3d_shader_instruction
*ins
)
4821 shader_addline(ins
->ctx
->buffer
, "continue;\n");
4824 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
4826 shader_addline(ins
->ctx
->buffer
, "}\n");
4827 shader_addline(ins
->ctx
->buffer
, "void subroutine%u()\n{\n", ins
->src
[0].reg
.idx
[0].offset
);
4829 /* Subroutines appear at the end of the shader. */
4830 ins
->ctx
->state
->in_subroutine
= TRUE
;
4833 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
4835 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
[0].offset
);
4838 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
4840 struct glsl_src_param src1_param
;
4842 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4843 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n",
4844 src1_param
.param_str
, ins
->src
[0].reg
.idx
[0].offset
);
4847 static void shader_glsl_ret(const struct wined3d_shader_instruction
*ins
)
4849 const struct wined3d_shader_version
*version
= &ins
->ctx
->shader
->reg_maps
.shader_version
;
4851 if (version
->major
>= 4 && !ins
->ctx
->state
->in_subroutine
)
4853 shader_glsl_generate_shader_epilogue(ins
->ctx
);
4854 shader_addline(ins
->ctx
->buffer
, "return;\n");
4858 static void shader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
4860 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4861 ins
->ctx
->reg_maps
->shader_version
.minor
);
4862 struct glsl_sample_function sample_function
;
4863 DWORD sample_flags
= 0;
4865 DWORD mask
= 0, swizzle
;
4866 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4868 /* 1.0-1.4: Use destination register as sampler source.
4869 * 2.0+: Use provided sampler source. */
4870 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4871 resource_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4873 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4875 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4877 DWORD flags
= (priv
->cur_ps_args
->tex_transform
>> resource_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
4878 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
4879 enum wined3d_shader_resource_type resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
4881 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4882 if (flags
& WINED3D_PSARGS_PROJECTED
&& resource_type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4884 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4885 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
4887 case WINED3D_TTFF_COUNT1
:
4888 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4890 case WINED3D_TTFF_COUNT2
:
4891 mask
= WINED3DSP_WRITEMASK_1
;
4893 case WINED3D_TTFF_COUNT3
:
4894 mask
= WINED3DSP_WRITEMASK_2
;
4896 case WINED3D_TTFF_COUNT4
:
4897 case WINED3D_TTFF_DISABLE
:
4898 mask
= WINED3DSP_WRITEMASK_3
;
4903 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4905 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
4907 if (src_mod
== WINED3DSPSM_DZ
) {
4908 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4909 mask
= WINED3DSP_WRITEMASK_2
;
4910 } else if (src_mod
== WINED3DSPSM_DW
) {
4911 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4912 mask
= WINED3DSP_WRITEMASK_3
;
4917 if ((ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
4918 && ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4920 /* ps 2.0 texldp instruction always divides by the fourth component. */
4921 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4922 mask
= WINED3DSP_WRITEMASK_3
;
4926 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, resource_idx
, sample_flags
, &sample_function
);
4927 mask
|= sample_function
.coord_mask
;
4928 sample_function
.coord_mask
= mask
;
4930 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
4931 else swizzle
= ins
->src
[1].swizzle
;
4933 /* 1.0-1.3: Use destination register as coordinate source.
4934 1.4+: Use provided coordinate source register. */
4935 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4938 shader_glsl_write_mask_to_str(mask
, coord_mask
);
4939 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
, NULL
,
4940 "T%u%s", resource_idx
, coord_mask
);
4944 struct glsl_src_param coord_param
;
4945 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
4946 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
4948 struct glsl_src_param bias
;
4949 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
4950 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
4951 NULL
, "%s", coord_param
.param_str
);
4953 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
, NULL
,
4954 "%s", coord_param
.param_str
);
4957 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4960 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
4962 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4963 struct glsl_src_param coord_param
, dx_param
, dy_param
;
4964 struct glsl_sample_function sample_function
;
4966 DWORD swizzle
= ins
->src
[1].swizzle
;
4968 if (!shader_glsl_has_core_grad(gl_info
, &ins
->ctx
->shader
->reg_maps
.shader_version
)
4969 && !gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
4971 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4972 shader_glsl_tex(ins
);
4976 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4978 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, WINED3D_GLSL_SAMPLE_GRAD
, &sample_function
);
4979 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4980 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.deriv_mask
, &dx_param
);
4981 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.deriv_mask
, &dy_param
);
4983 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
,
4984 NULL
, NULL
, "%s", coord_param
.param_str
);
4985 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4988 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
4990 const struct wined3d_shader_version
*shader_version
= &ins
->ctx
->reg_maps
->shader_version
;
4991 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4992 struct glsl_src_param coord_param
, lod_param
;
4993 struct glsl_sample_function sample_function
;
4994 DWORD swizzle
= ins
->src
[1].swizzle
;
4997 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4999 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, WINED3D_GLSL_SAMPLE_LOD
, &sample_function
);
5000 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
5002 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
5004 if (shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& !shader_glsl_has_core_grad(gl_info
, shader_version
)
5005 && !gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
5007 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5008 * However, the NVIDIA drivers allow them in fragment shaders as well,
5009 * even without the appropriate extension. */
5010 WARN("Using %s in fragment shader.\n", sample_function
.name
->buffer
);
5012 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
, NULL
,
5013 "%s", coord_param
.param_str
);
5014 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5017 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map
*sampler_map
,
5018 unsigned int resource_idx
, unsigned int sampler_idx
)
5020 struct wined3d_shader_sampler_map_entry
*entries
= sampler_map
->entries
;
5023 for (i
= 0; i
< sampler_map
->count
; ++i
)
5025 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
5026 return entries
[i
].bind_idx
;
5029 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx
, sampler_idx
);
5034 static void shader_glsl_atomic(const struct wined3d_shader_instruction
*ins
)
5036 const BOOL is_imm_instruction
= WINED3DSIH_IMM_ATOMIC_AND
<= ins
->handler_idx
5037 && ins
->handler_idx
<= WINED3DSIH_IMM_ATOMIC_XOR
;
5038 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5039 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
5040 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5041 struct glsl_src_param structure_idx
, offset
, data
, data2
;
5042 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5043 enum wined3d_shader_resource_type resource_type
;
5044 struct wined3d_string_buffer
*address
;
5045 enum wined3d_data_type data_type
;
5046 unsigned int resource_idx
, stride
;
5047 const char *op
, *resource
;
5051 resource_idx
= ins
->dst
[is_imm_instruction
].reg
.idx
[0].offset
;
5052 is_tgsm
= ins
->dst
[is_imm_instruction
].reg
.type
== WINED3DSPR_GROUPSHAREDMEM
;
5055 if (resource_idx
>= reg_maps
->tgsm_count
)
5057 ERR("Invalid TGSM index %u.\n", resource_idx
);
5061 data_type
= WINED3D_DATA_UINT
;
5063 stride
= reg_maps
->tgsm
[resource_idx
].stride
;
5067 if (resource_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5069 ERR("Invalid UAV index %u.\n", resource_idx
);
5072 resource_type
= reg_maps
->uav_resource_info
[resource_idx
].type
;
5073 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
5075 ERR("Unexpected resource type %#x.\n", resource_type
);
5079 data_type
= reg_maps
->uav_resource_info
[resource_idx
].data_type
;
5080 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
5081 stride
= reg_maps
->uav_resource_info
[resource_idx
].stride
;
5084 switch (ins
->handler_idx
)
5086 case WINED3DSIH_ATOMIC_AND
:
5087 case WINED3DSIH_IMM_ATOMIC_AND
:
5091 op
= "imageAtomicAnd";
5093 case WINED3DSIH_ATOMIC_CMP_STORE
:
5094 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH
:
5096 op
= "atomicCompSwap";
5098 op
= "imageAtomicCompSwap";
5100 case WINED3DSIH_ATOMIC_IADD
:
5101 case WINED3DSIH_IMM_ATOMIC_IADD
:
5105 op
= "imageAtomicAdd";
5107 case WINED3DSIH_ATOMIC_IMAX
:
5108 case WINED3DSIH_IMM_ATOMIC_IMAX
:
5112 op
= "imageAtomicMax";
5113 if (data_type
!= WINED3D_DATA_INT
)
5115 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins
->handler_idx
);
5119 case WINED3DSIH_ATOMIC_IMIN
:
5120 case WINED3DSIH_IMM_ATOMIC_IMIN
:
5124 op
= "imageAtomicMin";
5125 if (data_type
!= WINED3D_DATA_INT
)
5127 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins
->handler_idx
);
5131 case WINED3DSIH_ATOMIC_OR
:
5132 case WINED3DSIH_IMM_ATOMIC_OR
:
5136 op
= "imageAtomicOr";
5138 case WINED3DSIH_ATOMIC_UMAX
:
5139 case WINED3DSIH_IMM_ATOMIC_UMAX
:
5143 op
= "imageAtomicMax";
5144 if (data_type
!= WINED3D_DATA_UINT
)
5146 FIXME("Unhandled opcode %#x for signed integers.\n", ins
->handler_idx
);
5150 case WINED3DSIH_ATOMIC_UMIN
:
5151 case WINED3DSIH_IMM_ATOMIC_UMIN
:
5155 op
= "imageAtomicMin";
5156 if (data_type
!= WINED3D_DATA_UINT
)
5158 FIXME("Unhandled opcode %#x for signed integers.\n", ins
->handler_idx
);
5162 case WINED3DSIH_ATOMIC_XOR
:
5163 case WINED3DSIH_IMM_ATOMIC_XOR
:
5167 op
= "imageAtomicXor";
5169 case WINED3DSIH_IMM_ATOMIC_EXCH
:
5171 op
= "atomicExchange";
5173 op
= "imageAtomicExchange";
5176 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
5180 address
= string_buffer_get(priv
->string_buffers
);
5183 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &structure_idx
);
5184 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &offset
);
5185 string_buffer_sprintf(address
, "%s * %u + %s / 4", structure_idx
.param_str
, stride
, offset
.param_str
);
5189 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &offset
);
5190 string_buffer_sprintf(address
, "%s", offset
.param_str
);
5191 if (reg_maps
->uav_resource_info
[resource_idx
].flags
& WINED3D_VIEW_BUFFER_RAW
)
5192 shader_addline(address
, "/ 4");
5195 if (is_imm_instruction
)
5196 shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], data_type
);
5199 shader_addline(buffer
, "%s(%s_%s%u[%s], ",
5200 op
, shader_glsl_get_prefix(version
->type
), resource
, resource_idx
, address
->buffer
);
5202 shader_addline(buffer
, "%s(%s_%s%u, %s, ",
5203 op
, shader_glsl_get_prefix(version
->type
), resource
, resource_idx
, address
->buffer
);
5205 shader_glsl_add_src_param_ext(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &data
, data_type
);
5206 shader_addline(buffer
, "%s", data
.param_str
);
5207 if (ins
->src_count
>= 3)
5209 shader_glsl_add_src_param_ext(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &data2
, data_type
);
5210 shader_addline(buffer
, ", %s", data2
.param_str
);
5213 if (is_imm_instruction
)
5214 shader_addline(buffer
, ")");
5215 shader_addline(buffer
, ");\n");
5217 string_buffer_release(priv
->string_buffers
, address
);
5220 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction
*ins
)
5222 const char *prefix
= shader_glsl_get_prefix(ins
->ctx
->reg_maps
->shader_version
.type
);
5225 if (ins
->handler_idx
== WINED3DSIH_IMM_ATOMIC_ALLOC
)
5226 op
= "atomicCounterIncrement";
5228 op
= "atomicCounterDecrement";
5230 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5231 shader_addline(ins
->ctx
->buffer
, "%s(%s_counter%u));\n", op
, prefix
, ins
->src
[0].reg
.idx
[0].offset
);
5234 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction
*ins
)
5236 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5237 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
5238 enum wined3d_shader_resource_type resource_type
;
5239 struct glsl_src_param image_coord_param
;
5240 enum wined3d_data_type data_type
;
5241 DWORD coord_mask
, write_mask
;
5242 unsigned int uav_idx
;
5243 char dst_swizzle
[6];
5245 uav_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5246 if (uav_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5248 ERR("Invalid UAV index %u.\n", uav_idx
);
5251 resource_type
= reg_maps
->uav_resource_info
[uav_idx
].type
;
5252 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
5254 ERR("Unexpected resource type %#x.\n", resource_type
);
5255 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
5257 data_type
= reg_maps
->uav_resource_info
[uav_idx
].data_type
;
5258 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
5260 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], data_type
);
5261 shader_glsl_get_swizzle(&ins
->src
[1], FALSE
, write_mask
, dst_swizzle
);
5263 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &image_coord_param
);
5264 shader_addline(ins
->ctx
->buffer
, "imageLoad(%s_image%u, %s)%s);\n",
5265 shader_glsl_get_prefix(version
->type
), uav_idx
, image_coord_param
.param_str
, dst_swizzle
);
5268 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction
*ins
)
5270 const char *prefix
= shader_glsl_get_prefix(ins
->ctx
->reg_maps
->shader_version
.type
);
5271 const struct wined3d_shader_src_param
*src
= &ins
->src
[ins
->src_count
- 1];
5272 unsigned int i
, swizzle
, resource_idx
, bind_idx
, stride
, src_idx
= 0;
5273 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5274 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5275 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5276 struct glsl_src_param structure_idx
, offset
;
5277 struct wined3d_string_buffer
*address
;
5278 struct wined3d_shader_dst_param dst
;
5279 const char *function
, *resource
;
5281 resource_idx
= src
->reg
.idx
[0].offset
;
5282 if (src
->reg
.type
== WINED3DSPR_RESOURCE
)
5284 if (resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
5286 ERR("Invalid resource index %u.\n", resource_idx
);
5289 stride
= reg_maps
->resource_info
[resource_idx
].stride
;
5290 bind_idx
= shader_glsl_find_sampler(®_maps
->sampler_map
, resource_idx
, WINED3D_SAMPLER_DEFAULT
);
5291 function
= "texelFetch";
5292 resource
= "sampler";
5294 else if (src
->reg
.type
== WINED3DSPR_UAV
)
5296 if (resource_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5298 ERR("Invalid UAV index %u.\n", resource_idx
);
5301 stride
= reg_maps
->uav_resource_info
[resource_idx
].stride
;
5302 bind_idx
= resource_idx
;
5303 function
= "imageLoad";
5308 if (resource_idx
>= reg_maps
->tgsm_count
)
5310 ERR("Invalid TGSM index %u.\n", resource_idx
);
5313 stride
= reg_maps
->tgsm
[resource_idx
].stride
;
5314 bind_idx
= resource_idx
;
5319 address
= string_buffer_get(priv
->string_buffers
);
5320 if (ins
->handler_idx
== WINED3DSIH_LD_STRUCTURED
)
5322 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
++], WINED3DSP_WRITEMASK_0
, &structure_idx
);
5323 shader_addline(address
, "%s * %u + ", structure_idx
.param_str
, stride
);
5325 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
++], WINED3DSP_WRITEMASK_0
, &offset
);
5326 shader_addline(address
, "%s / 4", offset
.param_str
);
5329 for (i
= 0; i
< 4; ++i
)
5331 dst
.write_mask
= ins
->dst
[0].write_mask
& (WINED3DSP_WRITEMASK_0
<< i
);
5332 if (!shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
, dst
.reg
.data_type
))
5335 swizzle
= shader_glsl_swizzle_get_component(src
->swizzle
, i
);
5337 shader_addline(buffer
, "%s(%s_%s%u, %s + %u).x);\n",
5338 function
, prefix
, resource
, bind_idx
, address
->buffer
, swizzle
);
5340 shader_addline(buffer
, "%s_%s%u[%s + %u]);\n",
5341 prefix
, resource
, bind_idx
, address
->buffer
, swizzle
);
5344 string_buffer_release(priv
->string_buffers
, address
);
5347 static void shader_glsl_store_uav(const struct wined3d_shader_instruction
*ins
)
5349 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5350 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
5351 struct glsl_src_param image_coord_param
, image_data_param
;
5352 enum wined3d_shader_resource_type resource_type
;
5353 enum wined3d_data_type data_type
;
5354 unsigned int uav_idx
;
5357 uav_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5358 if (uav_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5360 ERR("Invalid UAV index %u.\n", uav_idx
);
5363 resource_type
= reg_maps
->uav_resource_info
[uav_idx
].type
;
5364 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
5366 ERR("Unexpected resource type %#x.\n", resource_type
);
5369 data_type
= reg_maps
->uav_resource_info
[uav_idx
].data_type
;
5370 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
5372 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &image_coord_param
);
5373 shader_glsl_add_src_param_ext(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &image_data_param
, data_type
);
5374 shader_addline(ins
->ctx
->buffer
, "imageStore(%s_image%u, %s, %s);\n",
5375 shader_glsl_get_prefix(version
->type
), uav_idx
,
5376 image_coord_param
.param_str
, image_data_param
.param_str
);
5379 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction
*ins
)
5381 const char *prefix
= shader_glsl_get_prefix(ins
->ctx
->reg_maps
->shader_version
.type
);
5382 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5383 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5384 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5385 struct glsl_src_param structure_idx
, offset
, data
;
5386 unsigned int i
, resource_idx
, stride
, src_idx
= 0;
5387 struct wined3d_string_buffer
*address
;
5391 resource_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5392 is_tgsm
= ins
->dst
[0].reg
.type
== WINED3DSPR_GROUPSHAREDMEM
;
5395 if (resource_idx
>= reg_maps
->tgsm_count
)
5397 ERR("Invalid TGSM index %u.\n", resource_idx
);
5400 stride
= reg_maps
->tgsm
[resource_idx
].stride
;
5404 if (resource_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5406 ERR("Invalid UAV index %u.\n", resource_idx
);
5409 stride
= reg_maps
->uav_resource_info
[resource_idx
].stride
;
5412 address
= string_buffer_get(priv
->string_buffers
);
5413 if (ins
->handler_idx
== WINED3DSIH_STORE_STRUCTURED
)
5415 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
++], WINED3DSP_WRITEMASK_0
, &structure_idx
);
5416 shader_addline(address
, "%s * %u + ", structure_idx
.param_str
, stride
);
5418 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
++], WINED3DSP_WRITEMASK_0
, &offset
);
5419 shader_addline(address
, "%s / 4", offset
.param_str
);
5421 for (i
= 0; i
< 4; ++i
)
5423 if (!(write_mask
= ins
->dst
[0].write_mask
& (WINED3DSP_WRITEMASK_0
<< i
)))
5426 shader_glsl_add_src_param(ins
, &ins
->src
[src_idx
], write_mask
, &data
);
5429 shader_addline(buffer
, "%s_g%u[%s + %u] = %s;\n",
5430 prefix
, resource_idx
, address
->buffer
, i
, data
.param_str
);
5432 shader_addline(buffer
, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5433 prefix
, resource_idx
, address
->buffer
, i
, data
.param_str
);
5436 string_buffer_release(priv
->string_buffers
, address
);
5439 static void shader_glsl_sync(const struct wined3d_shader_instruction
*ins
)
5441 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5442 unsigned int sync_flags
= ins
->flags
;
5444 if (sync_flags
& WINED3DSSF_THREAD_GROUP
)
5446 shader_addline(buffer
, "barrier();\n");
5447 sync_flags
&= ~(WINED3DSSF_THREAD_GROUP
| WINED3DSSF_GROUP_SHARED_MEMORY
);
5450 if (sync_flags
& WINED3DSSF_GROUP_SHARED_MEMORY
)
5452 shader_addline(buffer
, "memoryBarrierShared();\n");
5453 sync_flags
&= ~WINED3DSSF_GROUP_SHARED_MEMORY
;
5457 FIXME("Unhandled sync flags %#x.\n", sync_flags
);
5460 static const struct wined3d_shader_resource_info
*shader_glsl_get_resource_info(
5461 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_register
*reg
)
5463 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5464 unsigned int idx
= reg
->idx
[0].offset
;
5466 if (reg
->type
== WINED3DSPR_RESOURCE
)
5468 if (idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
5470 ERR("Invalid resource index %u.\n", idx
);
5473 return ®_maps
->resource_info
[idx
];
5476 if (reg
->type
== WINED3DSPR_UAV
)
5478 if (idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
5480 ERR("Invalid UAV index %u.\n", idx
);
5483 return ®_maps
->uav_resource_info
[idx
];
5486 FIXME("Unhandled register type %#x.\n", reg
->type
);
5490 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction
*ins
)
5492 const char *prefix
= shader_glsl_get_prefix(ins
->ctx
->reg_maps
->shader_version
.type
);
5493 const struct wined3d_shader_resource_info
*resource_info
;
5494 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5495 unsigned int resource_idx
;
5496 char dst_swizzle
[6];
5499 write_mask
= shader_glsl_append_dst(buffer
, ins
);
5500 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
5502 if (!(resource_info
= shader_glsl_get_resource_info(ins
, &ins
->src
[0].reg
)))
5504 resource_idx
= ins
->src
[0].reg
.idx
[0].offset
;
5506 shader_addline(buffer
, "ivec2(");
5507 if (ins
->src
[0].reg
.type
== WINED3DSPR_RESOURCE
)
5509 unsigned int bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
5510 resource_idx
, WINED3D_SAMPLER_DEFAULT
);
5511 shader_addline(buffer
, "textureSize(%s_sampler%u)", prefix
, bind_idx
);
5515 shader_addline(buffer
, "imageSize(%s_image%u)", prefix
, resource_idx
);
5517 if (resource_info
->stride
)
5518 shader_addline(buffer
, " / %u", resource_info
->stride
);
5519 else if (resource_info
->flags
& WINED3D_VIEW_BUFFER_RAW
)
5520 shader_addline(buffer
, " * 4");
5521 shader_addline(buffer
, ", %u)%s);\n", resource_info
->stride
, dst_swizzle
);
5524 static void shader_glsl_resinfo(const struct wined3d_shader_instruction
*ins
)
5526 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
5527 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
5528 enum wined3d_shader_resource_type resource_type
;
5529 enum wined3d_shader_register_type reg_type
;
5530 unsigned int resource_idx
, bind_idx
, i
;
5531 enum wined3d_data_type dst_data_type
;
5532 struct glsl_src_param lod_param
;
5533 char dst_swizzle
[6];
5536 dst_data_type
= ins
->dst
[0].reg
.data_type
;
5537 if (ins
->flags
== WINED3DSI_RESINFO_UINT
)
5538 dst_data_type
= WINED3D_DATA_UINT
;
5539 else if (ins
->flags
)
5540 FIXME("Unhandled flags %#x.\n", ins
->flags
);
5542 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], dst_data_type
);
5543 shader_glsl_get_swizzle(&ins
->src
[1], FALSE
, write_mask
, dst_swizzle
);
5545 reg_type
= ins
->src
[1].reg
.type
;
5546 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5547 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &lod_param
);
5548 if (reg_type
== WINED3DSPR_RESOURCE
)
5550 resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
5551 bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
5552 resource_idx
, WINED3D_SAMPLER_DEFAULT
);
5556 resource_type
= ins
->ctx
->reg_maps
->uav_resource_info
[resource_idx
].type
;
5557 bind_idx
= resource_idx
;
5560 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
5562 ERR("Unexpected resource type %#x.\n", resource_type
);
5563 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
5566 if (dst_data_type
== WINED3D_DATA_UINT
)
5567 shader_addline(ins
->ctx
->buffer
, "uvec4(");
5569 shader_addline(ins
->ctx
->buffer
, "vec4(");
5571 if (reg_type
== WINED3DSPR_RESOURCE
)
5573 shader_addline(ins
->ctx
->buffer
, "textureSize(%s_sampler%u, %s), ",
5574 shader_glsl_get_prefix(version
->type
), bind_idx
, lod_param
.param_str
);
5576 for (i
= 0; i
< 3 - resource_type_info
[resource_type
].resinfo_size
; ++i
)
5577 shader_addline(ins
->ctx
->buffer
, "0, ");
5579 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
5581 shader_addline(ins
->ctx
->buffer
, "textureQueryLevels(%s_sampler%u)",
5582 shader_glsl_get_prefix(version
->type
), bind_idx
);
5586 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5587 shader_addline(ins
->ctx
->buffer
, "1");
5592 shader_addline(ins
->ctx
->buffer
, "imageSize(%s_image%u), ",
5593 shader_glsl_get_prefix(version
->type
), bind_idx
);
5595 for (i
= 0; i
< 3 - resource_type_info
[resource_type
].resinfo_size
; ++i
)
5596 shader_addline(ins
->ctx
->buffer
, "0, ");
5598 /* For UAVs the returned miplevel count is always 1. */
5599 shader_addline(ins
->ctx
->buffer
, "1");
5602 shader_addline(ins
->ctx
->buffer
, ")%s);\n", dst_swizzle
);
5605 /* FIXME: The current implementation does not handle multisample textures correctly. */
5606 static void shader_glsl_ld(const struct wined3d_shader_instruction
*ins
)
5608 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
5609 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
5610 struct glsl_src_param coord_param
, lod_param
;
5611 struct glsl_sample_function sample_function
;
5612 DWORD flags
= WINED3D_GLSL_SAMPLE_LOAD
;
5615 if (wined3d_shader_instruction_has_texel_offset(ins
))
5616 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
5618 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5619 sampler_idx
= WINED3D_SAMPLER_DEFAULT
;
5621 if (resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
5623 ERR("Invalid resource index %u.\n", resource_idx
);
5626 has_lod_param
= reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_BUFFER
;
5628 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
5629 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
5630 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
5631 sampler_bind_idx
= shader_glsl_find_sampler(®_maps
->sampler_map
, resource_idx
, sampler_idx
);
5632 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, ins
->src
[1].swizzle
,
5633 NULL
, NULL
, has_lod_param
? lod_param
.param_str
: NULL
, &ins
->texel_offset
,
5634 "%s", coord_param
.param_str
);
5635 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5638 static void shader_glsl_sample(const struct wined3d_shader_instruction
*ins
)
5640 const char *lod_param_str
= NULL
, *dx_param_str
= NULL
, *dy_param_str
= NULL
;
5641 struct glsl_src_param coord_param
, lod_param
, dx_param
, dy_param
;
5642 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
5643 struct glsl_sample_function sample_function
;
5646 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_GRAD
)
5647 flags
|= WINED3D_GLSL_SAMPLE_GRAD
;
5648 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_LOD
)
5649 flags
|= WINED3D_GLSL_SAMPLE_LOD
;
5650 if (wined3d_shader_instruction_has_texel_offset(ins
))
5651 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
5653 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5654 sampler_idx
= ins
->src
[2].reg
.idx
[0].offset
;
5656 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
5657 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
5659 switch (ins
->handler_idx
)
5661 case WINED3DSIH_SAMPLE
:
5663 case WINED3DSIH_SAMPLE_B
:
5664 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &lod_param
);
5665 lod_param_str
= lod_param
.param_str
;
5667 case WINED3DSIH_SAMPLE_GRAD
:
5668 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.deriv_mask
, &dx_param
);
5669 shader_glsl_add_src_param(ins
, &ins
->src
[4], sample_function
.deriv_mask
, &dy_param
);
5670 dx_param_str
= dx_param
.param_str
;
5671 dy_param_str
= dy_param
.param_str
;
5673 case WINED3DSIH_SAMPLE_LOD
:
5674 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &lod_param
);
5675 lod_param_str
= lod_param
.param_str
;
5678 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
5682 sampler_bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
, resource_idx
, sampler_idx
);
5683 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, ins
->src
[1].swizzle
,
5684 dx_param_str
, dy_param_str
, lod_param_str
, &ins
->texel_offset
, "%s", coord_param
.param_str
);
5685 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5688 static void shader_glsl_sample_c(const struct wined3d_shader_instruction
*ins
)
5690 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
5691 struct glsl_src_param coord_param
, compare_param
;
5692 struct glsl_sample_function sample_function
;
5693 const char *lod_param
= NULL
;
5697 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_C_LZ
)
5700 flags
|= WINED3D_GLSL_SAMPLE_LOD
;
5703 if (wined3d_shader_instruction_has_texel_offset(ins
))
5704 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
5706 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5707 sampler_idx
= ins
->src
[2].reg
.idx
[0].offset
;
5709 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
5710 coord_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
5711 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
>> 1, &coord_param
);
5712 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &compare_param
);
5713 sampler_bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
, resource_idx
, sampler_idx
);
5714 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
,
5715 NULL
, NULL
, lod_param
, &ins
->texel_offset
, "vec%u(%s, %s)",
5716 coord_size
, coord_param
.param_str
, compare_param
.param_str
);
5717 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5720 static void shader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
5722 /* FIXME: Make this work for more than just 2D textures */
5723 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5724 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5726 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
5730 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
5731 shader_addline(buffer
, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5732 ins
->dst
[0].reg
.idx
[0].offset
, dst_mask
);
5736 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
5737 DWORD reg
= ins
->src
[0].reg
.idx
[0].offset
;
5738 char dst_swizzle
[6];
5740 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
5742 if (src_mod
== WINED3DSPSM_DZ
|| src_mod
== WINED3DSPSM_DW
)
5744 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
5745 struct glsl_src_param div_param
;
5746 DWORD src_writemask
= src_mod
== WINED3DSPSM_DZ
? WINED3DSP_WRITEMASK_2
: WINED3DSP_WRITEMASK_3
;
5748 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_writemask
, &div_param
);
5751 shader_addline(buffer
, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
5753 shader_addline(buffer
, "ffp_texcoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
5757 shader_addline(buffer
, "ffp_texcoord[%u]%s);\n", reg
, dst_swizzle
);
5762 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5763 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5764 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5765 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
5767 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5768 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5769 struct glsl_sample_function sample_function
;
5770 struct glsl_src_param src0_param
;
5773 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5775 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5776 * scalar, and projected sampling would require 4.
5778 * It is a dependent read - not valid with conditional NP2 textures
5780 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5781 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
5786 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
5787 NULL
, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
5791 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
5792 NULL
, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
5796 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
5797 NULL
, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
5801 FIXME("Unexpected mask size %u\n", mask_size
);
5804 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5807 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5808 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5809 static void shader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
5811 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5812 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
5813 struct glsl_src_param src0_param
;
5815 unsigned int mask_size
;
5817 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5818 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
5819 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5821 if (mask_size
> 1) {
5822 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
5824 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
5828 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5829 * Calculate the depth as dst.x / dst.y */
5830 static void shader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
5832 struct glsl_dst_param dst_param
;
5834 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
5836 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5837 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5838 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5839 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5842 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5843 dst_param
.reg_name
, dst_param
.reg_name
);
5846 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5847 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5848 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5849 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5851 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
5853 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5854 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
5855 struct glsl_src_param src0_param
;
5857 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5859 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
5860 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5863 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5864 * Calculate the 1st of a 2-row matrix multiplication. */
5865 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
5867 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5868 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5869 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5870 struct glsl_src_param src0_param
;
5872 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5873 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5876 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5877 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5878 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
5880 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5881 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5882 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5883 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5884 struct glsl_src_param src0_param
;
5886 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5887 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx
->current_row
, reg
, src0_param
.param_str
);
5888 tex_mx
->texcoord_w
[tex_mx
->current_row
++] = reg
;
5891 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
5893 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5894 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5895 struct glsl_sample_function sample_function
;
5896 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5897 struct glsl_src_param src0_param
;
5899 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5900 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5902 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5904 /* Sample the texture using the calculated coordinates */
5905 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
, "tmp0.xy");
5906 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5909 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5910 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5911 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
5913 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5914 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5915 struct glsl_sample_function sample_function
;
5916 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5917 struct glsl_src_param src0_param
;
5919 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5920 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5922 /* Dependent read, not valid with conditional NP2 */
5923 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5925 /* Sample the texture using the calculated coordinates */
5926 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
, "tmp0.xyz");
5927 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5929 tex_mx
->current_row
= 0;
5932 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5933 * Perform the 3rd row of a 3x3 matrix multiply */
5934 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
5936 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5937 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5938 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5939 struct glsl_src_param src0_param
;
5942 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5944 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5945 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
5946 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
5948 tex_mx
->current_row
= 0;
5951 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5952 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5953 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
5955 struct glsl_src_param src0_param
;
5956 struct glsl_src_param src1_param
;
5957 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5958 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5959 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5960 struct glsl_sample_function sample_function
;
5961 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5964 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5965 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
5967 /* Perform the last matrix multiply operation */
5968 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5969 /* Reflection calculation */
5970 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
5972 /* Dependent read, not valid with conditional NP2 */
5973 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5974 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
5976 /* Sample the texture */
5977 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
5978 NULL
, NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
5979 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5981 tex_mx
->current_row
= 0;
5984 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5985 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5986 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
5988 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5989 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5990 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5991 struct glsl_sample_function sample_function
;
5992 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5993 struct glsl_src_param src0_param
;
5996 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5998 /* Perform the last matrix multiply operation */
5999 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
6001 /* Construct the eye-ray vector from w coordinates */
6002 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6003 tex_mx
->texcoord_w
[0], tex_mx
->texcoord_w
[1], reg
);
6004 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6006 /* Dependent read, not valid with conditional NP2 */
6007 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
6008 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
6010 /* Sample the texture using the calculated coordinates */
6011 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
6012 NULL
, NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
6013 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6015 tex_mx
->current_row
= 0;
6018 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6019 * Apply a fake bump map transform.
6020 * texbem is pshader <= 1.3 only, this saves a few version checks
6022 static void shader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
6024 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
6025 struct glsl_sample_function sample_function
;
6026 struct glsl_src_param coord_param
;
6032 sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6033 flags
= (priv
->cur_ps_args
->tex_transform
>> sampler_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
6034 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
6036 /* Dependent read, not valid with conditional NP2 */
6037 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
6038 mask
= sample_function
.coord_mask
;
6040 shader_glsl_write_mask_to_str(mask
, coord_mask
);
6042 /* With projected textures, texbem only divides the static texture coord,
6043 * not the displacement, so we can't let GL handle this. */
6044 if (flags
& WINED3D_PSARGS_PROJECTED
)
6047 char coord_div_mask
[3];
6048 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
6050 case WINED3D_TTFF_COUNT1
:
6051 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6053 case WINED3D_TTFF_COUNT2
:
6054 div_mask
= WINED3DSP_WRITEMASK_1
;
6056 case WINED3D_TTFF_COUNT3
:
6057 div_mask
= WINED3DSP_WRITEMASK_2
;
6059 case WINED3D_TTFF_COUNT4
:
6060 case WINED3D_TTFF_DISABLE
:
6061 div_mask
= WINED3DSP_WRITEMASK_3
;
6064 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
6065 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
6068 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
6070 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
6071 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
6072 coord_param
.param_str
, coord_mask
);
6074 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
6076 struct glsl_src_param luminance_param
;
6077 struct glsl_dst_param dst_param
;
6079 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
6080 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
6082 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6083 dst_param
.reg_name
, dst_param
.mask_str
,
6084 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
6086 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6089 static void shader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
6091 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6092 struct glsl_src_param src0_param
, src1_param
;
6094 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
6095 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
6097 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
6098 shader_addline(ins
->ctx
->buffer
, "%s + bumpenv_mat%u * %s);\n",
6099 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
6102 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6103 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6104 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
6106 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6107 struct glsl_sample_function sample_function
;
6108 struct glsl_src_param src0_param
;
6110 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
6112 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
6113 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
6114 "%s.wx", src0_param
.reg_name
);
6115 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6118 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6119 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6120 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
6122 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6123 struct glsl_sample_function sample_function
;
6124 struct glsl_src_param src0_param
;
6126 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
6128 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
6129 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
6130 "%s.yz", src0_param
.reg_name
);
6131 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6134 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6135 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6136 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
6138 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
6139 struct glsl_sample_function sample_function
;
6140 struct glsl_src_param src0_param
;
6142 /* Dependent read, not valid with conditional NP2 */
6143 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
6144 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
6146 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
6147 "%s", src0_param
.param_str
);
6148 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
6151 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6152 * If any of the first 3 components are < 0, discard this pixel */
6153 static void shader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
6155 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 4)
6157 struct glsl_src_param src_param
;
6159 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
6160 shader_addline(ins
->ctx
->buffer
, "if (bool(%s)) discard;\n", src_param
.param_str
);
6164 struct glsl_dst_param dst_param
;
6166 /* The argument is a destination parameter, and no writemasks are allowed */
6167 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
6169 /* 2.0 shaders compare all 4 components in texkill. */
6170 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
6171 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
6172 /* 1.x shaders only compare the first 3 components, probably due to
6173 * the nature of the texkill instruction as a tex* instruction, and
6174 * phase, which kills all .w components. Even if all 4 components are
6175 * defined, only the first 3 are used. */
6177 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
6181 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6182 * dst = dot2(src0, src1) + src2 */
6183 static void shader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
6185 struct glsl_src_param src0_param
;
6186 struct glsl_src_param src1_param
;
6187 struct glsl_src_param src2_param
;
6189 unsigned int mask_size
;
6191 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
6192 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
6194 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
6195 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
6196 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
6198 if (mask_size
> 1) {
6199 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
6200 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
6202 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
6203 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
6207 static void shader_glsl_input_pack(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
6208 const struct wined3d_shader_signature
*input_signature
,
6209 const struct wined3d_shader_reg_maps
*reg_maps
,
6210 const struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
6214 for (i
= 0; i
< input_signature
->element_count
; ++i
)
6216 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
6217 const char *semantic_name
;
6222 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
6225 semantic_name
= input
->semantic_name
;
6226 semantic_idx
= input
->semantic_idx
;
6227 shader_glsl_write_mask_to_str(input
->mask
, reg_mask
);
6229 if (args
->vp_mode
== vertexshader
)
6231 if (input
->sysval_semantic
== WINED3D_SV_POSITION
&& !semantic_idx
)
6233 shader_addline(buffer
, "ps_in[%u]%s = vpos%s;\n",
6234 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6236 else if (args
->pointsprite
&& shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
6238 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input
->register_idx
);
6240 else if (input
->sysval_semantic
== WINED3D_SV_IS_FRONT_FACE
)
6242 shader_addline(buffer
, "ps_in[%u] = vec4("
6243 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
6244 input
->register_idx
);
6248 if (input
->sysval_semantic
)
6249 FIXME("Unhandled sysval semantic %#x.\n", input
->sysval_semantic
);
6250 shader_addline(buffer
, "ps_in[%u]%s = ps_link[%u]%s;\n",
6251 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
6252 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
);
6255 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
6257 if (args
->pointsprite
)
6258 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6259 shader
->u
.ps
.input_reg_map
[input
->register_idx
]);
6260 else if (args
->vp_mode
== pretransformed
&& args
->texcoords_initialized
& (1u << semantic_idx
))
6261 shader_addline(buffer
, "ps_in[%u]%s = %s[%u]%s;\n",
6262 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
6263 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
6264 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx
, reg_mask
);
6266 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6267 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6269 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
))
6272 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6273 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6274 else if (semantic_idx
== 1)
6275 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6276 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6278 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6279 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6283 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6284 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
6289 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
)
6291 struct glsl_program_key key
;
6293 key
.vs_id
= entry
->vs
.id
;
6294 key
.gs_id
= entry
->gs
.id
;
6295 key
.ps_id
= entry
->ps
.id
;
6296 key
.cs_id
= entry
->cs
.id
;
6298 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
6300 ERR("Failed to insert program entry.\n");
6304 static struct glsl_shader_prog_link
*get_glsl_program_entry(const struct shader_glsl_priv
*priv
,
6305 const struct glsl_program_key
*key
)
6307 struct wine_rb_entry
*entry
;
6309 entry
= wine_rb_get(&priv
->program_lookup
, key
);
6310 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
6313 /* Context activation is done by the caller. */
6314 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
6315 struct glsl_shader_prog_link
*entry
)
6317 wine_rb_remove(&priv
->program_lookup
, &entry
->program_lookup_entry
);
6319 GL_EXTCALL(glDeleteProgram(entry
->id
));
6321 list_remove(&entry
->vs
.shader_entry
);
6323 list_remove(&entry
->gs
.shader_entry
);
6325 list_remove(&entry
->ps
.shader_entry
);
6327 list_remove(&entry
->cs
.shader_entry
);
6328 HeapFree(GetProcessHeap(), 0, entry
);
6331 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv
*priv
,
6332 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
6333 const struct wined3d_shader_signature
*input_signature
,
6334 const struct wined3d_shader_reg_maps
*reg_maps_in
,
6335 const struct wined3d_shader_signature
*output_signature
,
6336 const struct wined3d_shader_reg_maps
*reg_maps_out
, const char *out_array_name
)
6338 struct wined3d_string_buffer
*destination
= string_buffer_get(&priv
->string_buffers
);
6339 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6340 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6341 unsigned int in_count
= vec4_varyings(3, gl_info
);
6342 unsigned int max_varyings
= legacy_context
? in_count
+ 2 : in_count
;
6343 DWORD in_idx
, *set
= NULL
;
6347 set
= wined3d_calloc(max_varyings
, sizeof(*set
));
6349 for (i
= 0; i
< input_signature
->element_count
; ++i
)
6351 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
6353 if (!(reg_maps_in
->input_registers
& (1u << input
->register_idx
)))
6356 in_idx
= map
[input
->register_idx
];
6357 /* Declared, but not read register */
6360 if (in_idx
>= max_varyings
)
6362 FIXME("More input varyings declared than supported, expect issues.\n");
6366 if (in_idx
== in_count
)
6367 string_buffer_sprintf(destination
, "gl_FrontColor");
6368 else if (in_idx
== in_count
+ 1)
6369 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
6371 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, in_idx
);
6376 for (j
= 0; j
< output_signature
->element_count
; ++j
)
6378 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[j
];
6381 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
))
6382 || input
->semantic_idx
!= output
->semantic_idx
6383 || strcmp(input
->semantic_name
, output
->semantic_name
)
6384 || !(mask
= input
->mask
& output
->mask
))
6387 if (set
[in_idx
] == ~0u)
6389 set
[in_idx
] |= mask
& reg_maps_out
->u
.output_registers_mask
[output
->register_idx
];
6390 shader_glsl_write_mask_to_str(mask
, reg_mask
);
6392 shader_addline(buffer
, "%s%s = shader_out[%u]%s;\n",
6393 destination
->buffer
, reg_mask
, output
->register_idx
, reg_mask
);
6397 for (i
= 0; i
< max_varyings
; ++i
)
6401 if (!set
[i
] || set
[i
] == WINED3DSP_WRITEMASK_ALL
)
6408 if (!(set
[i
] & WINED3DSP_WRITEMASK_0
))
6409 reg_mask
[size
++] = 'x';
6410 if (!(set
[i
] & WINED3DSP_WRITEMASK_1
))
6411 reg_mask
[size
++] = 'y';
6412 if (!(set
[i
] & WINED3DSP_WRITEMASK_2
))
6413 reg_mask
[size
++] = 'z';
6414 if (!(set
[i
] & WINED3DSP_WRITEMASK_3
))
6415 reg_mask
[size
++] = 'w';
6416 reg_mask
[size
] = '\0';
6419 string_buffer_sprintf(destination
, "gl_FrontColor");
6420 else if (i
== in_count
+ 1)
6421 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
6423 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, i
);
6426 shader_addline(buffer
, "%s.%s = 0.0;\n", destination
->buffer
, reg_mask
);
6428 shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
->buffer
, reg_mask
, size
);
6431 HeapFree(GetProcessHeap(), 0, set
);
6432 string_buffer_release(&priv
->string_buffers
, destination
);
6435 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv
*priv
,
6436 unsigned int input_count
, const struct wined3d_shader_signature
*output_signature
,
6437 const struct wined3d_shader_reg_maps
*reg_maps_out
, const char *out_array_name
)
6439 struct wined3d_string_buffer
*destination
= string_buffer_get(&priv
->string_buffers
);
6440 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6444 for (i
= 0; i
< output_signature
->element_count
; ++i
)
6446 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[i
];
6448 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
)))
6451 if (output
->register_idx
>= input_count
)
6454 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, output
->register_idx
);
6456 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
6458 shader_addline(buffer
, "%s%s = shader_out[%u]%s;\n",
6459 destination
->buffer
, reg_mask
, output
->register_idx
, reg_mask
);
6462 string_buffer_release(&priv
->string_buffers
, destination
);
6465 /* Context activation is done by the caller. */
6466 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv
*priv
,
6467 const struct wined3d_shader
*vs
, unsigned int input_count
,
6468 const struct wined3d_gl_info
*gl_info
)
6470 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6471 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6474 shader_addline(buffer
, "varying out vec4 gs_in[%u];\n", input_count
);
6476 shader_addline(buffer
, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count
);
6477 shader_addline(buffer
, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs
->limits
->packed_output
);
6479 shader_glsl_setup_sm4_shader_output(priv
, input_count
, &vs
->output_signature
, &vs
->reg_maps
,
6480 legacy_context
? "gs_in" : "gs_in.gs_in");
6482 shader_addline(buffer
, "}\n");
6485 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv
*priv
,
6486 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
6487 const struct wined3d_shader_signature
*input_signature
,
6488 const struct wined3d_shader_reg_maps
*reg_maps_in
, unsigned int input_count
,
6489 const struct wined3d_shader_signature
*output_signature
,
6490 const struct wined3d_shader_reg_maps
*reg_maps_out
, BOOL per_vertex_point_size
)
6492 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6493 const char *semantic_name
;
6498 /* First, sort out position and point size system values. */
6499 for (i
= 0; i
< output_signature
->element_count
; ++i
)
6501 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[i
];
6503 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
)))
6506 semantic_name
= output
->semantic_name
;
6507 semantic_idx
= output
->semantic_idx
;
6508 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
6510 if (output
->sysval_semantic
== WINED3D_SV_POSITION
&& !semantic_idx
)
6512 shader_addline(buffer
, "gl_Position%s = shader_out[%u]%s;\n",
6513 reg_mask
, output
->register_idx
, reg_mask
);
6515 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
6517 shader_addline(buffer
, "gl_PointSize = clamp(shader_out[%u].%c, "
6518 "ffp_point.size_min, ffp_point.size_max);\n", output
->register_idx
, reg_mask
[1]);
6520 else if (output
->sysval_semantic
)
6522 FIXME("Unhandled sysval semantic %#x.\n", output
->sysval_semantic
);
6526 /* Then, setup the pixel shader input. */
6527 if (reg_maps_out
->shader_version
.major
< 4)
6528 shader_glsl_setup_vs3_output(priv
, gl_info
, map
, input_signature
, reg_maps_in
,
6529 output_signature
, reg_maps_out
, "ps_link");
6531 shader_glsl_setup_sm4_shader_output(priv
, input_count
, output_signature
, reg_maps_out
, "ps_link");
6534 /* Context activation is done by the caller. */
6535 static GLuint
shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv
*priv
,
6536 const struct wined3d_shader
*vs
, const struct wined3d_shader
*ps
,
6537 BOOL per_vertex_point_size
, BOOL flatshading
, const struct wined3d_gl_info
*gl_info
)
6539 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6541 DWORD ps_major
= ps
? ps
->reg_maps
.shader_version
.major
: 0;
6543 const char *semantic_name
;
6546 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6548 string_buffer_clear(buffer
);
6550 shader_glsl_add_version_declaration(buffer
, gl_info
, &vs
->reg_maps
.shader_version
);
6552 if (per_vertex_point_size
)
6554 shader_addline(buffer
, "uniform struct\n{\n");
6555 shader_addline(buffer
, " float size_min;\n");
6556 shader_addline(buffer
, " float size_max;\n");
6557 shader_addline(buffer
, "} ffp_point;\n");
6562 DWORD colors_written_mask
[2] = {0};
6563 DWORD texcoords_written_mask
[MAX_TEXTURES
] = {0};
6565 if (!legacy_context
)
6567 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_diffuse;\n");
6568 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_specular;\n");
6569 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
6570 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
6573 shader_addline(buffer
, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs
->limits
->packed_output
);
6575 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
6577 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
6580 if (!(vs
->reg_maps
.output_registers
& (1u << output
->register_idx
)))
6583 semantic_name
= output
->semantic_name
;
6584 semantic_idx
= output
->semantic_idx
;
6585 write_mask
= output
->mask
;
6586 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
6588 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
) && semantic_idx
< 2)
6591 shader_addline(buffer
, "gl_Front%sColor%s = shader_out[%u]%s;\n",
6592 semantic_idx
? "Secondary" : "", reg_mask
, output
->register_idx
, reg_mask
);
6594 shader_addline(buffer
, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
6595 semantic_idx
? "specular" : "diffuse", reg_mask
, output
->register_idx
, reg_mask
);
6597 colors_written_mask
[semantic_idx
] = write_mask
;
6599 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
6601 shader_addline(buffer
, "gl_Position%s = shader_out[%u]%s;\n",
6602 reg_mask
, output
->register_idx
, reg_mask
);
6604 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
6606 if (semantic_idx
< MAX_TEXTURES
)
6608 shader_addline(buffer
, "%s[%u]%s = shader_out[%u]%s;\n",
6609 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord",
6610 semantic_idx
, reg_mask
, output
->register_idx
, reg_mask
);
6611 texcoords_written_mask
[semantic_idx
] = write_mask
;
6614 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
6616 shader_addline(buffer
, "gl_PointSize = clamp(shader_out[%u].%c, "
6617 "ffp_point.size_min, ffp_point.size_max);\n", output
->register_idx
, reg_mask
[1]);
6619 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_FOG
))
6621 shader_addline(buffer
, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6622 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6623 output
->register_idx
, reg_mask
[1]);
6627 for (i
= 0; i
< 2; ++i
)
6629 if (colors_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
6631 shader_glsl_write_mask_to_str(~colors_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
6633 shader_addline(buffer
, "%s%s = vec4(1.0)%s;\n",
6634 legacy_context
? "gl_FrontColor" : "ffp_varying_diffuse",
6635 reg_mask
, reg_mask
);
6637 shader_addline(buffer
, "%s%s = vec4(0.0)%s;\n",
6638 legacy_context
? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6639 reg_mask
, reg_mask
);
6642 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6644 if (ps
&& !(ps
->reg_maps
.texcoord
& (1u << i
)))
6647 if (texcoords_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
6649 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
6650 && !texcoords_written_mask
[i
])
6653 shader_glsl_write_mask_to_str(~texcoords_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
6654 shader_addline(buffer
, "%s[%u]%s = vec4(0.0)%s;\n",
6655 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
, reg_mask
, reg_mask
);
6661 UINT in_count
= min(vec4_varyings(ps_major
, gl_info
), ps
->limits
->packed_input
);
6663 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", in_count
);
6664 shader_addline(buffer
, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs
->limits
->packed_output
);
6665 shader_glsl_setup_sm3_rasterizer_input(priv
, gl_info
, ps
->u
.ps
.input_reg_map
, &ps
->input_signature
,
6666 &ps
->reg_maps
, 0, &vs
->output_signature
, &vs
->reg_maps
, per_vertex_point_size
);
6669 shader_addline(buffer
, "}\n");
6671 ret
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
6672 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6673 shader_glsl_compile(gl_info
, ret
, buffer
->buffer
);
6678 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv
*priv
,
6679 const struct wined3d_shader
*shader
, unsigned int input_count
,
6680 const struct wined3d_gl_info
*gl_info
)
6682 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6685 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info
), input_count
));
6687 shader_addline(buffer
, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6688 shader_glsl_get_prefix(shader
->reg_maps
.shader_version
.type
), shader
->limits
->packed_output
);
6690 shader_glsl_setup_sm3_rasterizer_input(priv
, gl_info
, NULL
, NULL
,
6691 NULL
, input_count
, &shader
->output_signature
, &shader
->reg_maps
, FALSE
);
6693 shader_addline(buffer
, "}\n");
6696 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer
*buffer
,
6697 const struct wined3d_gl_info
*gl_info
)
6699 const char *output
= get_fragment_output(gl_info
);
6701 shader_addline(buffer
, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output
);
6702 shader_addline(buffer
, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6703 shader_addline(buffer
, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output
);
6704 shader_addline(buffer
, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output
);
6705 shader_addline(buffer
, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output
);
6706 shader_addline(buffer
, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output
, output
);
6709 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer
*buffer
,
6710 const struct wined3d_gl_info
*gl_info
, enum wined3d_ffp_ps_fog_mode mode
)
6712 const char *output
= get_fragment_output(gl_info
);
6716 case WINED3D_FFP_PS_FOG_OFF
:
6719 case WINED3D_FFP_PS_FOG_LINEAR
:
6720 shader_addline(buffer
, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6723 case WINED3D_FFP_PS_FOG_EXP
:
6724 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6727 case WINED3D_FFP_PS_FOG_EXP2
:
6728 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6729 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6733 ERR("Invalid fog mode %#x.\n", mode
);
6737 shader_addline(buffer
, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6741 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer
*buffer
,
6742 const struct wined3d_gl_info
*gl_info
, enum wined3d_cmp_func alpha_func
)
6744 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6745 * flipping all the operators here, just negate the comparison below. */
6746 static const char * const comparison_operator
[] =
6748 "", /* WINED3D_CMP_NEVER */
6749 "<", /* WINED3D_CMP_LESS */
6750 "==", /* WINED3D_CMP_EQUAL */
6751 "<=", /* WINED3D_CMP_LESSEQUAL */
6752 ">", /* WINED3D_CMP_GREATER */
6753 "!=", /* WINED3D_CMP_NOTEQUAL */
6754 ">=", /* WINED3D_CMP_GREATEREQUAL */
6755 "" /* WINED3D_CMP_ALWAYS */
6758 if (alpha_func
== WINED3D_CMP_ALWAYS
)
6761 if (alpha_func
!= WINED3D_CMP_NEVER
)
6762 shader_addline(buffer
, "if (!(%s[0].a %s alpha_test_ref))\n",
6763 get_fragment_output(gl_info
), comparison_operator
[alpha_func
- WINED3D_CMP_NEVER
]);
6764 shader_addline(buffer
, " discard;\n");
6767 static void shader_glsl_enable_extensions(struct wined3d_string_buffer
*buffer
,
6768 const struct wined3d_gl_info
*gl_info
)
6770 if (gl_info
->supported
[ARB_GPU_SHADER5
])
6771 shader_addline(buffer
, "#extension GL_ARB_gpu_shader5 : enable\n");
6772 if (gl_info
->supported
[ARB_SHADER_ATOMIC_COUNTERS
])
6773 shader_addline(buffer
, "#extension GL_ARB_shader_atomic_counters : enable\n");
6774 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
6775 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
6776 if (gl_info
->supported
[ARB_SHADER_IMAGE_LOAD_STORE
])
6777 shader_addline(buffer
, "#extension GL_ARB_shader_image_load_store : enable\n");
6778 if (gl_info
->supported
[ARB_SHADER_IMAGE_SIZE
])
6779 shader_addline(buffer
, "#extension GL_ARB_shader_image_size : enable\n");
6780 if (gl_info
->supported
[ARB_SHADER_STORAGE_BUFFER_OBJECT
])
6781 shader_addline(buffer
, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
6782 if (gl_info
->supported
[ARB_SHADING_LANGUAGE_420PACK
])
6783 shader_addline(buffer
, "#extension GL_ARB_shading_language_420pack : enable\n");
6784 if (gl_info
->supported
[ARB_SHADING_LANGUAGE_PACKING
])
6785 shader_addline(buffer
, "#extension GL_ARB_shading_language_packing : enable\n");
6786 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
6787 shader_addline(buffer
, "#extension GL_ARB_texture_cube_map_array : enable\n");
6788 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
6789 shader_addline(buffer
, "#extension GL_ARB_texture_query_levels : enable\n");
6790 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
6791 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
6792 if (gl_info
->supported
[EXT_GPU_SHADER4
])
6793 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
6794 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
6795 shader_addline(buffer
, "#extension GL_EXT_texture_array : enable\n");
6798 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info
*gl_info
,
6799 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
6800 const struct ps_compile_args
*args
)
6802 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6804 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6805 if (reg_maps
->shader_version
.major
< 2)
6806 shader_addline(buffer
, "%s[0] = R0;\n", get_fragment_output(gl_info
));
6808 if (args
->srgb_correction
)
6809 shader_glsl_generate_srgb_write_correction(buffer
, gl_info
);
6811 /* SM < 3 does not replace the fog stage. */
6812 if (reg_maps
->shader_version
.major
< 3)
6813 shader_glsl_generate_fog_code(buffer
, gl_info
, args
->fog
);
6815 shader_glsl_generate_alpha_test(buffer
, gl_info
, args
->alpha_test_func
+ 1);
6818 /* Context activation is done by the caller. */
6819 static GLuint
shader_glsl_generate_pshader(const struct wined3d_context
*context
,
6820 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
6821 const struct wined3d_shader
*shader
,
6822 const struct ps_compile_args
*args
, struct ps_np2fixup_info
*np2fixup_info
)
6824 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6825 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6826 struct shader_glsl_ctx_priv priv_ctx
;
6827 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6830 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
6831 priv_ctx
.cur_ps_args
= args
;
6832 priv_ctx
.cur_np2fixup_info
= np2fixup_info
;
6833 priv_ctx
.string_buffers
= string_buffers
;
6835 shader_glsl_add_version_declaration(buffer
, gl_info
, ®_maps
->shader_version
);
6837 shader_glsl_enable_extensions(buffer
, gl_info
);
6838 if (gl_info
->supported
[ARB_DERIVATIVE_CONTROL
])
6839 shader_addline(buffer
, "#extension GL_ARB_derivative_control : enable\n");
6840 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
6841 shader_addline(buffer
, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6842 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
6843 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
6844 /* The spec says that it doesn't have to be explicitly enabled, but the
6845 * nvidia drivers write a warning if we don't do so. */
6846 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
6847 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
6849 /* Base Declarations */
6850 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
6852 shader_addline(buffer
, "void main()\n{\n");
6854 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6855 * add approximately 0.5. This causes off-by-one problems as spotted by
6856 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6857 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6858 * causes precision troubles when we just subtract 0.5.
6860 * To deal with that, just floor() the position. This will eliminate the
6861 * fraction on all cards.
6863 * TODO: Test how this behaves with multisampling.
6865 * An advantage of floor is that it works even if the driver doesn't add
6866 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6867 * to return in gl_FragCoord, even though coordinates specify the pixel
6868 * centers instead of the pixel corners. This code will behave correctly
6869 * on drivers that returns integer values. */
6872 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
6873 shader_addline(buffer
, "vpos = gl_FragCoord;\n");
6874 else if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_PIXEL_CENTER_INTEGER
)
6875 shader_addline(buffer
,
6876 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6878 shader_addline(buffer
,
6879 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6882 if (reg_maps
->shader_version
.major
< 3 || args
->vp_mode
!= vertexshader
)
6885 WORD map
= reg_maps
->texcoord
;
6889 if (glsl_is_color_reg_read(shader
, 0))
6890 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
6891 if (glsl_is_color_reg_read(shader
, 1))
6892 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
6895 for (i
= 0; map
; map
>>= 1, ++i
)
6899 if (args
->pointsprite
)
6900 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i
);
6901 else if (args
->texcoords_initialized
& (1u << i
))
6902 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n", i
,
6903 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
);
6905 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", i
);
6906 shader_addline(buffer
, "vec4 T%u = ffp_texcoord[%u];\n", i
, i
);
6911 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6914 /* Pack 3.0 inputs */
6915 if (reg_maps
->shader_version
.major
>= 3)
6916 shader_glsl_input_pack(shader
, buffer
, &shader
->input_signature
, reg_maps
, args
, gl_info
);
6918 /* Base Shader Body */
6919 if (FAILED(shader_generate_main(shader
, buffer
, reg_maps
, &priv_ctx
)))
6922 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6923 if (reg_maps
->shader_version
.major
< 4)
6924 shader_glsl_generate_ps_epilogue(gl_info
, buffer
, shader
, args
);
6926 shader_addline(buffer
, "}\n");
6928 shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
6929 TRACE("Compiling shader object %u.\n", shader_id
);
6930 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6935 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info
*gl_info
,
6936 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
6937 const struct vs_compile_args
*args
)
6939 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6940 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6943 /* Unpack outputs. */
6944 shader_addline(buffer
, "setup_vs_output(vs_out);\n");
6946 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6947 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6948 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6949 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6951 if (reg_maps
->shader_version
.major
< 3)
6953 if (args
->fog_src
== VS_FOG_Z
)
6954 shader_addline(buffer
, "%s = gl_Position.z;\n",
6955 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6956 else if (!reg_maps
->fog
)
6957 shader_addline(buffer
, "%s = 0.0;\n",
6958 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6961 /* We always store the clipplanes without y inversion. */
6962 if (args
->clip_enabled
)
6965 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
6967 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
6968 shader_addline(buffer
, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i
, i
);
6971 if (args
->point_size
&& !args
->per_vertex_point_size
)
6972 shader_addline(buffer
, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6974 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
6975 shader_glsl_fixup_position(buffer
);
6978 /* Context activation is done by the caller. */
6979 static GLuint
shader_glsl_generate_vshader(const struct wined3d_context
*context
,
6980 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
, const struct vs_compile_args
*args
)
6982 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
6983 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6984 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6985 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6986 struct shader_glsl_ctx_priv priv_ctx
;
6989 shader_glsl_add_version_declaration(buffer
, gl_info
, ®_maps
->shader_version
);
6991 shader_glsl_enable_extensions(buffer
, gl_info
);
6992 if (gl_info
->supported
[ARB_DRAW_INSTANCED
])
6993 shader_addline(buffer
, "#extension GL_ARB_draw_instanced : enable\n");
6994 if (gl_info
->supported
[ARB_EXPLICIT_ATTRIB_LOCATION
])
6995 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
6997 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
6998 priv_ctx
.cur_vs_args
= args
;
6999 priv_ctx
.string_buffers
= string_buffers
;
7001 /* Base Declarations */
7002 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
7004 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
7005 shader_addline(buffer
, "uniform vec4 pos_fixup;\n");
7007 if (reg_maps
->shader_version
.major
>= 4)
7009 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
7010 shader_glsl_generate_sm4_rasterizer_input_setup(priv
, shader
, args
->next_shader_input_count
, gl_info
);
7011 else if (args
->next_shader_type
== WINED3D_SHADER_TYPE_GEOMETRY
)
7012 shader_glsl_generate_vs_gs_setup(priv
, shader
, args
->next_shader_input_count
, gl_info
);
7015 shader_addline(buffer
, "void main()\n{\n");
7017 /* Base Shader Body */
7018 if (FAILED(shader_generate_main(shader
, buffer
, reg_maps
, &priv_ctx
)))
7021 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7022 if (reg_maps
->shader_version
.major
< 4)
7023 shader_glsl_generate_vs_epilogue(gl_info
, buffer
, shader
, args
);
7025 shader_addline(buffer
, "}\n");
7027 shader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
7028 TRACE("Compiling shader object %u.\n", shader_id
);
7029 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7034 /* Context activation is done by the caller. */
7035 static GLuint
shader_glsl_generate_geometry_shader(const struct wined3d_context
*context
,
7036 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
, const struct gs_compile_args
*args
)
7038 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
7039 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
7040 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
7041 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7042 struct shader_glsl_ctx_priv priv_ctx
;
7045 shader_glsl_add_version_declaration(buffer
, gl_info
, ®_maps
->shader_version
);
7047 shader_glsl_enable_extensions(buffer
, gl_info
);
7048 if (gl_info
->supported
[ARB_GEOMETRY_SHADER4
])
7049 shader_addline(buffer
, "#extension GL_ARB_geometry_shader4 : enable\n");
7051 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
7052 priv_ctx
.string_buffers
= string_buffers
;
7053 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
7054 if (!gl_info
->supported
[ARB_CLIP_CONTROL
])
7055 shader_addline(buffer
, "uniform vec4 pos_fixup;\n");
7056 shader_glsl_generate_sm4_rasterizer_input_setup(priv
, shader
, args
->ps_input_count
, gl_info
);
7057 shader_addline(buffer
, "void main()\n{\n");
7058 if (FAILED(shader_generate_main(shader
, buffer
, reg_maps
, &priv_ctx
)))
7060 shader_addline(buffer
, "}\n");
7062 shader_id
= GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER
));
7063 TRACE("Compiling shader object %u.\n", shader_id
);
7064 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7069 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context
*ctx
)
7071 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
7072 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
7073 const struct wined3d_shader
*shader
= ctx
->shader
;
7075 switch (shader
->reg_maps
.shader_version
.type
)
7077 case WINED3D_SHADER_TYPE_PIXEL
:
7078 shader_glsl_generate_ps_epilogue(gl_info
, ctx
->buffer
, shader
, priv
->cur_ps_args
);
7080 case WINED3D_SHADER_TYPE_VERTEX
:
7081 shader_glsl_generate_vs_epilogue(gl_info
, ctx
->buffer
, shader
, priv
->cur_vs_args
);
7083 case WINED3D_SHADER_TYPE_GEOMETRY
:
7084 case WINED3D_SHADER_TYPE_COMPUTE
:
7087 FIXME("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
7092 /* Context activation is done by the caller. */
7093 static GLuint
shader_glsl_generate_compute_shader(const struct wined3d_context
*context
,
7094 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
7095 const struct wined3d_shader
*shader
)
7097 const struct wined3d_shader_thread_group_size
*thread_group_size
= &shader
->u
.cs
.thread_group_size
;
7098 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
7099 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7100 struct shader_glsl_ctx_priv priv_ctx
;
7104 shader_glsl_add_version_declaration(buffer
, gl_info
, ®_maps
->shader_version
);
7106 shader_glsl_enable_extensions(buffer
, gl_info
);
7107 if (gl_info
->supported
[ARB_COMPUTE_SHADER
])
7108 shader_addline(buffer
, "#extension GL_ARB_compute_shader : enable\n");
7110 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
7111 priv_ctx
.string_buffers
= string_buffers
;
7112 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
7114 for (i
= 0; i
< reg_maps
->tgsm_count
; ++i
)
7116 if (reg_maps
->tgsm
[i
].size
)
7117 shader_addline(buffer
, "shared uint cs_g%u[%u];\n", i
, reg_maps
->tgsm
[i
].size
);
7120 shader_addline(buffer
, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
7121 thread_group_size
->x
, thread_group_size
->y
, thread_group_size
->z
);
7123 shader_addline(buffer
, "void main()\n{\n");
7124 shader_generate_main(shader
, buffer
, reg_maps
, &priv_ctx
);
7125 shader_addline(buffer
, "}\n");
7127 shader_id
= GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER
));
7128 TRACE("Compiling shader object %u.\n", shader_id
);
7129 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7134 static GLuint
find_glsl_pshader(const struct wined3d_context
*context
,
7135 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
7136 struct wined3d_shader
*shader
,
7137 const struct ps_compile_args
*args
, const struct ps_np2fixup_info
**np2fixup_info
)
7139 struct glsl_ps_compiled_shader
*gl_shaders
, *new_array
;
7140 struct glsl_shader_private
*shader_data
;
7141 struct ps_np2fixup_info
*np2fixup
;
7146 if (!shader
->backend_data
)
7148 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
7149 if (!shader
->backend_data
)
7151 ERR("Failed to allocate backend data.\n");
7155 shader_data
= shader
->backend_data
;
7156 gl_shaders
= shader_data
->gl_shaders
.ps
;
7158 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
7159 * so a linear search is more performant than a hashmap or a binary search
7160 * (cache coherency etc)
7162 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
7164 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
7166 if (args
->np2_fixup
)
7167 *np2fixup_info
= &gl_shaders
[i
].np2fixup
;
7168 return gl_shaders
[i
].id
;
7172 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
7173 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
7174 if (shader_data
->num_gl_shaders
)
7176 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
7177 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
,
7178 new_size
* sizeof(*gl_shaders
));
7182 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
7187 ERR("Out of memory\n");
7190 shader_data
->gl_shaders
.ps
= new_array
;
7191 shader_data
->shader_array_size
= new_size
;
7192 gl_shaders
= new_array
;
7195 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
7197 np2fixup
= &gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
;
7198 memset(np2fixup
, 0, sizeof(*np2fixup
));
7199 *np2fixup_info
= args
->np2_fixup
? np2fixup
: NULL
;
7201 pixelshader_update_resource_types(shader
, args
->tex_types
);
7203 string_buffer_clear(buffer
);
7204 ret
= shader_glsl_generate_pshader(context
, buffer
, string_buffers
, shader
, args
, np2fixup
);
7205 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
7210 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
7211 const DWORD use_map
)
7213 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
7214 if((stored
->clip_enabled
) != new->clip_enabled
) return FALSE
;
7215 if (stored
->point_size
!= new->point_size
)
7217 if (stored
->per_vertex_point_size
!= new->per_vertex_point_size
)
7219 if (stored
->flatshading
!= new->flatshading
)
7221 if (stored
->next_shader_type
!= new->next_shader_type
)
7223 if (stored
->next_shader_input_count
!= new->next_shader_input_count
)
7225 return stored
->fog_src
== new->fog_src
;
7228 static GLuint
find_glsl_vshader(const struct wined3d_context
*context
, struct shader_glsl_priv
*priv
,
7229 struct wined3d_shader
*shader
, const struct vs_compile_args
*args
)
7233 DWORD use_map
= context
->stream_info
.use_map
;
7234 struct glsl_vs_compiled_shader
*gl_shaders
, *new_array
;
7235 struct glsl_shader_private
*shader_data
;
7238 if (!shader
->backend_data
)
7240 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
7241 if (!shader
->backend_data
)
7243 ERR("Failed to allocate backend data.\n");
7247 shader_data
= shader
->backend_data
;
7248 gl_shaders
= shader_data
->gl_shaders
.vs
;
7250 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
7251 * so a linear search is more performant than a hashmap or a binary search
7252 * (cache coherency etc)
7254 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
7256 if (vs_args_equal(&gl_shaders
[i
].args
, args
, use_map
))
7257 return gl_shaders
[i
].id
;
7260 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
7262 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
7263 if (shader_data
->num_gl_shaders
)
7265 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
7266 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
,
7267 new_size
* sizeof(*gl_shaders
));
7271 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
7276 ERR("Out of memory\n");
7279 shader_data
->gl_shaders
.vs
= new_array
;
7280 shader_data
->shader_array_size
= new_size
;
7281 gl_shaders
= new_array
;
7284 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
7286 string_buffer_clear(&priv
->shader_buffer
);
7287 ret
= shader_glsl_generate_vshader(context
, priv
, shader
, args
);
7288 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
7293 static GLuint
find_glsl_geometry_shader(const struct wined3d_context
*context
,
7294 struct shader_glsl_priv
*priv
, struct wined3d_shader
*shader
, const struct gs_compile_args
*args
)
7296 struct glsl_gs_compiled_shader
*gl_shaders
, *new_array
;
7297 struct glsl_shader_private
*shader_data
;
7298 unsigned int i
, new_size
;
7301 if (!shader
->backend_data
)
7303 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
7305 ERR("Failed to allocate backend data.\n");
7309 shader_data
= shader
->backend_data
;
7310 gl_shaders
= shader_data
->gl_shaders
.gs
;
7312 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
7314 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
7315 return gl_shaders
[i
].id
;
7318 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
7320 if (shader_data
->num_gl_shaders
)
7322 new_size
= shader_data
->shader_array_size
+ 1;
7323 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
,
7324 new_size
* sizeof(*new_array
));
7328 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
7334 ERR("Failed to allocate GL shaders array.\n");
7337 shader_data
->gl_shaders
.gs
= new_array
;
7338 shader_data
->shader_array_size
= new_size
;
7339 gl_shaders
= new_array
;
7341 string_buffer_clear(&priv
->shader_buffer
);
7342 ret
= shader_glsl_generate_geometry_shader(context
, priv
, shader
, args
);
7343 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
7344 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
7349 static GLuint
find_glsl_compute_shader(const struct wined3d_context
*context
,
7350 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
7351 struct wined3d_shader
*shader
)
7353 struct glsl_cs_compiled_shader
*gl_shaders
;
7354 struct glsl_shader_private
*shader_data
;
7357 if (!shader
->backend_data
)
7359 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
7361 ERR("Failed to allocate backend data.\n");
7365 shader_data
= shader
->backend_data
;
7366 gl_shaders
= shader_data
->gl_shaders
.cs
;
7368 /* No shader variants are used for compute shaders. */
7369 if (shader_data
->num_gl_shaders
)
7370 return gl_shaders
[0].id
;
7372 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
7374 if (!(shader_data
->gl_shaders
.cs
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
))))
7376 ERR("Failed to allocate GL shader array.\n");
7379 shader_data
->shader_array_size
= 1;
7380 gl_shaders
= shader_data
->gl_shaders
.cs
;
7382 string_buffer_clear(buffer
);
7383 ret
= shader_glsl_generate_compute_shader(context
, buffer
, string_buffers
, shader
);
7384 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
7389 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs
, const char *material
)
7393 case WINED3D_MCS_MATERIAL
:
7395 case WINED3D_MCS_COLOR1
:
7396 return "ffp_attrib_diffuse";
7397 case WINED3D_MCS_COLOR2
:
7398 return "ffp_attrib_specular";
7400 ERR("Invalid material color source %#x.\n", mcs
);
7405 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer
*buffer
,
7406 const struct wined3d_ffp_vs_settings
*settings
, BOOL legacy_lighting
)
7408 const char *diffuse
, *specular
, *emissive
, *ambient
;
7409 unsigned int i
, idx
;
7411 if (!settings
->lighting
)
7413 shader_addline(buffer
, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
7414 shader_addline(buffer
, "ffp_varying_specular = ffp_attrib_specular;\n");
7418 shader_addline(buffer
, "vec3 ambient = ffp_light_ambient;\n");
7419 shader_addline(buffer
, "vec3 diffuse = vec3(0.0);\n");
7420 shader_addline(buffer
, "vec4 specular = vec4(0.0);\n");
7421 shader_addline(buffer
, "vec3 dir, dst;\n");
7422 shader_addline(buffer
, "float att, t;\n");
7424 ambient
= shader_glsl_ffp_mcs(settings
->ambient_source
, "ffp_material.ambient");
7425 diffuse
= shader_glsl_ffp_mcs(settings
->diffuse_source
, "ffp_material.diffuse");
7426 specular
= shader_glsl_ffp_mcs(settings
->specular_source
, "ffp_material.specular");
7427 emissive
= shader_glsl_ffp_mcs(settings
->emissive_source
, "ffp_material.emissive");
7430 for (i
= 0; i
< settings
->point_light_count
; ++i
, ++idx
)
7432 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx
);
7433 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
7434 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
7435 shader_addline(buffer
, "dst.x = 1.0;\n");
7436 if (legacy_lighting
)
7438 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx
, idx
);
7439 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
7443 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", idx
);
7445 shader_addline(buffer
, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7446 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx
, idx
, idx
);
7447 if (!legacy_lighting
)
7448 shader_addline(buffer
, "att = 1.0 / att;\n");
7449 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx
);
7450 if (!settings
->normal
)
7452 if (!legacy_lighting
)
7453 shader_addline(buffer
, "}\n");
7456 shader_addline(buffer
, "dir = normalize(dir);\n");
7457 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7458 " * ffp_light[%u].diffuse.xyz) * att;\n", idx
);
7459 if (settings
->localviewer
)
7460 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7462 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7463 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7464 " * ffp_light[%u].specular) * att;\n", idx
);
7465 if (!legacy_lighting
)
7466 shader_addline(buffer
, "}\n");
7469 for (i
= 0; i
< settings
->spot_light_count
; ++i
, ++idx
)
7471 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx
);
7472 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
7473 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
7474 shader_addline(buffer
, "dst.x = 1.0;\n");
7475 if (legacy_lighting
)
7477 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx
, idx
);
7478 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
7482 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", idx
);
7484 shader_addline(buffer
, "dir = normalize(dir);\n");
7485 shader_addline(buffer
, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx
);
7486 shader_addline(buffer
, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx
);
7487 shader_addline(buffer
, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx
);
7488 shader_addline(buffer
, "else att = pow((t - ffp_light[%u].cos_hphi)"
7489 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
7490 idx
, idx
, idx
, idx
);
7491 if (legacy_lighting
)
7492 shader_addline(buffer
, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7493 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7496 shader_addline(buffer
, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7497 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7499 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx
);
7500 if (!settings
->normal
)
7502 if (!legacy_lighting
)
7503 shader_addline(buffer
, "}\n");
7506 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7507 " * ffp_light[%u].diffuse.xyz) * att;\n", idx
);
7508 if (settings
->localviewer
)
7509 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7511 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7512 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7513 " * ffp_light[%u].specular) * att;\n", idx
);
7514 if (!legacy_lighting
)
7515 shader_addline(buffer
, "}\n");
7518 for (i
= 0; i
< settings
->directional_light_count
; ++i
, ++idx
)
7520 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", idx
);
7521 if (!settings
->normal
)
7523 shader_addline(buffer
, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx
);
7524 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7525 " * ffp_light[%u].diffuse.xyz;\n", idx
);
7526 /* TODO: In the non-local viewer case the halfvector is constant
7527 * and could be precomputed and stored in a uniform. */
7528 if (settings
->localviewer
)
7529 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7531 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7532 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7533 " * ffp_light[%u].specular;\n", idx
);
7536 for (i
= 0; i
< settings
->parallel_point_light_count
; ++i
, ++idx
)
7538 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", idx
);
7539 if (!settings
->normal
)
7541 shader_addline(buffer
, "dir = normalize(ffp_light[%u].position.xyz);\n", idx
);
7542 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7543 " * ffp_light[%u].diffuse.xyz;\n", idx
);
7544 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7545 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7546 " * ffp_light[%u].specular;\n", idx
);
7549 shader_addline(buffer
, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
7550 ambient
, diffuse
, emissive
);
7551 shader_addline(buffer
, "ffp_varying_diffuse.w = %s.w;\n", diffuse
);
7552 shader_addline(buffer
, "ffp_varying_specular = %s * specular;\n", specular
);
7555 /* Context activation is done by the caller. */
7556 static GLuint
shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
7557 const struct wined3d_ffp_vs_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
7559 static const struct attrib_info
7562 const char name
[24];
7566 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
7567 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
7568 /* TODO: Indexed vertex blending */
7569 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
7570 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
7571 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
7572 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
7573 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
7575 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
7576 BOOL legacy_lighting
= priv
->legacy_lighting
;
7579 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
7580 BOOL output_legacy_fogcoord
= legacy_context
;
7582 string_buffer_clear(buffer
);
7584 shader_glsl_add_version_declaration(buffer
, gl_info
, NULL
);
7586 if (shader_glsl_use_explicit_attrib_location(gl_info
))
7587 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
7589 for (i
= 0; i
< WINED3D_FFP_ATTRIBS_COUNT
; ++i
)
7591 const char *type
= i
< ARRAY_SIZE(attrib_info
) ? attrib_info
[i
].type
: "vec4";
7593 if (shader_glsl_use_explicit_attrib_location(gl_info
))
7594 shader_addline(buffer
, "layout(location = %u) ", i
);
7595 shader_addline(buffer
, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info
), type
, i
);
7597 shader_addline(buffer
, "\n");
7599 shader_addline(buffer
, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS
);
7600 shader_addline(buffer
, "uniform mat4 ffp_projection_matrix;\n");
7601 shader_addline(buffer
, "uniform mat3 ffp_normal_matrix;\n");
7602 shader_addline(buffer
, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES
);
7604 shader_addline(buffer
, "uniform struct\n{\n");
7605 shader_addline(buffer
, " vec4 emissive;\n");
7606 shader_addline(buffer
, " vec4 ambient;\n");
7607 shader_addline(buffer
, " vec4 diffuse;\n");
7608 shader_addline(buffer
, " vec4 specular;\n");
7609 shader_addline(buffer
, " float shininess;\n");
7610 shader_addline(buffer
, "} ffp_material;\n");
7612 shader_addline(buffer
, "uniform vec3 ffp_light_ambient;\n");
7613 shader_addline(buffer
, "uniform struct\n{\n");
7614 shader_addline(buffer
, " vec4 diffuse;\n");
7615 shader_addline(buffer
, " vec4 specular;\n");
7616 shader_addline(buffer
, " vec4 ambient;\n");
7617 shader_addline(buffer
, " vec4 position;\n");
7618 shader_addline(buffer
, " vec3 direction;\n");
7619 shader_addline(buffer
, " float range;\n");
7620 shader_addline(buffer
, " float falloff;\n");
7621 shader_addline(buffer
, " float c_att;\n");
7622 shader_addline(buffer
, " float l_att;\n");
7623 shader_addline(buffer
, " float q_att;\n");
7624 shader_addline(buffer
, " float cos_htheta;\n");
7625 shader_addline(buffer
, " float cos_hphi;\n");
7626 shader_addline(buffer
, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS
);
7628 if (settings
->point_size
)
7630 shader_addline(buffer
, "uniform struct\n{\n");
7631 shader_addline(buffer
, " float size;\n");
7632 shader_addline(buffer
, " float size_min;\n");
7633 shader_addline(buffer
, " float size_max;\n");
7634 shader_addline(buffer
, " float c_att;\n");
7635 shader_addline(buffer
, " float l_att;\n");
7636 shader_addline(buffer
, " float q_att;\n");
7637 shader_addline(buffer
, "} ffp_point;\n");
7642 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
7643 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
7644 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7645 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
7649 if (settings
->clipping
)
7650 shader_addline(buffer
, "uniform vec4 clip_planes[%u];\n", gl_info
->limits
.user_clip_distances
);
7652 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
7653 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
7654 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7655 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
7658 shader_addline(buffer
, "\nvoid main()\n{\n");
7659 shader_addline(buffer
, "float m;\n");
7660 shader_addline(buffer
, "vec3 r;\n");
7662 for (i
= 0; i
< ARRAY_SIZE(attrib_info
); ++i
)
7664 if (attrib_info
[i
].name
[0])
7665 shader_addline(buffer
, "%s %s = vs_in%u%s;\n", attrib_info
[i
].type
, attrib_info
[i
].name
,
7666 i
, settings
->swizzle_map
& (1u << i
) ? ".zyxw" : "");
7668 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7670 unsigned int coord_idx
= settings
->texgen
[i
] & 0x0000ffff;
7671 if ((settings
->texgen
[i
] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
7672 && settings
->texcoords
& (1u << i
))
7673 shader_addline(buffer
, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i
, coord_idx
+ WINED3D_FFP_TEXCOORD0
);
7676 shader_addline(buffer
, "ffp_attrib_blendweight[%u] = 1.0;\n", settings
->vertexblends
);
7678 if (settings
->transformed
)
7680 shader_addline(buffer
, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
7681 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7682 shader_addline(buffer
, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
7686 for (i
= 0; i
< settings
->vertexblends
; ++i
)
7687 shader_addline(buffer
, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings
->vertexblends
, i
);
7689 shader_addline(buffer
, "vec4 ec_pos = vec4(0.0);\n");
7690 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
7691 shader_addline(buffer
, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i
, i
);
7693 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7694 if (settings
->clipping
)
7696 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
7697 shader_addline(buffer
, "gl_ClipVertex = ec_pos;\n");
7699 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
7700 shader_addline(buffer
, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i
, i
);
7702 shader_addline(buffer
, "ec_pos /= ec_pos.w;\n");
7705 shader_addline(buffer
, "vec3 normal = vec3(0.0);\n");
7706 if (settings
->normal
)
7708 if (!settings
->vertexblends
)
7710 shader_addline(buffer
, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7714 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
7715 shader_addline(buffer
, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i
, i
);
7718 if (settings
->normalize
)
7719 shader_addline(buffer
, "normal = normalize(normal);\n");
7722 shader_glsl_ffp_vertex_lighting(buffer
, settings
, legacy_lighting
);
7725 shader_addline(buffer
, "gl_FrontColor = ffp_varying_diffuse;\n");
7726 shader_addline(buffer
, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7730 shader_addline(buffer
, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7731 shader_addline(buffer
, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7734 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7736 BOOL output_legacy_texcoord
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
7738 switch (settings
->texgen
[i
] & 0xffff0000)
7740 case WINED3DTSS_TCI_PASSTHRU
:
7741 if (settings
->texcoords
& (1u << i
))
7742 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7744 else if (gl_info
->limits
.glsl_varyings
>= wined3d_max_compat_varyings(gl_info
))
7745 shader_addline(buffer
, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i
);
7747 output_legacy_texcoord
= FALSE
;
7750 case WINED3DTSS_TCI_CAMERASPACENORMAL
:
7751 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i
, i
);
7754 case WINED3DTSS_TCI_CAMERASPACEPOSITION
:
7755 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i
, i
);
7758 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
:
7759 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7760 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i
, i
);
7763 case WINED3DTSS_TCI_SPHEREMAP
:
7764 shader_addline(buffer
, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7765 shader_addline(buffer
, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7766 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7767 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i
, i
);
7771 ERR("Unhandled texgen %#x.\n", settings
->texgen
[i
]);
7774 if (output_legacy_texcoord
)
7775 shader_addline(buffer
, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i
, i
);
7778 switch (settings
->fog_mode
)
7780 case WINED3D_FFP_VS_FOG_OFF
:
7781 output_legacy_fogcoord
= FALSE
;
7784 case WINED3D_FFP_VS_FOG_FOGCOORD
:
7785 shader_addline(buffer
, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7788 case WINED3D_FFP_VS_FOG_RANGE
:
7789 shader_addline(buffer
, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7792 case WINED3D_FFP_VS_FOG_DEPTH
:
7793 if (settings
->ortho_fog
)
7795 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
7796 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z;\n");
7798 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7799 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7801 else if (settings
->transformed
)
7803 shader_addline(buffer
, "ffp_varying_fogcoord = ec_pos.z;\n");
7807 shader_addline(buffer
, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7812 ERR("Unhandled fog mode %#x.\n", settings
->fog_mode
);
7815 if (output_legacy_fogcoord
)
7816 shader_addline(buffer
, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7818 if (settings
->point_size
)
7820 shader_addline(buffer
, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7821 " + ffp_point.l_att * length(ec_pos.xyz)"
7822 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7823 settings
->per_vertex_point_size
? "ffp_attrib_psize" : "ffp_point.size");
7824 shader_addline(buffer
, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7827 shader_addline(buffer
, "}\n");
7829 shader_obj
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
7830 shader_glsl_compile(gl_info
, shader_obj
, buffer
->buffer
);
7835 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer
*buffer
,
7836 DWORD argnum
, unsigned int stage
, DWORD arg
)
7840 if (arg
== ARG_UNUSED
)
7841 return "<unused arg>";
7843 switch (arg
& WINED3DTA_SELECTMASK
)
7845 case WINED3DTA_DIFFUSE
:
7846 ret
= "ffp_varying_diffuse";
7849 case WINED3DTA_CURRENT
:
7853 case WINED3DTA_TEXTURE
:
7856 case 0: ret
= "tex0"; break;
7857 case 1: ret
= "tex1"; break;
7858 case 2: ret
= "tex2"; break;
7859 case 3: ret
= "tex3"; break;
7860 case 4: ret
= "tex4"; break;
7861 case 5: ret
= "tex5"; break;
7862 case 6: ret
= "tex6"; break;
7863 case 7: ret
= "tex7"; break;
7865 ret
= "<invalid texture>";
7870 case WINED3DTA_TFACTOR
:
7874 case WINED3DTA_SPECULAR
:
7875 ret
= "ffp_varying_specular";
7878 case WINED3DTA_TEMP
:
7882 case WINED3DTA_CONSTANT
:
7885 case 0: ret
= "tss_const0"; break;
7886 case 1: ret
= "tss_const1"; break;
7887 case 2: ret
= "tss_const2"; break;
7888 case 3: ret
= "tss_const3"; break;
7889 case 4: ret
= "tss_const4"; break;
7890 case 5: ret
= "tss_const5"; break;
7891 case 6: ret
= "tss_const6"; break;
7892 case 7: ret
= "tss_const7"; break;
7894 ret
= "<invalid constant>";
7900 return "<unhandled arg>";
7903 if (arg
& WINED3DTA_COMPLEMENT
)
7905 shader_addline(buffer
, "arg%u = vec4(1.0) - %s;\n", argnum
, ret
);
7908 else if (argnum
== 1)
7910 else if (argnum
== 2)
7914 if (arg
& WINED3DTA_ALPHAREPLICATE
)
7916 shader_addline(buffer
, "arg%u = vec4(%s.w);\n", argnum
, ret
);
7919 else if (argnum
== 1)
7921 else if (argnum
== 2)
7928 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer
*buffer
, unsigned int stage
, BOOL color
,
7929 BOOL alpha
, DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
)
7931 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
7941 dstreg
= "temp_reg";
7945 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, dw_arg0
);
7946 arg1
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 1, stage
, dw_arg1
);
7947 arg2
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 2, stage
, dw_arg2
);
7951 case WINED3D_TOP_DISABLE
:
7954 case WINED3D_TOP_SELECT_ARG1
:
7955 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
);
7958 case WINED3D_TOP_SELECT_ARG2
:
7959 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg2
, dstmask
);
7962 case WINED3D_TOP_MODULATE
:
7963 shader_addline(buffer
, "%s%s = %s%s * %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7966 case WINED3D_TOP_MODULATE_4X
:
7967 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7968 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7971 case WINED3D_TOP_MODULATE_2X
:
7972 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7973 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7976 case WINED3D_TOP_ADD
:
7977 shader_addline(buffer
, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7978 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7981 case WINED3D_TOP_ADD_SIGNED
:
7982 shader_addline(buffer
, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7983 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7986 case WINED3D_TOP_ADD_SIGNED_2X
:
7987 shader_addline(buffer
, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7988 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7991 case WINED3D_TOP_SUBTRACT
:
7992 shader_addline(buffer
, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7993 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7996 case WINED3D_TOP_ADD_SMOOTH
:
7997 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7998 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
, dstmask
);
8001 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA
:
8002 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
8003 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8004 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
8007 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
8008 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
8009 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8010 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
8013 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
8014 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
8015 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8016 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
8019 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
8020 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
8021 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
8022 dstreg
, dstmask
, arg2
, dstmask
, arg0
, arg1
, dstmask
);
8025 case WINED3D_TOP_BLEND_CURRENT_ALPHA
:
8026 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
8027 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8028 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
8031 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR
:
8032 shader_addline(buffer
, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
8033 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
8036 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA
:
8037 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
8038 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
8041 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR
:
8042 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
8043 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
8045 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA
:
8046 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
8047 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
8050 case WINED3D_TOP_BUMPENVMAP
:
8051 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
8052 /* These are handled in the first pass, nothing to do. */
8055 case WINED3D_TOP_DOTPRODUCT3
:
8056 shader_addline(buffer
, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
8057 dstreg
, dstmask
, arg1
, arg2
, dstmask
);
8060 case WINED3D_TOP_MULTIPLY_ADD
:
8061 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
8062 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg0
, dstmask
);
8065 case WINED3D_TOP_LERP
:
8066 /* MSDN isn't quite right here. */
8067 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s%s);\n",
8068 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
, dstmask
);
8072 FIXME("Unhandled operation %#x.\n", op
);
8077 /* Context activation is done by the caller. */
8078 static GLuint
shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
8079 const struct ffp_frag_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
8081 struct wined3d_string_buffer
*tex_reg_name
= string_buffer_get(&priv
->string_buffers
);
8082 enum wined3d_cmp_func alpha_test_func
= settings
->alpha_test_func
+ 1;
8083 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
8084 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
8085 BYTE lum_map
= 0, bump_map
= 0, tex_map
= 0, tss_const_map
= 0;
8086 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
8087 UINT lowest_disabled_stage
;
8089 DWORD arg0
, arg1
, arg2
;
8092 string_buffer_clear(buffer
);
8094 /* Find out which textures are read */
8095 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
8097 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
8100 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
8101 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
8102 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
8104 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
8105 || (stage
== 0 && settings
->color_key_enabled
))
8106 tex_map
|= 1u << stage
;
8107 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
8108 tfactor_used
= TRUE
;
8109 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
8110 tempreg_used
= TRUE
;
8111 if (settings
->op
[stage
].dst
== tempreg
)
8112 tempreg_used
= TRUE
;
8113 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
8114 tss_const_map
|= 1u << stage
;
8116 switch (settings
->op
[stage
].cop
)
8118 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
8119 lum_map
|= 1u << stage
;
8121 case WINED3D_TOP_BUMPENVMAP
:
8122 bump_map
|= 1u << stage
;
8124 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
8125 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
8126 tex_map
|= 1u << stage
;
8129 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
8130 tfactor_used
= TRUE
;
8137 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
8140 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
8141 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
8142 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
8144 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
)
8145 tex_map
|= 1u << stage
;
8146 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
8147 tfactor_used
= TRUE
;
8148 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
8149 tempreg_used
= TRUE
;
8150 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
8151 tss_const_map
|= 1u << stage
;
8153 lowest_disabled_stage
= stage
;
8155 shader_glsl_add_version_declaration(buffer
, gl_info
, NULL
);
8157 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
8158 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
8160 if (!needs_legacy_glsl_syntax(gl_info
))
8161 shader_addline(buffer
, "out vec4 ps_out[1];\n");
8163 shader_addline(buffer
, "vec4 tmp0, tmp1;\n");
8164 shader_addline(buffer
, "vec4 ret;\n");
8165 if (tempreg_used
|| settings
->sRGB_write
)
8166 shader_addline(buffer
, "vec4 temp_reg = vec4(0.0);\n");
8167 shader_addline(buffer
, "vec4 arg0, arg1, arg2;\n");
8169 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
8171 if (tss_const_map
& (1u << stage
))
8172 shader_addline(buffer
, "uniform vec4 tss_const%u;\n", stage
);
8174 if (!(tex_map
& (1u << stage
)))
8177 switch (settings
->op
[stage
].tex_type
)
8179 case WINED3D_GL_RES_TYPE_TEX_1D
:
8180 shader_addline(buffer
, "uniform sampler1D ps_sampler%u;\n", stage
);
8182 case WINED3D_GL_RES_TYPE_TEX_2D
:
8183 shader_addline(buffer
, "uniform sampler2D ps_sampler%u;\n", stage
);
8185 case WINED3D_GL_RES_TYPE_TEX_3D
:
8186 shader_addline(buffer
, "uniform sampler3D ps_sampler%u;\n", stage
);
8188 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
8189 shader_addline(buffer
, "uniform samplerCube ps_sampler%u;\n", stage
);
8191 case WINED3D_GL_RES_TYPE_TEX_RECT
:
8192 shader_addline(buffer
, "uniform sampler2DRect ps_sampler%u;\n", stage
);
8195 FIXME("Unhandled sampler type %#x.\n", settings
->op
[stage
].tex_type
);
8199 shader_addline(buffer
, "vec4 tex%u;\n", stage
);
8201 if (!(bump_map
& (1u << stage
)))
8203 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", stage
);
8205 if (!(lum_map
& (1u << stage
)))
8207 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", stage
);
8208 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", stage
);
8211 shader_addline(buffer
, "uniform vec4 tex_factor;\n");
8212 if (settings
->color_key_enabled
)
8213 shader_addline(buffer
, "uniform vec4 color_key[2];\n");
8214 shader_addline(buffer
, "uniform vec4 specular_enable;\n");
8216 if (settings
->sRGB_write
)
8218 shader_addline(buffer
, "const vec4 srgb_const0 = ");
8219 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
8220 shader_addline(buffer
, ";\n");
8221 shader_addline(buffer
, "const vec4 srgb_const1 = ");
8222 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
8223 shader_addline(buffer
, ";\n");
8226 shader_addline(buffer
, "uniform struct\n{\n");
8227 shader_addline(buffer
, " vec4 color;\n");
8228 shader_addline(buffer
, " float density;\n");
8229 shader_addline(buffer
, " float end;\n");
8230 shader_addline(buffer
, " float scale;\n");
8231 shader_addline(buffer
, "} ffp_fog;\n");
8233 if (alpha_test_func
!= WINED3D_CMP_ALWAYS
)
8234 shader_addline(buffer
, "uniform float alpha_test_ref;\n");
8238 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
8239 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
8240 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
8241 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
8242 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
8246 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
8247 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
8248 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
8249 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
8250 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
8253 shader_addline(buffer
, "void main()\n{\n");
8257 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
8258 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
8261 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
8263 if (tex_map
& (1u << stage
))
8265 if (settings
->pointsprite
)
8266 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage
);
8267 else if (settings
->texcoords_initialized
& (1u << stage
))
8268 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n",
8269 stage
, legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", stage
);
8271 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", stage
);
8275 if (legacy_context
&& settings
->fog
!= WINED3D_FFP_PS_FOG_OFF
)
8276 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
8278 if (lowest_disabled_stage
< 7 && settings
->emul_clipplanes
)
8279 shader_addline(buffer
, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
8281 /* Generate texture sampling instructions */
8282 for (stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3D_TOP_DISABLE
; ++stage
)
8284 const char *texture_function
, *coord_mask
;
8287 if (!(tex_map
& (1u << stage
)))
8290 if (settings
->op
[stage
].projected
== proj_none
)
8294 else if (settings
->op
[stage
].projected
== proj_count4
8295 || settings
->op
[stage
].projected
== proj_count3
)
8301 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
8305 if (settings
->op
[stage
].tex_type
== WINED3D_GL_RES_TYPE_TEX_CUBE
)
8308 switch (settings
->op
[stage
].tex_type
)
8310 case WINED3D_GL_RES_TYPE_TEX_1D
:
8313 texture_function
= "texture1DProj";
8318 texture_function
= "texture1D";
8322 case WINED3D_GL_RES_TYPE_TEX_2D
:
8325 texture_function
= "texture2DProj";
8330 texture_function
= "texture2D";
8334 case WINED3D_GL_RES_TYPE_TEX_3D
:
8337 texture_function
= "texture3DProj";
8338 coord_mask
= "xyzw";
8342 texture_function
= "texture3D";
8346 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
8347 texture_function
= "textureCube";
8350 case WINED3D_GL_RES_TYPE_TEX_RECT
:
8353 texture_function
= "texture2DRectProj";
8358 texture_function
= "texture2DRect";
8363 FIXME("Unhandled texture type %#x.\n", settings
->op
[stage
].tex_type
);
8364 texture_function
= "";
8365 coord_mask
= "xyzw";
8368 if (!needs_legacy_glsl_syntax(gl_info
))
8369 texture_function
= proj
? "textureProj" : "texture";
8372 && (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP
8373 || settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
))
8375 shader_addline(buffer
, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage
- 1, stage
- 1);
8377 /* With projective textures, texbem only divides the static
8378 * texture coord, not the displacement, so multiply the
8379 * displacement with the dividing parameter before passing it to
8381 if (settings
->op
[stage
].projected
!= proj_none
)
8383 if (settings
->op
[stage
].projected
== proj_count4
)
8385 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
8387 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].ww;\n", stage
);
8391 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
8393 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].zz;\n", stage
);
8398 shader_addline(buffer
, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage
);
8401 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ret.%s);\n",
8402 stage
, texture_function
, stage
, coord_mask
);
8404 if (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
8405 shader_addline(buffer
, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
8406 stage
, stage
- 1, stage
- 1, stage
- 1);
8408 else if (settings
->op
[stage
].projected
== proj_count3
)
8410 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
8411 stage
, texture_function
, stage
, stage
);
8415 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
8416 stage
, texture_function
, stage
, stage
, coord_mask
);
8419 string_buffer_sprintf(tex_reg_name
, "tex%u", stage
);
8420 shader_glsl_color_correction_ext(buffer
, tex_reg_name
->buffer
, WINED3DSP_WRITEMASK_ALL
,
8421 settings
->op
[stage
].color_fixup
);
8424 if (settings
->color_key_enabled
)
8426 shader_addline(buffer
, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
8427 shader_addline(buffer
, " discard;\n");
8430 shader_addline(buffer
, "ret = ffp_varying_diffuse;\n");
8432 /* Generate the main shader */
8433 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
8437 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
8440 if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
8441 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
8442 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
8443 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
8444 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
8445 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
8446 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
8447 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
8448 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
8449 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
8450 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
8451 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
8453 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
8454 && settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
8455 && settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
8456 && settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
8458 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
8460 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
8461 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
8462 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
8466 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
8467 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
8468 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
8470 else if (settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP
8471 && settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
8473 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
8474 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
8475 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
8476 shader_glsl_ffp_fragment_op(buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
8477 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
8478 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
8482 shader_addline(buffer
, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
8483 get_fragment_output(gl_info
));
8485 if (settings
->sRGB_write
)
8486 shader_glsl_generate_srgb_write_correction(buffer
, gl_info
);
8488 shader_glsl_generate_fog_code(buffer
, gl_info
, settings
->fog
);
8490 shader_glsl_generate_alpha_test(buffer
, gl_info
, alpha_test_func
);
8492 shader_addline(buffer
, "}\n");
8494 shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
8495 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
8497 string_buffer_release(&priv
->string_buffers
, tex_reg_name
);
8501 static struct glsl_ffp_vertex_shader
*shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
8502 const struct wined3d_gl_info
*gl_info
, const struct wined3d_ffp_vs_settings
*settings
)
8504 struct glsl_ffp_vertex_shader
*shader
;
8505 const struct wine_rb_entry
*entry
;
8507 if ((entry
= wine_rb_get(&priv
->ffp_vertex_shaders
, settings
)))
8508 return WINE_RB_ENTRY_VALUE(entry
, struct glsl_ffp_vertex_shader
, desc
.entry
);
8510 if (!(shader
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader
))))
8513 shader
->desc
.settings
= *settings
;
8514 shader
->id
= shader_glsl_generate_ffp_vertex_shader(priv
, settings
, gl_info
);
8515 list_init(&shader
->linked_programs
);
8516 if (wine_rb_put(&priv
->ffp_vertex_shaders
, &shader
->desc
.settings
, &shader
->desc
.entry
) == -1)
8517 ERR("Failed to insert ffp vertex shader.\n");
8522 static struct glsl_ffp_fragment_shader
*shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
8523 const struct wined3d_gl_info
*gl_info
, const struct ffp_frag_settings
*args
)
8525 struct glsl_ffp_fragment_shader
*glsl_desc
;
8526 const struct ffp_frag_desc
*desc
;
8528 if ((desc
= find_ffp_frag_shader(&priv
->ffp_fragment_shaders
, args
)))
8529 return CONTAINING_RECORD(desc
, struct glsl_ffp_fragment_shader
, entry
);
8531 if (!(glsl_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc
))))
8534 glsl_desc
->entry
.settings
= *args
;
8535 glsl_desc
->id
= shader_glsl_generate_ffp_fragment_shader(priv
, args
, gl_info
);
8536 list_init(&glsl_desc
->linked_programs
);
8537 add_ffp_frag_shader(&priv
->ffp_fragment_shaders
, &glsl_desc
->entry
);
8543 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
8544 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_vs_program
*vs
, unsigned int vs_c_count
)
8547 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
8549 for (i
= 0; i
< vs_c_count
; ++i
)
8551 string_buffer_sprintf(name
, "vs_c[%u]", i
);
8552 vs
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8554 memset(&vs
->uniform_f_locations
[vs_c_count
], 0xff, (WINED3D_MAX_VS_CONSTS_F
- vs_c_count
) * sizeof(GLuint
));
8556 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
8558 string_buffer_sprintf(name
, "vs_i[%u]", i
);
8559 vs
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8562 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
8564 string_buffer_sprintf(name
, "vs_b[%u]", i
);
8565 vs
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8568 vs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "pos_fixup"));
8570 for (i
= 0; i
< MAX_VERTEX_BLENDS
; ++i
)
8572 string_buffer_sprintf(name
, "ffp_modelview_matrix[%u]", i
);
8573 vs
->modelview_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8575 vs
->projection_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_projection_matrix"));
8576 vs
->normal_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_normal_matrix"));
8577 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
8579 string_buffer_sprintf(name
, "ffp_texture_matrix[%u]", i
);
8580 vs
->texture_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8582 vs
->material_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.ambient"));
8583 vs
->material_diffuse_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.diffuse"));
8584 vs
->material_specular_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.specular"));
8585 vs
->material_emissive_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.emissive"));
8586 vs
->material_shininess_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.shininess"));
8587 vs
->light_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_light_ambient"));
8588 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
8590 string_buffer_sprintf(name
, "ffp_light[%u].diffuse", i
);
8591 vs
->light_location
[i
].diffuse
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8592 string_buffer_sprintf(name
, "ffp_light[%u].specular", i
);
8593 vs
->light_location
[i
].specular
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8594 string_buffer_sprintf(name
, "ffp_light[%u].ambient", i
);
8595 vs
->light_location
[i
].ambient
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8596 string_buffer_sprintf(name
, "ffp_light[%u].position", i
);
8597 vs
->light_location
[i
].position
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8598 string_buffer_sprintf(name
, "ffp_light[%u].direction", i
);
8599 vs
->light_location
[i
].direction
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8600 string_buffer_sprintf(name
, "ffp_light[%u].range", i
);
8601 vs
->light_location
[i
].range
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8602 string_buffer_sprintf(name
, "ffp_light[%u].falloff", i
);
8603 vs
->light_location
[i
].falloff
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8604 string_buffer_sprintf(name
, "ffp_light[%u].c_att", i
);
8605 vs
->light_location
[i
].c_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8606 string_buffer_sprintf(name
, "ffp_light[%u].l_att", i
);
8607 vs
->light_location
[i
].l_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8608 string_buffer_sprintf(name
, "ffp_light[%u].q_att", i
);
8609 vs
->light_location
[i
].q_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8610 string_buffer_sprintf(name
, "ffp_light[%u].cos_htheta", i
);
8611 vs
->light_location
[i
].cos_htheta
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8612 string_buffer_sprintf(name
, "ffp_light[%u].cos_hphi", i
);
8613 vs
->light_location
[i
].cos_hphi
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8615 vs
->pointsize_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size"));
8616 vs
->pointsize_min_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_min"));
8617 vs
->pointsize_max_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_max"));
8618 vs
->pointsize_c_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.c_att"));
8619 vs
->pointsize_l_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.l_att"));
8620 vs
->pointsize_q_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.q_att"));
8621 vs
->clip_planes_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "clip_planes"));
8623 string_buffer_release(&priv
->string_buffers
, name
);
8626 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
8627 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_gs_program
*gs
)
8629 gs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "pos_fixup"));
8632 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info
*gl_info
,
8633 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_ps_program
*ps
, unsigned int ps_c_count
)
8636 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
8638 for (i
= 0; i
< ps_c_count
; ++i
)
8640 string_buffer_sprintf(name
, "ps_c[%u]", i
);
8641 ps
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8643 memset(&ps
->uniform_f_locations
[ps_c_count
], 0xff, (WINED3D_MAX_PS_CONSTS_F
- ps_c_count
) * sizeof(GLuint
));
8645 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
8647 string_buffer_sprintf(name
, "ps_i[%u]", i
);
8648 ps
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8651 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
8653 string_buffer_sprintf(name
, "ps_b[%u]", i
);
8654 ps
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8657 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
8659 string_buffer_sprintf(name
, "bumpenv_mat%u", i
);
8660 ps
->bumpenv_mat_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8661 string_buffer_sprintf(name
, "bumpenv_lum_scale%u", i
);
8662 ps
->bumpenv_lum_scale_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8663 string_buffer_sprintf(name
, "bumpenv_lum_offset%u", i
);
8664 ps
->bumpenv_lum_offset_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8665 string_buffer_sprintf(name
, "tss_const%u", i
);
8666 ps
->tss_constant_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
8669 ps
->tex_factor_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "tex_factor"));
8670 ps
->specular_enable_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "specular_enable"));
8672 ps
->fog_color_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.color"));
8673 ps
->fog_density_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.density"));
8674 ps
->fog_end_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.end"));
8675 ps
->fog_scale_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.scale"));
8677 ps
->alpha_test_ref_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "alpha_test_ref"));
8679 ps
->np2_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ps_samplerNP2Fixup"));
8680 ps
->ycorrection_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ycorrection"));
8681 ps
->color_key_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "color_key"));
8683 string_buffer_release(&priv
->string_buffers
, name
);
8686 /* Context activation is done by the caller. */
8687 static void set_glsl_compute_shader_program(const struct wined3d_context
*context
,
8688 const struct wined3d_state
*state
, struct shader_glsl_priv
*priv
, struct glsl_context_data
*ctx_data
)
8690 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8691 struct glsl_shader_prog_link
*entry
= NULL
;
8692 struct wined3d_shader
*shader
;
8693 struct glsl_program_key key
;
8694 GLuint program_id
, cs_id
;
8696 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_COMPUTE
)))
8699 if (!(shader
= state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
]))
8701 WARN("Compute shader is NULL.\n");
8702 ctx_data
->glsl_program
= NULL
;
8706 cs_id
= find_glsl_compute_shader(context
, &priv
->shader_buffer
, &priv
->string_buffers
, shader
);
8707 memset(&key
, 0, sizeof(key
));
8709 if ((entry
= get_glsl_program_entry(priv
, &key
)))
8711 ctx_data
->glsl_program
= entry
;
8715 program_id
= GL_EXTCALL(glCreateProgram());
8716 TRACE("Created new GLSL shader program %u.\n", program_id
);
8718 if (!(entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
))))
8720 ERR("Out of memory.\n");
8723 entry
->id
= program_id
;
8727 entry
->cs
.id
= cs_id
;
8728 entry
->constant_version
= 0;
8729 entry
->ps
.np2_fixup_info
= NULL
;
8730 add_glsl_program_entry(priv
, entry
);
8732 ctx_data
->glsl_program
= entry
;
8734 TRACE("Attaching GLSL shader object %u to program %u.\n", cs_id
, program_id
);
8735 GL_EXTCALL(glAttachShader(program_id
, cs_id
));
8736 checkGLcall("glAttachShader");
8738 list_add_head(&shader
->linked_programs
, &entry
->cs
.shader_entry
);
8740 TRACE("Linking GLSL shader program %u.\n", program_id
);
8741 GL_EXTCALL(glLinkProgram(program_id
));
8742 shader_glsl_validate_link(gl_info
, program_id
);
8744 GL_EXTCALL(glUseProgram(program_id
));
8745 checkGLcall("glUseProgram");
8746 shader_glsl_load_program_resources(context
, priv
, program_id
, shader
);
8747 shader_glsl_load_images(gl_info
, priv
, program_id
, &shader
->reg_maps
);
8749 entry
->constant_update_mask
= 0;
8752 /* Context activation is done by the caller. */
8753 static void set_glsl_shader_program(const struct wined3d_context
*context
, const struct wined3d_state
*state
,
8754 struct shader_glsl_priv
*priv
, struct glsl_context_data
*ctx_data
)
8756 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8757 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
8758 const struct ps_np2fixup_info
*np2fixup_info
= NULL
;
8759 struct glsl_shader_prog_link
*entry
= NULL
;
8760 struct wined3d_shader
*vshader
= NULL
;
8761 struct wined3d_shader
*gshader
= NULL
;
8762 struct wined3d_shader
*pshader
= NULL
;
8763 GLuint reorder_shader_id
= 0;
8764 struct glsl_program_key key
;
8770 struct list
*ps_list
, *vs_list
;
8772 struct wined3d_string_buffer
*tmp_name
;
8774 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_VERTEX
)) && ctx_data
->glsl_program
)
8776 vs_id
= ctx_data
->glsl_program
->vs
.id
;
8777 vs_list
= &ctx_data
->glsl_program
->vs
.shader_entry
;
8781 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
8782 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
8784 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_GEOMETRY
))
8785 && ctx_data
->glsl_program
->gs
.id
)
8787 gs_id
= ctx_data
->glsl_program
->gs
.id
;
8791 struct gs_compile_args args
;
8793 find_gs_compile_args(state
, gshader
, &args
);
8794 gs_id
= find_glsl_geometry_shader(context
, priv
, gshader
, &args
);
8798 else if (use_vs(state
))
8800 struct vs_compile_args vs_compile_args
;
8802 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
8803 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
8805 find_vs_compile_args(state
, vshader
, context
->stream_info
.swizzle_map
, &vs_compile_args
, d3d_info
);
8806 vs_id
= find_glsl_vshader(context
, priv
, vshader
, &vs_compile_args
);
8807 vs_list
= &vshader
->linked_programs
;
8809 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_GEOMETRY
))
8810 && ctx_data
->glsl_program
->gs
.id
)
8812 gs_id
= ctx_data
->glsl_program
->gs
.id
;
8816 struct gs_compile_args gs_compile_args
;
8818 find_gs_compile_args(state
, gshader
, &gs_compile_args
);
8819 gs_id
= find_glsl_geometry_shader(context
, priv
, gshader
, &gs_compile_args
);
8822 else if (priv
->vertex_pipe
== &glsl_vertex_pipe
)
8824 struct glsl_ffp_vertex_shader
*ffp_shader
;
8825 struct wined3d_ffp_vs_settings settings
;
8827 wined3d_ffp_get_vs_settings(context
, state
, &settings
);
8828 ffp_shader
= shader_glsl_find_ffp_vertex_shader(priv
, gl_info
, &settings
);
8829 vs_id
= ffp_shader
->id
;
8830 vs_list
= &ffp_shader
->linked_programs
;
8833 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_PIXEL
)) && ctx_data
->glsl_program
)
8835 ps_id
= ctx_data
->glsl_program
->ps
.id
;
8836 ps_list
= &ctx_data
->glsl_program
->ps
.shader_entry
;
8839 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
8841 else if (use_ps(state
))
8843 struct ps_compile_args ps_compile_args
;
8844 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
8845 find_ps_compile_args(state
, pshader
, context
->stream_info
.position_transformed
, &ps_compile_args
, context
);
8846 ps_id
= find_glsl_pshader(context
, &priv
->shader_buffer
, &priv
->string_buffers
,
8847 pshader
, &ps_compile_args
, &np2fixup_info
);
8848 ps_list
= &pshader
->linked_programs
;
8850 else if (priv
->fragment_pipe
== &glsl_fragment_pipe
)
8852 struct glsl_ffp_fragment_shader
*ffp_shader
;
8853 struct ffp_frag_settings settings
;
8855 gen_ffp_frag_op(context
, state
, &settings
, FALSE
);
8856 ffp_shader
= shader_glsl_find_ffp_fragment_shader(priv
, gl_info
, &settings
);
8857 ps_id
= ffp_shader
->id
;
8858 ps_list
= &ffp_shader
->linked_programs
;
8865 if ((!vs_id
&& !gs_id
&& !ps_id
) || (entry
= get_glsl_program_entry(priv
, &key
)))
8867 ctx_data
->glsl_program
= entry
;
8871 /* If we get to this point, then no matching program exists, so we create one */
8872 program_id
= GL_EXTCALL(glCreateProgram());
8873 TRACE("Created new GLSL shader program %u.\n", program_id
);
8875 /* Create the entry */
8876 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
8877 entry
->id
= program_id
;
8878 entry
->vs
.id
= vs_id
;
8879 entry
->gs
.id
= gs_id
;
8880 entry
->ps
.id
= ps_id
;
8882 entry
->constant_version
= 0;
8883 entry
->ps
.np2_fixup_info
= np2fixup_info
;
8884 /* Add the hash table entry */
8885 add_glsl_program_entry(priv
, entry
);
8887 /* Set the current program */
8888 ctx_data
->glsl_program
= entry
;
8890 /* Attach GLSL vshader */
8893 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id
, program_id
);
8894 GL_EXTCALL(glAttachShader(program_id
, vs_id
));
8895 checkGLcall("glAttachShader");
8897 list_add_head(vs_list
, &entry
->vs
.shader_entry
);
8902 attribs_map
= vshader
->reg_maps
.input_registers
;
8903 if (vshader
->reg_maps
.shader_version
.major
< 4)
8905 reorder_shader_id
= shader_glsl_generate_vs3_rasterizer_input_setup(priv
, vshader
, pshader
,
8906 state
->gl_primitive_type
== GL_POINTS
&& vshader
->reg_maps
.point_size
,
8907 d3d_info
->emulated_flatshading
8908 && state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
, gl_info
);
8909 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id
, program_id
);
8910 GL_EXTCALL(glAttachShader(program_id
, reorder_shader_id
));
8911 checkGLcall("glAttachShader");
8912 /* Flag the reorder function for deletion, it will be freed
8913 * automatically when the program is destroyed. */
8914 GL_EXTCALL(glDeleteShader(reorder_shader_id
));
8919 attribs_map
= (1u << WINED3D_FFP_ATTRIBS_COUNT
) - 1;
8922 if (!shader_glsl_use_explicit_attrib_location(gl_info
))
8924 /* Bind vertex attributes to a corresponding index number to match
8925 * the same index numbers as ARB_vertex_programs (makes loading
8926 * vertex attributes simpler). With this method, we can use the
8927 * exact same code to load the attributes later for both ARB and
8930 * We have to do this here because we need to know the Program ID
8931 * in order to make the bindings work, and it has to be done prior
8932 * to linking the GLSL program. */
8933 tmp_name
= string_buffer_get(&priv
->string_buffers
);
8934 for (i
= 0; attribs_map
; attribs_map
>>= 1, ++i
)
8936 if (!(attribs_map
& 1))
8939 string_buffer_sprintf(tmp_name
, "vs_in%u", i
);
8940 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
8941 if (vshader
&& vshader
->reg_maps
.shader_version
.major
>= 4)
8943 string_buffer_sprintf(tmp_name
, "vs_in_uint%u", i
);
8944 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
8945 string_buffer_sprintf(tmp_name
, "vs_in_int%u", i
);
8946 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
8949 checkGLcall("glBindAttribLocation");
8950 string_buffer_release(&priv
->string_buffers
, tmp_name
);
8955 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id
, program_id
);
8956 GL_EXTCALL(glAttachShader(program_id
, gs_id
));
8957 checkGLcall("glAttachShader");
8959 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
8961 TRACE("input type %s, output type %s, vertices out %u.\n",
8962 debug_d3dprimitivetype(gshader
->u
.gs
.input_type
),
8963 debug_d3dprimitivetype(gshader
->u
.gs
.output_type
),
8964 gshader
->u
.gs
.vertices_out
);
8965 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_INPUT_TYPE_ARB
,
8966 gl_primitive_type_from_d3d(gshader
->u
.gs
.input_type
)));
8967 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_OUTPUT_TYPE_ARB
,
8968 gl_primitive_type_from_d3d(gshader
->u
.gs
.output_type
)));
8969 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_VERTICES_OUT_ARB
,
8970 gshader
->u
.gs
.vertices_out
));
8971 checkGLcall("glProgramParameteriARB");
8974 list_add_head(&gshader
->linked_programs
, &entry
->gs
.shader_entry
);
8977 /* Attach GLSL pshader */
8980 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id
, program_id
);
8981 GL_EXTCALL(glAttachShader(program_id
, ps_id
));
8982 checkGLcall("glAttachShader");
8984 list_add_head(ps_list
, &entry
->ps
.shader_entry
);
8987 /* Link the program */
8988 TRACE("Linking GLSL shader program %u.\n", program_id
);
8989 GL_EXTCALL(glLinkProgram(program_id
));
8990 shader_glsl_validate_link(gl_info
, program_id
);
8992 shader_glsl_init_vs_uniform_locations(gl_info
, priv
, program_id
, &entry
->vs
,
8993 vshader
? vshader
->limits
->constant_float
: 0);
8994 shader_glsl_init_gs_uniform_locations(gl_info
, priv
, program_id
, &entry
->gs
);
8995 shader_glsl_init_ps_uniform_locations(gl_info
, priv
, program_id
, &entry
->ps
,
8996 pshader
? pshader
->limits
->constant_float
: 0);
8997 checkGLcall("Find glsl program uniform locations");
8999 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
9001 if (pshader
&& pshader
->reg_maps
.shader_version
.major
>= 3
9002 && pshader
->u
.ps
.declared_in_count
> vec4_varyings(3, gl_info
))
9004 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id
);
9005 entry
->vs
.vertex_color_clamp
= GL_FALSE
;
9009 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
9014 /* With core profile we never change vertex_color_clamp from
9015 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
9016 * glClampColorARB(). */
9017 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
9020 /* Set the shader to allow uniform loading on it */
9021 GL_EXTCALL(glUseProgram(program_id
));
9022 checkGLcall("glUseProgram");
9024 entry
->constant_update_mask
= 0;
9027 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
9028 if (vshader
->reg_maps
.integer_constants
)
9029 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_I
;
9030 if (vshader
->reg_maps
.boolean_constants
)
9031 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_B
;
9032 if (entry
->vs
.pos_fixup_location
!= -1)
9033 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
9035 shader_glsl_load_program_resources(context
, priv
, program_id
, vshader
);
9039 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
9040 | WINED3D_SHADER_CONST_FFP_PROJ
;
9042 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
9044 if (entry
->vs
.modelview_matrix_location
[i
] != -1)
9046 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
9051 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
9053 if (entry
->vs
.texture_matrix_location
[i
] != -1)
9055 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
9059 if (entry
->vs
.material_ambient_location
!= -1 || entry
->vs
.material_diffuse_location
!= -1
9060 || entry
->vs
.material_specular_location
!= -1
9061 || entry
->vs
.material_emissive_location
!= -1
9062 || entry
->vs
.material_shininess_location
!= -1)
9063 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
9064 if (entry
->vs
.light_ambient_location
!= -1)
9065 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
9067 if (entry
->vs
.clip_planes_location
!= -1)
9068 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
9069 if (entry
->vs
.pointsize_min_location
!= -1)
9070 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
9074 if (entry
->gs
.pos_fixup_location
!= -1)
9075 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
9077 shader_glsl_load_program_resources(context
, priv
, program_id
, gshader
);
9084 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
9085 if (pshader
->reg_maps
.integer_constants
)
9086 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_I
;
9087 if (pshader
->reg_maps
.boolean_constants
)
9088 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_B
;
9089 if (entry
->ps
.ycorrection_location
!= -1)
9090 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
9092 shader_glsl_load_program_resources(context
, priv
, program_id
, pshader
);
9093 shader_glsl_load_images(gl_info
, priv
, program_id
, &pshader
->reg_maps
);
9097 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
9099 shader_glsl_load_samplers_range(gl_info
, priv
, program_id
,
9100 shader_glsl_get_prefix(WINED3D_SHADER_TYPE_PIXEL
),
9101 0, MAX_TEXTURES
, context
->tex_unit_map
);
9104 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
9106 if (entry
->ps
.bumpenv_mat_location
[i
] != -1)
9108 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_BUMP_ENV
;
9113 if (entry
->ps
.fog_color_location
!= -1)
9114 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
9115 if (entry
->ps
.alpha_test_ref_location
!= -1)
9116 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_ALPHA_TEST
;
9117 if (entry
->ps
.np2_fixup_location
!= -1)
9118 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_NP2_FIXUP
;
9119 if (entry
->ps
.color_key_location
!= -1)
9120 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
9124 /* Context activation is done by the caller. */
9125 static void shader_glsl_select(void *shader_priv
, struct wined3d_context
*context
,
9126 const struct wined3d_state
*state
)
9128 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
9129 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9130 struct shader_glsl_priv
*priv
= shader_priv
;
9131 GLenum current_vertex_color_clamp
;
9132 GLuint program_id
, prev_id
;
9134 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
9135 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
9137 prev_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
9139 set_glsl_shader_program(context
, state
, priv
, ctx_data
);
9141 if (ctx_data
->glsl_program
)
9143 program_id
= ctx_data
->glsl_program
->id
;
9144 current_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
9149 current_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
9152 if (ctx_data
->vertex_color_clamp
!= current_vertex_color_clamp
)
9154 ctx_data
->vertex_color_clamp
= current_vertex_color_clamp
;
9155 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
9157 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
9158 checkGLcall("glClampColorARB");
9162 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
9166 TRACE("Using GLSL program %u.\n", program_id
);
9168 if (prev_id
!= program_id
)
9170 GL_EXTCALL(glUseProgram(program_id
));
9171 checkGLcall("glUseProgram");
9174 context
->constant_update_mask
|= ctx_data
->glsl_program
->constant_update_mask
;
9177 context
->shader_update_mask
|= (1u << WINED3D_SHADER_TYPE_COMPUTE
);
9180 /* Context activation is done by the caller. */
9181 static void shader_glsl_select_compute(void *shader_priv
, struct wined3d_context
*context
,
9182 const struct wined3d_state
*state
)
9184 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
9185 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9186 struct shader_glsl_priv
*priv
= shader_priv
;
9187 GLuint program_id
, prev_id
;
9189 prev_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
9190 set_glsl_compute_shader_program(context
, state
, priv
, ctx_data
);
9191 program_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
9193 TRACE("Using GLSL program %u.\n", program_id
);
9195 if (prev_id
!= program_id
)
9197 GL_EXTCALL(glUseProgram(program_id
));
9198 checkGLcall("glUseProgram");
9201 context
->shader_update_mask
|= (1u << WINED3D_SHADER_TYPE_PIXEL
)
9202 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
9203 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
9204 | (1u << WINED3D_SHADER_TYPE_HULL
)
9205 | (1u << WINED3D_SHADER_TYPE_DOMAIN
);
9208 /* "context" is not necessarily the currently active context. */
9209 static void shader_glsl_invalidate_current_program(struct wined3d_context
*context
)
9211 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
9213 ctx_data
->glsl_program
= NULL
;
9214 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
9215 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
9216 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
9217 | (1u << WINED3D_SHADER_TYPE_HULL
)
9218 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
9219 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
9222 /* Context activation is done by the caller. */
9223 static void shader_glsl_disable(void *shader_priv
, struct wined3d_context
*context
)
9225 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
9226 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9227 struct shader_glsl_priv
*priv
= shader_priv
;
9229 shader_glsl_invalidate_current_program(context
);
9230 GL_EXTCALL(glUseProgram(0));
9231 checkGLcall("glUseProgram");
9233 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
9234 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
9236 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
9238 ctx_data
->vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
9239 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, GL_FIXED_ONLY_ARB
));
9240 checkGLcall("glClampColorARB");
9244 static void shader_glsl_invalidate_contexts_program(struct wined3d_device
*device
,
9245 const struct glsl_shader_prog_link
*program
)
9247 const struct glsl_context_data
*ctx_data
;
9248 struct wined3d_context
*context
;
9251 for (i
= 0; i
< device
->context_count
; ++i
)
9253 context
= device
->contexts
[i
];
9254 ctx_data
= context
->shader_backend_data
;
9256 if (ctx_data
->glsl_program
== program
)
9257 shader_glsl_invalidate_current_program(context
);
9261 static void shader_glsl_destroy(struct wined3d_shader
*shader
)
9263 struct glsl_shader_private
*shader_data
= shader
->backend_data
;
9264 struct wined3d_device
*device
= shader
->device
;
9265 struct shader_glsl_priv
*priv
= device
->shader_priv
;
9266 const struct wined3d_gl_info
*gl_info
;
9267 const struct list
*linked_programs
;
9268 struct wined3d_context
*context
;
9270 if (!shader_data
|| !shader_data
->num_gl_shaders
)
9272 HeapFree(GetProcessHeap(), 0, shader_data
);
9273 shader
->backend_data
= NULL
;
9277 context
= context_acquire(device
, NULL
, 0);
9278 gl_info
= context
->gl_info
;
9280 TRACE("Deleting linked programs.\n");
9281 linked_programs
= &shader
->linked_programs
;
9282 if (linked_programs
->next
)
9284 struct glsl_shader_prog_link
*entry
, *entry2
;
9287 switch (shader
->reg_maps
.shader_version
.type
)
9289 case WINED3D_SHADER_TYPE_PIXEL
:
9291 struct glsl_ps_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.ps
;
9293 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
9295 TRACE("Deleting pixel shader %u.\n", gl_shaders
[i
].id
);
9296 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
9297 checkGLcall("glDeleteShader");
9299 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
);
9301 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
9302 struct glsl_shader_prog_link
, ps
.shader_entry
)
9304 shader_glsl_invalidate_contexts_program(device
, entry
);
9305 delete_glsl_program_entry(priv
, gl_info
, entry
);
9311 case WINED3D_SHADER_TYPE_VERTEX
:
9313 struct glsl_vs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.vs
;
9315 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
9317 TRACE("Deleting vertex shader %u.\n", gl_shaders
[i
].id
);
9318 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
9319 checkGLcall("glDeleteShader");
9321 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
);
9323 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
9324 struct glsl_shader_prog_link
, vs
.shader_entry
)
9326 shader_glsl_invalidate_contexts_program(device
, entry
);
9327 delete_glsl_program_entry(priv
, gl_info
, entry
);
9333 case WINED3D_SHADER_TYPE_GEOMETRY
:
9335 struct glsl_gs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.gs
;
9337 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
9339 TRACE("Deleting geometry shader %u.\n", gl_shaders
[i
].id
);
9340 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
9341 checkGLcall("glDeleteShader");
9343 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
);
9345 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
9346 struct glsl_shader_prog_link
, gs
.shader_entry
)
9348 shader_glsl_invalidate_contexts_program(device
, entry
);
9349 delete_glsl_program_entry(priv
, gl_info
, entry
);
9355 case WINED3D_SHADER_TYPE_COMPUTE
:
9357 struct glsl_cs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.cs
;
9359 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
9361 TRACE("Deleting compute shader %u.\n", gl_shaders
[i
].id
);
9362 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
9363 checkGLcall("glDeleteShader");
9365 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.cs
);
9367 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
9368 struct glsl_shader_prog_link
, cs
.shader_entry
)
9370 shader_glsl_invalidate_contexts_program(device
, entry
);
9371 delete_glsl_program_entry(priv
, gl_info
, entry
);
9378 ERR("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
9383 HeapFree(GetProcessHeap(), 0, shader
->backend_data
);
9384 shader
->backend_data
= NULL
;
9386 context_release(context
);
9389 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
9391 const struct glsl_program_key
*k
= key
;
9392 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
9393 const struct glsl_shader_prog_link
, program_lookup_entry
);
9395 if (k
->vs_id
> prog
->vs
.id
) return 1;
9396 else if (k
->vs_id
< prog
->vs
.id
) return -1;
9398 if (k
->gs_id
> prog
->gs
.id
) return 1;
9399 else if (k
->gs_id
< prog
->gs
.id
) return -1;
9401 if (k
->ps_id
> prog
->ps
.id
) return 1;
9402 else if (k
->ps_id
< prog
->ps
.id
) return -1;
9404 if (k
->cs_id
> prog
->cs
.id
) return 1;
9405 else if (k
->cs_id
< prog
->cs
.id
) return -1;
9410 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
9412 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
)
9413 + constant_count
* sizeof(*heap
->contained
)
9414 + constant_count
* sizeof(*heap
->positions
);
9415 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
9419 ERR("Failed to allocate memory\n");
9423 heap
->entries
= mem
;
9424 heap
->entries
[1].version
= 0;
9425 heap
->contained
= (BOOL
*)(heap
->entries
+ constant_count
+ 1);
9426 memset(heap
->contained
, 0, constant_count
* sizeof(*heap
->contained
));
9427 heap
->positions
= (unsigned int *)(heap
->contained
+ constant_count
);
9433 static void constant_heap_free(struct constant_heap
*heap
)
9435 HeapFree(GetProcessHeap(), 0, heap
->entries
);
9438 static HRESULT
shader_glsl_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
9439 const struct fragment_pipeline
*fragment_pipe
)
9441 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
9442 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
9443 SIZE_T stack_size
= wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F
, WINED3D_MAX_PS_CONSTS_F
)) + 1;
9444 struct fragment_caps fragment_caps
;
9445 void *vertex_priv
, *fragment_priv
;
9447 string_buffer_list_init(&priv
->string_buffers
);
9449 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&glsl_shader_backend
, priv
)))
9451 ERR("Failed to initialize vertex pipe.\n");
9452 HeapFree(GetProcessHeap(), 0, priv
);
9456 if (!(fragment_priv
= fragment_pipe
->alloc_private(&glsl_shader_backend
, priv
)))
9458 ERR("Failed to initialize fragment pipe.\n");
9459 vertex_pipe
->vp_free(device
);
9460 HeapFree(GetProcessHeap(), 0, priv
);
9464 if (!string_buffer_init(&priv
->shader_buffer
))
9466 ERR("Failed to initialize shader buffer.\n");
9470 if (!(priv
->stack
= wined3d_calloc(stack_size
, sizeof(*priv
->stack
))))
9472 ERR("Failed to allocate memory.\n");
9476 if (!constant_heap_init(&priv
->vconst_heap
, WINED3D_MAX_VS_CONSTS_F
))
9478 ERR("Failed to initialize vertex shader constant heap\n");
9482 if (!constant_heap_init(&priv
->pconst_heap
, WINED3D_MAX_PS_CONSTS_F
))
9484 ERR("Failed to initialize pixel shader constant heap\n");
9488 wine_rb_init(&priv
->program_lookup
, glsl_program_key_compare
);
9490 priv
->next_constant_version
= 1;
9491 priv
->vertex_pipe
= vertex_pipe
;
9492 priv
->fragment_pipe
= fragment_pipe
;
9493 fragment_pipe
->get_caps(gl_info
, &fragment_caps
);
9494 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
9495 priv
->legacy_lighting
= device
->wined3d
->flags
& WINED3D_LEGACY_FFP_LIGHTING
;
9497 device
->vertex_priv
= vertex_priv
;
9498 device
->fragment_priv
= fragment_priv
;
9499 device
->shader_priv
= priv
;
9504 constant_heap_free(&priv
->pconst_heap
);
9505 constant_heap_free(&priv
->vconst_heap
);
9506 HeapFree(GetProcessHeap(), 0, priv
->stack
);
9507 string_buffer_free(&priv
->shader_buffer
);
9508 fragment_pipe
->free_private(device
);
9509 vertex_pipe
->vp_free(device
);
9510 HeapFree(GetProcessHeap(), 0, priv
);
9511 return E_OUTOFMEMORY
;
9514 /* Context activation is done by the caller. */
9515 static void shader_glsl_free(struct wined3d_device
*device
)
9517 struct shader_glsl_priv
*priv
= device
->shader_priv
;
9519 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
9520 constant_heap_free(&priv
->pconst_heap
);
9521 constant_heap_free(&priv
->vconst_heap
);
9522 HeapFree(GetProcessHeap(), 0, priv
->stack
);
9523 string_buffer_list_cleanup(&priv
->string_buffers
);
9524 string_buffer_free(&priv
->shader_buffer
);
9525 priv
->fragment_pipe
->free_private(device
);
9526 priv
->vertex_pipe
->vp_free(device
);
9528 HeapFree(GetProcessHeap(), 0, device
->shader_priv
);
9529 device
->shader_priv
= NULL
;
9532 static BOOL
shader_glsl_allocate_context_data(struct wined3d_context
*context
)
9534 struct glsl_context_data
*ctx_data
;
9535 if (!(ctx_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ctx_data
))))
9537 ctx_data
->vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
9538 context
->shader_backend_data
= ctx_data
;
9542 static void shader_glsl_free_context_data(struct wined3d_context
*context
)
9544 HeapFree(GetProcessHeap(), 0, context
->shader_backend_data
);
9547 static void shader_glsl_init_context_state(struct wined3d_context
*context
)
9549 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9551 gl_info
->gl_ops
.gl
.p_glEnable(GL_PROGRAM_POINT_SIZE
);
9552 checkGLcall("GL_PROGRAM_POINT_SIZE");
9555 static void shader_glsl_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
9559 /* FIXME: Check for the specific extensions required for SM5 support
9560 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
9561 * soon as we introduce them, adjusting the GL / GLSL version checks
9563 if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(4, 30) && gl_info
->supported
[WINED3D_GL_VERSION_4_3
]
9564 && gl_info
->supported
[ARB_COMPUTE_SHADER
]
9565 && gl_info
->supported
[ARB_DERIVATIVE_CONTROL
]
9566 && gl_info
->supported
[ARB_GPU_SHADER5
]
9567 && gl_info
->supported
[ARB_SHADER_ATOMIC_COUNTERS
]
9568 && gl_info
->supported
[ARB_SHADER_IMAGE_LOAD_STORE
]
9569 && gl_info
->supported
[ARB_SHADER_IMAGE_SIZE
]
9570 && gl_info
->supported
[ARB_SHADING_LANGUAGE_PACKING
])
9572 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 50) && gl_info
->supported
[WINED3D_GL_VERSION_3_2
]
9573 && gl_info
->supported
[ARB_SHADER_BIT_ENCODING
] && gl_info
->supported
[ARB_SAMPLER_OBJECTS
]
9574 && gl_info
->supported
[ARB_TEXTURE_SWIZZLE
])
9576 /* Support for texldd and texldl instructions in pixel shaders is required
9578 else if (shader_glsl_has_core_grad(gl_info
, NULL
) || gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
9582 TRACE("Shader model %u.\n", shader_model
);
9584 caps
->vs_version
= min(wined3d_settings
.max_sm_vs
, shader_model
);
9585 caps
->hs_version
= min(wined3d_settings
.max_sm_hs
, shader_model
);
9586 caps
->ds_version
= min(wined3d_settings
.max_sm_ds
, shader_model
);
9587 caps
->gs_version
= min(wined3d_settings
.max_sm_gs
, shader_model
);
9588 caps
->ps_version
= min(wined3d_settings
.max_sm_ps
, shader_model
);
9589 caps
->cs_version
= min(wined3d_settings
.max_sm_cs
, shader_model
);
9591 caps
->vs_version
= gl_info
->supported
[ARB_VERTEX_SHADER
] ? caps
->vs_version
: 0;
9592 caps
->ps_version
= gl_info
->supported
[ARB_FRAGMENT_SHADER
] ? caps
->ps_version
: 0;
9594 caps
->vs_uniform_count
= min(WINED3D_MAX_VS_CONSTS_F
, gl_info
->limits
.glsl_vs_float_constants
);
9595 caps
->ps_uniform_count
= min(WINED3D_MAX_PS_CONSTS_F
, gl_info
->limits
.glsl_ps_float_constants
);
9596 caps
->varying_count
= gl_info
->limits
.glsl_varyings
;
9598 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
9599 * Direct3D minimum requirement.
9601 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
9602 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
9604 * The problem is that the refrast clamps temporary results in the shader to
9605 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
9606 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
9607 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
9608 * offer a way to query this.
9610 if (shader_model
>= 4)
9611 caps
->ps_1x_max_value
= FLT_MAX
;
9613 caps
->ps_1x_max_value
= 1024.0f
;
9615 /* Ideally we'd only set caps like sRGB writes here if supported by both
9616 * the shader backend and the fragment pipe, but we can get called before
9617 * shader_glsl_alloc(). */
9618 caps
->wined3d_caps
= WINED3D_SHADER_CAP_VS_CLIPPING
9619 | WINED3D_SHADER_CAP_SRGB_WRITE
;
9622 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
9624 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
9626 TRACE("Checking support for fixup:\n");
9627 dump_color_fixup_desc(fixup
);
9630 /* We support everything except YUV conversions. */
9631 if (!is_complex_fixup(fixup
))
9637 TRACE("[FAILED]\n");
9641 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
9643 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
9644 /* WINED3DSIH_ADD */ shader_glsl_binop
,
9645 /* WINED3DSIH_AND */ shader_glsl_binop
,
9646 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic
,
9647 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic
,
9648 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic
,
9649 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic
,
9650 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic
,
9651 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic
,
9652 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic
,
9653 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic
,
9654 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic
,
9655 /* WINED3DSIH_BEM */ shader_glsl_bem
,
9656 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op
,
9657 /* WINED3DSIH_BFREV */ shader_glsl_map2gl
,
9658 /* WINED3DSIH_BREAK */ shader_glsl_break
,
9659 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
9660 /* WINED3DSIH_BREAKP */ shader_glsl_breakp
,
9661 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo
,
9662 /* WINED3DSIH_CALL */ shader_glsl_call
,
9663 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
9664 /* WINED3DSIH_CASE */ shader_glsl_case
,
9665 /* WINED3DSIH_CMP */ shader_glsl_conditional_move
,
9666 /* WINED3DSIH_CND */ shader_glsl_cnd
,
9667 /* WINED3DSIH_CONTINUE */ shader_glsl_continue
,
9668 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl
,
9669 /* WINED3DSIH_CRS */ shader_glsl_cross
,
9670 /* WINED3DSIH_CUT */ shader_glsl_cut
,
9671 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut
,
9672 /* WINED3DSIH_DCL */ shader_glsl_nop
,
9673 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop
,
9674 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL
,
9675 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL
,
9676 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop
,
9677 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL
,
9678 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ NULL
,
9679 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL
,
9680 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop
,
9681 /* WINED3DSIH_DCL_INDEX_RANGE */ NULL
,
9682 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop
,
9683 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop
,
9684 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL
,
9685 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop
,
9686 /* WINED3DSIH_DCL_INPUT_PS */ NULL
,
9687 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL
,
9688 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL
,
9689 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop
,
9690 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop
,
9691 /* WINED3DSIH_DCL_INTERFACE */ NULL
,
9692 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop
,
9693 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL
,
9694 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop
,
9695 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop
,
9696 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop
,
9697 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop
,
9698 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop
,
9699 /* WINED3DSIH_DCL_STREAM */ NULL
,
9700 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop
,
9701 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL
,
9702 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL
,
9703 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL
,
9704 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop
,
9705 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop
,
9706 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop
,
9707 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop
,
9708 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop
,
9709 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop
,
9710 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop
,
9711 /* WINED3DSIH_DEF */ shader_glsl_nop
,
9712 /* WINED3DSIH_DEFAULT */ shader_glsl_default
,
9713 /* WINED3DSIH_DEFB */ shader_glsl_nop
,
9714 /* WINED3DSIH_DEFI */ shader_glsl_nop
,
9715 /* WINED3DSIH_DIV */ shader_glsl_binop
,
9716 /* WINED3DSIH_DP2 */ shader_glsl_dot
,
9717 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add
,
9718 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
9719 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
9720 /* WINED3DSIH_DST */ shader_glsl_dst
,
9721 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
9722 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl
,
9723 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl
,
9724 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
9725 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl
,
9726 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl
,
9727 /* WINED3DSIH_ELSE */ shader_glsl_else
,
9728 /* WINED3DSIH_EMIT */ shader_glsl_emit
,
9729 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit
,
9730 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
9731 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
9732 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
9733 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end
,
9734 /* WINED3DSIH_EQ */ shader_glsl_relop
,
9735 /* WINED3DSIH_EXP */ shader_glsl_scalar_op
,
9736 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
9737 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16
,
9738 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16
,
9739 /* WINED3DSIH_FCALL */ NULL
,
9740 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl
,
9741 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl
,
9742 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl
,
9743 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
9744 /* WINED3DSIH_FTOI */ shader_glsl_to_int
,
9745 /* WINED3DSIH_FTOU */ shader_glsl_to_uint
,
9746 /* WINED3DSIH_GATHER4 */ NULL
,
9747 /* WINED3DSIH_GATHER4_C */ NULL
,
9748 /* WINED3DSIH_GE */ shader_glsl_relop
,
9749 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL
,
9750 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop
,
9751 /* WINED3DSIH_HS_FORK_PHASE */ NULL
,
9752 /* WINED3DSIH_HS_JOIN_PHASE */ NULL
,
9753 /* WINED3DSIH_IADD */ shader_glsl_binop
,
9754 /* WINED3DSIH_IEQ */ shader_glsl_relop
,
9755 /* WINED3DSIH_IF */ shader_glsl_if
,
9756 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
9757 /* WINED3DSIH_IGE */ shader_glsl_relop
,
9758 /* WINED3DSIH_ILT */ shader_glsl_relop
,
9759 /* WINED3DSIH_IMAD */ shader_glsl_mad
,
9760 /* WINED3DSIH_IMAX */ shader_glsl_map2gl
,
9761 /* WINED3DSIH_IMIN */ shader_glsl_map2gl
,
9762 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter
,
9763 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic
,
9764 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic
,
9765 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter
,
9766 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic
,
9767 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic
,
9768 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic
,
9769 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic
,
9770 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic
,
9771 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic
,
9772 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic
,
9773 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic
,
9774 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended
,
9775 /* WINED3DSIH_INE */ shader_glsl_relop
,
9776 /* WINED3DSIH_INEG */ shader_glsl_unary_op
,
9777 /* WINED3DSIH_ISHL */ shader_glsl_binop
,
9778 /* WINED3DSIH_ISHR */ shader_glsl_binop
,
9779 /* WINED3DSIH_ITOF */ shader_glsl_to_float
,
9780 /* WINED3DSIH_LABEL */ shader_glsl_label
,
9781 /* WINED3DSIH_LD */ shader_glsl_ld
,
9782 /* WINED3DSIH_LD2DMS */ NULL
,
9783 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured
,
9784 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured
,
9785 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav
,
9786 /* WINED3DSIH_LIT */ shader_glsl_lit
,
9787 /* WINED3DSIH_LOD */ NULL
,
9788 /* WINED3DSIH_LOG */ shader_glsl_scalar_op
,
9789 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op
,
9790 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
9791 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
9792 /* WINED3DSIH_LT */ shader_glsl_relop
,
9793 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
9794 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
9795 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
9796 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
9797 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
9798 /* WINED3DSIH_MAD */ shader_glsl_mad
,
9799 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
9800 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
9801 /* WINED3DSIH_MOV */ shader_glsl_mov
,
9802 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
9803 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move
,
9804 /* WINED3DSIH_MUL */ shader_glsl_binop
,
9805 /* WINED3DSIH_NE */ shader_glsl_relop
,
9806 /* WINED3DSIH_NOP */ shader_glsl_nop
,
9807 /* WINED3DSIH_NOT */ shader_glsl_unary_op
,
9808 /* WINED3DSIH_NRM */ shader_glsl_nrm
,
9809 /* WINED3DSIH_OR */ shader_glsl_binop
,
9810 /* WINED3DSIH_PHASE */ shader_glsl_nop
,
9811 /* WINED3DSIH_POW */ shader_glsl_pow
,
9812 /* WINED3DSIH_RCP */ shader_glsl_scalar_op
,
9813 /* WINED3DSIH_REP */ shader_glsl_rep
,
9814 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo
,
9815 /* WINED3DSIH_RET */ shader_glsl_ret
,
9816 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl
,
9817 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl
,
9818 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl
,
9819 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl
,
9820 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op
,
9821 /* WINED3DSIH_SAMPLE */ shader_glsl_sample
,
9822 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample
,
9823 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c
,
9824 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c
,
9825 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample
,
9826 /* WINED3DSIH_SAMPLE_INFO */ NULL
,
9827 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample
,
9828 /* WINED3DSIH_SAMPLE_POS */ NULL
,
9829 /* WINED3DSIH_SETP */ NULL
,
9830 /* WINED3DSIH_SGE */ shader_glsl_compare
,
9831 /* WINED3DSIH_SGN */ shader_glsl_sgn
,
9832 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
9833 /* WINED3DSIH_SLT */ shader_glsl_compare
,
9834 /* WINED3DSIH_SQRT */ shader_glsl_map2gl
,
9835 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured
,
9836 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured
,
9837 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav
,
9838 /* WINED3DSIH_SUB */ shader_glsl_binop
,
9839 /* WINED3DSIH_SWAPC */ NULL
,
9840 /* WINED3DSIH_SWITCH */ shader_glsl_switch
,
9841 /* WINED3DSIH_SYNC */ shader_glsl_sync
,
9842 /* WINED3DSIH_TEX */ shader_glsl_tex
,
9843 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem
,
9844 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem
,
9845 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord
,
9846 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth
,
9847 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3
,
9848 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex
,
9849 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill
,
9850 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
9851 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
9852 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth
,
9853 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad
,
9854 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex
,
9855 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3
,
9856 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
9857 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad
,
9858 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec
,
9859 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex
,
9860 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec
,
9861 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar
,
9862 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb
,
9863 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb
,
9864 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op
,
9865 /* WINED3DSIH_UDIV */ shader_glsl_udiv
,
9866 /* WINED3DSIH_UGE */ shader_glsl_relop
,
9867 /* WINED3DSIH_ULT */ shader_glsl_relop
,
9868 /* WINED3DSIH_UMAX */ shader_glsl_map2gl
,
9869 /* WINED3DSIH_UMIN */ shader_glsl_map2gl
,
9870 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended
,
9871 /* WINED3DSIH_USHR */ shader_glsl_binop
,
9872 /* WINED3DSIH_UTOF */ shader_glsl_to_float
,
9873 /* WINED3DSIH_XOR */ shader_glsl_binop
,
9876 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
9877 SHADER_HANDLER hw_fct
;
9879 /* Select handler */
9880 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
9882 /* Unhandled opcode */
9885 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
9890 shader_glsl_add_instruction_modifiers(ins
);
9893 static BOOL
shader_glsl_has_ffp_proj_control(void *shader_priv
)
9895 struct shader_glsl_priv
*priv
= shader_priv
;
9897 return priv
->ffp_proj_control
;
9900 const struct wined3d_shader_backend_ops glsl_shader_backend
=
9902 shader_glsl_handle_instruction
,
9904 shader_glsl_select_compute
,
9905 shader_glsl_disable
,
9906 shader_glsl_update_float_vertex_constants
,
9907 shader_glsl_update_float_pixel_constants
,
9908 shader_glsl_load_constants
,
9909 shader_glsl_destroy
,
9912 shader_glsl_allocate_context_data
,
9913 shader_glsl_free_context_data
,
9914 shader_glsl_init_context_state
,
9915 shader_glsl_get_caps
,
9916 shader_glsl_color_fixup_supported
,
9917 shader_glsl_has_ffp_proj_control
,
9920 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
) {}
9922 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info
*gl_info
, struct wined3d_vertex_caps
*caps
)
9924 caps
->xyzrhw
= TRUE
;
9925 caps
->emulated_flatshading
= !gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
9926 caps
->ffp_generic_attributes
= TRUE
;
9927 caps
->max_active_lights
= MAX_ACTIVE_LIGHTS
;
9928 caps
->max_vertex_blend_matrices
= MAX_VERTEX_BLENDS
;
9929 caps
->max_vertex_blend_matrix_index
= 0;
9930 caps
->vertex_processing_caps
= WINED3DVTXPCAPS_TEXGEN
9931 | WINED3DVTXPCAPS_MATERIALSOURCE7
9932 | WINED3DVTXPCAPS_VERTEXFOG
9933 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9934 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9935 | WINED3DVTXPCAPS_LOCALVIEWER
9936 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP
;
9937 caps
->fvf_caps
= WINED3DFVFCAPS_PSIZE
| 8; /* 8 texture coordinates. */
9938 caps
->max_user_clip_planes
= gl_info
->limits
.user_clip_distances
;
9939 caps
->raster_caps
= WINED3DPRASTERCAPS_FOGRANGE
;
9942 static DWORD
glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
9944 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
9945 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
9949 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
9951 struct shader_glsl_priv
*priv
;
9953 if (shader_backend
== &glsl_shader_backend
)
9956 wine_rb_init(&priv
->ffp_vertex_shaders
, wined3d_ffp_vertex_program_key_compare
);
9960 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9965 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry
*entry
, void *context
)
9967 struct glsl_ffp_vertex_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
9968 struct glsl_ffp_vertex_shader
, desc
.entry
);
9969 struct glsl_shader_prog_link
*program
, *program2
;
9970 struct glsl_ffp_destroy_ctx
*ctx
= context
;
9972 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
9973 struct glsl_shader_prog_link
, vs
.shader_entry
)
9975 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
9977 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
9978 HeapFree(GetProcessHeap(), 0, shader
);
9981 /* Context activation is done by the caller. */
9982 static void glsl_vertex_pipe_vp_free(struct wined3d_device
*device
)
9984 struct shader_glsl_priv
*priv
= device
->vertex_priv
;
9985 struct glsl_ffp_destroy_ctx ctx
;
9988 ctx
.gl_info
= &device
->adapter
->gl_info
;
9989 wine_rb_destroy(&priv
->ffp_vertex_shaders
, shader_glsl_free_ffp_vertex_shader
, &ctx
);
9992 static void glsl_vertex_pipe_nop(struct wined3d_context
*context
,
9993 const struct wined3d_state
*state
, DWORD state_id
) {}
9995 static void glsl_vertex_pipe_shader(struct wined3d_context
*context
,
9996 const struct wined3d_state
*state
, DWORD state_id
)
9998 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
10001 static void glsl_vertex_pipe_vdecl(struct wined3d_context
*context
,
10002 const struct wined3d_state
*state
, DWORD state_id
)
10004 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
10005 BOOL normal
= !!(context
->stream_info
.use_map
& (1u << WINED3D_FFP_NORMAL
));
10006 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
10007 BOOL transformed
= context
->stream_info
.position_transformed
;
10008 BOOL wasrhw
= context
->last_was_rhw
;
10011 context
->last_was_rhw
= transformed
;
10013 /* If the vertex declaration contains a transformed position attribute,
10014 * the draw uses the fixed function vertex pipeline regardless of any
10015 * vertex shader set by the application. */
10016 if (transformed
!= wasrhw
10017 || context
->stream_info
.swizzle_map
!= context
->last_swizzle_map
)
10018 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
10020 context
->last_swizzle_map
= context
->stream_info
.swizzle_map
;
10022 if (!use_vs(state
))
10024 if (context
->last_was_vshader
)
10026 if (legacy_context
)
10027 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
10028 clipplane(context
, state
, STATE_CLIPPLANE(i
));
10030 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
10033 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
10035 /* Because of settings->texcoords, we have to regenerate the vertex
10036 * shader on a vdecl change if there aren't enough varyings to just
10037 * always output all the texture coordinates. */
10038 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
10039 || normal
!= context
->last_was_normal
)
10040 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
10043 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.major
== 1
10044 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.minor
<= 3)
10045 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
10049 if (!context
->last_was_vshader
)
10051 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
10052 if (legacy_context
)
10053 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
10054 clipplane(context
, state
, STATE_CLIPPLANE(i
));
10056 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
10060 context
->last_was_vshader
= use_vs(state
);
10061 context
->last_was_normal
= normal
;
10064 static void glsl_vertex_pipe_vs(struct wined3d_context
*context
,
10065 const struct wined3d_state
*state
, DWORD state_id
)
10067 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
10068 /* Different vertex shaders potentially require a different vertex attributes setup. */
10069 if (!isStateDirty(context
, STATE_VDECL
))
10070 context_apply_state(context
, state
, STATE_VDECL
);
10073 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context
*context
,
10074 const struct wined3d_state
*state
, DWORD state_id
)
10076 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]
10077 && state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.shader_version
.major
>= 4)
10078 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
10081 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context
*context
,
10082 const struct wined3d_state
*state
, DWORD state_id
)
10084 if (state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
])
10085 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_GEOMETRY
;
10086 else if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]
10087 && state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.shader_version
.major
>= 4)
10088 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
10091 static void glsl_vertex_pipe_world(struct wined3d_context
*context
,
10092 const struct wined3d_state
*state
, DWORD state_id
)
10094 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
;
10097 static void glsl_vertex_pipe_vertexblend(struct wined3d_context
*context
,
10098 const struct wined3d_state
*state
, DWORD state_id
)
10100 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
10103 static void glsl_vertex_pipe_view(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
10105 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
10108 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
10109 | WINED3D_SHADER_CONST_FFP_LIGHTS
10110 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
10112 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
10114 for (k
= 0; k
< gl_info
->limits
.user_clip_distances
; ++k
)
10116 if (!isStateDirty(context
, STATE_CLIPPLANE(k
)))
10117 clipplane(context
, state
, STATE_CLIPPLANE(k
));
10122 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
10126 static void glsl_vertex_pipe_projection(struct wined3d_context
*context
,
10127 const struct wined3d_state
*state
, DWORD state_id
)
10129 /* Table fog behavior depends on the projection matrix. */
10130 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
10131 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] != WINED3D_FOG_NONE
)
10132 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
10133 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PROJ
;
10136 static void glsl_vertex_pipe_viewport(struct wined3d_context
*context
,
10137 const struct wined3d_state
*state
, DWORD state_id
)
10139 if (!isStateDirty(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
)))
10140 glsl_vertex_pipe_projection(context
, state
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
10141 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
))
10142 && state
->render_states
[WINED3D_RS_POINTSCALEENABLE
])
10143 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
10144 context
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
10147 static void glsl_vertex_pipe_texmatrix(struct wined3d_context
*context
,
10148 const struct wined3d_state
*state
, DWORD state_id
)
10150 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
10153 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context
*context
,
10154 const struct wined3d_state
*state
, DWORD state_id
)
10156 DWORD sampler
= state_id
- STATE_SAMPLER(0);
10157 const struct wined3d_texture
*texture
= state
->textures
[sampler
];
10163 if (sampler
>= MAX_TEXTURES
)
10166 if ((np2
= !(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
10167 || context
->lastWasPow2Texture
& (1u << sampler
))
10170 context
->lastWasPow2Texture
|= 1u << sampler
;
10172 context
->lastWasPow2Texture
&= ~(1u << sampler
);
10174 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
10178 static void glsl_vertex_pipe_material(struct wined3d_context
*context
,
10179 const struct wined3d_state
*state
, DWORD state_id
)
10181 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
10184 static void glsl_vertex_pipe_light(struct wined3d_context
*context
,
10185 const struct wined3d_state
*state
, DWORD state_id
)
10187 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
10190 static void glsl_vertex_pipe_pointsize(struct wined3d_context
*context
,
10191 const struct wined3d_state
*state
, DWORD state_id
)
10193 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
10196 static void glsl_vertex_pipe_pointscale(struct wined3d_context
*context
,
10197 const struct wined3d_state
*state
, DWORD state_id
)
10199 if (!use_vs(state
))
10200 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
10203 static void glsl_vertex_pointsprite_core(struct wined3d_context
*context
,
10204 const struct wined3d_state
*state
, DWORD state_id
)
10206 static unsigned int once
;
10208 if (state
->gl_primitive_type
== GL_POINTS
&& !state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
] && !once
++)
10209 FIXME("Non-point sprite points not supported in core profile.\n");
10212 static void glsl_vertex_pipe_shademode(struct wined3d_context
*context
,
10213 const struct wined3d_state
*state
, DWORD state_id
)
10215 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
10218 static void glsl_vertex_pipe_clip_plane(struct wined3d_context
*context
,
10219 const struct wined3d_state
*state
, DWORD state_id
)
10221 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
10222 UINT index
= state_id
- STATE_CLIPPLANE(0);
10224 if (index
>= gl_info
->limits
.user_clip_distances
)
10227 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
10230 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states
[] =
10232 {STATE_VDECL
, {STATE_VDECL
, glsl_vertex_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
10233 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_vertex_pipe_vs
}, WINED3D_GL_EXT_NONE
},
10234 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY
), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY
), glsl_vertex_pipe_geometry_shader
}, WINED3D_GL_EXT_NONE
},
10235 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_vertex_pipe_pixel_shader
}, WINED3D_GL_EXT_NONE
},
10236 {STATE_MATERIAL
, {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10237 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_vertex_pipe_material
}, WINED3D_GL_EXT_NONE
},
10239 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10240 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10241 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10242 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10243 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10244 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10245 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10246 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10247 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10248 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10249 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10250 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10251 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10252 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10253 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10254 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10255 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10256 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10257 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10258 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10259 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10260 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10261 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10262 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10263 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10264 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10265 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10266 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10267 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10268 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10269 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10270 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10271 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10272 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10273 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10274 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10275 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10276 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10277 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10278 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10279 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10280 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10281 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10282 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10283 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10284 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10285 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10286 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10287 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10288 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10289 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10290 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10291 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10292 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10293 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10294 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10295 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10296 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10297 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10298 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10299 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10300 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10301 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
10302 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
10304 {STATE_LIGHT_TYPE
, {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10305 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
10306 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
10307 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
10308 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
10309 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
10310 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
10311 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
10312 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
10314 {STATE_VIEWPORT
, {STATE_VIEWPORT
, glsl_vertex_pipe_viewport
}, WINED3D_GL_EXT_NONE
},
10315 /* Transform states */
10316 {STATE_TRANSFORM(WINED3D_TS_VIEW
), {STATE_TRANSFORM(WINED3D_TS_VIEW
), glsl_vertex_pipe_view
}, WINED3D_GL_EXT_NONE
},
10317 {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), glsl_vertex_pipe_projection
}, WINED3D_GL_EXT_NONE
},
10318 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
10319 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
10320 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
10321 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
10322 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
10323 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
10324 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
10325 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
10326 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world
}, WINED3D_GL_EXT_NONE
},
10327 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
10328 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
10329 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
10330 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
10331 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
10332 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
10333 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
10334 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
10335 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
10336 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
10337 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
10338 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10339 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10340 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10341 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10342 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10343 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10344 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10345 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10347 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
10348 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10349 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10350 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10351 {STATE_RENDER(WINED3D_RS_CLIPPING
), {STATE_RENDER(WINED3D_RS_CLIPPING
), state_clipping
}, WINED3D_GL_EXT_NONE
},
10352 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE
), {STATE_RENDER(WINED3D_RS_CLIPPING
), NULL
}, WINED3D_GL_EXT_NONE
},
10353 {STATE_RENDER(WINED3D_RS_LIGHTING
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10354 {STATE_RENDER(WINED3D_RS_AMBIENT
), {STATE_RENDER(WINED3D_RS_AMBIENT
), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
10355 {STATE_RENDER(WINED3D_RS_COLORVERTEX
), {STATE_RENDER(WINED3D_RS_COLORVERTEX
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
10356 {STATE_RENDER(WINED3D_RS_LOCALVIEWER
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10357 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10358 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10359 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10360 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10361 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10362 {STATE_RENDER(WINED3D_RS_VERTEXBLEND
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10363 {STATE_RENDER(WINED3D_RS_POINTSIZE
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
10364 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), glsl_vertex_pipe_pointsize
}, WINED3D_GL_EXT_NONE
},
10365 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite
}, ARB_POINT_SPRITE
},
10366 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite_w
}, WINED3D_GL_LEGACY_CONTEXT
},
10367 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_vertex_pointsprite_core
}, WINED3D_GL_EXT_NONE
},
10368 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), glsl_vertex_pipe_pointscale
}, WINED3D_GL_EXT_NONE
},
10369 {STATE_RENDER(WINED3D_RS_POINTSCALE_A
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10370 {STATE_RENDER(WINED3D_RS_POINTSCALE_B
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10371 {STATE_RENDER(WINED3D_RS_POINTSCALE_C
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10372 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
10373 {STATE_RENDER(WINED3D_RS_TWEENFACTOR
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10374 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
10375 /* NP2 texture matrix fixups. They are not needed if
10376 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
10377 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
10379 {STATE_SAMPLER(0), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
10380 {STATE_SAMPLER(0), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
10381 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
10382 {STATE_SAMPLER(1), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
10383 {STATE_SAMPLER(1), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
10384 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
10385 {STATE_SAMPLER(2), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
10386 {STATE_SAMPLER(2), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
10387 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
10388 {STATE_SAMPLER(3), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
10389 {STATE_SAMPLER(3), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
10390 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
10391 {STATE_SAMPLER(4), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
10392 {STATE_SAMPLER(4), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
10393 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
10394 {STATE_SAMPLER(5), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
10395 {STATE_SAMPLER(5), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
10396 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
10397 {STATE_SAMPLER(6), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
10398 {STATE_SAMPLER(6), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
10399 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
10400 {STATE_SAMPLER(7), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
10401 {STATE_SAMPLER(7), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
10402 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
10403 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
10404 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_nop
}, WINED3D_GL_LEGACY_CONTEXT
},
10405 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_shademode
}, WINED3D_GL_EXT_NONE
},
10406 {0 /* Terminate */, {0, NULL
}, WINED3D_GL_EXT_NONE
},
10410 * - Implement vertex tweening. */
10411 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe
=
10413 glsl_vertex_pipe_vp_enable
,
10414 glsl_vertex_pipe_vp_get_caps
,
10415 glsl_vertex_pipe_vp_get_emul_mask
,
10416 glsl_vertex_pipe_vp_alloc
,
10417 glsl_vertex_pipe_vp_free
,
10418 glsl_vertex_pipe_vp_states
,
10421 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
)
10423 /* Nothing to do. */
10426 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info
*gl_info
, struct fragment_caps
*caps
)
10428 caps
->wined3d_caps
= WINED3D_FRAGMENT_CAP_PROJ_CONTROL
10429 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
10430 | WINED3D_FRAGMENT_CAP_COLOR_KEY
;
10431 caps
->PrimitiveMiscCaps
= WINED3DPMISCCAPS_TSSARGTEMP
10432 | WINED3DPMISCCAPS_PERSTAGECONSTANT
;
10433 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
10434 | WINED3DTEXOPCAPS_SELECTARG1
10435 | WINED3DTEXOPCAPS_SELECTARG2
10436 | WINED3DTEXOPCAPS_MODULATE4X
10437 | WINED3DTEXOPCAPS_MODULATE2X
10438 | WINED3DTEXOPCAPS_MODULATE
10439 | WINED3DTEXOPCAPS_ADDSIGNED2X
10440 | WINED3DTEXOPCAPS_ADDSIGNED
10441 | WINED3DTEXOPCAPS_ADD
10442 | WINED3DTEXOPCAPS_SUBTRACT
10443 | WINED3DTEXOPCAPS_ADDSMOOTH
10444 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
10445 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
10446 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
10447 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
10448 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
10449 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
10450 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
10451 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
10452 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
10453 | WINED3DTEXOPCAPS_DOTPRODUCT3
10454 | WINED3DTEXOPCAPS_MULTIPLYADD
10455 | WINED3DTEXOPCAPS_LERP
10456 | WINED3DTEXOPCAPS_BUMPENVMAP
10457 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
10458 caps
->MaxTextureBlendStages
= MAX_TEXTURES
;
10459 caps
->MaxSimultaneousTextures
= min(gl_info
->limits
.fragment_samplers
, MAX_TEXTURES
);
10462 static DWORD
glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
10464 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
10465 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
10469 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
10471 struct shader_glsl_priv
*priv
;
10473 if (shader_backend
== &glsl_shader_backend
)
10475 priv
= shader_priv
;
10476 wine_rb_init(&priv
->ffp_fragment_shaders
, wined3d_ffp_frag_program_key_compare
);
10480 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
10485 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry
*entry
, void *context
)
10487 struct glsl_ffp_fragment_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
10488 struct glsl_ffp_fragment_shader
, entry
.entry
);
10489 struct glsl_shader_prog_link
*program
, *program2
;
10490 struct glsl_ffp_destroy_ctx
*ctx
= context
;
10492 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
10493 struct glsl_shader_prog_link
, ps
.shader_entry
)
10495 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
10497 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
10498 HeapFree(GetProcessHeap(), 0, shader
);
10501 /* Context activation is done by the caller. */
10502 static void glsl_fragment_pipe_free(struct wined3d_device
*device
)
10504 struct shader_glsl_priv
*priv
= device
->fragment_priv
;
10505 struct glsl_ffp_destroy_ctx ctx
;
10508 ctx
.gl_info
= &device
->adapter
->gl_info
;
10509 wine_rb_destroy(&priv
->ffp_fragment_shaders
, shader_glsl_free_ffp_fragment_shader
, &ctx
);
10512 static void glsl_fragment_pipe_shader(struct wined3d_context
*context
,
10513 const struct wined3d_state
*state
, DWORD state_id
)
10515 context
->last_was_pshader
= use_ps(state
);
10517 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
10520 static void glsl_fragment_pipe_fogparams(struct wined3d_context
*context
,
10521 const struct wined3d_state
*state
, DWORD state_id
)
10523 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
10526 static void glsl_fragment_pipe_fog(struct wined3d_context
*context
,
10527 const struct wined3d_state
*state
, DWORD state_id
)
10529 BOOL use_vshader
= use_vs(state
);
10530 enum fogsource new_source
;
10531 DWORD fogstart
= state
->render_states
[WINED3D_RS_FOGSTART
];
10532 DWORD fogend
= state
->render_states
[WINED3D_RS_FOGEND
];
10534 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
10536 if (!state
->render_states
[WINED3D_RS_FOGENABLE
])
10539 if (state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
10542 new_source
= FOGSOURCE_VS
;
10543 else if (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
|| context
->stream_info
.position_transformed
)
10544 new_source
= FOGSOURCE_COORD
;
10546 new_source
= FOGSOURCE_FFP
;
10550 new_source
= FOGSOURCE_FFP
;
10553 if (new_source
!= context
->fog_source
|| fogstart
== fogend
)
10555 context
->fog_source
= new_source
;
10556 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
10560 static void glsl_fragment_pipe_vdecl(struct wined3d_context
*context
,
10561 const struct wined3d_state
*state
, DWORD state_id
)
10563 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10564 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
10565 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
10567 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
)))
10568 glsl_fragment_pipe_fog(context
, state
, state_id
);
10571 static void glsl_fragment_pipe_vs(struct wined3d_context
*context
,
10572 const struct wined3d_state
*state
, DWORD state_id
)
10574 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10575 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
10576 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
10579 static void glsl_fragment_pipe_tex_transform(struct wined3d_context
*context
,
10580 const struct wined3d_state
*state
, DWORD state_id
)
10582 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
10585 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context
*context
,
10586 const struct wined3d_state
*state
, DWORD state_id
)
10588 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
10591 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context
*context
,
10592 const struct wined3d_state
*state
, DWORD state_id
)
10594 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
10595 GLint func
= wined3d_gl_compare_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
]);
10596 float ref
= state
->render_states
[WINED3D_RS_ALPHAREF
] / 255.0f
;
10600 gl_info
->gl_ops
.gl
.p_glAlphaFunc(func
, ref
);
10601 checkGLcall("glAlphaFunc");
10605 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context
*context
,
10606 const struct wined3d_state
*state
, DWORD state_id
)
10608 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
10611 static void glsl_fragment_pipe_alpha_test(struct wined3d_context
*context
,
10612 const struct wined3d_state
*state
, DWORD state_id
)
10614 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
10616 if (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
])
10618 gl_info
->gl_ops
.gl
.p_glEnable(GL_ALPHA_TEST
);
10619 checkGLcall("glEnable(GL_ALPHA_TEST)");
10623 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
10624 checkGLcall("glDisable(GL_ALPHA_TEST)");
10628 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context
*context
,
10629 const struct wined3d_state
*state
, DWORD state_id
)
10631 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_ALPHA_TEST
;
10634 static void glsl_fragment_pipe_color_key(struct wined3d_context
*context
,
10635 const struct wined3d_state
*state
, DWORD state_id
)
10637 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
10640 static void glsl_fragment_pipe_shademode(struct wined3d_context
*context
,
10641 const struct wined3d_state
*state
, DWORD state_id
)
10643 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
10646 static const struct StateEntryTemplate glsl_fragment_pipe_state_template
[] =
10648 {STATE_VDECL
, {STATE_VDECL
, glsl_fragment_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
10649 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_fragment_pipe_vs
}, WINED3D_GL_EXT_NONE
},
10650 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10651 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10652 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10653 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10654 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10655 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10656 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10657 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10658 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10659 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10660 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10661 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10662 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10663 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10664 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10665 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10666 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10667 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10668 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10669 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10670 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10671 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10672 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10673 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10674 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10675 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10676 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10677 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10678 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10679 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10680 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10681 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10682 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10683 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10684 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10685 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10686 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10687 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10688 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10689 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10690 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10691 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10692 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10693 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10694 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10695 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10696 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10697 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10698 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10699 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10700 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10701 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10702 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10703 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10704 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10705 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10706 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10707 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10708 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10709 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10710 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10711 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10712 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10713 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10714 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10715 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10716 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10717 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10718 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10719 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10720 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10721 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10722 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10723 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
10724 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), glsl_fragment_pipe_alpha_test_func
}, WINED3D_GL_LEGACY_CONTEXT
},
10725 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10726 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), NULL
}, WINED3D_GL_LEGACY_CONTEXT
},
10727 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHAREF
), glsl_fragment_pipe_core_alpha_test_ref
}, WINED3D_GL_EXT_NONE
},
10728 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_alpha_test
}, WINED3D_GL_LEGACY_CONTEXT
},
10729 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_core_alpha_test
}, WINED3D_GL_EXT_NONE
},
10730 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10731 {STATE_COLOR_KEY
, { STATE_COLOR_KEY
, glsl_fragment_pipe_color_key
}, WINED3D_GL_EXT_NONE
},
10732 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_fragment_pipe_fog
}, WINED3D_GL_EXT_NONE
},
10733 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10734 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10735 {STATE_RENDER(WINED3D_RS_FOGSTART
), {STATE_RENDER(WINED3D_RS_FOGSTART
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
10736 {STATE_RENDER(WINED3D_RS_FOGEND
), {STATE_RENDER(WINED3D_RS_FOGSTART
), NULL
}, WINED3D_GL_EXT_NONE
},
10737 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), state_srgbwrite
}, ARB_FRAMEBUFFER_SRGB
},
10738 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10739 {STATE_RENDER(WINED3D_RS_FOGCOLOR
), {STATE_RENDER(WINED3D_RS_FOGCOLOR
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
10740 {STATE_RENDER(WINED3D_RS_FOGDENSITY
), {STATE_RENDER(WINED3D_RS_FOGDENSITY
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
10741 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, ARB_POINT_SPRITE
},
10742 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, WINED3D_GL_VERSION_2_0
},
10743 {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
},
10744 {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
},
10745 {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
},
10746 {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
},
10747 {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
},
10748 {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
},
10749 {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
},
10750 {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
},
10751 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10752 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10753 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10754 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10755 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10756 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10757 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10758 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10759 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10760 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
10761 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), state_shademode
}, WINED3D_GL_LEGACY_CONTEXT
},
10762 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_fragment_pipe_shademode
}, WINED3D_GL_EXT_NONE
},
10763 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE
},
10766 static BOOL
glsl_fragment_pipe_alloc_context_data(struct wined3d_context
*context
)
10771 static void glsl_fragment_pipe_free_context_data(struct wined3d_context
*context
)
10775 const struct fragment_pipeline glsl_fragment_pipe
=
10777 glsl_fragment_pipe_enable
,
10778 glsl_fragment_pipe_get_caps
,
10779 glsl_fragment_pipe_get_emul_mask
,
10780 glsl_fragment_pipe_alloc
,
10781 glsl_fragment_pipe_free
,
10782 glsl_fragment_pipe_alloc_context_data
,
10783 glsl_fragment_pipe_free_context_data
,
10784 shader_glsl_color_fixup_supported
,
10785 glsl_fragment_pipe_state_template
,