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