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 <keithw@vmware.com>
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
45extern "C" {
46#endif
47
48struct st_external_sampler_key
49{
50   GLuint lower_nv12;             /**< bitmask of 2 plane YUV samplers */
51   GLuint lower_iyuv;             /**< bitmask of 3 plane YUV samplers */
52   GLuint lower_xy_uxvx;          /**< bitmask of 2 plane YUV samplers */
53   GLuint lower_yx_xuxv;          /**< bitmask of 2 plane YUV samplers */
54   GLuint lower_ayuv;
55   GLuint lower_xyuv;
56   GLuint lower_yuv;
57   GLuint lower_yu_yv;
58   GLuint lower_y41x;
59   GLuint bt709;
60   GLuint bt2020;
61   GLuint yuv_full_range;
62};
63
64static inline struct st_external_sampler_key
65st_get_external_sampler_key(struct st_context *st, struct gl_program *prog)
66{
67   unsigned mask = prog->ExternalSamplersUsed;
68   struct st_external_sampler_key key;
69
70   memset(&key, 0, sizeof(key));
71
72   while (unlikely(mask)) {
73      unsigned unit = u_bit_scan(&mask);
74      struct gl_texture_object *stObj =
75            st_get_texture_object(st->ctx, prog, unit);
76      enum pipe_format format = st_get_view_format(stObj);
77
78      /* if resource format matches then YUV wasn't lowered */
79      if (format == stObj->pt->format)
80         continue;
81
82      switch (format) {
83      case PIPE_FORMAT_NV12:
84         if (stObj->pt->format == PIPE_FORMAT_R8_G8B8_420_UNORM) {
85            key.lower_yuv |= (1 << unit);
86            break;
87         }
88         FALLTHROUGH;
89      case PIPE_FORMAT_P010:
90      case PIPE_FORMAT_P012:
91      case PIPE_FORMAT_P016:
92         key.lower_nv12 |= (1 << unit);
93         break;
94      case PIPE_FORMAT_IYUV:
95         key.lower_iyuv |= (1 << unit);
96         break;
97      case PIPE_FORMAT_YUYV:
98         if (stObj->pt->format == PIPE_FORMAT_R8G8_R8B8_UNORM) {
99            key.lower_yu_yv |= (1 << unit);
100            break;
101         }
102         FALLTHROUGH;
103      case PIPE_FORMAT_Y210:
104      case PIPE_FORMAT_Y212:
105      case PIPE_FORMAT_Y216:
106         key.lower_yx_xuxv |= (1 << unit);
107         break;
108      case PIPE_FORMAT_UYVY:
109         if (stObj->pt->format == PIPE_FORMAT_G8R8_B8R8_UNORM) {
110            key.lower_yu_yv |= (1 << unit);
111            break;
112         }
113         key.lower_xy_uxvx |= (1 << unit);
114         break;
115      case PIPE_FORMAT_AYUV:
116         key.lower_ayuv |= (1 << unit);
117         break;
118      case PIPE_FORMAT_XYUV:
119         key.lower_xyuv |= (1 << unit);
120         break;
121      case PIPE_FORMAT_Y410:
122      case PIPE_FORMAT_Y412:
123      case PIPE_FORMAT_Y416:
124         key.lower_y41x |= (1 << unit);
125         break;
126      default:
127         printf("mesa: st_get_external_sampler_key: unhandled pipe format %u\n",
128                format);
129         break;
130      }
131
132      switch (stObj->yuv_color_space) {
133      case GL_TEXTURE_YUV_COLOR_SPACE_REC601:
134         break;
135      case GL_TEXTURE_YUV_COLOR_SPACE_REC709:
136         key.bt709 |= (1 << unit);
137         break;
138      case GL_TEXTURE_YUV_COLOR_SPACE_REC2020:
139         key.bt2020 |= (1 << unit);
140         break;
141      }
142
143      if (stObj->yuv_full_range)
144         key.yuv_full_range |= (1 << unit);
145   }
146
147   return key;
148}
149
150/** Fragment program variant key
151 *
152 * Please update st_get_fp_variant() perf_debug() when adding fields.
153 */
154struct st_fp_variant_key
155{
156   struct st_context *st;         /**< variants are per-context */
157
158   /** for glBitmap */
159   GLuint bitmap:1;               /**< glBitmap variant? */
160
161   /** for glDrawPixels */
162   GLuint drawpixels:1;           /**< glDrawPixels variant */
163   GLuint scaleAndBias:1;         /**< glDrawPixels w/ scale and/or bias? */
164   GLuint pixelMaps:1;            /**< glDrawPixels w/ pixel lookup map? */
165
166   /** for ARB_color_buffer_float */
167   GLuint clamp_color:1;
168
169   /** for ARB_sample_shading */
170   GLuint persample_shading:1;
171
172   /** needed for ATI_fragment_shader */
173   GLuint fog:2;
174
175   /** for OpenGL 1.0 on modern hardware */
176   GLuint lower_two_sided_color:1;
177
178   GLuint lower_flatshade:1;
179   GLuint lower_texcoord_replace:MAX_TEXTURE_COORD_UNITS;
180   unsigned lower_alpha_func:3;
181
182   /** needed for ATI_fragment_shader */
183   uint8_t texture_index[MAX_NUM_FRAGMENT_REGISTERS_ATI];
184
185   struct st_external_sampler_key external;
186
187   /* bitmask of sampler units; PIPE_CAP_GL_CLAMP */
188   uint32_t gl_clamp[3];
189};
190
191/**
192 * Base class for shader variants.
193 */
194struct st_variant
195{
196   /** next in linked list */
197   struct st_variant *next;
198
199   /** st_context from the shader key */
200   struct st_context *st;
201
202   void *driver_shader;
203};
204
205/**
206 * Variant of a fragment program.
207 */
208struct st_fp_variant
209{
210   struct st_variant base;
211
212   /** Parameters which generated this version of fragment program */
213   struct st_fp_variant_key key;
214
215   /** For glBitmap variants */
216   uint bitmap_sampler;
217
218   /** For glDrawPixels variants */
219   unsigned drawpix_sampler;
220   unsigned pixelmap_sampler;
221};
222
223
224/** Shader key shared by other shaders.
225 *
226 * Please update st_get_common_variant() perf_debug() when adding fields.
227 */
228struct st_common_variant_key
229{
230   struct st_context *st;          /**< variants are per-context */
231   bool passthrough_edgeflags;
232
233   /** for ARB_color_buffer_float */
234   bool clamp_color;
235
236   /** lower glPointSize to gl_PointSize */
237   boolean export_point_size;
238
239   /* for user-defined clip-planes */
240   uint8_t lower_ucp;
241
242   /* Whether st_variant::driver_shader is for the draw module,
243    * not for the driver.
244    */
245   bool is_draw_shader;
246
247   /* bitmask of sampler units; PIPE_CAP_GL_CLAMP */
248   uint32_t gl_clamp[3];
249};
250
251
252/**
253 * Common shader variant.
254 */
255struct st_common_variant
256{
257   struct st_variant base;
258
259   /* Parameters which generated this variant. */
260   struct st_common_variant_key key;
261
262   /* Bitfield of VERT_BIT_* bits matching vertex shader inputs. */
263   GLbitfield vert_attrib_mask;
264};
265
266static inline struct st_common_variant *
267st_common_variant(struct st_variant *v)
268{
269   return (struct st_common_variant*)v;
270}
271
272static inline struct st_fp_variant *
273st_fp_variant(struct st_variant *v)
274{
275   return (struct st_fp_variant*)v;
276}
277
278/**
279 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
280 */
281static inline unsigned
282st_get_generic_varying_index(struct st_context *st, GLuint attr)
283{
284   return tgsi_get_generic_gl_varying_index((gl_varying_slot)attr,
285                                            st->needs_texcoord_semantic);
286}
287
288extern void
289st_set_prog_affected_state_flags(struct gl_program *prog);
290
291
292extern struct st_fp_variant *
293st_get_fp_variant(struct st_context *st,
294                  struct gl_program *stfp,
295                  const struct st_fp_variant_key *key);
296
297extern struct st_common_variant *
298st_get_common_variant(struct st_context *st,
299                      struct gl_program *p,
300                      const struct st_common_variant_key *key);
301
302extern void
303st_release_variants(struct st_context *st, struct gl_program *p);
304
305extern void
306st_release_program(struct st_context *st, struct gl_program **p);
307
308extern void
309st_destroy_program_variants(struct st_context *st);
310
311extern void
312st_finalize_nir_before_variants(struct nir_shader *nir);
313
314extern void
315st_prepare_vertex_program(struct gl_program *stvp);
316
317extern void
318st_translate_stream_output_info(struct gl_program *prog);
319
320extern void
321st_serialize_nir(struct gl_program *stp);
322
323extern void
324st_finalize_program(struct st_context *st, struct gl_program *prog);
325
326struct pipe_shader_state *
327st_create_nir_shader(struct st_context *st, struct pipe_shader_state *state);
328
329GLboolean st_program_string_notify(struct gl_context *ctx,
330                                   GLenum target,
331                                   struct gl_program *prog);
332
333bool
334st_can_add_pointsize_to_program(struct st_context *st, struct gl_program *prog);
335
336#ifdef __cplusplus
337}
338#endif
339
340#endif
341