1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <[email protected]>
31 */
32
33
34 #ifndef ST_PROGRAM_H
35 #define ST_PROGRAM_H
36
37 #include "main/atifragshader.h"
38 #include "program/program.h"
39 #include "pipe/p_state.h"
40 #include "tgsi/tgsi_from_mesa.h"
41 #include "st_context.h"
42 #include "st_texture.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 struct st_external_sampler_key
49 {
50 GLuint lower_nv12; /**< bitmask of 2 plane YUV samplers */
51 GLuint lower_nv21;
52 GLuint lower_iyuv; /**< bitmask of 3 plane YUV samplers */
53 GLuint lower_xy_uxvx; /**< bitmask of 2 plane YUV samplers */
54 GLuint lower_xy_vxux; /**< bitmask of 2 plane YUV samplers */
55 GLuint lower_yx_xuxv; /**< bitmask of 2 plane YUV samplers */
56 GLuint lower_yx_xvxu; /**< bitmask of 2 plane YUV samplers */
57 GLuint lower_ayuv;
58 GLuint lower_xyuv;
59 GLuint lower_yuv;
60 GLuint lower_yu_yv;
61 GLuint lower_yv_yu;
62 GLuint lower_y41x;
63 GLuint bt709;
64 GLuint bt2020;
65 GLuint yuv_full_range;
66 };
67
68 static inline struct st_external_sampler_key
st_get_external_sampler_key(struct st_context * st,struct gl_program * prog)69 st_get_external_sampler_key(struct st_context *st, struct gl_program *prog)
70 {
71 unsigned mask = prog->ExternalSamplersUsed;
72 struct st_external_sampler_key key;
73
74 memset(&key, 0, sizeof(key));
75
76 while (unlikely(mask)) {
77 unsigned unit = u_bit_scan(&mask);
78 struct gl_texture_object *stObj =
79 st_get_texture_object(st->ctx, prog, unit);
80 enum pipe_format format = st_get_view_format(stObj);
81
82 /* if resource format matches then YUV wasn't lowered */
83 if (format == stObj->pt->format)
84 continue;
85
86 switch (format) {
87 case PIPE_FORMAT_NV12:
88 if (stObj->pt->format == PIPE_FORMAT_R8_G8B8_420_UNORM) {
89 key.lower_yuv |= (1 << unit);
90 break;
91 }
92 FALLTHROUGH;
93 case PIPE_FORMAT_P010:
94 case PIPE_FORMAT_P012:
95 case PIPE_FORMAT_P016:
96 case PIPE_FORMAT_P030:
97 key.lower_nv12 |= (1 << unit);
98 break;
99 case PIPE_FORMAT_NV21:
100 if (stObj->pt->format == PIPE_FORMAT_R8_B8G8_420_UNORM) {
101 key.lower_yuv |= (1 << unit);
102 break;
103 }
104 key.lower_nv21 |= (1 << unit);
105 break;
106 case PIPE_FORMAT_IYUV:
107 if (stObj->pt->format == PIPE_FORMAT_R8_G8_B8_420_UNORM ||
108 stObj->pt->format == PIPE_FORMAT_R8_B8_G8_420_UNORM) {
109 key.lower_yuv |= (1 << unit);
110 break;
111 }
112 key.lower_iyuv |= (1 << unit);
113 break;
114 case PIPE_FORMAT_YUYV:
115 if (stObj->pt->format == PIPE_FORMAT_R8G8_R8B8_UNORM) {
116 key.lower_yu_yv |= (1 << unit);
117 break;
118 }
119 FALLTHROUGH;
120 case PIPE_FORMAT_Y210:
121 case PIPE_FORMAT_Y212:
122 case PIPE_FORMAT_Y216:
123 key.lower_yx_xuxv |= (1 << unit);
124 break;
125 case PIPE_FORMAT_UYVY:
126 if (stObj->pt->format == PIPE_FORMAT_G8R8_B8R8_UNORM) {
127 key.lower_yu_yv |= (1 << unit);
128 break;
129 }
130 key.lower_xy_uxvx |= (1 << unit);
131 break;
132 case PIPE_FORMAT_VYUY:
133 if (stObj->pt->format == PIPE_FORMAT_B8R8_G8R8_UNORM) {
134 key.lower_yv_yu |= (1 << unit);
135 break;
136 }
137 key.lower_xy_vxux |= (1 << unit);
138 break;
139 case PIPE_FORMAT_YVYU:
140 if (stObj->pt->format == PIPE_FORMAT_R8B8_R8G8_UNORM) {
141 key.lower_yv_yu |= (1 << unit);
142 break;
143 }
144 key.lower_yx_xvxu |= (1 << unit);
145 break;
146 case PIPE_FORMAT_AYUV:
147 key.lower_ayuv |= (1 << unit);
148 break;
149 case PIPE_FORMAT_XYUV:
150 key.lower_xyuv |= (1 << unit);
151 break;
152 case PIPE_FORMAT_Y410:
153 case PIPE_FORMAT_Y412:
154 case PIPE_FORMAT_Y416:
155 key.lower_y41x |= (1 << unit);
156 break;
157 default:
158 printf("mesa: st_get_external_sampler_key: unhandled pipe format %u\n",
159 format);
160 break;
161 }
162
163 switch (stObj->yuv_color_space) {
164 case GL_TEXTURE_YUV_COLOR_SPACE_REC601:
165 break;
166 case GL_TEXTURE_YUV_COLOR_SPACE_REC709:
167 key.bt709 |= (1 << unit);
168 break;
169 case GL_TEXTURE_YUV_COLOR_SPACE_REC2020:
170 key.bt2020 |= (1 << unit);
171 break;
172 }
173
174 if (stObj->yuv_full_range)
175 key.yuv_full_range |= (1 << unit);
176 }
177
178 return key;
179 }
180
181 /** Fragment program variant key
182 *
183 * Please update st_get_fp_variant() perf_debug() when adding fields.
184 */
185 struct st_fp_variant_key
186 {
187 struct st_context *st; /**< variants are per-context */
188
189 /** for glBitmap */
190 GLuint bitmap:1; /**< glBitmap variant? */
191
192 /** for glDrawPixels */
193 GLuint drawpixels:1; /**< glDrawPixels variant */
194 GLuint scaleAndBias:1; /**< glDrawPixels w/ scale and/or bias? */
195 GLuint pixelMaps:1; /**< glDrawPixels w/ pixel lookup map? */
196
197 /** for ARB_color_buffer_float */
198 GLuint clamp_color:1;
199
200 /** for ARB_sample_shading */
201 GLuint persample_shading:1;
202
203 /** needed for ATI_fragment_shader */
204 GLuint fog:2;
205
206 /** for OpenGL 1.0 on modern hardware */
207 GLuint lower_two_sided_color:1;
208
209 GLuint lower_flatshade:1;
210 unsigned lower_alpha_func:3;
211
212 /** needed for ATI_fragment_shader */
213 uint8_t texture_index[MAX_NUM_FRAGMENT_REGISTERS_ATI];
214
215 struct st_external_sampler_key external;
216
217 /* bitmask of sampler units; PIPE_CAP_GL_CLAMP */
218 uint32_t gl_clamp[3];
219
220 /* bitmask of shadow samplers with depth textures in them for ARB programs; */
221 GLbitfield depth_textures;
222 };
223
224 /**
225 * Base class for shader variants.
226 */
227 struct st_variant
228 {
229 /** next in linked list */
230 struct st_variant *next;
231
232 /** st_context from the shader key */
233 struct st_context *st;
234
235 void *driver_shader;
236 };
237
238 /**
239 * Variant of a fragment program.
240 */
241 struct st_fp_variant
242 {
243 struct st_variant base;
244
245 /** Parameters which generated this version of fragment program */
246 struct st_fp_variant_key key;
247
248 /** For glBitmap variants */
249 uint bitmap_sampler;
250
251 /** For glDrawPixels variants */
252 unsigned drawpix_sampler;
253 unsigned pixelmap_sampler;
254 };
255
256
257 /** Shader key shared by other shaders.
258 *
259 * Please update st_get_common_variant() perf_debug() when adding fields.
260 */
261 struct st_common_variant_key
262 {
263 struct st_context *st; /**< variants are per-context */
264 bool passthrough_edgeflags;
265
266 /** for ARB_color_buffer_float */
267 bool clamp_color;
268
269 /** lower glPointSize to gl_PointSize */
270 bool export_point_size;
271
272 /* for user-defined clip-planes */
273 uint8_t lower_ucp;
274
275 /* Whether st_variant::driver_shader is for the draw module,
276 * not for the driver.
277 */
278 bool is_draw_shader;
279
280 /* bitmask of sampler units; PIPE_CAP_GL_CLAMP */
281 uint32_t gl_clamp[3];
282 };
283
284
285 /**
286 * Common shader variant.
287 */
288 struct st_common_variant
289 {
290 struct st_variant base;
291
292 /* Parameters which generated this variant. */
293 struct st_common_variant_key key;
294
295 /* Bitfield of VERT_BIT_* bits matching vertex shader inputs. */
296 GLbitfield vert_attrib_mask;
297 };
298
299 static inline struct st_common_variant *
st_common_variant(struct st_variant * v)300 st_common_variant(struct st_variant *v)
301 {
302 return (struct st_common_variant*)v;
303 }
304
305 static inline struct st_fp_variant *
st_fp_variant(struct st_variant * v)306 st_fp_variant(struct st_variant *v)
307 {
308 return (struct st_fp_variant*)v;
309 }
310
311 /**
312 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
313 */
314 static inline unsigned
st_get_generic_varying_index(struct st_context * st,GLuint attr)315 st_get_generic_varying_index(struct st_context *st, GLuint attr)
316 {
317 return tgsi_get_generic_gl_varying_index((gl_varying_slot)attr,
318 st->needs_texcoord_semantic);
319 }
320
321 extern void
322 st_set_prog_affected_state_flags(struct gl_program *prog);
323
324
325 extern struct st_fp_variant *
326 st_get_fp_variant(struct st_context *st,
327 struct gl_program *stfp,
328 const struct st_fp_variant_key *key);
329
330 extern struct st_common_variant *
331 st_get_common_variant(struct st_context *st,
332 struct gl_program *p,
333 const struct st_common_variant_key *key);
334
335 extern void
336 st_release_variants(struct st_context *st, struct gl_program *p);
337
338 extern void
339 st_release_program(struct st_context *st, struct gl_program **p);
340
341 extern void
342 st_destroy_program_variants(struct st_context *st);
343
344 extern void
345 st_finalize_nir_before_variants(struct nir_shader *nir);
346
347 extern void
348 st_prepare_vertex_program(struct gl_program *stvp);
349
350 extern void
351 st_translate_stream_output_info(struct gl_program *prog);
352
353 extern void
354 st_serialize_nir(struct gl_program *stp);
355 void
356 st_serialize_base_nir(struct gl_program *prog, struct nir_shader *nir);
357
358 extern void
359 st_finalize_program(struct st_context *st, struct gl_program *prog);
360
361 struct pipe_shader_state *
362 st_create_nir_shader(struct st_context *st, struct pipe_shader_state *state);
363
364 GLboolean st_program_string_notify(struct gl_context *ctx,
365 GLenum target,
366 struct gl_program *prog);
367
368 #ifdef __cplusplus
369 }
370 #endif
371
372 #endif
373