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