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