st/mesa: move utility functions, macros into new st_util.h file
[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/arrayobj.h"
32 #include "main/mtypes.h"
33 #include "state_tracker/st_api.h"
34 #include "main/fbobject.h"
35 #include "state_tracker/st_atom.h"
36 #include "util/u_helpers.h"
37 #include "util/u_inlines.h"
38 #include "util/list.h"
39 #include "vbo/vbo.h"
40
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46
47 struct dd_function_table;
48 struct draw_context;
49 struct draw_stage;
50 struct gen_mipmap_state;
51 struct st_context;
52 struct st_fragment_program;
53 struct st_perf_monitor_group;
54 struct u_upload_mgr;
55
56
57 struct st_bitmap_cache
58 {
59 /** Window pos to render the cached image */
60 GLint xpos, ypos;
61 /** Bounds of region used in window coords */
62 GLint xmin, ymin, xmax, ymax;
63
64 GLfloat color[4];
65
66 /** Bitmap's Z position */
67 GLfloat zpos;
68
69 struct pipe_resource *texture;
70 struct pipe_transfer *trans;
71
72 GLboolean empty;
73
74 /** An I8 texture image: */
75 ubyte *buffer;
76 };
77
78 struct st_bound_handles
79 {
80 unsigned num_handles;
81 uint64_t *handles;
82 };
83
84
85 #define NUM_DRAWPIX_CACHE_ENTRIES 4
86
87 struct drawpix_cache_entry
88 {
89 GLsizei width, height;
90 GLenum format, type;
91 const void *user_pointer; /**< Last user 'pixels' pointer */
92 void *image; /**< Copy of the glDrawPixels image data */
93 struct pipe_resource *texture;
94 unsigned age;
95 };
96
97
98 struct st_context
99 {
100 struct st_context_iface iface;
101
102 struct gl_context *ctx;
103
104 struct pipe_context *pipe;
105
106 struct draw_context *draw; /**< For selection/feedback/rastpos only */
107 struct draw_stage *feedback_stage; /**< For GL_FEEDBACK rendermode */
108 struct draw_stage *selection_stage; /**< For GL_SELECT rendermode */
109 struct draw_stage *rastpos_stage; /**< For glRasterPos */
110 GLboolean clamp_frag_color_in_shader;
111 GLboolean clamp_vert_color_in_shader;
112 boolean has_stencil_export; /**< can do shader stencil export? */
113 boolean has_time_elapsed;
114 boolean has_shader_model3;
115 boolean has_etc1;
116 boolean has_etc2;
117 boolean has_astc_2d_ldr;
118 boolean prefer_blit_based_texture_transfer;
119 boolean force_persample_in_shader;
120 boolean has_shareable_shaders;
121 boolean has_half_float_packing;
122 boolean has_multi_draw_indirect;
123 boolean has_single_pipe_stat;
124 boolean has_indep_blend_func;
125 boolean needs_rgb_dst_alpha_override;
126 boolean can_bind_const_buffer_as_vertex;
127
128 /**
129 * If a shader can be created when we get its source.
130 * This means it has only 1 variant, not counting glBitmap and
131 * glDrawPixels.
132 */
133 boolean shader_has_one_variant[MESA_SHADER_STAGES];
134
135 boolean needs_texcoord_semantic;
136 boolean apply_texture_swizzle_to_border_color;
137
138 /* On old libGL's for linux we need to invalidate the drawables
139 * on glViewpport calls, this is set via a option.
140 */
141 boolean invalidate_on_gl_viewport;
142 boolean draw_needs_minmax_index;
143 boolean vertex_array_out_of_memory;
144 boolean has_hw_atomics;
145
146 /* Some state is contained in constant objects.
147 * Other state is just parameter values.
148 */
149 struct {
150 struct pipe_blend_state blend;
151 struct pipe_depth_stencil_alpha_state depth_stencil;
152 struct pipe_rasterizer_state rasterizer;
153 struct pipe_sampler_state frag_samplers[PIPE_MAX_SAMPLERS];
154 GLuint num_frag_samplers;
155 struct pipe_sampler_view *frag_sampler_views[PIPE_MAX_SAMPLERS];
156 GLuint num_sampler_views[PIPE_SHADER_TYPES];
157 struct pipe_clip_state clip;
158 struct {
159 void *ptr;
160 unsigned size;
161 } constants[PIPE_SHADER_TYPES];
162 unsigned fb_width;
163 unsigned fb_height;
164 unsigned fb_num_samples;
165 unsigned fb_num_layers;
166 unsigned fb_num_cb;
167 unsigned num_viewports;
168 struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
169 struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
170 struct {
171 unsigned num;
172 boolean include;
173 struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
174 } window_rects;
175
176 GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */
177
178 GLuint fb_orientation;
179
180 bool enable_sample_locations;
181 unsigned sample_locations_samples;
182 uint8_t sample_locations[
183 PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
184 PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
185 } state;
186
187 uint64_t dirty; /**< dirty states */
188
189 /** This masks out unused shader resources. Only valid in draw calls. */
190 uint64_t active_states;
191
192 unsigned pin_thread_counter; /* for L3 thread pinning on AMD Zen */
193
194 /* If true, further analysis of states is required to know if something
195 * has changed. Used mainly for shaders.
196 */
197 bool gfx_shaders_may_be_dirty;
198 bool compute_shader_may_be_dirty;
199
200 GLboolean vertdata_edgeflags;
201 GLboolean edgeflag_culls_prims;
202
203 struct st_vertex_program *vp; /**< Currently bound vertex program */
204 struct st_fragment_program *fp; /**< Currently bound fragment program */
205 struct st_common_program *gp; /**< Currently bound geometry program */
206 struct st_common_program *tcp; /**< Currently bound tess control program */
207 struct st_common_program *tep; /**< Currently bound tess eval program */
208 struct st_compute_program *cp; /**< Currently bound compute program */
209
210 struct st_vp_variant *vp_variant;
211
212 struct {
213 struct pipe_resource *pixelmap_texture;
214 struct pipe_sampler_view *pixelmap_sampler_view;
215 } pixel_xfer;
216
217 /** for glBitmap */
218 struct {
219 struct pipe_rasterizer_state rasterizer;
220 struct pipe_sampler_state sampler;
221 struct pipe_sampler_state atlas_sampler;
222 enum pipe_format tex_format;
223 struct st_bitmap_cache cache;
224 } bitmap;
225
226 /** for glDraw/CopyPixels */
227 struct {
228 void *zs_shaders[4];
229 } drawpix;
230
231 /** Cache of glDrawPixels images */
232 struct {
233 struct drawpix_cache_entry entries[NUM_DRAWPIX_CACHE_ENTRIES];
234 unsigned age;
235 } drawpix_cache;
236
237 /** for glReadPixels */
238 struct {
239 struct pipe_resource *src;
240 struct pipe_resource *cache;
241 enum pipe_format dst_format;
242 unsigned level;
243 unsigned layer;
244 unsigned hits;
245 } readpix_cache;
246
247 /** for glClear */
248 struct {
249 struct pipe_rasterizer_state raster;
250 struct pipe_viewport_state viewport;
251 void *vs;
252 void *fs;
253 void *vs_layered;
254 void *gs_layered;
255 } clear;
256
257 /* For gl(Compressed)Tex(Sub)Image */
258 struct {
259 struct pipe_rasterizer_state raster;
260 struct pipe_blend_state upload_blend;
261 void *vs;
262 void *gs;
263 void *upload_fs[3];
264 void *download_fs[3][PIPE_MAX_TEXTURE_TYPES];
265 bool upload_enabled;
266 bool download_enabled;
267 bool rgba_only;
268 bool layers;
269 bool use_gs;
270 } pbo;
271
272 /** for drawing with st_util_vertex */
273 struct pipe_vertex_element util_velems[3];
274
275 /** passthrough vertex shader matching the util_velem attributes */
276 void *passthrough_vs;
277
278 enum pipe_texture_target internal_target;
279
280 struct cso_context *cso_context;
281
282 void *winsys_drawable_handle;
283
284 /* The number of vertex buffers from the last call of validate_arrays. */
285 unsigned last_num_vbuffers;
286
287 int32_t draw_stamp;
288 int32_t read_stamp;
289
290 struct st_config_options options;
291
292 struct st_perf_monitor_group *perfmon;
293
294 enum pipe_reset_status reset_status;
295
296 /* Array of bound texture/image handles which are resident in the context.
297 */
298 struct st_bound_handles bound_texture_handles[PIPE_SHADER_TYPES];
299 struct st_bound_handles bound_image_handles[PIPE_SHADER_TYPES];
300
301 /* Winsys buffers */
302 struct list_head winsys_buffers;
303
304 /* Throttling for texture uploads and similar operations to limit memory
305 * usage by limiting the number of in-flight operations based on
306 * the estimated allocated size needed to execute those operations.
307 */
308 struct util_throttle throttle;
309 };
310
311
312 /*
313 * Get the state tracker context for the given Mesa context.
314 */
315 static inline struct st_context *
316 st_context(struct gl_context *ctx)
317 {
318 return ctx->st;
319 }
320
321
322 extern struct st_context *
323 st_create_context(gl_api api, struct pipe_context *pipe,
324 const struct gl_config *visual,
325 struct st_context *share,
326 const struct st_config_options *options,
327 bool no_error);
328
329 extern void
330 st_destroy_context(struct st_context *st);
331
332
333 extern void
334 st_init_driver_functions(struct pipe_screen *screen,
335 struct dd_function_table *functions);
336
337
338 extern void
339 st_invalidate_buffers(struct st_context *st);
340
341
342 extern uint64_t
343 st_get_active_states(struct gl_context *ctx);
344
345
346 /**
347 * Wrapper for struct gl_framebuffer.
348 * This is an opaque type to the outside world.
349 */
350 struct st_framebuffer
351 {
352 struct gl_framebuffer Base;
353
354 struct st_framebuffer_iface *iface;
355 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
356 unsigned num_statts;
357 int32_t stamp;
358 int32_t iface_stamp;
359 uint32_t iface_ID;
360
361 /* list of framebuffer objects */
362 struct list_head head;
363 };
364
365
366 #ifdef __cplusplus
367 }
368 #endif
369
370 #endif