gallium: Use enum pipe_shader_type in set_sampler_views()
[mesa.git] / src / mesa / state_tracker / st_context.h
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 #ifndef ST_CONTEXT_H
29 #define ST_CONTEXT_H
30
31 #include "main/mtypes.h"
32 #include "pipe/p_state.h"
33 #include "state_tracker/st_api.h"
34 #include "main/fbobject.h"
35 #include "state_tracker/st_atom.h"
36
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42
43 struct bitmap_cache;
44 struct dd_function_table;
45 struct draw_context;
46 struct draw_stage;
47 struct gen_mipmap_state;
48 struct st_context;
49 struct st_fragment_program;
50 struct st_perf_monitor_group;
51 struct u_upload_mgr;
52
53
54 /** For drawing quads for glClear, glDraw/CopyPixels, glBitmap, etc. */
55 struct st_util_vertex
56 {
57 float x, y, z;
58 float r, g, b, a;
59 float s, t;
60 };
61
62
63 struct st_context
64 {
65 struct st_context_iface iface;
66
67 struct gl_context *ctx;
68
69 struct pipe_context *pipe;
70
71 struct u_upload_mgr *uploader, *indexbuf_uploader, *constbuf_uploader;
72
73 struct draw_context *draw; /**< For selection/feedback/rastpos only */
74 struct draw_stage *feedback_stage; /**< For GL_FEEDBACK rendermode */
75 struct draw_stage *selection_stage; /**< For GL_SELECT rendermode */
76 struct draw_stage *rastpos_stage; /**< For glRasterPos */
77 GLboolean clamp_frag_color_in_shader;
78 GLboolean clamp_vert_color_in_shader;
79 boolean has_stencil_export; /**< can do shader stencil export? */
80 boolean has_time_elapsed;
81 boolean has_shader_model3;
82 boolean has_etc1;
83 boolean has_etc2;
84 boolean prefer_blit_based_texture_transfer;
85 boolean force_persample_in_shader;
86 boolean has_shareable_shaders;
87 boolean has_half_float_packing;
88 boolean has_multi_draw_indirect;
89
90 /**
91 * If a shader can be created when we get its source.
92 * This means it has only 1 variant, not counting glBitmap and
93 * glDrawPixels.
94 */
95 boolean shader_has_one_variant[MESA_SHADER_STAGES];
96
97 boolean needs_texcoord_semantic;
98 boolean apply_texture_swizzle_to_border_color;
99
100 /* On old libGL's for linux we need to invalidate the drawables
101 * on glViewpport calls, this is set via a option.
102 */
103 boolean invalidate_on_gl_viewport;
104
105 boolean vertex_array_out_of_memory;
106
107 /* Some state is contained in constant objects.
108 * Other state is just parameter values.
109 */
110 struct {
111 struct pipe_blend_state blend;
112 struct pipe_depth_stencil_alpha_state depth_stencil;
113 struct pipe_rasterizer_state rasterizer;
114 struct pipe_sampler_state samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
115 GLuint num_samplers[PIPE_SHADER_TYPES];
116 struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
117 GLuint num_sampler_views[PIPE_SHADER_TYPES];
118 struct pipe_clip_state clip;
119 struct {
120 void *ptr;
121 unsigned size;
122 } constants[PIPE_SHADER_TYPES];
123 struct pipe_framebuffer_state framebuffer;
124 struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
125 struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
126 struct {
127 unsigned num;
128 boolean include;
129 struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
130 } window_rects;
131 unsigned sample_mask;
132
133 GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */
134
135 GLuint fb_orientation;
136 } state;
137
138 char vendor[100];
139 char renderer[100];
140
141 uint64_t dirty; /**< dirty states */
142
143 /** This masks out unused shader resources. Only valid in draw calls. */
144 uint64_t active_states;
145
146 /* If true, further analysis of states is required to know if something
147 * has changed. Used mainly for shaders.
148 */
149 bool gfx_shaders_may_be_dirty;
150 bool compute_shader_may_be_dirty;
151
152 GLboolean vertdata_edgeflags;
153 GLboolean edgeflag_culls_prims;
154
155 /** Mapping from VARYING_SLOT_x to post-transformed vertex slot */
156 const GLuint *vertex_result_to_slot;
157
158 struct st_vertex_program *vp; /**< Currently bound vertex program */
159 struct st_fragment_program *fp; /**< Currently bound fragment program */
160 struct st_geometry_program *gp; /**< Currently bound geometry program */
161 struct st_tessctrl_program *tcp; /**< Currently bound tess control program */
162 struct st_tesseval_program *tep; /**< Currently bound tess eval program */
163 struct st_compute_program *cp; /**< Currently bound compute program */
164
165 struct st_vp_variant *vp_variant;
166 struct st_fp_variant *fp_variant;
167 struct st_basic_variant *gp_variant;
168 struct st_basic_variant *tcp_variant;
169 struct st_basic_variant *tep_variant;
170 struct st_basic_variant *cp_variant;
171
172 struct {
173 struct pipe_resource *pixelmap_texture;
174 struct pipe_sampler_view *pixelmap_sampler_view;
175 } pixel_xfer;
176
177 /** for glBitmap */
178 struct {
179 struct pipe_rasterizer_state rasterizer;
180 struct pipe_sampler_state sampler;
181 struct pipe_sampler_state atlas_sampler;
182 enum pipe_format tex_format;
183 void *vs;
184 struct bitmap_cache *cache;
185 } bitmap;
186
187 /** for glDraw/CopyPixels */
188 struct {
189 void *zs_shaders[4];
190 void *vert_shaders[2]; /**< ureg shaders */
191 } drawpix;
192
193 struct {
194 GLsizei width, height;
195 GLenum format, type;
196 const void *user_pointer; /**< Last user 'pixels' pointer */
197 void *image; /**< Copy of the glDrawPixels image data */
198 struct pipe_resource *texture;
199 } drawpix_cache;
200
201 /** for glReadPixels */
202 struct {
203 struct pipe_resource *src;
204 struct pipe_resource *cache;
205 enum pipe_format dst_format;
206 unsigned level;
207 unsigned layer;
208 unsigned hits;
209 } readpix_cache;
210
211 /** for glClear */
212 struct {
213 struct pipe_rasterizer_state raster;
214 struct pipe_viewport_state viewport;
215 void *vs;
216 void *fs;
217 void *vs_layered;
218 void *gs_layered;
219 } clear;
220
221 /* For gl(Compressed)Tex(Sub)Image */
222 struct {
223 struct pipe_rasterizer_state raster;
224 struct pipe_blend_state upload_blend;
225 void *vs;
226 void *gs;
227 void *upload_fs;
228 void *download_fs[PIPE_MAX_TEXTURE_TYPES];
229 bool upload_enabled;
230 bool download_enabled;
231 bool rgba_only;
232 bool layers;
233 bool use_gs;
234 } pbo;
235
236 /** for drawing with st_util_vertex */
237 struct pipe_vertex_element util_velems[3];
238
239 void *passthrough_fs; /**< simple pass-through frag shader */
240
241 enum pipe_texture_target internal_target;
242
243 struct cso_context *cso_context;
244
245 void *winsys_drawable_handle;
246
247 /* The number of vertex buffers from the last call of validate_arrays. */
248 unsigned last_num_vbuffers;
249
250 int32_t draw_stamp;
251 int32_t read_stamp;
252
253 struct st_config_options options;
254
255 struct st_perf_monitor_group *perfmon;
256 };
257
258
259 /* Need this so that we can implement Mesa callbacks in this module.
260 */
261 static inline struct st_context *st_context(struct gl_context *ctx)
262 {
263 return ctx->st;
264 }
265
266
267 /**
268 * Wrapper for struct gl_framebuffer.
269 * This is an opaque type to the outside world.
270 */
271 struct st_framebuffer
272 {
273 struct gl_framebuffer Base;
274 void *Private;
275
276 struct st_framebuffer_iface *iface;
277 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
278 unsigned num_statts;
279 int32_t stamp;
280 int32_t iface_stamp;
281 };
282
283
284 extern void st_init_driver_functions(struct pipe_screen *screen,
285 struct dd_function_table *functions);
286
287 void st_invalidate_state(struct gl_context * ctx, GLbitfield new_state);
288
289 void st_invalidate_readpix_cache(struct st_context *st);
290
291
292 #define Y_0_TOP 1
293 #define Y_0_BOTTOM 2
294
295 static inline GLuint
296 st_fb_orientation(const struct gl_framebuffer *fb)
297 {
298 if (fb && _mesa_is_winsys_fbo(fb)) {
299 /* Drawing into a window (on-screen buffer).
300 *
301 * Negate Y scale to flip image vertically.
302 * The NDC Y coords prior to viewport transformation are in the range
303 * [y=-1=bottom, y=1=top]
304 * Hardware window coords are in the range [y=0=top, y=H-1=bottom] where
305 * H is the window height.
306 * Use the viewport transformation to invert Y.
307 */
308 return Y_0_TOP;
309 }
310 else {
311 /* Drawing into user-created FBO (very likely a texture).
312 *
313 * For textures, T=0=Bottom, so by extension Y=0=Bottom for rendering.
314 */
315 return Y_0_BOTTOM;
316 }
317 }
318
319
320 static inline enum pipe_shader_type
321 st_shader_stage_to_ptarget(gl_shader_stage stage)
322 {
323 switch (stage) {
324 case MESA_SHADER_VERTEX:
325 return PIPE_SHADER_VERTEX;
326 case MESA_SHADER_FRAGMENT:
327 return PIPE_SHADER_FRAGMENT;
328 case MESA_SHADER_GEOMETRY:
329 return PIPE_SHADER_GEOMETRY;
330 case MESA_SHADER_TESS_CTRL:
331 return PIPE_SHADER_TESS_CTRL;
332 case MESA_SHADER_TESS_EVAL:
333 return PIPE_SHADER_TESS_EVAL;
334 case MESA_SHADER_COMPUTE:
335 return PIPE_SHADER_COMPUTE;
336 }
337
338 assert(!"should not be reached");
339 return PIPE_SHADER_VERTEX;
340 }
341
342 static inline bool
343 st_user_clip_planes_enabled(struct gl_context *ctx)
344 {
345 return (ctx->API == API_OPENGL_COMPAT ||
346 ctx->API == API_OPENGLES) && /* only ES 1.x */
347 ctx->Transform.ClipPlanesEnabled;
348 }
349
350 /** clear-alloc a struct-sized object, with casting */
351 #define ST_CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
352
353
354 extern struct st_context *
355 st_create_context(gl_api api, struct pipe_context *pipe,
356 const struct gl_config *visual,
357 struct st_context *share,
358 const struct st_config_options *options);
359
360 extern void
361 st_destroy_context(struct st_context *st);
362
363 uint64_t
364 st_get_active_states(struct gl_context *ctx);
365
366
367 #ifdef __cplusplus
368 }
369 #endif
370
371 #endif