gallium: Unify reference counting.
[mesa.git] / src / gallium / include / pipe / p_state.h
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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
29 /**
30 * @file
31 *
32 * Abstract graphics pipe state objects.
33 *
34 * Basic notes:
35 * 1. Want compact representations, so we use bitfields.
36 * 2. Put bitfields before other (GLfloat) fields.
37 */
38
39
40 #ifndef PIPE_STATE_H
41 #define PIPE_STATE_H
42
43 #include "p_compiler.h"
44 #include "p_defines.h"
45 #include "p_format.h"
46 #include "p_refcnt.h"
47 #include "p_screen.h"
48
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54
55 /**
56 * Implementation limits
57 */
58 #define PIPE_MAX_ATTRIBS 32
59 #define PIPE_MAX_CLIP_PLANES 6
60 #define PIPE_MAX_COLOR_BUFS 8
61 #define PIPE_MAX_CONSTANT 32
62 #define PIPE_MAX_SAMPLERS 16
63 #define PIPE_MAX_SHADER_INPUTS 16
64 #define PIPE_MAX_SHADER_OUTPUTS 16
65 #define PIPE_MAX_TEXTURE_LEVELS 16
66
67
68 /* fwd decls */
69 struct pipe_surface;
70
71
72 /**
73 * The driver will certainly subclass this to include actual memory
74 * management information.
75 */
76 struct pipe_buffer
77 {
78 struct pipe_reference reference;
79 struct pipe_screen *screen;
80 unsigned alignment;
81 unsigned usage;
82 unsigned size;
83 };
84
85
86 /**
87 * Primitive (point/line/tri) rasterization info
88 */
89 struct pipe_rasterizer_state
90 {
91 unsigned flatshade:1;
92 unsigned light_twoside:1;
93 unsigned front_winding:2; /**< PIPE_WINDING_x */
94 unsigned cull_mode:2; /**< PIPE_WINDING_x */
95 unsigned fill_cw:2; /**< PIPE_POLYGON_MODE_x */
96 unsigned fill_ccw:2; /**< PIPE_POLYGON_MODE_x */
97 unsigned offset_cw:1;
98 unsigned offset_ccw:1;
99 unsigned scissor:1;
100 unsigned poly_smooth:1;
101 unsigned poly_stipple_enable:1;
102 unsigned point_smooth:1;
103 unsigned point_sprite:1;
104 unsigned point_size_per_vertex:1; /**< size computed in vertex shader */
105 unsigned multisample:1; /* XXX maybe more ms state in future */
106 unsigned line_smooth:1;
107 unsigned line_stipple_enable:1;
108 unsigned line_stipple_factor:8; /**< [1..256] actually */
109 unsigned line_stipple_pattern:16;
110 unsigned line_last_pixel:1;
111 unsigned bypass_clipping:1;
112 unsigned bypass_vs:1; /**< Skip the vertex shader. Note that the shader is
113 still needed though, to indicate inputs/outputs */
114 unsigned origin_lower_left:1; /**< Is (0,0) the lower-left corner? */
115 unsigned flatshade_first:1; /**< take color attribute from the first vertex of a primitive */
116 unsigned gl_rasterization_rules:1; /**< enable tweaks for GL rasterization? */
117
118 float line_width;
119 float point_size; /**< used when no per-vertex size */
120 float point_size_min; /* XXX - temporary, will go away */
121 float point_size_max; /* XXX - temporary, will go away */
122 float offset_units;
123 float offset_scale;
124 ubyte sprite_coord_mode[PIPE_MAX_SHADER_OUTPUTS]; /**< PIPE_SPRITE_COORD_ */
125 };
126
127
128 struct pipe_poly_stipple
129 {
130 unsigned stipple[32];
131 };
132
133
134 struct pipe_viewport_state
135 {
136 float scale[4];
137 float translate[4];
138 };
139
140
141 struct pipe_scissor_state
142 {
143 unsigned minx:16;
144 unsigned miny:16;
145 unsigned maxx:16;
146 unsigned maxy:16;
147 };
148
149
150 struct pipe_clip_state
151 {
152 float ucp[PIPE_MAX_CLIP_PLANES][4];
153 unsigned nr;
154 };
155
156
157 /**
158 * Constants for vertex/fragment shaders
159 */
160 struct pipe_constant_buffer
161 {
162 struct pipe_buffer *buffer;
163 };
164
165
166 struct pipe_shader_state
167 {
168 const struct tgsi_token *tokens;
169 };
170
171
172 struct pipe_depth_state
173 {
174 unsigned enabled:1; /**< depth test enabled? */
175 unsigned writemask:1; /**< allow depth buffer writes? */
176 unsigned func:3; /**< depth test func (PIPE_FUNC_x) */
177 unsigned occlusion_count:1; /**< do occlusion counting? */
178 };
179
180
181 struct pipe_stencil_state
182 {
183 unsigned enabled:1; /**< stencil[0]: stencil enabled, stencil[1]: two-side enabled */
184 unsigned func:3; /**< PIPE_FUNC_x */
185 unsigned fail_op:3; /**< PIPE_STENCIL_OP_x */
186 unsigned zpass_op:3; /**< PIPE_STENCIL_OP_x */
187 unsigned zfail_op:3; /**< PIPE_STENCIL_OP_x */
188 ubyte ref_value;
189 ubyte valuemask;
190 ubyte writemask;
191 };
192
193
194 struct pipe_alpha_state
195 {
196 unsigned enabled:1;
197 unsigned func:3; /**< PIPE_FUNC_x */
198 float ref_value; /**< reference value */
199 };
200
201
202 struct pipe_depth_stencil_alpha_state
203 {
204 struct pipe_depth_state depth;
205 struct pipe_stencil_state stencil[2]; /**< [0] = front, [1] = back */
206 struct pipe_alpha_state alpha;
207 };
208
209
210 struct pipe_blend_state
211 {
212 unsigned blend_enable:1;
213
214 unsigned rgb_func:3; /**< PIPE_BLEND_x */
215 unsigned rgb_src_factor:5; /**< PIPE_BLENDFACTOR_x */
216 unsigned rgb_dst_factor:5; /**< PIPE_BLENDFACTOR_x */
217
218 unsigned alpha_func:3; /**< PIPE_BLEND_x */
219 unsigned alpha_src_factor:5; /**< PIPE_BLENDFACTOR_x */
220 unsigned alpha_dst_factor:5; /**< PIPE_BLENDFACTOR_x */
221
222 unsigned logicop_enable:1;
223 unsigned logicop_func:4; /**< PIPE_LOGICOP_x */
224
225 unsigned colormask:4; /**< bitmask of PIPE_MASK_R/G/B/A */
226 unsigned dither:1;
227 };
228
229
230 struct pipe_blend_color
231 {
232 float color[4];
233 };
234
235
236 struct pipe_framebuffer_state
237 {
238 unsigned width, height;
239
240 /** multiple colorbuffers for multiple render targets */
241 unsigned nr_cbufs;
242 struct pipe_surface *cbufs[PIPE_MAX_COLOR_BUFS];
243
244 struct pipe_surface *zsbuf; /**< Z/stencil buffer */
245 };
246
247
248 /**
249 * Texture sampler state.
250 */
251 struct pipe_sampler_state
252 {
253 unsigned wrap_s:3; /**< PIPE_TEX_WRAP_x */
254 unsigned wrap_t:3; /**< PIPE_TEX_WRAP_x */
255 unsigned wrap_r:3; /**< PIPE_TEX_WRAP_x */
256 unsigned min_img_filter:2; /**< PIPE_TEX_FILTER_x */
257 unsigned min_mip_filter:2; /**< PIPE_TEX_MIPFILTER_x */
258 unsigned mag_img_filter:2; /**< PIPE_TEX_FILTER_x */
259 unsigned compare_mode:1; /**< PIPE_TEX_COMPARE_x */
260 unsigned compare_func:3; /**< PIPE_FUNC_x */
261 unsigned normalized_coords:1; /**< Are coords normalized to [0,1]? */
262 unsigned prefilter:4; /**< Wierd sampling state exposed by some api's */
263 float shadow_ambient; /**< shadow test fail color/intensity */
264 float lod_bias; /**< LOD/lambda bias */
265 float min_lod, max_lod; /**< LOD clamp range, after bias */
266 float border_color[4];
267 float max_anisotropy;
268 };
269
270
271 /**
272 * 2D surface. This is basically a view into a memory buffer.
273 * May be a renderbuffer, texture mipmap level, etc.
274 */
275 struct pipe_surface
276 {
277 struct pipe_reference reference;
278 enum pipe_format format; /**< PIPE_FORMAT_x */
279 unsigned status; /**< PIPE_SURFACE_STATUS_x */
280 unsigned clear_value; /**< XXX may be temporary */
281 unsigned width; /**< logical width in pixels */
282 unsigned height; /**< logical height in pixels */
283 unsigned layout; /**< PIPE_SURFACE_LAYOUT_x */
284 unsigned offset; /**< offset from start of buffer, in bytes */
285 unsigned usage; /**< PIPE_BUFFER_USAGE_* */
286
287 struct pipe_texture *texture; /**< texture into which this is a view */
288 unsigned face;
289 unsigned level;
290 unsigned zslice;
291 };
292
293
294 /**
295 * Transfer object. For data transfer to/from a texture.
296 */
297 struct pipe_transfer
298 {
299 enum pipe_format format; /**< PIPE_FORMAT_x */
300 unsigned x; /**< x offset from start of texture image */
301 unsigned y; /**< y offset from start of texture image */
302 unsigned width; /**< logical width in pixels */
303 unsigned height; /**< logical height in pixels */
304 struct pipe_format_block block;
305 unsigned nblocksx; /**< allocated width in blocks */
306 unsigned nblocksy; /**< allocated height in blocks */
307 unsigned stride; /**< stride in bytes between rows of blocks */
308 unsigned usage; /**< PIPE_TRANSFER_* */
309
310 struct pipe_texture *texture; /**< texture to transfer to/from */
311 unsigned face;
312 unsigned level;
313 unsigned zslice;
314 };
315
316
317 /**
318 * Texture object.
319 */
320 struct pipe_texture
321 {
322 struct pipe_reference reference;
323
324 enum pipe_texture_target target; /**< PIPE_TEXTURE_x */
325 enum pipe_format format; /**< PIPE_FORMAT_x */
326
327 unsigned width[PIPE_MAX_TEXTURE_LEVELS];
328 unsigned height[PIPE_MAX_TEXTURE_LEVELS];
329 unsigned depth[PIPE_MAX_TEXTURE_LEVELS];
330
331 struct pipe_format_block block;
332 unsigned nblocksx[PIPE_MAX_TEXTURE_LEVELS]; /**< allocated width in blocks */
333 unsigned nblocksy[PIPE_MAX_TEXTURE_LEVELS]; /**< allocated height in blocks */
334
335 unsigned last_level:8; /**< Index of last mipmap level present/defined */
336 unsigned compressed:1;
337
338 unsigned nr_samples:8; /**< for multisampled surfaces, nr of samples */
339
340 unsigned tex_usage; /* PIPE_TEXTURE_USAGE_* */
341
342 struct pipe_screen *screen; /**< screen that this texture belongs to */
343 };
344
345
346 /**
347 * A vertex buffer. Typically, all the vertex data/attributes for
348 * drawing something will be in one buffer. But it's also possible, for
349 * example, to put colors in one buffer and texcoords in another.
350 */
351 struct pipe_vertex_buffer
352 {
353 unsigned stride; /**< stride to same attrib in next vertex, in bytes */
354 unsigned max_index; /**< number of vertices in this buffer */
355 unsigned buffer_offset; /**< offset to start of data in buffer, in bytes */
356 struct pipe_buffer *buffer; /**< the actual buffer */
357 };
358
359
360 /**
361 * Information to describe a vertex attribute (position, color, etc)
362 */
363 struct pipe_vertex_element
364 {
365 /** Offset of this attribute, in bytes, from the start of the vertex */
366 unsigned src_offset;
367
368 /** Which vertex_buffer (as given to pipe->set_vertex_buffer()) does
369 * this attribute live in?
370 */
371 unsigned vertex_buffer_index:8;
372 unsigned nr_components:8;
373
374 enum pipe_format src_format; /**< PIPE_FORMAT_* */
375 };
376
377
378 /* Reference counting helper functions */
379 static INLINE void
380 pipe_buffer_reference(struct pipe_buffer **ptr, struct pipe_buffer *buf)
381 {
382 struct pipe_buffer *old_buf = *ptr;
383
384 if (pipe_reference((struct pipe_reference **)ptr, &buf->reference))
385 old_buf->screen->buffer_destroy(old_buf);
386 }
387
388 static INLINE void
389 pipe_surface_reference(struct pipe_surface **ptr, struct pipe_surface *surf)
390 {
391 struct pipe_surface *old_surf = *ptr;
392
393 if (pipe_reference((struct pipe_reference **)ptr, &surf->reference))
394 old_surf->texture->screen->tex_surface_destroy(old_surf);
395 }
396
397 static INLINE void
398 pipe_texture_reference(struct pipe_texture **ptr, struct pipe_texture *tex)
399 {
400 struct pipe_texture *old_tex = *ptr;
401
402 if (pipe_reference((struct pipe_reference **)ptr, &tex->reference))
403 old_tex->screen->texture_destroy(old_tex);
404 }
405
406
407 #ifdef __cplusplus
408 }
409 #endif
410
411 #endif