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