1 /**************************************************************************
3 * Copyright 2006 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/arrayobj.h"
30 #include "main/attrib.h"
31 #include "main/blend.h"
32 #include "main/bufferobj.h"
33 #include "main/buffers.h"
34 #include "main/depth.h"
35 #include "main/enable.h"
36 #include "main/matrix.h"
37 #include "main/macros.h"
38 #include "main/polygon.h"
39 #include "main/shaders.h"
40 #include "main/stencil.h"
41 #include "main/texstate.h"
42 #include "main/varray.h"
43 #include "main/viewport.h"
44 #include "shader/arbprogram.h"
45 #include "shader/program.h"
46 #include "dri_metaops.h"
49 meta_set_passthrough_transform(struct dri_metaops
*meta
)
51 GLcontext
*ctx
= meta
->ctx
;
53 meta
->saved_vp_x
= ctx
->Viewport
.X
;
54 meta
->saved_vp_y
= ctx
->Viewport
.Y
;
55 meta
->saved_vp_width
= ctx
->Viewport
.Width
;
56 meta
->saved_vp_height
= ctx
->Viewport
.Height
;
57 meta
->saved_matrix_mode
= ctx
->Transform
.MatrixMode
;
59 meta
->internal_viewport_call
= GL_TRUE
;
60 _mesa_Viewport(0, 0, ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
61 meta
->internal_viewport_call
= GL_FALSE
;
63 _mesa_MatrixMode(GL_PROJECTION
);
66 _mesa_Ortho(0, ctx
->DrawBuffer
->Width
, 0, ctx
->DrawBuffer
->Height
, 1, -1);
68 _mesa_MatrixMode(GL_MODELVIEW
);
74 meta_restore_transform(struct dri_metaops
*meta
)
76 _mesa_MatrixMode(GL_PROJECTION
);
78 _mesa_MatrixMode(GL_MODELVIEW
);
81 _mesa_MatrixMode(meta
->saved_matrix_mode
);
83 meta
->internal_viewport_call
= GL_TRUE
;
84 _mesa_Viewport(meta
->saved_vp_x
, meta
->saved_vp_y
,
85 meta
->saved_vp_width
, meta
->saved_vp_height
);
86 meta
->internal_viewport_call
= GL_FALSE
;
91 * Set up a vertex program to pass through the position and first texcoord
95 meta_set_passthrough_vertex_program(struct dri_metaops
*meta
)
97 GLcontext
*ctx
= meta
->ctx
;
98 static const char *vp
=
101 "DP4 vertexClip.x, state.matrix.mvp.row[0], vertex.position;\n"
102 "DP4 vertexClip.y, state.matrix.mvp.row[1], vertex.position;\n"
103 "DP4 vertexClip.z, state.matrix.mvp.row[2], vertex.position;\n"
104 "DP4 vertexClip.w, state.matrix.mvp.row[3], vertex.position;\n"
105 "MOV result.position, vertexClip;\n"
106 "MOV result.texcoord[0], vertex.texcoord[0];\n"
107 "MOV result.color, vertex.color;\n"
110 assert(meta
->saved_vp
== NULL
);
112 _mesa_reference_vertprog(ctx
, &meta
->saved_vp
,
113 ctx
->VertexProgram
.Current
);
114 if (meta
->passthrough_vp
== NULL
) {
116 _mesa_GenPrograms(1, &prog_name
);
117 _mesa_BindProgram(GL_VERTEX_PROGRAM_ARB
, prog_name
);
118 _mesa_ProgramStringARB(GL_VERTEX_PROGRAM_ARB
,
119 GL_PROGRAM_FORMAT_ASCII_ARB
,
120 strlen(vp
), (const GLubyte
*)vp
);
121 _mesa_reference_vertprog(ctx
, &meta
->passthrough_vp
,
122 ctx
->VertexProgram
.Current
);
123 _mesa_DeletePrograms(1, &prog_name
);
126 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
127 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
128 meta
->passthrough_vp
);
129 ctx
->Driver
.BindProgram(ctx
, GL_VERTEX_PROGRAM_ARB
,
130 &meta
->passthrough_vp
->Base
);
132 meta
->saved_vp_enable
= ctx
->VertexProgram
.Enabled
;
133 _mesa_Enable(GL_VERTEX_PROGRAM_ARB
);
137 * Restores the previous vertex program after
138 * meta_set_passthrough_vertex_program()
141 meta_restore_vertex_program(struct dri_metaops
*meta
)
143 GLcontext
*ctx
= meta
->ctx
;
145 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
146 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
148 _mesa_reference_vertprog(ctx
, &meta
->saved_vp
, NULL
);
149 ctx
->Driver
.BindProgram(ctx
, GL_VERTEX_PROGRAM_ARB
,
150 &ctx
->VertexProgram
.Current
->Base
);
152 if (!meta
->saved_vp_enable
)
153 _mesa_Disable(GL_VERTEX_PROGRAM_ARB
);
157 * Binds the given program string to GL_FRAGMENT_PROGRAM_ARB, caching the
161 meta_set_fragment_program(struct dri_metaops
*meta
,
162 struct gl_fragment_program
**prog
,
163 const char *prog_string
)
165 GLcontext
*ctx
= meta
->ctx
;
166 assert(meta
->saved_fp
== NULL
);
168 _mesa_reference_fragprog(ctx
, &meta
->saved_fp
,
169 ctx
->FragmentProgram
.Current
);
172 _mesa_GenPrograms(1, &prog_name
);
173 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, prog_name
);
174 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
,
175 GL_PROGRAM_FORMAT_ASCII_ARB
,
176 strlen(prog_string
), (const GLubyte
*)prog_string
);
177 _mesa_reference_fragprog(ctx
, prog
, ctx
->FragmentProgram
.Current
);
178 /* Note that DeletePrograms unbinds the program on us */
179 _mesa_DeletePrograms(1, &prog_name
);
182 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
183 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, *prog
);
184 ctx
->Driver
.BindProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, &((*prog
)->Base
));
186 meta
->saved_fp_enable
= ctx
->FragmentProgram
.Enabled
;
187 _mesa_Enable(GL_FRAGMENT_PROGRAM_ARB
);
191 * Restores the previous fragment program after
192 * meta_set_fragment_program()
195 meta_restore_fragment_program(struct dri_metaops
*meta
)
197 GLcontext
*ctx
= meta
->ctx
;
199 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
200 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
202 _mesa_reference_fragprog(ctx
, &meta
->saved_fp
, NULL
);
203 ctx
->Driver
.BindProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
204 &ctx
->FragmentProgram
.Current
->Base
);
206 if (!meta
->saved_fp_enable
)
207 _mesa_Disable(GL_FRAGMENT_PROGRAM_ARB
);
210 static const float default_texcoords
[4][2] = { { 0.0, 0.0 },
216 meta_set_default_texrect(struct dri_metaops
*meta
)
218 GLcontext
*ctx
= meta
->ctx
;
219 struct gl_client_array
*old_texcoord_array
;
221 meta
->saved_active_texture
= ctx
->Texture
.CurrentUnit
;
222 if (meta
->saved_array_vbo
== NULL
) {
223 _mesa_reference_buffer_object(ctx
, &meta
->saved_array_vbo
,
224 ctx
->Array
.ArrayBufferObj
);
227 old_texcoord_array
= &ctx
->Array
.ArrayObj
->TexCoord
[0];
228 meta
->saved_texcoord_type
= old_texcoord_array
->Type
;
229 meta
->saved_texcoord_size
= old_texcoord_array
->Size
;
230 meta
->saved_texcoord_stride
= old_texcoord_array
->Stride
;
231 meta
->saved_texcoord_enable
= old_texcoord_array
->Enabled
;
232 meta
->saved_texcoord_ptr
= old_texcoord_array
->Ptr
;
233 _mesa_reference_buffer_object(ctx
, &meta
->saved_texcoord_vbo
,
234 old_texcoord_array
->BufferObj
);
236 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
238 if (meta
->texcoord_vbo
== NULL
) {
241 _mesa_GenBuffersARB(1, &vbo_name
);
242 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo_name
);
243 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(default_texcoords
),
244 default_texcoords
, GL_STATIC_DRAW_ARB
);
245 _mesa_reference_buffer_object(ctx
, &meta
->texcoord_vbo
,
246 ctx
->Array
.ArrayBufferObj
);
248 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
,
249 meta
->texcoord_vbo
->Name
);
251 _mesa_TexCoordPointer(2, GL_FLOAT
, 2 * sizeof(GLfloat
), NULL
);
253 _mesa_Enable(GL_TEXTURE_COORD_ARRAY
);
257 meta_restore_texcoords(struct dri_metaops
*meta
)
259 GLcontext
*ctx
= meta
->ctx
;
261 /* Restore the old TexCoordPointer */
262 if (meta
->saved_texcoord_vbo
) {
263 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
,
264 meta
->saved_texcoord_vbo
->Name
);
265 _mesa_reference_buffer_object(ctx
, &meta
->saved_texcoord_vbo
, NULL
);
267 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, 0);
270 _mesa_TexCoordPointer(meta
->saved_texcoord_size
,
271 meta
->saved_texcoord_type
,
272 meta
->saved_texcoord_stride
,
273 meta
->saved_texcoord_ptr
);
274 if (!meta
->saved_texcoord_enable
)
275 _mesa_Disable(GL_TEXTURE_COORD_ARRAY
);
277 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+
278 meta
->saved_active_texture
);
280 if (meta
->saved_array_vbo
) {
281 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
,
282 meta
->saved_array_vbo
->Name
);
283 _mesa_reference_buffer_object(ctx
, &meta
->saved_array_vbo
, NULL
);
285 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, 0);
291 * Perform glClear where mask contains only color, depth, and/or stencil.
293 * The implementation is based on calling into Mesa to set GL state and
294 * performing normal triangle rendering. The intent of this path is to
295 * have as generic a path as possible, so that any driver could make use of
300 * Per-context one-time init of things for intl_clear_tris().
301 * Basically set up a private array object for vertex/color arrays.
304 meta_init_clear(struct dri_metaops
*meta
)
306 GLcontext
*ctx
= meta
->ctx
;
307 struct gl_array_object
*arraySave
= NULL
;
308 const GLuint arrayBuffer
= ctx
->Array
.ArrayBufferObj
->Name
;
309 const GLuint elementBuffer
= ctx
->Array
.ElementArrayBufferObj
->Name
;
311 /* create new array object */
312 meta
->clear
.arrayObj
= _mesa_new_array_object(ctx
, ~0);
314 /* save current array object, bind new one */
315 _mesa_reference_array_object(ctx
, &arraySave
, ctx
->Array
.ArrayObj
);
316 ctx
->NewState
|= _NEW_ARRAY
;
317 ctx
->Array
.NewState
|= _NEW_ARRAY_ALL
;
318 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, meta
->clear
.arrayObj
);
320 /* one-time setup of vertex arrays (pos, color) */
321 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, 0);
322 _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
323 _mesa_ColorPointer(4, GL_FLOAT
, 4 * sizeof(GLfloat
), meta
->clear
.color
);
324 _mesa_VertexPointer(3, GL_FLOAT
, 3 * sizeof(GLfloat
), meta
->clear
.vertices
);
325 _mesa_Enable(GL_COLOR_ARRAY
);
326 _mesa_Enable(GL_VERTEX_ARRAY
);
328 /* restore original array object */
329 ctx
->NewState
|= _NEW_ARRAY
;
330 ctx
->Array
.NewState
|= _NEW_ARRAY_ALL
;
331 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, arraySave
);
332 _mesa_reference_array_object(ctx
, &arraySave
, NULL
);
334 /* restore original buffer objects */
335 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, arrayBuffer
);
336 _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB
, elementBuffer
);
342 * Perform glClear where mask contains only color, depth, and/or stencil.
344 * The implementation is based on calling into Mesa to set GL state and
345 * performing normal triangle rendering. The intent of this path is to
346 * have as generic a path as possible, so that any driver could make use of
350 meta_clear_tris(struct dri_metaops
*meta
, GLbitfield mask
)
352 GLcontext
*ctx
= meta
->ctx
;
354 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
356 GLboolean saved_fp_enable
= GL_FALSE
, saved_vp_enable
= GL_FALSE
;
357 GLuint saved_shader_program
= 0;
358 unsigned int saved_active_texture
;
359 struct gl_array_object
*arraySave
= NULL
;
361 if (!meta
->clear
.arrayObj
)
362 meta_init_clear(meta
);
364 assert((mask
& ~(TRI_CLEAR_COLOR_BITS
| BUFFER_BIT_DEPTH
|
365 BUFFER_BIT_STENCIL
)) == 0);
367 _mesa_PushAttrib(GL_COLOR_BUFFER_BIT
|
368 GL_DEPTH_BUFFER_BIT
|
371 GL_STENCIL_BUFFER_BIT
|
375 saved_active_texture
= ctx
->Texture
.CurrentUnit
;
377 /* Disable existing GL state we don't want to apply to a clear. */
378 _mesa_Disable(GL_ALPHA_TEST
);
379 _mesa_Disable(GL_BLEND
);
380 _mesa_Disable(GL_CULL_FACE
);
381 _mesa_Disable(GL_FOG
);
382 _mesa_Disable(GL_POLYGON_SMOOTH
);
383 _mesa_Disable(GL_POLYGON_STIPPLE
);
384 _mesa_Disable(GL_POLYGON_OFFSET_FILL
);
385 _mesa_Disable(GL_LIGHTING
);
386 _mesa_Disable(GL_CLIP_PLANE0
);
387 _mesa_Disable(GL_CLIP_PLANE1
);
388 _mesa_Disable(GL_CLIP_PLANE2
);
389 _mesa_Disable(GL_CLIP_PLANE3
);
390 _mesa_Disable(GL_CLIP_PLANE4
);
391 _mesa_Disable(GL_CLIP_PLANE5
);
392 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
393 if (ctx
->Extensions
.ARB_fragment_program
&& ctx
->FragmentProgram
.Enabled
) {
394 saved_fp_enable
= GL_TRUE
;
395 _mesa_Disable(GL_FRAGMENT_PROGRAM_ARB
);
397 if (ctx
->Extensions
.ARB_vertex_program
&& ctx
->VertexProgram
.Enabled
) {
398 saved_vp_enable
= GL_TRUE
;
399 _mesa_Disable(GL_VERTEX_PROGRAM_ARB
);
401 if (ctx
->Extensions
.ARB_shader_objects
&& ctx
->Shader
.CurrentProgram
) {
402 saved_shader_program
= ctx
->Shader
.CurrentProgram
->Name
;
403 _mesa_UseProgramObjectARB(0);
406 if (ctx
->Texture
._EnabledUnits
!= 0) {
409 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
410 _mesa_ActiveTextureARB(GL_TEXTURE0
+ i
);
411 _mesa_Disable(GL_TEXTURE_1D
);
412 _mesa_Disable(GL_TEXTURE_2D
);
413 _mesa_Disable(GL_TEXTURE_3D
);
414 if (ctx
->Extensions
.ARB_texture_cube_map
)
415 _mesa_Disable(GL_TEXTURE_CUBE_MAP_ARB
);
416 if (ctx
->Extensions
.NV_texture_rectangle
)
417 _mesa_Disable(GL_TEXTURE_RECTANGLE_NV
);
418 if (ctx
->Extensions
.MESA_texture_array
) {
419 _mesa_Disable(GL_TEXTURE_1D_ARRAY_EXT
);
420 _mesa_Disable(GL_TEXTURE_2D_ARRAY_EXT
);
425 /* save current array object, bind our private one */
426 _mesa_reference_array_object(ctx
, &arraySave
, ctx
->Array
.ArrayObj
);
427 ctx
->NewState
|= _NEW_ARRAY
;
428 ctx
->Array
.NewState
|= _NEW_ARRAY_ALL
;
429 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, meta
->clear
.arrayObj
);
431 meta_set_passthrough_transform(meta
);
433 for (i
= 0; i
< 4; i
++) {
434 COPY_4FV(meta
->clear
.color
[i
], ctx
->Color
.ClearColor
);
437 /* convert clear Z from [0,1] to NDC coord in [-1,1] */
438 dst_z
= -1.0 + 2.0 * ctx
->Depth
.Clear
;
440 /* The ClearDepth value is unaffected by DepthRange, so do a default
443 _mesa_DepthRange(0.0, 1.0);
445 /* Prepare the vertices, which are the same regardless of which buffer we're
448 meta
->clear
.vertices
[0][0] = fb
->_Xmin
;
449 meta
->clear
.vertices
[0][1] = fb
->_Ymin
;
450 meta
->clear
.vertices
[0][2] = dst_z
;
451 meta
->clear
.vertices
[1][0] = fb
->_Xmax
;
452 meta
->clear
.vertices
[1][1] = fb
->_Ymin
;
453 meta
->clear
.vertices
[1][2] = dst_z
;
454 meta
->clear
.vertices
[2][0] = fb
->_Xmax
;
455 meta
->clear
.vertices
[2][1] = fb
->_Ymax
;
456 meta
->clear
.vertices
[2][2] = dst_z
;
457 meta
->clear
.vertices
[3][0] = fb
->_Xmin
;
458 meta
->clear
.vertices
[3][1] = fb
->_Ymax
;
459 meta
->clear
.vertices
[3][2] = dst_z
;
462 GLuint this_mask
= 0;
465 color_bit
= _mesa_ffs(mask
& TRI_CLEAR_COLOR_BITS
);
467 this_mask
|= (1 << (color_bit
- 1));
469 /* Clear depth/stencil in the same pass as color. */
470 this_mask
|= (mask
& (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
));
472 /* Select the current color buffer and use the color write mask if
473 * we have one, otherwise don't write any color channels.
475 if (this_mask
& BUFFER_BIT_FRONT_LEFT
)
476 _mesa_DrawBuffer(GL_FRONT_LEFT
);
477 else if (this_mask
& BUFFER_BIT_BACK_LEFT
)
478 _mesa_DrawBuffer(GL_BACK_LEFT
);
479 else if (color_bit
!= 0)
480 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
+
481 (color_bit
- BUFFER_COLOR0
- 1));
483 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
485 /* Control writing of the depth clear value to depth. */
486 if (this_mask
& BUFFER_BIT_DEPTH
) {
487 _mesa_DepthFunc(GL_ALWAYS
);
488 _mesa_Enable(GL_DEPTH_TEST
);
490 _mesa_Disable(GL_DEPTH_TEST
);
491 _mesa_DepthMask(GL_FALSE
);
494 /* Control writing of the stencil clear value to stencil. */
495 if (this_mask
& BUFFER_BIT_STENCIL
) {
496 _mesa_Enable(GL_STENCIL_TEST
);
497 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
498 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
499 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
500 ctx
->Stencil
.Clear
& 0x7fffffff,
501 ctx
->Stencil
.WriteMask
[0]);
503 _mesa_Disable(GL_STENCIL_TEST
);
506 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
511 meta_restore_transform(meta
);
513 _mesa_ActiveTextureARB(GL_TEXTURE0
+ saved_active_texture
);
515 _mesa_Enable(GL_FRAGMENT_PROGRAM_ARB
);
517 _mesa_Enable(GL_VERTEX_PROGRAM_ARB
);
519 if (saved_shader_program
)
520 _mesa_UseProgramObjectARB(saved_shader_program
);
524 /* restore current array object */
525 ctx
->NewState
|= _NEW_ARRAY
;
526 ctx
->Array
.NewState
|= _NEW_ARRAY_ALL
;
527 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, arraySave
);
528 _mesa_reference_array_object(ctx
, &arraySave
, NULL
);
531 void meta_init_metaops(GLcontext
*ctx
, struct dri_metaops
*meta
)
536 void meta_destroy_metaops(struct dri_metaops
*meta
)
538 if (meta
->clear
.arrayObj
)
539 _mesa_delete_array_object(meta
->ctx
, meta
->clear
.arrayObj
);