1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2009 Intel Corporation.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include "main/glheader.h"
30 #include "main/enums.h"
31 #include "main/image.h"
32 #include "main/mtypes.h"
33 #include "main/arrayobj.h"
34 #include "main/attrib.h"
35 #include "main/blend.h"
36 #include "main/bufferobj.h"
37 #include "main/buffers.h"
38 #include "main/depth.h"
39 #include "main/enable.h"
40 #include "main/macros.h"
41 #include "main/matrix.h"
42 #include "main/polygon.h"
43 #include "main/texstate.h"
44 #include "main/shaders.h"
45 #include "main/stencil.h"
46 #include "main/varray.h"
47 #include "glapi/dispatch.h"
48 #include "swrast/swrast.h"
50 #include "intel_context.h"
51 #include "intel_blit.h"
52 #include "intel_chipset.h"
53 #include "intel_clear.h"
54 #include "intel_fbo.h"
55 #include "intel_pixel.h"
56 #include "intel_regions.h"
58 #define FILE_DEBUG_FLAG DEBUG_BLIT
60 #define TRI_CLEAR_COLOR_BITS (BUFFER_BIT_BACK_LEFT | \
61 BUFFER_BIT_FRONT_LEFT | \
73 * Per-context one-time init of things for intl_clear_tris().
74 * Basically set up a private array object for vertex/color arrays.
77 init_clear(GLcontext
*ctx
)
79 struct intel_context
*intel
= intel_context(ctx
);
80 struct gl_array_object
*arraySave
= NULL
;
81 const GLuint arrayBuffer
= ctx
->Array
.ArrayBufferObj
->Name
;
82 const GLuint elementBuffer
= ctx
->Array
.ElementArrayBufferObj
->Name
;
84 /* create new array object */
85 intel
->clear
.arrayObj
= _mesa_new_array_object(ctx
, ~0);
87 /* save current array object, bind new one */
88 _mesa_reference_array_object(ctx
, &arraySave
, ctx
->Array
.ArrayObj
);
89 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, intel
->clear
.arrayObj
);
91 /* one-time setup of vertex arrays (pos, color) */
92 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, 0);
93 _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
94 _mesa_ColorPointer(4, GL_FLOAT
, 4 * sizeof(GLfloat
), intel
->clear
.color
);
95 _mesa_VertexPointer(3, GL_FLOAT
, 3 * sizeof(GLfloat
), intel
->clear
.vertices
);
96 _mesa_Enable(GL_COLOR_ARRAY
);
97 _mesa_Enable(GL_VERTEX_ARRAY
);
99 /* restore original array object */
100 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, arraySave
);
101 _mesa_reference_array_object(ctx
, &arraySave
, NULL
);
103 /* restore original buffer objects */
104 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, arrayBuffer
);
105 _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB
, elementBuffer
);
111 * Perform glClear where mask contains only color, depth, and/or stencil.
113 * The implementation is based on calling into Mesa to set GL state and
114 * performing normal triangle rendering. The intent of this path is to
115 * have as generic a path as possible, so that any driver could make use of
119 intel_clear_tris(GLcontext
*ctx
, GLbitfield mask
)
121 struct intel_context
*intel
= intel_context(ctx
);
123 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
125 GLboolean saved_fp_enable
= GL_FALSE
, saved_vp_enable
= GL_FALSE
;
126 GLuint saved_shader_program
= 0;
127 unsigned int saved_active_texture
;
128 struct gl_array_object
*arraySave
= NULL
;
130 if (!intel
->clear
.arrayObj
)
133 assert((mask
& ~(TRI_CLEAR_COLOR_BITS
| BUFFER_BIT_DEPTH
|
134 BUFFER_BIT_STENCIL
)) == 0);
136 _mesa_PushAttrib(GL_COLOR_BUFFER_BIT
|
137 GL_DEPTH_BUFFER_BIT
|
140 GL_STENCIL_BUFFER_BIT
|
143 saved_active_texture
= ctx
->Texture
.CurrentUnit
;
145 /* Disable existing GL state we don't want to apply to a clear. */
146 _mesa_Disable(GL_ALPHA_TEST
);
147 _mesa_Disable(GL_BLEND
);
148 _mesa_Disable(GL_CULL_FACE
);
149 _mesa_Disable(GL_FOG
);
150 _mesa_Disable(GL_POLYGON_SMOOTH
);
151 _mesa_Disable(GL_POLYGON_STIPPLE
);
152 _mesa_Disable(GL_POLYGON_OFFSET_FILL
);
153 _mesa_Disable(GL_LIGHTING
);
154 _mesa_Disable(GL_CLIP_PLANE0
);
155 _mesa_Disable(GL_CLIP_PLANE1
);
156 _mesa_Disable(GL_CLIP_PLANE2
);
157 _mesa_Disable(GL_CLIP_PLANE3
);
158 _mesa_Disable(GL_CLIP_PLANE4
);
159 _mesa_Disable(GL_CLIP_PLANE5
);
160 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
161 if (ctx
->Extensions
.ARB_fragment_program
&& ctx
->FragmentProgram
.Enabled
) {
162 saved_fp_enable
= GL_TRUE
;
163 _mesa_Disable(GL_FRAGMENT_PROGRAM_ARB
);
165 if (ctx
->Extensions
.ARB_vertex_program
&& ctx
->VertexProgram
.Enabled
) {
166 saved_vp_enable
= GL_TRUE
;
167 _mesa_Disable(GL_VERTEX_PROGRAM_ARB
);
169 if (ctx
->Extensions
.ARB_shader_objects
&& ctx
->Shader
.CurrentProgram
) {
170 saved_shader_program
= ctx
->Shader
.CurrentProgram
->Name
;
171 _mesa_UseProgramObjectARB(0);
174 if (ctx
->Texture
._EnabledUnits
!= 0) {
177 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
178 _mesa_ActiveTextureARB(GL_TEXTURE0
+ i
);
179 _mesa_Disable(GL_TEXTURE_1D
);
180 _mesa_Disable(GL_TEXTURE_2D
);
181 _mesa_Disable(GL_TEXTURE_3D
);
182 if (ctx
->Extensions
.ARB_texture_cube_map
)
183 _mesa_Disable(GL_TEXTURE_CUBE_MAP_ARB
);
184 if (ctx
->Extensions
.NV_texture_rectangle
)
185 _mesa_Disable(GL_TEXTURE_RECTANGLE_NV
);
186 if (ctx
->Extensions
.MESA_texture_array
) {
187 _mesa_Disable(GL_TEXTURE_1D_ARRAY_EXT
);
188 _mesa_Disable(GL_TEXTURE_2D_ARRAY_EXT
);
193 /* save current array object, bind our private one */
194 _mesa_reference_array_object(ctx
, &arraySave
, ctx
->Array
.ArrayObj
);
195 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, intel
->clear
.arrayObj
);
197 intel_meta_set_passthrough_transform(intel
);
199 for (i
= 0; i
< 4; i
++) {
200 COPY_4FV(intel
->clear
.color
[i
], ctx
->Color
.ClearColor
);
203 /* convert clear Z from [0,1] to NDC coord in [-1,1] */
204 dst_z
= -1.0 + 2.0 * ctx
->Depth
.Clear
;
206 /* Prepare the vertices, which are the same regardless of which buffer we're
209 intel
->clear
.vertices
[0][0] = fb
->_Xmin
;
210 intel
->clear
.vertices
[0][1] = fb
->_Ymin
;
211 intel
->clear
.vertices
[0][2] = dst_z
;
212 intel
->clear
.vertices
[1][0] = fb
->_Xmax
;
213 intel
->clear
.vertices
[1][1] = fb
->_Ymin
;
214 intel
->clear
.vertices
[1][2] = dst_z
;
215 intel
->clear
.vertices
[2][0] = fb
->_Xmax
;
216 intel
->clear
.vertices
[2][1] = fb
->_Ymax
;
217 intel
->clear
.vertices
[2][2] = dst_z
;
218 intel
->clear
.vertices
[3][0] = fb
->_Xmin
;
219 intel
->clear
.vertices
[3][1] = fb
->_Ymax
;
220 intel
->clear
.vertices
[3][2] = dst_z
;
223 GLuint this_mask
= 0;
226 color_bit
= _mesa_ffs(mask
& TRI_CLEAR_COLOR_BITS
);
228 this_mask
|= (1 << (color_bit
- 1));
230 /* Clear depth/stencil in the same pass as color. */
231 this_mask
|= (mask
& (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
));
233 /* Select the current color buffer and use the color write mask if
234 * we have one, otherwise don't write any color channels.
236 if (this_mask
& BUFFER_BIT_FRONT_LEFT
)
237 _mesa_DrawBuffer(GL_FRONT_LEFT
);
238 else if (this_mask
& BUFFER_BIT_BACK_LEFT
)
239 _mesa_DrawBuffer(GL_BACK_LEFT
);
240 else if (color_bit
!= 0)
241 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
+
242 (color_bit
- BUFFER_COLOR0
- 1));
244 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
246 /* Control writing of the depth clear value to depth. */
247 if (this_mask
& BUFFER_BIT_DEPTH
) {
248 _mesa_DepthFunc(GL_ALWAYS
);
249 _mesa_Enable(GL_DEPTH_TEST
);
251 _mesa_Disable(GL_DEPTH_TEST
);
252 _mesa_DepthMask(GL_FALSE
);
255 /* Control writing of the stencil clear value to stencil. */
256 if (this_mask
& BUFFER_BIT_STENCIL
) {
257 _mesa_Enable(GL_STENCIL_TEST
);
258 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
259 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
260 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
262 ctx
->Stencil
.WriteMask
[0]);
264 _mesa_Disable(GL_STENCIL_TEST
);
267 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
272 intel_meta_restore_transform(intel
);
274 _mesa_ActiveTextureARB(GL_TEXTURE0
+ saved_active_texture
);
276 _mesa_Enable(GL_FRAGMENT_PROGRAM_ARB
);
278 _mesa_Enable(GL_VERTEX_PROGRAM_ARB
);
280 if (saved_shader_program
)
281 _mesa_UseProgramObjectARB(saved_shader_program
);
285 /* restore current array object */
286 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, arraySave
);
287 _mesa_reference_array_object(ctx
, &arraySave
, NULL
);
290 static const char *buffer_names
[] = {
291 [BUFFER_FRONT_LEFT
] = "front",
292 [BUFFER_BACK_LEFT
] = "back",
293 [BUFFER_FRONT_RIGHT
] = "front right",
294 [BUFFER_BACK_RIGHT
] = "back right",
295 [BUFFER_DEPTH
] = "depth",
296 [BUFFER_STENCIL
] = "stencil",
297 [BUFFER_ACCUM
] = "accum",
298 [BUFFER_AUX0
] = "aux0",
299 [BUFFER_COLOR0
] = "color0",
300 [BUFFER_COLOR1
] = "color1",
301 [BUFFER_COLOR2
] = "color2",
302 [BUFFER_COLOR3
] = "color3",
303 [BUFFER_COLOR4
] = "color4",
304 [BUFFER_COLOR5
] = "color5",
305 [BUFFER_COLOR6
] = "color6",
306 [BUFFER_COLOR7
] = "color7",
310 * Called by ctx->Driver.Clear.
313 intelClear(GLcontext
*ctx
, GLbitfield mask
)
315 const GLuint colorMask
= *((GLuint
*) & ctx
->Color
.ColorMask
);
316 GLbitfield tri_mask
= 0;
317 GLbitfield blit_mask
= 0;
318 GLbitfield swrast_mask
= 0;
319 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
323 fprintf(stderr
, "%s\n", __FUNCTION__
);
325 /* HW color buffers (front, back, aux, generic FBO, etc) */
326 if (colorMask
== ~0) {
327 /* clear all R,G,B,A */
328 /* XXX FBO: need to check if colorbuffers are software RBOs! */
329 blit_mask
|= (mask
& BUFFER_BITS_COLOR
);
332 /* glColorMask in effect */
333 tri_mask
|= (mask
& (BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
));
337 if (mask
& BUFFER_BIT_STENCIL
) {
338 const struct intel_region
*stencilRegion
339 = intel_get_rb_region(fb
, BUFFER_STENCIL
);
341 /* have hw stencil */
342 if (stencilRegion
->tiling
== I915_TILING_Y
||
343 (ctx
->Stencil
.WriteMask
[0] & 0xff) != 0xff) {
344 /* We have to use the 3D engine if we're clearing a partial mask
345 * of the stencil buffer, or if we're on a 965 which has a tiled
346 * depth/stencil buffer in a layout we can't blit to.
348 tri_mask
|= BUFFER_BIT_STENCIL
;
351 /* clearing all stencil bits, use blitting */
352 blit_mask
|= BUFFER_BIT_STENCIL
;
358 if (mask
& BUFFER_BIT_DEPTH
) {
359 const struct intel_region
*irb
= intel_get_rb_region(fb
, BUFFER_DEPTH
);
361 /* clear depth with whatever method is used for stencil (see above) */
362 if (irb
->tiling
== I915_TILING_Y
|| tri_mask
& BUFFER_BIT_STENCIL
)
363 tri_mask
|= BUFFER_BIT_DEPTH
;
365 blit_mask
|= BUFFER_BIT_DEPTH
;
368 /* If we're doing a tri pass for depth/stencil, include a likely color
371 if (mask
& (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
)) {
372 int color_bit
= _mesa_ffs(mask
& TRI_CLEAR_COLOR_BITS
);
373 if (color_bit
!= 0) {
374 tri_mask
|= blit_mask
& (1 << (color_bit
- 1));
375 blit_mask
&= ~(1 << (color_bit
- 1));
379 /* SW fallback clearing */
380 swrast_mask
= mask
& ~tri_mask
& ~blit_mask
;
382 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
383 GLuint bufBit
= 1 << i
;
384 if ((blit_mask
| tri_mask
) & bufBit
) {
385 if (!fb
->Attachment
[i
].Renderbuffer
->ClassID
) {
386 blit_mask
&= ~bufBit
;
388 swrast_mask
|= bufBit
;
394 if (INTEL_DEBUG
& DEBUG_BLIT
) {
396 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
397 if (blit_mask
& (1 << i
))
398 DBG(" %s", buffer_names
[i
]);
402 intelClearWithBlit(ctx
, blit_mask
);
406 if (INTEL_DEBUG
& DEBUG_BLIT
) {
408 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
409 if (tri_mask
& (1 << i
))
410 DBG(" %s", buffer_names
[i
]);
414 intel_clear_tris(ctx
, tri_mask
);
418 if (INTEL_DEBUG
& DEBUG_BLIT
) {
419 DBG("swrast clear:");
420 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
421 if (swrast_mask
& (1 << i
))
422 DBG(" %s", buffer_names
[i
]);
426 _swrast_Clear(ctx
, swrast_mask
);
432 intelInitClearFuncs(struct dd_function_table
*functions
)
434 functions
->Clear
= intelClear
;