st/mesa: use PK2H/UP2H when supported
[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
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41
42 struct bitmap_cache;
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 #define ST_NEW_MESA (1 << 0) /* Mesa state has changed */
54 #define ST_NEW_FRAGMENT_PROGRAM (1 << 1)
55 #define ST_NEW_VERTEX_PROGRAM (1 << 2)
56 #define ST_NEW_FRAMEBUFFER (1 << 3)
57 #define ST_NEW_TESS_STATE (1 << 4)
58 #define ST_NEW_GEOMETRY_PROGRAM (1 << 5)
59 #define ST_NEW_VERTEX_ARRAYS (1 << 6)
60 #define ST_NEW_RASTERIZER (1 << 7)
61 #define ST_NEW_UNIFORM_BUFFER (1 << 8)
62 #define ST_NEW_TESSCTRL_PROGRAM (1 << 9)
63 #define ST_NEW_TESSEVAL_PROGRAM (1 << 10)
64 #define ST_NEW_SAMPLER_VIEWS (1 << 11)
65
66
67 struct st_state_flags {
68 GLuint mesa;
69 uint64_t st;
70 };
71
72 struct st_tracked_state {
73 const char *name;
74 struct st_state_flags dirty;
75 void (*update)( struct st_context *st );
76 };
77
78
79
80 struct st_context
81 {
82 struct st_context_iface iface;
83
84 struct gl_context *ctx;
85
86 struct pipe_context *pipe;
87
88 struct u_upload_mgr *uploader, *indexbuf_uploader, *constbuf_uploader;
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
106 /**
107 * If a shader can be created when we get its source.
108 * This means it has only 1 variant, not counting glBitmap and
109 * glDrawPixels.
110 */
111 boolean shader_has_one_variant[MESA_SHADER_STAGES];
112
113 boolean needs_texcoord_semantic;
114 boolean apply_texture_swizzle_to_border_color;
115
116 /* On old libGL's for linux we need to invalidate the drawables
117 * on glViewpport calls, this is set via a option.
118 */
119 boolean invalidate_on_gl_viewport;
120
121 boolean vertex_array_out_of_memory;
122
123 /* Some state is contained in constant objects.
124 * Other state is just parameter values.
125 */
126 struct {
127 struct pipe_blend_state blend;
128 struct pipe_depth_stencil_alpha_state depth_stencil;
129 struct pipe_rasterizer_state rasterizer;
130 struct pipe_sampler_state samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
131 GLuint num_samplers[PIPE_SHADER_TYPES];
132 struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
133 GLuint num_sampler_views[PIPE_SHADER_TYPES];
134 struct pipe_clip_state clip;
135 struct {
136 void *ptr;
137 unsigned size;
138 } constants[PIPE_SHADER_TYPES];
139 struct pipe_framebuffer_state framebuffer;
140 struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
141 struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
142 unsigned sample_mask;
143
144 GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */
145
146 GLuint fb_orientation;
147 } state;
148
149 char vendor[100];
150 char renderer[100];
151
152 struct st_state_flags dirty;
153
154 GLboolean vertdata_edgeflags;
155 GLboolean edgeflag_culls_prims;
156
157 /** Mapping from VARYING_SLOT_x to post-transformed vertex slot */
158 const GLuint *vertex_result_to_slot;
159
160 struct st_vertex_program *vp; /**< Currently bound vertex program */
161 struct st_fragment_program *fp; /**< Currently bound fragment program */
162 struct st_geometry_program *gp; /**< Currently bound geometry program */
163 struct st_tessctrl_program *tcp; /**< Currently bound tess control program */
164 struct st_tesseval_program *tep; /**< Currently bound tess eval program */
165
166 struct st_vp_variant *vp_variant;
167 struct st_fp_variant *fp_variant;
168 struct st_gp_variant *gp_variant;
169 struct st_tcp_variant *tcp_variant;
170 struct st_tep_variant *tep_variant;
171
172 struct gl_texture_object *default_texture;
173
174 struct {
175 struct pipe_resource *pixelmap_texture;
176 struct pipe_sampler_view *pixelmap_sampler_view;
177 } pixel_xfer;
178
179 /** for glBitmap */
180 struct {
181 struct pipe_rasterizer_state rasterizer;
182 struct pipe_sampler_state samplers[2];
183 enum pipe_format tex_format;
184 void *vs;
185 struct bitmap_cache *cache;
186 } bitmap;
187
188 /** for glDraw/CopyPixels */
189 struct {
190 void *zs_shaders[4];
191 void *vert_shaders[2]; /**< ureg shaders */
192 } drawpix;
193
194 /** for glClear */
195 struct {
196 struct pipe_rasterizer_state raster;
197 struct pipe_viewport_state viewport;
198 void *vs;
199 void *fs;
200 void *vs_layered;
201 void *gs_layered;
202 } clear;
203
204 /** used for anything using util_draw_vertex_buffer */
205 struct pipe_vertex_element velems_util_draw[3];
206
207 void *passthrough_fs; /**< simple pass-through frag shader */
208
209 enum pipe_texture_target internal_target;
210
211 struct cso_context *cso_context;
212
213 void *winsys_drawable_handle;
214
215 /* The number of vertex buffers from the last call of validate_arrays. */
216 unsigned last_num_vbuffers;
217
218 int32_t draw_stamp;
219 int32_t read_stamp;
220
221 struct st_config_options options;
222
223 struct st_perf_monitor_group *perfmon;
224 };
225
226
227 /* Need this so that we can implement Mesa callbacks in this module.
228 */
229 static inline struct st_context *st_context(struct gl_context *ctx)
230 {
231 return ctx->st;
232 }
233
234
235 /**
236 * Wrapper for struct gl_framebuffer.
237 * This is an opaque type to the outside world.
238 */
239 struct st_framebuffer
240 {
241 struct gl_framebuffer Base;
242 void *Private;
243
244 struct st_framebuffer_iface *iface;
245 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
246 unsigned num_statts;
247 int32_t stamp;
248 int32_t iface_stamp;
249 };
250
251
252 extern void st_init_driver_functions(struct pipe_screen *screen,
253 struct dd_function_table *functions);
254
255 void st_invalidate_state(struct gl_context * ctx, GLuint new_state);
256
257
258
259 #define Y_0_TOP 1
260 #define Y_0_BOTTOM 2
261
262 static inline GLuint
263 st_fb_orientation(const struct gl_framebuffer *fb)
264 {
265 if (fb && _mesa_is_winsys_fbo(fb)) {
266 /* Drawing into a window (on-screen buffer).
267 *
268 * Negate Y scale to flip image vertically.
269 * The NDC Y coords prior to viewport transformation are in the range
270 * [y=-1=bottom, y=1=top]
271 * Hardware window coords are in the range [y=0=top, y=H-1=bottom] where
272 * H is the window height.
273 * Use the viewport transformation to invert Y.
274 */
275 return Y_0_TOP;
276 }
277 else {
278 /* Drawing into user-created FBO (very likely a texture).
279 *
280 * For textures, T=0=Bottom, so by extension Y=0=Bottom for rendering.
281 */
282 return Y_0_BOTTOM;
283 }
284 }
285
286
287 static inline unsigned
288 st_shader_stage_to_ptarget(gl_shader_stage stage)
289 {
290 switch (stage) {
291 case MESA_SHADER_VERTEX:
292 return PIPE_SHADER_VERTEX;
293 case MESA_SHADER_FRAGMENT:
294 return PIPE_SHADER_FRAGMENT;
295 case MESA_SHADER_GEOMETRY:
296 return PIPE_SHADER_GEOMETRY;
297 case MESA_SHADER_TESS_CTRL:
298 return PIPE_SHADER_TESS_CTRL;
299 case MESA_SHADER_TESS_EVAL:
300 return PIPE_SHADER_TESS_EVAL;
301 case MESA_SHADER_COMPUTE:
302 return PIPE_SHADER_COMPUTE;
303 }
304
305 assert(!"should not be reached");
306 return PIPE_SHADER_VERTEX;
307 }
308
309
310 /** clear-alloc a struct-sized object, with casting */
311 #define ST_CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
312
313
314 extern struct st_context *
315 st_create_context(gl_api api, struct pipe_context *pipe,
316 const struct gl_config *visual,
317 struct st_context *share,
318 const struct st_config_options *options);
319
320 extern void
321 st_destroy_context(struct st_context *st);
322
323
324 #ifdef __cplusplus
325 }
326 #endif
327
328 #endif