e0f29e3cebfe4bc05ce3945ca3352dab50675875
[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 /* If true, further analysis of states is required to know if something
144 * has changed. Used mainly for shaders.
145 */
146 bool gfx_shaders_may_be_dirty;
147 bool compute_shader_may_be_dirty;
148
149 GLboolean vertdata_edgeflags;
150 GLboolean edgeflag_culls_prims;
151
152 /** Mapping from VARYING_SLOT_x to post-transformed vertex slot */
153 const GLuint *vertex_result_to_slot;
154
155 struct st_vertex_program *vp; /**< Currently bound vertex program */
156 struct st_fragment_program *fp; /**< Currently bound fragment program */
157 struct st_geometry_program *gp; /**< Currently bound geometry program */
158 struct st_tessctrl_program *tcp; /**< Currently bound tess control program */
159 struct st_tesseval_program *tep; /**< Currently bound tess eval program */
160 struct st_compute_program *cp; /**< Currently bound compute program */
161
162 struct st_vp_variant *vp_variant;
163 struct st_fp_variant *fp_variant;
164 struct st_basic_variant *gp_variant;
165 struct st_basic_variant *tcp_variant;
166 struct st_basic_variant *tep_variant;
167 struct st_basic_variant *cp_variant;
168
169 struct {
170 struct pipe_resource *pixelmap_texture;
171 struct pipe_sampler_view *pixelmap_sampler_view;
172 } pixel_xfer;
173
174 /** for glBitmap */
175 struct {
176 struct pipe_rasterizer_state rasterizer;
177 struct pipe_sampler_state sampler;
178 struct pipe_sampler_state atlas_sampler;
179 enum pipe_format tex_format;
180 void *vs;
181 struct bitmap_cache *cache;
182 } bitmap;
183
184 /** for glDraw/CopyPixels */
185 struct {
186 void *zs_shaders[4];
187 void *vert_shaders[2]; /**< ureg shaders */
188 } drawpix;
189
190 struct {
191 GLsizei width, height;
192 GLenum format, type;
193 const void *user_pointer; /**< Last user 'pixels' pointer */
194 void *image; /**< Copy of the glDrawPixels image data */
195 struct pipe_resource *texture;
196 } drawpix_cache;
197
198 /** for glReadPixels */
199 struct {
200 struct pipe_resource *src;
201 struct pipe_resource *cache;
202 enum pipe_format dst_format;
203 unsigned level;
204 unsigned layer;
205 unsigned hits;
206 } readpix_cache;
207
208 /** for glClear */
209 struct {
210 struct pipe_rasterizer_state raster;
211 struct pipe_viewport_state viewport;
212 void *vs;
213 void *fs;
214 void *vs_layered;
215 void *gs_layered;
216 } clear;
217
218 /* For gl(Compressed)Tex(Sub)Image */
219 struct {
220 struct pipe_rasterizer_state raster;
221 struct pipe_blend_state upload_blend;
222 void *vs;
223 void *gs;
224 void *upload_fs;
225 void *download_fs[PIPE_MAX_TEXTURE_TYPES];
226 bool upload_enabled;
227 bool download_enabled;
228 bool rgba_only;
229 bool layers;
230 bool use_gs;
231 } pbo;
232
233 /** for drawing with st_util_vertex */
234 struct pipe_vertex_element util_velems[3];
235
236 void *passthrough_fs; /**< simple pass-through frag shader */
237
238 enum pipe_texture_target internal_target;
239
240 struct cso_context *cso_context;
241
242 void *winsys_drawable_handle;
243
244 /* The number of vertex buffers from the last call of validate_arrays. */
245 unsigned last_num_vbuffers;
246
247 int32_t draw_stamp;
248 int32_t read_stamp;
249
250 struct st_config_options options;
251
252 struct st_perf_monitor_group *perfmon;
253 };
254
255
256 /* Need this so that we can implement Mesa callbacks in this module.
257 */
258 static inline struct st_context *st_context(struct gl_context *ctx)
259 {
260 return ctx->st;
261 }
262
263
264 /**
265 * Wrapper for struct gl_framebuffer.
266 * This is an opaque type to the outside world.
267 */
268 struct st_framebuffer
269 {
270 struct gl_framebuffer Base;
271 void *Private;
272
273 struct st_framebuffer_iface *iface;
274 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
275 unsigned num_statts;
276 int32_t stamp;
277 int32_t iface_stamp;
278 };
279
280
281 extern void st_init_driver_functions(struct pipe_screen *screen,
282 struct dd_function_table *functions);
283
284 void st_invalidate_state(struct gl_context * ctx, GLbitfield new_state);
285
286 void st_invalidate_readpix_cache(struct st_context *st);
287
288
289 #define Y_0_TOP 1
290 #define Y_0_BOTTOM 2
291
292 static inline GLuint
293 st_fb_orientation(const struct gl_framebuffer *fb)
294 {
295 if (fb && _mesa_is_winsys_fbo(fb)) {
296 /* Drawing into a window (on-screen buffer).
297 *
298 * Negate Y scale to flip image vertically.
299 * The NDC Y coords prior to viewport transformation are in the range
300 * [y=-1=bottom, y=1=top]
301 * Hardware window coords are in the range [y=0=top, y=H-1=bottom] where
302 * H is the window height.
303 * Use the viewport transformation to invert Y.
304 */
305 return Y_0_TOP;
306 }
307 else {
308 /* Drawing into user-created FBO (very likely a texture).
309 *
310 * For textures, T=0=Bottom, so by extension Y=0=Bottom for rendering.
311 */
312 return Y_0_BOTTOM;
313 }
314 }
315
316
317 static inline unsigned
318 st_shader_stage_to_ptarget(gl_shader_stage stage)
319 {
320 switch (stage) {
321 case MESA_SHADER_VERTEX:
322 return PIPE_SHADER_VERTEX;
323 case MESA_SHADER_FRAGMENT:
324 return PIPE_SHADER_FRAGMENT;
325 case MESA_SHADER_GEOMETRY:
326 return PIPE_SHADER_GEOMETRY;
327 case MESA_SHADER_TESS_CTRL:
328 return PIPE_SHADER_TESS_CTRL;
329 case MESA_SHADER_TESS_EVAL:
330 return PIPE_SHADER_TESS_EVAL;
331 case MESA_SHADER_COMPUTE:
332 return PIPE_SHADER_COMPUTE;
333 }
334
335 assert(!"should not be reached");
336 return PIPE_SHADER_VERTEX;
337 }
338
339
340 /** clear-alloc a struct-sized object, with casting */
341 #define ST_CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
342
343
344 extern struct st_context *
345 st_create_context(gl_api api, struct pipe_context *pipe,
346 const struct gl_config *visual,
347 struct st_context *share,
348 const struct st_config_options *options);
349
350 extern void
351 st_destroy_context(struct st_context *st);
352
353
354 #ifdef __cplusplus
355 }
356 #endif
357
358 #endif