2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/glformats.h"
50 #include "main/image.h"
51 #include "main/macros.h"
52 #include "main/matrix.h"
53 #include "main/mipmap.h"
54 #include "main/pixel.h"
56 #include "main/polygon.h"
57 #include "main/queryobj.h"
58 #include "main/readpix.h"
59 #include "main/scissor.h"
60 #include "main/shaderapi.h"
61 #include "main/shaderobj.h"
62 #include "main/state.h"
63 #include "main/stencil.h"
64 #include "main/texobj.h"
65 #include "main/texenv.h"
66 #include "main/texgetimage.h"
67 #include "main/teximage.h"
68 #include "main/texparam.h"
69 #include "main/texstate.h"
70 #include "main/transformfeedback.h"
71 #include "main/uniforms.h"
72 #include "main/varray.h"
73 #include "main/viewport.h"
74 #include "main/samplerobj.h"
75 #include "program/program.h"
76 #include "swrast/swrast.h"
77 #include "drivers/common/meta.h"
78 #include "main/enums.h"
79 #include "main/glformats.h"
80 #include "../glsl/ralloc.h"
82 /** Return offset in bytes of the field within a vertex struct */
83 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
85 static struct blit_shader
*
86 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
);
88 static void meta_glsl_blit_cleanup(struct blit_state
*blit
);
89 static void cleanup_temp_texture(struct temp_texture
*tex
);
90 static void meta_glsl_clear_cleanup(struct clear_state
*clear
);
91 static void meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state
*mipmap
);
92 static void meta_decompress_cleanup(struct decompress_state
*decompress
);
93 static void meta_drawpix_cleanup(struct drawpix_state
*drawpix
);
94 static void blit_shader_table_cleanup(struct blit_shader_table
*table
);
97 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
103 shader
= _mesa_CreateShaderObjectARB(target
);
104 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
105 _mesa_CompileShader(shader
);
107 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
111 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
113 _mesa_DeleteObjectARB(shader
);
119 _mesa_DeleteObjectARB(shader
);
123 _mesa_GetShaderInfoLog(shader
, size
, NULL
, info
);
125 "meta program compile failed:\n%s\n"
130 _mesa_DeleteObjectARB(shader
);
136 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
141 _mesa_LinkProgram(program
);
143 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
147 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
155 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
156 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
164 * Generate a generic shader to blit from a texture to a framebuffer
166 * \param ctx Current GL context
167 * \param texTarget Texture target that will be the source of the blit
169 * \returns a handle to a shader program on success or zero on failure.
172 setup_blit_shader(struct gl_context
*ctx
,
174 struct blit_shader_table
*table
)
176 const char *vs_source
;
179 void *const mem_ctx
= ralloc_context(NULL
);
180 struct blit_shader
*shader
= choose_blit_shader(target
, table
);
182 assert(shader
!= NULL
);
184 if (shader
->shader_prog
!= 0) {
185 _mesa_UseProgram(shader
->shader_prog
);
189 /* The version check is a little tricky. API is set to API_OPENGLES2 even
190 * for OpenGL ES 3.0 contexts, and GLSLVersion may be set to 140, for
191 * example, in an OpenGL ES 2.0 context.
193 if ((ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30)
194 || ctx
->Const
.GLSLVersion
< 130) {
196 "attribute vec2 position;\n"
197 "attribute vec3 textureCoords;\n"
198 "varying vec4 texCoords;\n"
201 " texCoords = textureCoords;\n"
202 " gl_Position = vec4(position, 0.0, 1.0);\n"
205 fs_source
= ralloc_asprintf(mem_ctx
,
206 "#extension GL_EXT_texture_array : enable\n"
207 "#extension GL_ARB_texture_cube_map_array: enable\n"
209 "precision highp float;\n"
211 "uniform %s texSampler;\n"
212 "varying vec4 texCoords;\n"
215 " gl_FragColor = %s(texSampler, %s);\n"
216 " gl_FragDepth = gl_FragColor.x;\n"
219 shader
->func
, shader
->texcoords
);
222 vs_source
= ralloc_asprintf(mem_ctx
,
224 "in vec2 position;\n"
225 "in vec4 textureCoords;\n"
226 "out vec4 texCoords;\n"
229 " texCoords = textureCoords;\n"
230 " gl_Position = vec4(position, 0.0, 1.0);\n"
232 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
233 fs_source
= ralloc_asprintf(mem_ctx
,
235 "#extension GL_ARB_texture_cube_map_array: enable\n"
237 "precision highp float;\n"
239 "uniform %s texSampler;\n"
240 "in vec4 texCoords;\n"
241 "out vec4 out_color;\n"
245 " out_color = texture(texSampler, %s);\n"
246 " gl_FragDepth = out_color.x;\n"
248 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
253 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
254 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
256 shader
->shader_prog
= _mesa_CreateProgramObjectARB();
257 _mesa_AttachShader(shader
->shader_prog
, fs
);
258 _mesa_DeleteObjectARB(fs
);
259 _mesa_AttachShader(shader
->shader_prog
, vs
);
260 _mesa_DeleteObjectARB(vs
);
261 _mesa_BindAttribLocation(shader
->shader_prog
, 0, "position");
262 _mesa_BindAttribLocation(shader
->shader_prog
, 1, "texcoords");
263 link_program_with_debug(ctx
, shader
->shader_prog
);
264 ralloc_free(mem_ctx
);
266 _mesa_UseProgram(shader
->shader_prog
);
270 * Configure vertex buffer and vertex array objects for tests
272 * Regardless of whether a new VAO and new VBO are created, the objects
273 * referenced by \c VAO and \c VBO will be bound into the GL state vector
274 * when this function terminates.
276 * \param VAO Storage for vertex array object handle. If 0, a new VAO
278 * \param VBO Storage for vertex buffer object handle. If 0, a new VBO
279 * will be created. The new VBO will have storage for 4
280 * \c vertex structures.
281 * \param use_generic_attributes Should generic attributes 0 and 1 be used,
282 * or should traditional, fixed-function color and texture
283 * coordinate be used?
284 * \param vertex_size Number of components for attribute 0 / vertex.
285 * \param texcoord_size Number of components for attribute 1 / texture
286 * coordinate. If this is 0, attribute 1 will not be set or
288 * \param color_size Number of components for attribute 1 / primary color.
289 * If this is 0, attribute 1 will not be set or enabled.
291 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
292 * Use \c texcoord_size instead.
295 setup_vertex_objects(GLuint
*VAO
, GLuint
*VBO
, bool use_generic_attributes
,
296 unsigned vertex_size
, unsigned texcoord_size
,
302 /* create vertex array object */
303 _mesa_GenVertexArrays(1, VAO
);
304 _mesa_BindVertexArray(*VAO
);
306 /* create vertex array buffer */
307 _mesa_GenBuffers(1, VBO
);
308 _mesa_BindBuffer(GL_ARRAY_BUFFER
, *VBO
);
309 _mesa_BufferData(GL_ARRAY_BUFFER
, 4 * sizeof(struct vertex
), NULL
,
312 /* setup vertex arrays */
313 if (use_generic_attributes
) {
314 assert(color_size
== 0);
316 _mesa_VertexAttribPointer(0, vertex_size
, GL_FLOAT
, GL_FALSE
,
317 sizeof(struct vertex
), OFFSET(x
));
318 _mesa_EnableVertexAttribArray(0);
320 if (texcoord_size
> 0) {
321 _mesa_VertexAttribPointer(1, texcoord_size
, GL_FLOAT
, GL_FALSE
,
322 sizeof(struct vertex
), OFFSET(tex
));
323 _mesa_EnableVertexAttribArray(1);
326 _mesa_VertexPointer(vertex_size
, GL_FLOAT
, sizeof(struct vertex
),
328 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
330 if (texcoord_size
> 0) {
331 _mesa_TexCoordPointer(texcoord_size
, GL_FLOAT
,
332 sizeof(struct vertex
), OFFSET(tex
));
333 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
336 if (color_size
> 0) {
337 _mesa_ColorPointer(color_size
, GL_FLOAT
,
338 sizeof(struct vertex
), OFFSET(r
));
339 _mesa_EnableClientState(GL_COLOR_ARRAY
);
343 _mesa_BindVertexArray(*VAO
);
344 _mesa_BindBuffer(GL_ARRAY_BUFFER
, *VBO
);
349 * Initialize meta-ops for a context.
350 * To be called once during context creation.
353 _mesa_meta_init(struct gl_context
*ctx
)
357 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
362 * Free context meta-op state.
363 * To be called once during context destruction.
366 _mesa_meta_free(struct gl_context
*ctx
)
368 GET_CURRENT_CONTEXT(old_context
);
369 _mesa_make_current(ctx
, NULL
, NULL
);
370 meta_glsl_blit_cleanup(&ctx
->Meta
->Blit
);
371 meta_glsl_clear_cleanup(&ctx
->Meta
->Clear
);
372 meta_glsl_generate_mipmap_cleanup(&ctx
->Meta
->Mipmap
);
373 cleanup_temp_texture(&ctx
->Meta
->TempTex
);
374 meta_decompress_cleanup(&ctx
->Meta
->Decompress
);
375 meta_drawpix_cleanup(&ctx
->Meta
->DrawPix
);
377 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
379 _mesa_make_current(NULL
, NULL
, NULL
);
386 * Enter meta state. This is like a light-weight version of glPushAttrib
387 * but it also resets most GL state back to default values.
389 * \param state bitmask of MESA_META_* flags indicating which attribute groups
390 * to save and reset to their defaults
393 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
395 struct save_state
*save
;
397 /* hope MAX_META_OPS_DEPTH is large enough */
398 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
400 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
401 memset(save
, 0, sizeof(*save
));
402 save
->SavedState
= state
;
404 /* Pausing transform feedback needs to be done early, or else we won't be
405 * able to change other state.
407 save
->TransformFeedbackNeedsResume
=
408 _mesa_is_xfb_active_and_unpaused(ctx
);
409 if (save
->TransformFeedbackNeedsResume
)
410 _mesa_PauseTransformFeedback();
412 /* After saving the current occlusion object, call EndQuery so that no
413 * occlusion querying will be active during the meta-operation.
415 if (state
& MESA_META_OCCLUSION_QUERY
) {
416 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
417 if (save
->CurrentOcclusionObject
)
418 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
421 if (state
& MESA_META_ALPHA_TEST
) {
422 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
423 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
424 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
425 if (ctx
->Color
.AlphaEnabled
)
426 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
429 if (state
& MESA_META_BLEND
) {
430 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
431 if (ctx
->Color
.BlendEnabled
) {
432 if (ctx
->Extensions
.EXT_draw_buffers2
) {
434 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
435 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
439 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
442 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
443 if (ctx
->Color
.ColorLogicOpEnabled
)
444 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
447 if (state
& MESA_META_COLOR_MASK
) {
448 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
449 sizeof(ctx
->Color
.ColorMask
));
450 if (!ctx
->Color
.ColorMask
[0][0] ||
451 !ctx
->Color
.ColorMask
[0][1] ||
452 !ctx
->Color
.ColorMask
[0][2] ||
453 !ctx
->Color
.ColorMask
[0][3])
454 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
457 if (state
& MESA_META_DEPTH_TEST
) {
458 save
->Depth
= ctx
->Depth
; /* struct copy */
460 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
463 if ((state
& MESA_META_FOG
)
464 && ctx
->API
!= API_OPENGL_CORE
465 && ctx
->API
!= API_OPENGLES2
) {
466 save
->Fog
= ctx
->Fog
.Enabled
;
467 if (ctx
->Fog
.Enabled
)
468 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
471 if (state
& MESA_META_PIXEL_STORE
) {
472 save
->Pack
= ctx
->Pack
;
473 save
->Unpack
= ctx
->Unpack
;
474 ctx
->Pack
= ctx
->DefaultPacking
;
475 ctx
->Unpack
= ctx
->DefaultPacking
;
478 if (state
& MESA_META_PIXEL_TRANSFER
) {
479 save
->RedScale
= ctx
->Pixel
.RedScale
;
480 save
->RedBias
= ctx
->Pixel
.RedBias
;
481 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
482 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
483 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
484 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
485 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
486 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
487 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
488 ctx
->Pixel
.RedScale
= 1.0F
;
489 ctx
->Pixel
.RedBias
= 0.0F
;
490 ctx
->Pixel
.GreenScale
= 1.0F
;
491 ctx
->Pixel
.GreenBias
= 0.0F
;
492 ctx
->Pixel
.BlueScale
= 1.0F
;
493 ctx
->Pixel
.BlueBias
= 0.0F
;
494 ctx
->Pixel
.AlphaScale
= 1.0F
;
495 ctx
->Pixel
.AlphaBias
= 0.0F
;
496 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
498 ctx
->NewState
|=_NEW_PIXEL
;
501 if (state
& MESA_META_RASTERIZATION
) {
502 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
503 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
504 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
505 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
506 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
507 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
508 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
509 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
510 if (ctx
->API
== API_OPENGL_COMPAT
) {
511 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
512 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
514 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
517 if (state
& MESA_META_SCISSOR
) {
518 save
->Scissor
= ctx
->Scissor
; /* struct copy */
519 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
522 if (state
& MESA_META_SHADER
) {
525 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
526 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
527 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
528 ctx
->VertexProgram
.Current
);
529 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
532 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
533 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
534 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
535 ctx
->FragmentProgram
.Current
);
536 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
539 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
540 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
541 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
544 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
545 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
],
546 ctx
->Shader
.CurrentProgram
[i
]);
548 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
549 ctx
->Shader
.ActiveProgram
);
554 if (state
& MESA_META_STENCIL_TEST
) {
555 save
->Stencil
= ctx
->Stencil
; /* struct copy */
556 if (ctx
->Stencil
.Enabled
)
557 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
558 /* NOTE: other stencil state not reset */
561 if (state
& MESA_META_TEXTURE
) {
564 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
565 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
566 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
568 /* Disable all texture units */
569 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
570 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
571 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
572 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
573 if (ctx
->Texture
.Unit
[u
].Enabled
||
574 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
575 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
576 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
577 if (ctx
->Extensions
.ARB_texture_cube_map
)
578 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
579 if (_mesa_is_gles(ctx
) &&
580 ctx
->Extensions
.OES_EGL_image_external
)
581 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
583 if (ctx
->API
== API_OPENGL_COMPAT
) {
584 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
585 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
586 if (ctx
->Extensions
.NV_texture_rectangle
)
587 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
588 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
589 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
590 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
591 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
593 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_STR_OES
, GL_FALSE
);
599 /* save current texture objects for unit[0] only */
600 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
601 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
602 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
605 /* set defaults for unit[0] */
606 _mesa_ActiveTexture(GL_TEXTURE0
);
607 _mesa_ClientActiveTexture(GL_TEXTURE0
);
608 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
609 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
613 if (state
& MESA_META_TRANSFORM
) {
614 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
615 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
616 16 * sizeof(GLfloat
));
617 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
618 16 * sizeof(GLfloat
));
619 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
620 16 * sizeof(GLfloat
));
621 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
622 /* set 1:1 vertex:pixel coordinate transform */
623 _mesa_ActiveTexture(GL_TEXTURE0
);
624 _mesa_MatrixMode(GL_TEXTURE
);
625 _mesa_LoadIdentity();
626 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
627 _mesa_MatrixMode(GL_MODELVIEW
);
628 _mesa_LoadIdentity();
629 _mesa_MatrixMode(GL_PROJECTION
);
630 _mesa_LoadIdentity();
632 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
633 * This can occur when there is no draw buffer.
635 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
636 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
637 0.0, ctx
->DrawBuffer
->Height
,
641 if (state
& MESA_META_CLIP
) {
642 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
643 if (ctx
->Transform
.ClipPlanesEnabled
) {
645 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
646 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
651 if (state
& MESA_META_VERTEX
) {
652 /* save vertex array object state */
653 _mesa_reference_vao(ctx
, &save
->VAO
,
655 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
656 ctx
->Array
.ArrayBufferObj
);
657 /* set some default state? */
660 if (state
& MESA_META_VIEWPORT
) {
661 /* save viewport state */
662 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
663 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
664 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
665 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
666 /* set viewport to match window size */
667 if (ctx
->ViewportArray
[0].X
!= 0 ||
668 ctx
->ViewportArray
[0].Y
!= 0 ||
669 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
670 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
671 _mesa_set_viewport(ctx
, 0, 0, 0,
672 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
674 /* save depth range state */
675 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
676 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
677 /* set depth range to default */
678 _mesa_DepthRange(0.0, 1.0);
681 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
682 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
684 /* Generally in here we want to do clamping according to whether
685 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
686 * regardless of the internal implementation of the metaops.
688 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
689 ctx
->Extensions
.ARB_color_buffer_float
)
690 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
693 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
694 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
696 /* Generally in here we never want vertex color clamping --
697 * result clamping is only dependent on fragment clamping.
699 if (ctx
->Extensions
.ARB_color_buffer_float
)
700 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
703 if (state
& MESA_META_CONDITIONAL_RENDER
) {
704 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
705 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
707 if (ctx
->Query
.CondRenderQuery
)
708 _mesa_EndConditionalRender();
711 if (state
& MESA_META_SELECT_FEEDBACK
) {
712 save
->RenderMode
= ctx
->RenderMode
;
713 if (ctx
->RenderMode
== GL_SELECT
) {
714 save
->Select
= ctx
->Select
; /* struct copy */
715 _mesa_RenderMode(GL_RENDER
);
716 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
717 save
->Feedback
= ctx
->Feedback
; /* struct copy */
718 _mesa_RenderMode(GL_RENDER
);
722 if (state
& MESA_META_MULTISAMPLE
) {
723 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
724 if (ctx
->Multisample
.Enabled
)
725 _mesa_set_multisample(ctx
, GL_FALSE
);
728 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
729 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
730 if (ctx
->Color
.sRGBEnabled
)
731 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
736 save
->Lighting
= ctx
->Light
.Enabled
;
737 if (ctx
->Light
.Enabled
)
738 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
739 save
->RasterDiscard
= ctx
->RasterDiscard
;
740 if (ctx
->RasterDiscard
)
741 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
747 * Leave meta state. This is like a light-weight version of glPopAttrib().
750 _mesa_meta_end(struct gl_context
*ctx
)
752 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
753 const GLbitfield state
= save
->SavedState
;
756 /* After starting a new occlusion query, initialize the results to the
757 * values saved previously. The driver will then continue to increment
760 if (state
& MESA_META_OCCLUSION_QUERY
) {
761 if (save
->CurrentOcclusionObject
) {
762 _mesa_BeginQuery(save
->CurrentOcclusionObject
->Target
,
763 save
->CurrentOcclusionObject
->Id
);
764 ctx
->Query
.CurrentOcclusionObject
->Result
= save
->CurrentOcclusionObject
->Result
;
768 if (state
& MESA_META_ALPHA_TEST
) {
769 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
770 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
771 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
774 if (state
& MESA_META_BLEND
) {
775 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
776 if (ctx
->Extensions
.EXT_draw_buffers2
) {
778 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
779 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
783 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
786 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
787 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
790 if (state
& MESA_META_COLOR_MASK
) {
792 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
793 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
795 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
796 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
800 save
->ColorMask
[i
][0],
801 save
->ColorMask
[i
][1],
802 save
->ColorMask
[i
][2],
803 save
->ColorMask
[i
][3]);
809 if (state
& MESA_META_DEPTH_TEST
) {
810 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
811 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
812 _mesa_DepthFunc(save
->Depth
.Func
);
813 _mesa_DepthMask(save
->Depth
.Mask
);
816 if ((state
& MESA_META_FOG
)
817 && ctx
->API
!= API_OPENGL_CORE
818 && ctx
->API
!= API_OPENGLES2
) {
819 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
822 if (state
& MESA_META_PIXEL_STORE
) {
823 ctx
->Pack
= save
->Pack
;
824 ctx
->Unpack
= save
->Unpack
;
827 if (state
& MESA_META_PIXEL_TRANSFER
) {
828 ctx
->Pixel
.RedScale
= save
->RedScale
;
829 ctx
->Pixel
.RedBias
= save
->RedBias
;
830 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
831 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
832 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
833 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
834 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
835 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
836 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
838 ctx
->NewState
|=_NEW_PIXEL
;
841 if (state
& MESA_META_RASTERIZATION
) {
842 /* Core context requires that front and back mode be the same.
844 if (ctx
->API
== API_OPENGL_CORE
) {
845 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
847 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
848 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
850 if (ctx
->API
== API_OPENGL_COMPAT
) {
851 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
852 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
854 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
855 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
858 if (state
& MESA_META_SCISSOR
) {
861 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
862 _mesa_set_scissor(ctx
, i
,
863 save
->Scissor
.ScissorArray
[i
].X
,
864 save
->Scissor
.ScissorArray
[i
].Y
,
865 save
->Scissor
.ScissorArray
[i
].Width
,
866 save
->Scissor
.ScissorArray
[i
].Height
);
867 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
868 (save
->Scissor
.EnableFlags
>> i
) & 1);
872 if (state
& MESA_META_SHADER
) {
873 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
874 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
875 save
->VertexProgramEnabled
);
876 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
877 save
->VertexProgram
);
878 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
881 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
882 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
883 save
->FragmentProgramEnabled
);
884 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
885 save
->FragmentProgram
);
886 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
889 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
890 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
891 save
->ATIFragmentShaderEnabled
);
894 if (ctx
->Extensions
.ARB_vertex_shader
) {
895 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
,
896 save
->Shader
[MESA_SHADER_VERTEX
]);
899 if (_mesa_has_geometry_shaders(ctx
))
900 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
901 save
->Shader
[MESA_SHADER_GEOMETRY
]);
903 if (ctx
->Extensions
.ARB_fragment_shader
)
904 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
905 save
->Shader
[MESA_SHADER_FRAGMENT
]);
907 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
910 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
911 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
], NULL
);
912 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
915 if (state
& MESA_META_STENCIL_TEST
) {
916 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
918 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
919 _mesa_ClearStencil(stencil
->Clear
);
920 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_stencil_two_side
) {
921 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
922 stencil
->TestTwoSide
);
923 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
924 ? GL_BACK
: GL_FRONT
);
927 _mesa_StencilFuncSeparate(GL_FRONT
,
928 stencil
->Function
[0],
930 stencil
->ValueMask
[0]);
931 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
932 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
933 stencil
->ZFailFunc
[0],
934 stencil
->ZPassFunc
[0]);
936 _mesa_StencilFuncSeparate(GL_BACK
,
937 stencil
->Function
[1],
939 stencil
->ValueMask
[1]);
940 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
941 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
942 stencil
->ZFailFunc
[1],
943 stencil
->ZPassFunc
[1]);
946 if (state
& MESA_META_TEXTURE
) {
949 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
951 /* restore texenv for unit[0] */
952 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
953 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
956 /* restore texture objects for unit[0] only */
957 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
958 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
959 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
960 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
961 save
->CurrentTexture
[tgt
]);
963 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
966 /* Restore fixed function texture enables, texgen */
967 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
968 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
969 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
970 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
971 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
974 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
975 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
976 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
981 /* restore current unit state */
982 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
983 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
986 if (state
& MESA_META_TRANSFORM
) {
987 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
988 _mesa_ActiveTexture(GL_TEXTURE0
);
989 _mesa_MatrixMode(GL_TEXTURE
);
990 _mesa_LoadMatrixf(save
->TextureMatrix
);
991 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
993 _mesa_MatrixMode(GL_MODELVIEW
);
994 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
996 _mesa_MatrixMode(GL_PROJECTION
);
997 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
999 _mesa_MatrixMode(save
->MatrixMode
);
1002 if (state
& MESA_META_CLIP
) {
1003 if (save
->ClipPlanesEnabled
) {
1005 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1006 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1007 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1013 if (state
& MESA_META_VERTEX
) {
1014 /* restore vertex buffer object */
1015 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1016 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1018 /* restore vertex array object */
1019 _mesa_BindVertexArray(save
->VAO
->Name
);
1020 _mesa_reference_vao(ctx
, &save
->VAO
, NULL
);
1023 if (state
& MESA_META_VIEWPORT
) {
1024 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1025 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1026 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1027 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1028 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1029 save
->ViewportW
, save
->ViewportH
);
1031 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1034 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1035 ctx
->Extensions
.ARB_color_buffer_float
) {
1036 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1039 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1040 ctx
->Extensions
.ARB_color_buffer_float
) {
1041 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1044 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1045 if (save
->CondRenderQuery
)
1046 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1047 save
->CondRenderMode
);
1050 if (state
& MESA_META_SELECT_FEEDBACK
) {
1051 if (save
->RenderMode
== GL_SELECT
) {
1052 _mesa_RenderMode(GL_SELECT
);
1053 ctx
->Select
= save
->Select
;
1054 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1055 _mesa_RenderMode(GL_FEEDBACK
);
1056 ctx
->Feedback
= save
->Feedback
;
1060 if (state
& MESA_META_MULTISAMPLE
) {
1061 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1062 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1065 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1066 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1067 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1071 if (save
->Lighting
) {
1072 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1074 if (save
->RasterDiscard
) {
1075 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1077 if (save
->TransformFeedbackNeedsResume
)
1078 _mesa_ResumeTransformFeedback();
1080 ctx
->Meta
->SaveStackDepth
--;
1085 * Determine whether Mesa is currently in a meta state.
1088 _mesa_meta_in_progress(struct gl_context
*ctx
)
1090 return ctx
->Meta
->SaveStackDepth
!= 0;
1095 * Convert Z from a normalized value in the range [0, 1] to an object-space
1096 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1097 * default/identity ortho projection results in the original Z value.
1098 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1099 * value comes from the clear value or raster position.
1101 static INLINE GLfloat
1102 invert_z(GLfloat normZ
)
1104 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1110 * One-time init for a temp_texture object.
1111 * Choose tex target, compute max tex size, etc.
1114 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1116 /* prefer texture rectangle */
1117 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1118 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1119 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1120 tex
->NPOT
= GL_TRUE
;
1123 /* use 2D texture, NPOT if possible */
1124 tex
->Target
= GL_TEXTURE_2D
;
1125 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1126 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1128 tex
->MinSize
= 16; /* 16 x 16 at least */
1129 assert(tex
->MaxSize
> 0);
1131 _mesa_GenTextures(1, &tex
->TexObj
);
1135 cleanup_temp_texture(struct temp_texture
*tex
)
1139 _mesa_DeleteTextures(1, &tex
->TexObj
);
1145 * Return pointer to temp_texture info for non-bitmap ops.
1146 * This does some one-time init if needed.
1148 static struct temp_texture
*
1149 get_temp_texture(struct gl_context
*ctx
)
1151 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1154 init_temp_texture(ctx
, tex
);
1162 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1163 * We use a separate texture for bitmaps to reduce texture
1164 * allocation/deallocation.
1166 static struct temp_texture
*
1167 get_bitmap_temp_texture(struct gl_context
*ctx
)
1169 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1172 init_temp_texture(ctx
, tex
);
1179 * Return pointer to depth temp_texture.
1180 * This does some one-time init if needed.
1182 static struct temp_texture
*
1183 get_temp_depth_texture(struct gl_context
*ctx
)
1185 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1188 init_temp_texture(ctx
, tex
);
1195 * Compute the width/height of texture needed to draw an image of the
1196 * given size. Return a flag indicating whether the current texture
1197 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1198 * allocated (glTexImage2D).
1199 * Also, compute s/t texcoords for drawing.
1201 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1204 alloc_texture(struct temp_texture
*tex
,
1205 GLsizei width
, GLsizei height
, GLenum intFormat
)
1207 GLboolean newTex
= GL_FALSE
;
1209 ASSERT(width
<= tex
->MaxSize
);
1210 ASSERT(height
<= tex
->MaxSize
);
1212 if (width
> tex
->Width
||
1213 height
> tex
->Height
||
1214 intFormat
!= tex
->IntFormat
) {
1215 /* alloc new texture (larger or different format) */
1218 /* use non-power of two size */
1219 tex
->Width
= MAX2(tex
->MinSize
, width
);
1220 tex
->Height
= MAX2(tex
->MinSize
, height
);
1223 /* find power of two size */
1225 w
= h
= tex
->MinSize
;
1234 tex
->IntFormat
= intFormat
;
1239 /* compute texcoords */
1240 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1241 tex
->Sright
= (GLfloat
) width
;
1242 tex
->Ttop
= (GLfloat
) height
;
1245 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1246 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1254 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1257 setup_copypix_texture(struct gl_context
*ctx
,
1258 struct temp_texture
*tex
,
1259 GLint srcX
, GLint srcY
,
1260 GLsizei width
, GLsizei height
, GLenum intFormat
,
1265 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1266 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1267 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1268 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1269 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1271 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1273 /* copy framebuffer image to texture */
1275 /* create new tex image */
1276 if (tex
->Width
== width
&& tex
->Height
== height
) {
1277 /* create new tex with framebuffer data */
1278 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1279 srcX
, srcY
, width
, height
, 0);
1282 /* create empty texture */
1283 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1284 tex
->Width
, tex
->Height
, 0,
1285 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1287 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1288 0, 0, srcX
, srcY
, width
, height
);
1292 /* replace existing tex image */
1293 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1294 0, 0, srcX
, srcY
, width
, height
);
1300 * Setup/load texture for glDrawPixels.
1303 setup_drawpix_texture(struct gl_context
*ctx
,
1304 struct temp_texture
*tex
,
1306 GLsizei width
, GLsizei height
,
1307 GLenum format
, GLenum type
,
1308 const GLvoid
*pixels
)
1310 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1311 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1312 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1313 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1314 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1316 /* copy pixel data to texture */
1318 /* create new tex image */
1319 if (tex
->Width
== width
&& tex
->Height
== height
) {
1320 /* create new tex and load image data */
1321 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1322 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1325 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1327 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1328 ctx
->Unpack
.BufferObj
);
1329 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1330 /* create empty texture */
1331 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1332 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1333 if (save_unpack_obj
!= NULL
)
1334 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1335 save_unpack_obj
->Name
);
1337 _mesa_TexSubImage2D(tex
->Target
, 0,
1338 0, 0, width
, height
, format
, type
, pixels
);
1342 /* replace existing tex image */
1343 _mesa_TexSubImage2D(tex
->Target
, 0,
1344 0, 0, width
, height
, format
, type
, pixels
);
1351 * One-time init for drawing depth pixels.
1354 init_blit_depth_pixels(struct gl_context
*ctx
)
1356 static const char *program
=
1358 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1361 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1362 struct temp_texture
*tex
= get_temp_texture(ctx
);
1363 const char *texTarget
;
1365 assert(blit
->DepthFP
== 0);
1367 /* replace %s with "RECT" or "2D" */
1368 assert(strlen(program
) + 4 < sizeof(program2
));
1369 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1373 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1375 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
1376 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1377 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1378 strlen(program2
), (const GLubyte
*) program2
);
1382 setup_ff_tnl_for_blit(GLuint
*VAO
, GLuint
*VBO
, unsigned texcoord_size
)
1384 setup_vertex_objects(VAO
, VBO
, false, 2, texcoord_size
, 0);
1386 /* setup projection matrix */
1387 _mesa_MatrixMode(GL_PROJECTION
);
1388 _mesa_LoadIdentity();
1392 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
1393 struct blit_state
*blit
,
1396 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
1397 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
1399 setup_vertex_objects(&blit
->VAO
, &blit
->VBO
, true, 2, 2, 0);
1401 setup_blit_shader(ctx
, target
, &blit
->shaders
);
1405 * Try to do a glBlitFramebuffer using no-copy texturing.
1406 * We can do this when the src renderbuffer is actually a texture.
1408 * \return new buffer mask indicating the buffers left to blit using the
1412 blitframebuffer_texture(struct gl_context
*ctx
,
1413 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1414 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1415 GLbitfield mask
, GLenum filter
, GLint flipX
,
1416 GLint flipY
, GLboolean glsl_version
)
1418 if (mask
& GL_COLOR_BUFFER_BIT
) {
1419 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1420 const struct gl_renderbuffer_attachment
*readAtt
=
1421 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1423 if (readAtt
&& readAtt
->Texture
) {
1424 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1425 const GLint dstX
= MIN2(dstX0
, dstX1
);
1426 const GLint dstY
= MIN2(dstY0
, dstY1
);
1427 const GLint dstW
= abs(dstX1
- dstX0
);
1428 const GLint dstH
= abs(dstY1
- dstY0
);
1429 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1430 const GLuint srcLevel
= readAtt
->TextureLevel
;
1431 const GLint baseLevelSave
= texObj
->BaseLevel
;
1432 const GLint maxLevelSave
= texObj
->MaxLevel
;
1433 const GLenum target
= texObj
->Target
;
1434 GLuint sampler
, samplerSave
=
1435 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1436 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1438 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1439 /* Can't handle other texture types at this time */
1443 /* Choose between glsl version and fixed function version of
1444 * BlitFramebuffer function.
1447 setup_glsl_blit_framebuffer(ctx
, blit
, target
);
1450 setup_ff_tnl_for_blit(&ctx
->Meta
->Blit
.VAO
,
1451 &ctx
->Meta
->Blit
.VBO
,
1455 _mesa_GenSamplers(1, &sampler
);
1456 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1459 printf("Blit from texture!\n");
1460 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1461 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1464 /* Prepare src texture state */
1465 _mesa_BindTexture(target
, texObj
->Name
);
1466 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1467 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1468 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1469 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1470 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1472 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1473 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1475 /* Always do our blits with no sRGB decode or encode. Note that
1476 * GL_FRAMEBUFFER_SRGB has already been disabled by
1477 * _mesa_meta_begin().
1479 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1480 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1481 GL_SKIP_DECODE_EXT
);
1484 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1485 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1486 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1489 /* Prepare vertex data (the VBO was previously created and bound) */
1491 struct vertex verts
[4];
1492 GLfloat s0
, t0
, s1
, t1
;
1494 if (target
== GL_TEXTURE_2D
) {
1495 const struct gl_texture_image
*texImage
1496 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1497 s0
= srcX0
/ (float) texImage
->Width
;
1498 s1
= srcX1
/ (float) texImage
->Width
;
1499 t0
= srcY0
/ (float) texImage
->Height
;
1500 t1
= srcY1
/ (float) texImage
->Height
;
1503 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1510 /* Silence valgrind warnings about reading uninitialized stack. */
1511 memset(verts
, 0, sizeof(verts
));
1513 /* setup vertex positions */
1514 verts
[0].x
= -1.0F
* flipX
;
1515 verts
[0].y
= -1.0F
* flipY
;
1516 verts
[1].x
= 1.0F
* flipX
;
1517 verts
[1].y
= -1.0F
* flipY
;
1518 verts
[2].x
= 1.0F
* flipX
;
1519 verts
[2].y
= 1.0F
* flipY
;
1520 verts
[3].x
= -1.0F
* flipX
;
1521 verts
[3].y
= 1.0F
* flipY
;
1523 verts
[0].tex
[0] = s0
;
1524 verts
[0].tex
[1] = t0
;
1525 verts
[1].tex
[0] = s1
;
1526 verts
[1].tex
[1] = t0
;
1527 verts
[2].tex
[0] = s1
;
1528 verts
[2].tex
[1] = t1
;
1529 verts
[3].tex
[0] = s0
;
1530 verts
[3].tex
[1] = t1
;
1532 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1535 /* setup viewport */
1536 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1537 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1538 _mesa_DepthMask(GL_FALSE
);
1539 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1541 /* Restore texture object state, the texture binding will
1542 * be restored by _mesa_meta_end().
1544 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1545 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1546 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1549 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1550 _mesa_DeleteSamplers(1, &sampler
);
1552 /* Done with color buffer */
1553 mask
&= ~GL_COLOR_BUFFER_BIT
;
1562 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1563 * of texture mapping and polygon rendering.
1566 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1567 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1568 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1569 GLbitfield mask
, GLenum filter
)
1571 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1572 struct temp_texture
*tex
= get_temp_texture(ctx
);
1573 struct temp_texture
*depthTex
= get_temp_depth_texture(ctx
);
1574 const GLsizei maxTexSize
= tex
->MaxSize
;
1575 const GLint srcX
= MIN2(srcX0
, srcX1
);
1576 const GLint srcY
= MIN2(srcY0
, srcY1
);
1577 const GLint srcW
= abs(srcX1
- srcX0
);
1578 const GLint srcH
= abs(srcY1
- srcY0
);
1579 const GLint dstX
= MIN2(dstX0
, dstX1
);
1580 const GLint dstY
= MIN2(dstY0
, dstY1
);
1581 const GLint dstW
= abs(dstX1
- dstX0
);
1582 const GLint dstH
= abs(dstY1
- dstY0
);
1583 const GLint srcFlipX
= (srcX1
- srcX0
) / srcW
;
1584 const GLint srcFlipY
= (srcY1
- srcY0
) / srcH
;
1585 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
1586 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
1587 const GLint flipX
= srcFlipX
* dstFlipX
;
1588 const GLint flipY
= srcFlipY
* dstFlipY
;
1590 struct vertex verts
[4];
1592 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
1593 ctx
->Extensions
.ARB_fragment_shader
&&
1594 (ctx
->API
!= API_OPENGLES
);
1596 /* In addition to falling back if the blit size is larger than the maximum
1597 * texture size, fallback if the source is multisampled. This fallback can
1598 * be removed once Mesa gets support ARB_texture_multisample.
1600 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1601 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1602 /* XXX avoid this fallback */
1603 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1604 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1608 /* only scissor effects blit so save/clear all other relevant state */
1609 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1611 /* Try faster, direct texture approach first */
1612 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1613 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
,
1614 dstFlipX
, dstFlipY
, use_glsl_version
);
1616 _mesa_meta_end(ctx
);
1620 /* Choose between glsl version and fixed function version of
1621 * BlitFramebuffer function.
1623 if (use_glsl_version
) {
1624 setup_glsl_blit_framebuffer(ctx
, blit
, tex
->Target
);
1627 setup_ff_tnl_for_blit(&blit
->VAO
, &blit
->VBO
, 2);
1630 /* Silence valgrind warnings about reading uninitialized stack. */
1631 memset(verts
, 0, sizeof(verts
));
1633 /* Continue with "normal" approach which involves copying the src rect
1634 * into a temporary texture and is "blitted" by drawing a textured quad.
1637 /* setup vertex positions */
1638 verts
[0].x
= -1.0F
* flipX
;
1639 verts
[0].y
= -1.0F
* flipY
;
1640 verts
[1].x
= 1.0F
* flipX
;
1641 verts
[1].y
= -1.0F
* flipY
;
1642 verts
[2].x
= 1.0F
* flipX
;
1643 verts
[2].y
= 1.0F
* flipY
;
1644 verts
[3].x
= -1.0F
* flipX
;
1645 verts
[3].y
= 1.0F
* flipY
;
1649 /* glEnable() in gles2 and gles3 doesn't allow GL_TEXTURE_{1D, 2D, etc.}
1652 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1653 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1655 if (mask
& GL_COLOR_BUFFER_BIT
) {
1656 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1657 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
1658 const GLenum rb_base_format
=
1659 _mesa_base_tex_format(ctx
, colorReadRb
->InternalFormat
);
1661 /* Using the exact source rectangle to create the texture does incorrect
1662 * linear filtering along the edges. So, allocate the texture extended along
1663 * edges by one pixel in x, y directions.
1665 setup_copypix_texture(ctx
, tex
,
1666 srcX
- 1, srcY
- 1, srcW
+ 2, srcH
+ 2,
1667 rb_base_format
, filter
);
1668 /* texcoords (after texture allocation!) */
1670 verts
[0].tex
[0] = 1.0F
;
1671 verts
[0].tex
[1] = 1.0F
;
1672 verts
[1].tex
[0] = tex
->Sright
- 1.0F
;
1673 verts
[1].tex
[1] = 1.0F
;
1674 verts
[2].tex
[0] = tex
->Sright
- 1.0F
;
1675 verts
[2].tex
[1] = tex
->Ttop
- 1.0F
;
1676 verts
[3].tex
[0] = 1.0F
;
1677 verts
[3].tex
[1] = tex
->Ttop
- 1.0F
;
1679 /* upload new vertex data */
1680 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1683 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1684 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1685 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1686 _mesa_DepthMask(GL_FALSE
);
1687 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1688 mask
&= ~GL_COLOR_BUFFER_BIT
;
1691 if ((mask
& GL_DEPTH_BUFFER_BIT
) &&
1692 _mesa_is_desktop_gl(ctx
) &&
1693 ctx
->Extensions
.ARB_depth_texture
&&
1694 ctx
->Extensions
.ARB_fragment_program
) {
1696 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1700 newTex
= alloc_texture(depthTex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1701 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
, GL_DEPTH_COMPONENT
,
1702 GL_UNSIGNED_INT
, tmp
);
1703 setup_drawpix_texture(ctx
, depthTex
, newTex
,
1704 srcW
, srcH
, GL_DEPTH_COMPONENT
,
1705 GL_UNSIGNED_INT
, tmp
);
1707 /* texcoords (after texture allocation!) */
1709 verts
[0].tex
[0] = 0.0F
;
1710 verts
[0].tex
[1] = 0.0F
;
1711 verts
[1].tex
[0] = depthTex
->Sright
;
1712 verts
[1].tex
[1] = 0.0F
;
1713 verts
[2].tex
[0] = depthTex
->Sright
;
1714 verts
[2].tex
[1] = depthTex
->Ttop
;
1715 verts
[3].tex
[0] = 0.0F
;
1716 verts
[3].tex
[1] = depthTex
->Ttop
;
1718 /* upload new vertex data */
1719 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1723 init_blit_depth_pixels(ctx
);
1725 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1726 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1727 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1728 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1729 _mesa_DepthFunc(GL_ALWAYS
);
1730 _mesa_DepthMask(GL_TRUE
);
1732 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1733 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1734 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1735 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1741 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1742 /* XXX can't easily do stencil */
1745 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1746 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1748 _mesa_meta_end(ctx
);
1751 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1752 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1757 meta_glsl_blit_cleanup(struct blit_state
*blit
)
1760 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
1762 _mesa_DeleteBuffers(1, &blit
->VBO
);
1765 if (blit
->DepthFP
) {
1766 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
1770 blit_shader_table_cleanup(&blit
->shaders
);
1772 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
1773 blit
->depthTex
.TexObj
= 0;
1778 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1781 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1783 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1784 struct vertex verts
[4];
1785 /* save all state but scissor, pixel pack/unpack */
1786 GLbitfield metaSave
= (MESA_META_ALL
-
1788 MESA_META_PIXEL_STORE
-
1789 MESA_META_CONDITIONAL_RENDER
-
1790 MESA_META_FRAMEBUFFER_SRGB
);
1791 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1793 if (buffers
& BUFFER_BITS_COLOR
) {
1794 /* if clearing color buffers, don't save/restore colormask */
1795 metaSave
-= MESA_META_COLOR_MASK
;
1798 _mesa_meta_begin(ctx
, metaSave
);
1800 setup_vertex_objects(&clear
->VAO
, &clear
->VBO
, false, 3, 0, 4);
1802 /* GL_COLOR_BUFFER_BIT */
1803 if (buffers
& BUFFER_BITS_COLOR
) {
1804 /* leave colormask, glDrawBuffer state as-is */
1806 /* Clears never have the color clamped. */
1807 if (ctx
->Extensions
.ARB_color_buffer_float
)
1808 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1811 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1812 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1815 /* GL_DEPTH_BUFFER_BIT */
1816 if (buffers
& BUFFER_BIT_DEPTH
) {
1817 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1818 _mesa_DepthFunc(GL_ALWAYS
);
1819 _mesa_DepthMask(GL_TRUE
);
1822 assert(!ctx
->Depth
.Test
);
1825 /* GL_STENCIL_BUFFER_BIT */
1826 if (buffers
& BUFFER_BIT_STENCIL
) {
1827 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1828 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1829 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1830 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1831 ctx
->Stencil
.Clear
& stencilMax
,
1832 ctx
->Stencil
.WriteMask
[0]);
1835 assert(!ctx
->Stencil
.Enabled
);
1838 /* vertex positions/colors */
1840 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1841 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1842 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1843 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1844 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1861 for (i
= 0; i
< 4; i
++) {
1862 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1863 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1864 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1865 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1868 /* upload new vertex data */
1869 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1870 GL_DYNAMIC_DRAW_ARB
);
1874 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1876 _mesa_meta_end(ctx
);
1880 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1882 const char *vs_source
=
1883 "attribute vec4 position;\n"
1886 " gl_Position = position;\n"
1888 const char *gs_source
=
1890 "layout(triangles) in;\n"
1891 "layout(triangle_strip, max_vertices = 4) out;\n"
1892 "uniform int layer;\n"
1895 " for (int i = 0; i < 3; i++) {\n"
1896 " gl_Layer = layer;\n"
1897 " gl_Position = gl_in[i].gl_Position;\n"
1901 const char *fs_source
=
1903 "precision highp float;\n"
1905 "uniform vec4 color;\n"
1908 " gl_FragColor = color;\n"
1910 GLuint vs
, gs
= 0, fs
;
1911 bool has_integer_textures
;
1913 setup_vertex_objects(&clear
->VAO
, &clear
->VBO
, true, 3, 0, 0);
1915 if (clear
->ShaderProg
!= 0)
1918 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1919 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
1920 _mesa_CompileShader(vs
);
1922 if (_mesa_has_geometry_shaders(ctx
)) {
1923 gs
= _mesa_CreateShaderObjectARB(GL_GEOMETRY_SHADER
);
1924 _mesa_ShaderSource(gs
, 1, &gs_source
, NULL
);
1925 _mesa_CompileShader(gs
);
1928 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1929 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
1930 _mesa_CompileShader(fs
);
1932 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1933 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1934 _mesa_DeleteObjectARB(fs
);
1936 _mesa_AttachShader(clear
->ShaderProg
, gs
);
1937 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1938 _mesa_DeleteObjectARB(vs
);
1939 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
1940 _mesa_LinkProgram(clear
->ShaderProg
);
1942 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
1945 clear
->LayerLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
1949 has_integer_textures
= _mesa_is_gles3(ctx
) ||
1950 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
1952 if (has_integer_textures
) {
1953 void *shader_source_mem_ctx
= ralloc_context(NULL
);
1954 const char *vs_int_source
=
1955 ralloc_asprintf(shader_source_mem_ctx
,
1957 "in vec4 position;\n"
1960 " gl_Position = position;\n"
1962 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
1963 const char *fs_int_source
=
1964 ralloc_asprintf(shader_source_mem_ctx
,
1967 "precision highp float;\n"
1969 "uniform ivec4 color;\n"
1970 "out ivec4 out_color;\n"
1974 " out_color = color;\n"
1976 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
1978 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1979 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1980 ralloc_free(shader_source_mem_ctx
);
1982 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1983 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1984 _mesa_DeleteObjectARB(fs
);
1986 _mesa_AttachShader(clear
->IntegerShaderProg
, gs
);
1987 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1988 _mesa_DeleteObjectARB(vs
);
1989 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
1991 /* Note that user-defined out attributes get automatically assigned
1992 * locations starting from 0, so we don't need to explicitly
1993 * BindFragDataLocation to 0.
1996 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1998 clear
->IntegerColorLocation
=
1999 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
2001 clear
->IntegerLayerLocation
=
2002 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "layer");
2006 _mesa_DeleteObjectARB(gs
);
2010 meta_glsl_clear_cleanup(struct clear_state
*clear
)
2012 if (clear
->VAO
== 0)
2014 _mesa_DeleteVertexArrays(1, &clear
->VAO
);
2016 _mesa_DeleteBuffers(1, &clear
->VBO
);
2018 _mesa_DeleteObjectARB(clear
->ShaderProg
);
2019 clear
->ShaderProg
= 0;
2021 if (clear
->IntegerShaderProg
) {
2022 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
2023 clear
->IntegerShaderProg
= 0;
2028 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2031 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2033 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2034 GLbitfield metaSave
;
2035 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2036 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
2037 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
2038 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
2039 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
2040 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
2041 const float z
= -invert_z(ctx
->Depth
.Clear
);
2042 struct vertex verts
[4];
2044 metaSave
= (MESA_META_ALPHA_TEST
|
2046 MESA_META_DEPTH_TEST
|
2047 MESA_META_RASTERIZATION
|
2049 MESA_META_STENCIL_TEST
|
2051 MESA_META_VIEWPORT
|
2053 MESA_META_CLAMP_FRAGMENT_COLOR
|
2054 MESA_META_MULTISAMPLE
|
2055 MESA_META_OCCLUSION_QUERY
);
2057 if (!(buffers
& BUFFER_BITS_COLOR
)) {
2058 /* We'll use colormask to disable color writes. Otherwise,
2059 * respect color mask
2061 metaSave
|= MESA_META_COLOR_MASK
;
2064 _mesa_meta_begin(ctx
, metaSave
);
2066 meta_glsl_clear_init(ctx
, clear
);
2068 if (fb
->_IntegerColor
) {
2069 _mesa_UseProgram(clear
->IntegerShaderProg
);
2070 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
2071 ctx
->Color
.ClearColor
.i
);
2073 _mesa_UseProgram(clear
->ShaderProg
);
2074 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
2075 ctx
->Color
.ClearColor
.f
);
2078 /* GL_COLOR_BUFFER_BIT */
2079 if (buffers
& BUFFER_BITS_COLOR
) {
2080 /* leave colormask, glDrawBuffer state as-is */
2082 /* Clears never have the color clamped. */
2083 if (ctx
->Extensions
.ARB_color_buffer_float
)
2084 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2087 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2088 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2091 /* GL_DEPTH_BUFFER_BIT */
2092 if (buffers
& BUFFER_BIT_DEPTH
) {
2093 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2094 _mesa_DepthFunc(GL_ALWAYS
);
2095 _mesa_DepthMask(GL_TRUE
);
2098 assert(!ctx
->Depth
.Test
);
2101 /* GL_STENCIL_BUFFER_BIT */
2102 if (buffers
& BUFFER_BIT_STENCIL
) {
2103 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2104 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2105 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2106 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2107 ctx
->Stencil
.Clear
& stencilMax
,
2108 ctx
->Stencil
.WriteMask
[0]);
2111 assert(!ctx
->Stencil
.Enabled
);
2114 /* vertex positions */
2128 /* upload new vertex data */
2129 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2130 GL_DYNAMIC_DRAW_ARB
);
2133 if (fb
->MaxNumLayers
> 0) {
2135 for (layer
= 0; layer
< fb
->MaxNumLayers
; layer
++) {
2136 if (fb
->_IntegerColor
)
2137 _mesa_Uniform1i(clear
->IntegerLayerLocation
, layer
);
2139 _mesa_Uniform1i(clear
->LayerLocation
, layer
);
2140 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2143 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2146 _mesa_meta_end(ctx
);
2150 * Meta implementation of ctx->Driver.CopyPixels() in terms
2151 * of texture mapping and polygon rendering and GLSL shaders.
2154 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2155 GLsizei width
, GLsizei height
,
2156 GLint dstX
, GLint dstY
, GLenum type
)
2158 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2159 struct temp_texture
*tex
= get_temp_texture(ctx
);
2160 struct vertex verts
[4];
2162 if (type
!= GL_COLOR
||
2163 ctx
->_ImageTransferState
||
2165 width
> tex
->MaxSize
||
2166 height
> tex
->MaxSize
) {
2167 /* XXX avoid this fallback */
2168 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2172 /* Most GL state applies to glCopyPixels, but a there's a few things
2173 * we need to override:
2175 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2178 MESA_META_TRANSFORM
|
2181 MESA_META_VIEWPORT
));
2183 setup_vertex_objects(©pix
->VAO
, ©pix
->VBO
, false, 3, 2, 0);
2185 /* Silence valgrind warnings about reading uninitialized stack. */
2186 memset(verts
, 0, sizeof(verts
));
2188 /* Alloc/setup texture */
2189 setup_copypix_texture(ctx
, tex
, srcX
, srcY
, width
, height
,
2190 GL_RGBA
, GL_NEAREST
);
2192 /* vertex positions, texcoords (after texture allocation!) */
2194 const GLfloat dstX0
= (GLfloat
) dstX
;
2195 const GLfloat dstY0
= (GLfloat
) dstY
;
2196 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2197 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2198 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2203 verts
[0].tex
[0] = 0.0F
;
2204 verts
[0].tex
[1] = 0.0F
;
2208 verts
[1].tex
[0] = tex
->Sright
;
2209 verts
[1].tex
[1] = 0.0F
;
2213 verts
[2].tex
[0] = tex
->Sright
;
2214 verts
[2].tex
[1] = tex
->Ttop
;
2218 verts
[3].tex
[0] = 0.0F
;
2219 verts
[3].tex
[1] = tex
->Ttop
;
2221 /* upload new vertex data */
2222 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2225 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2227 /* draw textured quad */
2228 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2230 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2232 _mesa_meta_end(ctx
);
2236 meta_drawpix_cleanup(struct drawpix_state
*drawpix
)
2238 if (drawpix
->VAO
!= 0) {
2239 _mesa_DeleteVertexArrays(1, &drawpix
->VAO
);
2242 _mesa_DeleteBuffers(1, &drawpix
->VBO
);
2246 if (drawpix
->StencilFP
!= 0) {
2247 _mesa_DeleteProgramsARB(1, &drawpix
->StencilFP
);
2248 drawpix
->StencilFP
= 0;
2251 if (drawpix
->DepthFP
!= 0) {
2252 _mesa_DeleteProgramsARB(1, &drawpix
->DepthFP
);
2253 drawpix
->DepthFP
= 0;
2258 * When the glDrawPixels() image size is greater than the max rectangle
2259 * texture size we use this function to break the glDrawPixels() image
2260 * into tiles which fit into the max texture size.
2263 tiled_draw_pixels(struct gl_context
*ctx
,
2265 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2266 GLenum format
, GLenum type
,
2267 const struct gl_pixelstore_attrib
*unpack
,
2268 const GLvoid
*pixels
)
2270 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2273 if (tileUnpack
.RowLength
== 0)
2274 tileUnpack
.RowLength
= width
;
2276 for (i
= 0; i
< width
; i
+= tileSize
) {
2277 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2278 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2280 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2282 for (j
= 0; j
< height
; j
+= tileSize
) {
2283 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2284 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2286 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2288 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2289 format
, type
, &tileUnpack
, pixels
);
2296 * One-time init for drawing stencil pixels.
2299 init_draw_stencil_pixels(struct gl_context
*ctx
)
2301 /* This program is run eight times, once for each stencil bit.
2302 * The stencil values to draw are found in an 8-bit alpha texture.
2303 * We read the texture/stencil value and test if bit 'b' is set.
2304 * If the bit is not set, use KIL to kill the fragment.
2305 * Finally, we use the stencil test to update the stencil buffer.
2307 * The basic algorithm for checking if a bit is set is:
2308 * if (is_odd(value / (1 << bit)))
2309 * result is one (or non-zero).
2312 * The program parameter contains three values:
2313 * parm.x = 255 / (1 << bit)
2317 static const char *program
=
2319 "PARAM parm = program.local[0]; \n"
2321 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2322 "# t = t * 255 / bit \n"
2323 "MUL t.x, t.a, parm.x; \n"
2326 "SUB t.x, t.x, t.y; \n"
2328 "MUL t.x, t.x, parm.y; \n"
2329 "# t = fract(t.x) \n"
2330 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2331 "# t.x = (t.x == 0 ? 1 : 0) \n"
2332 "SGE t.x, -t.x, parm.z; \n"
2334 "# for debug only \n"
2335 "#MOV result.color, t.x; \n"
2337 char program2
[1000];
2338 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2339 struct temp_texture
*tex
= get_temp_texture(ctx
);
2340 const char *texTarget
;
2342 assert(drawpix
->StencilFP
== 0);
2344 /* replace %s with "RECT" or "2D" */
2345 assert(strlen(program
) + 4 < sizeof(program2
));
2346 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2350 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2352 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2353 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2354 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2355 strlen(program2
), (const GLubyte
*) program2
);
2360 * One-time init for drawing depth pixels.
2363 init_draw_depth_pixels(struct gl_context
*ctx
)
2365 static const char *program
=
2367 "PARAM color = program.local[0]; \n"
2368 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2369 "MOV result.color, color; \n"
2372 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2373 struct temp_texture
*tex
= get_temp_texture(ctx
);
2374 const char *texTarget
;
2376 assert(drawpix
->DepthFP
== 0);
2378 /* replace %s with "RECT" or "2D" */
2379 assert(strlen(program
) + 4 < sizeof(program2
));
2380 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2384 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2386 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2387 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2388 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2389 strlen(program2
), (const GLubyte
*) program2
);
2394 * Meta implementation of ctx->Driver.DrawPixels() in terms
2395 * of texture mapping and polygon rendering.
2398 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2399 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2400 GLenum format
, GLenum type
,
2401 const struct gl_pixelstore_attrib
*unpack
,
2402 const GLvoid
*pixels
)
2404 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2405 struct temp_texture
*tex
= get_temp_texture(ctx
);
2406 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2407 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2408 struct vertex verts
[4];
2409 GLenum texIntFormat
;
2410 GLboolean fallback
, newTex
;
2411 GLbitfield metaExtraSave
= 0x0;
2414 * Determine if we can do the glDrawPixels with texture mapping.
2416 fallback
= GL_FALSE
;
2417 if (ctx
->Fog
.Enabled
) {
2421 if (_mesa_is_color_format(format
)) {
2422 /* use more compact format when possible */
2423 /* XXX disable special case for GL_LUMINANCE for now to work around
2424 * apparent i965 driver bug (see bug #23670).
2426 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2427 texIntFormat
= format
;
2429 texIntFormat
= GL_RGBA
;
2431 /* If we're not supposed to clamp the resulting color, then just
2432 * promote our texture to fully float. We could do better by
2433 * just going for the matching set of channels, in floating
2436 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2437 ctx
->Extensions
.ARB_texture_float
)
2438 texIntFormat
= GL_RGBA32F
;
2440 else if (_mesa_is_stencil_format(format
)) {
2441 if (ctx
->Extensions
.ARB_fragment_program
&&
2442 ctx
->Pixel
.IndexShift
== 0 &&
2443 ctx
->Pixel
.IndexOffset
== 0 &&
2444 type
== GL_UNSIGNED_BYTE
) {
2445 /* We'll store stencil as alpha. This only works for GLubyte
2446 * image data because of how incoming values are mapped to alpha
2449 texIntFormat
= GL_ALPHA
;
2450 metaExtraSave
= (MESA_META_COLOR_MASK
|
2451 MESA_META_DEPTH_TEST
|
2452 MESA_META_PIXEL_TRANSFER
|
2454 MESA_META_STENCIL_TEST
);
2460 else if (_mesa_is_depth_format(format
)) {
2461 if (ctx
->Extensions
.ARB_depth_texture
&&
2462 ctx
->Extensions
.ARB_fragment_program
) {
2463 texIntFormat
= GL_DEPTH_COMPONENT
;
2464 metaExtraSave
= (MESA_META_SHADER
);
2475 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2476 format
, type
, unpack
, pixels
);
2481 * Check image size against max texture size, draw as tiles if needed.
2483 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2484 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2485 format
, type
, unpack
, pixels
);
2489 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2490 * but a there's a few things we need to override:
2492 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2495 MESA_META_TRANSFORM
|
2498 MESA_META_VIEWPORT
|
2501 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2503 setup_vertex_objects(&drawpix
->VAO
, &drawpix
->VBO
, false, 3, 2, 0);
2505 /* Silence valgrind warnings about reading uninitialized stack. */
2506 memset(verts
, 0, sizeof(verts
));
2508 /* vertex positions, texcoords (after texture allocation!) */
2510 const GLfloat x0
= (GLfloat
) x
;
2511 const GLfloat y0
= (GLfloat
) y
;
2512 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2513 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2514 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2519 verts
[0].tex
[0] = 0.0F
;
2520 verts
[0].tex
[1] = 0.0F
;
2524 verts
[1].tex
[0] = tex
->Sright
;
2525 verts
[1].tex
[1] = 0.0F
;
2529 verts
[2].tex
[0] = tex
->Sright
;
2530 verts
[2].tex
[1] = tex
->Ttop
;
2534 verts
[3].tex
[0] = 0.0F
;
2535 verts
[3].tex
[1] = tex
->Ttop
;
2538 /* upload new vertex data */
2539 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2540 verts
, GL_DYNAMIC_DRAW_ARB
);
2542 /* set given unpack params */
2543 ctx
->Unpack
= *unpack
;
2545 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2547 if (_mesa_is_stencil_format(format
)) {
2548 /* Drawing stencil */
2551 if (!drawpix
->StencilFP
)
2552 init_draw_stencil_pixels(ctx
);
2554 setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2555 GL_ALPHA
, type
, pixels
);
2557 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2559 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2561 /* set all stencil bits to 0 */
2562 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2563 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2564 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2566 /* set stencil bits to 1 where needed */
2567 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2569 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2570 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2572 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2573 const GLuint mask
= 1 << bit
;
2574 if (mask
& origStencilMask
) {
2575 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2576 _mesa_StencilMask(mask
);
2578 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2579 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2581 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2585 else if (_mesa_is_depth_format(format
)) {
2587 if (!drawpix
->DepthFP
)
2588 init_draw_depth_pixels(ctx
);
2590 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2591 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2593 /* polygon color = current raster color */
2594 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2595 ctx
->Current
.RasterColor
);
2597 setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2598 format
, type
, pixels
);
2600 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2604 setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2605 format
, type
, pixels
);
2606 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2609 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2611 /* restore unpack params */
2612 ctx
->Unpack
= unpackSave
;
2614 _mesa_meta_end(ctx
);
2618 alpha_test_raster_color(struct gl_context
*ctx
)
2620 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2621 GLfloat ref
= ctx
->Color
.AlphaRef
;
2623 switch (ctx
->Color
.AlphaFunc
) {
2629 return alpha
== ref
;
2631 return alpha
<= ref
;
2635 return alpha
!= ref
;
2637 return alpha
>= ref
;
2647 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2648 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2649 * tracker would improve performance a lot.
2652 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2653 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2654 const struct gl_pixelstore_attrib
*unpack
,
2655 const GLubyte
*bitmap1
)
2657 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2658 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2659 const GLenum texIntFormat
= GL_ALPHA
;
2660 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2662 struct vertex verts
[4];
2667 * Check if swrast fallback is needed.
2669 if (ctx
->_ImageTransferState
||
2670 ctx
->FragmentProgram
._Enabled
||
2672 ctx
->Texture
._EnabledUnits
||
2673 width
> tex
->MaxSize
||
2674 height
> tex
->MaxSize
) {
2675 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2679 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2682 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2683 * but a there's a few things we need to override:
2685 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2686 MESA_META_PIXEL_STORE
|
2687 MESA_META_RASTERIZATION
|
2690 MESA_META_TRANSFORM
|
2693 MESA_META_VIEWPORT
));
2695 setup_vertex_objects(&bitmap
->VAO
, &bitmap
->VBO
, false, 3, 2, 4);
2697 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2699 /* Silence valgrind warnings about reading uninitialized stack. */
2700 memset(verts
, 0, sizeof(verts
));
2702 /* vertex positions, texcoords, colors (after texture allocation!) */
2704 const GLfloat x0
= (GLfloat
) x
;
2705 const GLfloat y0
= (GLfloat
) y
;
2706 const GLfloat x1
= (GLfloat
) (x
+ width
);
2707 const GLfloat y1
= (GLfloat
) (y
+ height
);
2708 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2714 verts
[0].tex
[0] = 0.0F
;
2715 verts
[0].tex
[1] = 0.0F
;
2719 verts
[1].tex
[0] = tex
->Sright
;
2720 verts
[1].tex
[1] = 0.0F
;
2724 verts
[2].tex
[0] = tex
->Sright
;
2725 verts
[2].tex
[1] = tex
->Ttop
;
2729 verts
[3].tex
[0] = 0.0F
;
2730 verts
[3].tex
[1] = tex
->Ttop
;
2732 for (i
= 0; i
< 4; i
++) {
2733 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2734 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2735 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2736 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2739 /* upload new vertex data */
2740 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2743 /* choose different foreground/background alpha values */
2744 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2745 bg
= (fg
> 127 ? 0 : 255);
2747 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2749 _mesa_meta_end(ctx
);
2753 bitmap8
= malloc(width
* height
);
2755 memset(bitmap8
, bg
, width
* height
);
2756 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2757 bitmap8
, width
, fg
);
2759 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2761 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2762 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2764 setup_drawpix_texture(ctx
, tex
, newTex
, width
, height
,
2765 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2767 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2769 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2774 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2776 _mesa_meta_end(ctx
);
2781 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2782 * software fallback. The fallback path will require that the texture
2783 * images are mapped.
2784 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2787 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2788 struct gl_texture_object
*texObj
)
2790 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2791 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2792 struct gl_texture_image
*baseImage
;
2796 /* check for fallbacks */
2797 if (target
== GL_TEXTURE_3D
||
2798 target
== GL_TEXTURE_1D_ARRAY
||
2799 target
== GL_TEXTURE_2D_ARRAY
) {
2800 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
2801 "glGenerateMipmap() to %s target\n",
2802 _mesa_lookup_enum_by_nr(target
));
2806 srcLevel
= texObj
->BaseLevel
;
2807 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2809 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
2810 "glGenerateMipmap() couldn't find base teximage\n");
2814 if (_mesa_is_format_compressed(baseImage
->TexFormat
)) {
2815 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
2816 "glGenerateMipmap() with %s format\n",
2817 _mesa_get_format_name(baseImage
->TexFormat
));
2821 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2822 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2823 /* The texture format is sRGB but we can't turn off sRGB->linear
2824 * texture sample conversion. So we won't be able to generate the
2825 * right colors when rendering. Need to use a fallback.
2827 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
2828 "glGenerateMipmap() of sRGB texture without "
2834 * Test that we can actually render in the texture's format.
2837 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
2838 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2840 if (target
== GL_TEXTURE_1D
) {
2841 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
2842 GL_COLOR_ATTACHMENT0_EXT
,
2843 target
, texObj
->Name
, srcLevel
);
2846 /* other work is needed to enable 3D mipmap generation */
2847 else if (target
== GL_TEXTURE_3D
) {
2849 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
2850 GL_COLOR_ATTACHMENT0_EXT
,
2851 target
, texObj
->Name
, srcLevel
, zoffset
);
2856 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
2857 GL_COLOR_ATTACHMENT0_EXT
,
2858 target
, texObj
->Name
, srcLevel
);
2861 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
2863 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
2865 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2866 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
2867 "glGenerateMipmap() got incomplete FBO\n");
2876 * Compute the texture coordinates for the four vertices of a quad for
2877 * drawing a 2D texture image or slice of a cube/3D texture.
2878 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2879 * \param slice slice of a 1D/2D array texture or 3D texture
2880 * \param width width of the texture image
2881 * \param height height of the texture image
2882 * \param coords0/1/2/3 returns the computed texcoords
2885 setup_texture_coords(GLenum faceTarget
,
2895 static const GLfloat st
[4][2] = {
2896 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2901 /* Currently all texture targets want the W component to be 1.0.
2908 switch (faceTarget
) {
2912 case GL_TEXTURE_2D_ARRAY
:
2913 if (faceTarget
== GL_TEXTURE_3D
) {
2914 assert(slice
< depth
);
2916 r
= (slice
+ 0.5f
) / depth
;
2918 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2922 coords0
[0] = 0.0F
; /* s */
2923 coords0
[1] = 0.0F
; /* t */
2924 coords0
[2] = r
; /* r */
2935 case GL_TEXTURE_RECTANGLE_ARB
:
2936 coords0
[0] = 0.0F
; /* s */
2937 coords0
[1] = 0.0F
; /* t */
2938 coords0
[2] = 0.0F
; /* r */
2939 coords1
[0] = (float) width
;
2942 coords2
[0] = (float) width
;
2943 coords2
[1] = (float) height
;
2946 coords3
[1] = (float) height
;
2949 case GL_TEXTURE_1D_ARRAY
:
2950 coords0
[0] = 0.0F
; /* s */
2951 coords0
[1] = (float) slice
; /* t */
2952 coords0
[2] = 0.0F
; /* r */
2954 coords1
[1] = (float) slice
;
2957 coords2
[1] = (float) slice
;
2960 coords3
[1] = (float) slice
;
2964 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2965 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2966 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2967 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2968 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2969 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2970 /* loop over quad verts */
2971 for (i
= 0; i
< 4; i
++) {
2972 /* Compute sc = +/-scale and tc = +/-scale.
2973 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2974 * though that can still sometimes happen with this scale factor...
2976 const GLfloat scale
= 0.9999f
;
2977 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2978 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2998 coord
[3] = (float) (slice
/ 6);
3000 switch (faceTarget
) {
3001 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3006 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3011 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3016 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3021 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3026 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3037 assert(0 && "unexpected target in meta setup_texture_coords()");
3041 static struct blit_shader
*
3042 choose_blit_shader(GLenum target
, struct blit_shader_table
*table
)
3046 table
->sampler_1d
.type
= "sampler1D";
3047 table
->sampler_1d
.func
= "texture1D";
3048 table
->sampler_1d
.texcoords
= "texCoords.x";
3049 return &table
->sampler_1d
;
3051 table
->sampler_2d
.type
= "sampler2D";
3052 table
->sampler_2d
.func
= "texture2D";
3053 table
->sampler_2d
.texcoords
= "texCoords.xy";
3054 return &table
->sampler_2d
;
3055 case GL_TEXTURE_RECTANGLE
:
3056 table
->sampler_rect
.type
= "sampler2DRect";
3057 table
->sampler_rect
.func
= "texture2DRect";
3058 table
->sampler_rect
.texcoords
= "texCoords.xy";
3059 return &table
->sampler_rect
;
3061 /* Code for mipmap generation with 3D textures is not used yet.
3062 * It's a sw fallback.
3064 table
->sampler_3d
.type
= "sampler3D";
3065 table
->sampler_3d
.func
= "texture3D";
3066 table
->sampler_3d
.texcoords
= "texCoords.xyz";
3067 return &table
->sampler_3d
;
3068 case GL_TEXTURE_CUBE_MAP
:
3069 table
->sampler_cubemap
.type
= "samplerCube";
3070 table
->sampler_cubemap
.func
= "textureCube";
3071 table
->sampler_cubemap
.texcoords
= "texCoords.xyz";
3072 return &table
->sampler_cubemap
;
3073 case GL_TEXTURE_1D_ARRAY
:
3074 table
->sampler_1d_array
.type
= "sampler1DArray";
3075 table
->sampler_1d_array
.func
= "texture1DArray";
3076 table
->sampler_1d_array
.texcoords
= "texCoords.xy";
3077 return &table
->sampler_1d_array
;
3078 case GL_TEXTURE_2D_ARRAY
:
3079 table
->sampler_2d_array
.type
= "sampler2DArray";
3080 table
->sampler_2d_array
.func
= "texture2DArray";
3081 table
->sampler_2d_array
.texcoords
= "texCoords.xyz";
3082 return &table
->sampler_2d_array
;
3083 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3084 table
->sampler_cubemap_array
.type
= "samplerCubeArray";
3085 table
->sampler_cubemap_array
.func
= "textureCubeArray";
3086 table
->sampler_cubemap_array
.texcoords
= "texCoords.xyzw";
3087 return &table
->sampler_cubemap_array
;
3089 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3090 " setup_texture_sampler()\n", target
);
3096 blit_shader_table_cleanup(struct blit_shader_table
*table
)
3098 _mesa_DeleteObjectARB(table
->sampler_1d
.shader_prog
);
3099 _mesa_DeleteObjectARB(table
->sampler_2d
.shader_prog
);
3100 _mesa_DeleteObjectARB(table
->sampler_3d
.shader_prog
);
3101 _mesa_DeleteObjectARB(table
->sampler_rect
.shader_prog
);
3102 _mesa_DeleteObjectARB(table
->sampler_cubemap
.shader_prog
);
3103 _mesa_DeleteObjectARB(table
->sampler_1d_array
.shader_prog
);
3104 _mesa_DeleteObjectARB(table
->sampler_2d_array
.shader_prog
);
3105 _mesa_DeleteObjectARB(table
->sampler_cubemap_array
.shader_prog
);
3107 table
->sampler_1d
.shader_prog
= 0;
3108 table
->sampler_2d
.shader_prog
= 0;
3109 table
->sampler_3d
.shader_prog
= 0;
3110 table
->sampler_rect
.shader_prog
= 0;
3111 table
->sampler_cubemap
.shader_prog
= 0;
3112 table
->sampler_1d_array
.shader_prog
= 0;
3113 table
->sampler_2d_array
.shader_prog
= 0;
3114 table
->sampler_cubemap_array
.shader_prog
= 0;
3118 meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state
*mipmap
)
3120 if (mipmap
->VAO
== 0)
3122 _mesa_DeleteVertexArrays(1, &mipmap
->VAO
);
3124 _mesa_DeleteBuffers(1, &mipmap
->VBO
);
3127 blit_shader_table_cleanup(&mipmap
->shaders
);
3132 * Called via ctx->Driver.GenerateMipmap()
3133 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3137 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3138 struct gl_texture_object
*texObj
)
3140 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3141 struct vertex verts
[4];
3142 const GLuint baseLevel
= texObj
->BaseLevel
;
3143 const GLuint maxLevel
= texObj
->MaxLevel
;
3144 const GLint maxLevelSave
= texObj
->MaxLevel
;
3145 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3146 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3147 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3148 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3149 ctx
->Extensions
.ARB_fragment_shader
&&
3150 (ctx
->API
!= API_OPENGLES
);
3153 const GLint slice
= 0;
3156 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3157 _mesa_generate_mipmap(ctx
, target
, texObj
);
3161 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3162 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3163 faceTarget
= target
;
3164 target
= GL_TEXTURE_CUBE_MAP
;
3167 faceTarget
= target
;
3170 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3172 /* Choose between glsl version and fixed function version of
3173 * GenerateMipmap function.
3175 if (use_glsl_version
) {
3176 setup_vertex_objects(&mipmap
->VAO
, &mipmap
->VBO
, true,
3178 setup_blit_shader(ctx
, target
, &mipmap
->shaders
);
3181 setup_ff_tnl_for_blit(&mipmap
->VAO
, &mipmap
->VBO
, 3);
3182 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3185 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3186 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3188 if (currentTexUnitSave
!= 0)
3189 _mesa_BindTexture(target
, texObj
->Name
);
3192 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3195 if (!mipmap
->Sampler
) {
3196 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3197 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3199 _mesa_SamplerParameteri(mipmap
->Sampler
,
3200 GL_TEXTURE_MIN_FILTER
,
3201 GL_LINEAR_MIPMAP_LINEAR
);
3202 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3203 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3204 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3205 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3207 /* We don't want to encode or decode sRGB values; treat them as linear.
3208 * This is not technically correct for GLES3 but we don't get any API
3209 * error at the moment.
3211 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3212 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3213 GL_SKIP_DECODE_EXT
);
3217 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3220 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3222 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
3223 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3225 assert(!genMipmapSave
);
3227 /* Silence valgrind warnings about reading uninitialized stack. */
3228 memset(verts
, 0, sizeof(verts
));
3230 /* Setup texture coordinates */
3231 setup_texture_coords(faceTarget
,
3233 0, 0, 1, /* width, height never used here */
3239 /* setup vertex positions */
3249 /* upload vertex data */
3250 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3251 verts
, GL_DYNAMIC_DRAW_ARB
);
3253 /* texture is already locked, unlock now */
3254 _mesa_unlock_texture(ctx
, texObj
);
3256 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3257 const struct gl_texture_image
*srcImage
;
3258 const GLuint srcLevel
= dstLevel
- 1;
3259 GLsizei srcWidth
, srcHeight
, srcDepth
;
3260 GLsizei dstWidth
, dstHeight
, dstDepth
;
3263 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3264 assert(srcImage
->Border
== 0);
3267 srcWidth
= srcImage
->Width
;
3268 srcHeight
= srcImage
->Height
;
3269 srcDepth
= srcImage
->Depth
;
3272 dstWidth
= MAX2(1, srcWidth
/ 2);
3273 dstHeight
= MAX2(1, srcHeight
/ 2);
3274 dstDepth
= MAX2(1, srcDepth
/ 2);
3276 if (dstWidth
== srcImage
->Width
&&
3277 dstHeight
== srcImage
->Height
&&
3278 dstDepth
== srcImage
->Depth
) {
3283 /* Allocate storage for the destination mipmap image(s) */
3285 /* Set MaxLevel large enough to hold the new level when we allocate it */
3286 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3288 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3289 dstWidth
, dstHeight
, dstDepth
,
3291 srcImage
->InternalFormat
,
3292 srcImage
->TexFormat
)) {
3293 /* All done. We either ran out of memory or we would go beyond the
3294 * last valid level of an immutable texture if we continued.
3299 /* limit minification to src level */
3300 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3302 /* Set to draw into the current dstLevel */
3303 if (target
== GL_TEXTURE_1D
) {
3304 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3305 GL_COLOR_ATTACHMENT0_EXT
,
3310 else if (target
== GL_TEXTURE_3D
) {
3311 GLint zoffset
= 0; /* XXX unfinished */
3312 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3313 GL_COLOR_ATTACHMENT0_EXT
,
3320 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3321 GL_COLOR_ATTACHMENT0_EXT
,
3327 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3330 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3331 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3332 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3333 "_mesa_meta_GenerateMipmap()");
3337 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3338 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3340 /* setup viewport */
3341 _mesa_set_viewport(ctx
, 0, 0, 0, dstWidth
, dstHeight
);
3343 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3346 _mesa_lock_texture(ctx
, texObj
); /* relock */
3348 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3350 _mesa_meta_end(ctx
);
3352 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3354 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3356 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3361 * Determine the GL data type to use for the temporary image read with
3362 * ReadPixels() and passed to Tex[Sub]Image().
3365 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
3369 baseFormat
= _mesa_get_format_base_format(format
);
3371 switch (baseFormat
) {
3378 case GL_LUMINANCE_ALPHA
:
3380 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
3381 return GL_UNSIGNED_BYTE
;
3382 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
3383 return GL_UNSIGNED_SHORT
;
3385 GLenum datatype
= _mesa_get_format_datatype(format
);
3386 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
3390 case GL_DEPTH_COMPONENT
: {
3391 GLenum datatype
= _mesa_get_format_datatype(format
);
3392 if (datatype
== GL_FLOAT
)
3395 return GL_UNSIGNED_INT
;
3397 case GL_DEPTH_STENCIL
: {
3398 GLenum datatype
= _mesa_get_format_datatype(format
);
3399 if (datatype
== GL_FLOAT
)
3400 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
3402 return GL_UNSIGNED_INT_24_8
;
3405 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3413 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3414 * Have to be careful with locking and meta state for pixel transfer.
3417 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3418 struct gl_texture_image
*texImage
,
3419 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3420 struct gl_renderbuffer
*rb
,
3422 GLsizei width
, GLsizei height
)
3424 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3425 GLenum format
, type
;
3429 /* The gl_renderbuffer is part of the interface for
3430 * dd_function_table::CopyTexSubImage, but this implementation does not use
3435 /* Choose format/type for temporary image buffer */
3436 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3437 if (format
== GL_LUMINANCE
||
3438 format
== GL_LUMINANCE_ALPHA
||
3439 format
== GL_INTENSITY
) {
3440 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3441 * temp image buffer because glReadPixels will do L=R+G+B which is
3442 * not what we want (should be L=R).
3447 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3448 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3449 format
= _mesa_base_format_to_integer_format(format
);
3451 bpp
= _mesa_bytes_per_pixel(format
, type
);
3453 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3458 * Alloc image buffer (XXX could use a PBO)
3460 buf
= malloc(width
* height
* bpp
);
3462 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3466 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3469 * Read image from framebuffer (disable pixel transfer ops)
3471 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3472 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3473 format
, type
, &ctx
->Pack
, buf
);
3474 _mesa_meta_end(ctx
);
3476 _mesa_update_state(ctx
); /* to update pixel transfer state */
3479 * Store texture data (with pixel transfer ops)
3481 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3483 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3484 assert(yoffset
== 0);
3485 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3486 xoffset
, zoffset
, 0, width
, 1, 1,
3487 format
, type
, buf
, &ctx
->Unpack
);
3489 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3490 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3491 format
, type
, buf
, &ctx
->Unpack
);
3494 _mesa_meta_end(ctx
);
3496 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3503 meta_decompress_cleanup(struct decompress_state
*decompress
)
3505 if (decompress
->FBO
!= 0) {
3506 _mesa_DeleteFramebuffers(1, &decompress
->FBO
);
3507 _mesa_DeleteRenderbuffers(1, &decompress
->RBO
);
3510 if (decompress
->VAO
!= 0) {
3511 _mesa_DeleteVertexArrays(1, &decompress
->VAO
);
3512 _mesa_DeleteBuffers(1, &decompress
->VBO
);
3515 if (decompress
->Sampler
!= 0)
3516 _mesa_DeleteSamplers(1, &decompress
->Sampler
);
3518 memset(decompress
, 0, sizeof(*decompress
));
3522 * Decompress a texture image by drawing a quad with the compressed
3523 * texture and reading the pixels out of the color buffer.
3524 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3525 * \param destFormat format, ala glReadPixels
3526 * \param destType type, ala glReadPixels
3527 * \param dest destination buffer
3528 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3531 decompress_texture_image(struct gl_context
*ctx
,
3532 struct gl_texture_image
*texImage
,
3534 GLenum destFormat
, GLenum destType
,
3537 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3538 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3539 const GLint width
= texImage
->Width
;
3540 const GLint height
= texImage
->Height
;
3541 const GLint depth
= texImage
->Height
;
3542 const GLenum target
= texObj
->Target
;
3544 struct vertex verts
[4];
3545 GLuint fboDrawSave
, fboReadSave
;
3548 const bool use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3549 ctx
->Extensions
.ARB_fragment_shader
&&
3550 (ctx
->API
!= API_OPENGLES
);
3553 assert(target
== GL_TEXTURE_3D
||
3554 target
== GL_TEXTURE_2D_ARRAY
||
3555 target
== GL_TEXTURE_CUBE_MAP_ARRAY
);
3560 case GL_TEXTURE_1D_ARRAY
:
3561 assert(!"No compressed 1D textures.");
3565 assert(!"No compressed 3D textures.");
3568 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3569 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ (slice
% 6);
3572 case GL_TEXTURE_CUBE_MAP
:
3573 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3577 faceTarget
= target
;
3581 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3582 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3583 fboReadSave
= ctx
->ReadBuffer
->Name
;
3584 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3586 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3588 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3589 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3591 /* Create/bind FBO/renderbuffer */
3592 if (decompress
->FBO
== 0) {
3593 _mesa_GenFramebuffers(1, &decompress
->FBO
);
3594 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
3595 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3596 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3597 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
3598 GL_COLOR_ATTACHMENT0_EXT
,
3599 GL_RENDERBUFFER_EXT
,
3603 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3606 /* alloc dest surface */
3607 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3608 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3609 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3611 decompress
->Width
= width
;
3612 decompress
->Height
= height
;
3615 if (use_glsl_version
) {
3616 setup_vertex_objects(&decompress
->VAO
, &decompress
->VBO
, true,
3619 setup_blit_shader(ctx
, target
, &decompress
->shaders
);
3621 setup_ff_tnl_for_blit(&decompress
->VAO
, &decompress
->VBO
, 3);
3624 if (!decompress
->Sampler
) {
3625 _mesa_GenSamplers(1, &decompress
->Sampler
);
3626 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3627 /* nearest filtering */
3628 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3629 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3630 /* No sRGB decode or encode.*/
3631 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3632 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3633 GL_SKIP_DECODE_EXT
);
3637 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3640 /* Silence valgrind warnings about reading uninitialized stack. */
3641 memset(verts
, 0, sizeof(verts
));
3643 setup_texture_coords(faceTarget
, slice
, width
, height
, depth
,
3649 /* setup vertex positions */
3659 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
3661 /* upload new vertex data */
3662 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3664 /* setup texture state */
3665 _mesa_BindTexture(target
, texObj
->Name
);
3667 if (!use_glsl_version
)
3668 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3671 /* save texture object state */
3672 const GLint baseLevelSave
= texObj
->BaseLevel
;
3673 const GLint maxLevelSave
= texObj
->MaxLevel
;
3675 /* restrict sampling to the texture level of interest */
3676 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3677 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3678 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3681 /* render quad w/ texture into renderbuffer */
3682 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3684 /* Restore texture object state, the texture binding will
3685 * be restored by _mesa_meta_end().
3687 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3688 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3689 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3694 /* read pixels from renderbuffer */
3696 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3697 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
3699 /* The pixel transfer state will be set to default values at this point
3700 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3701 * turned off (as required by glGetTexImage) but we need to handle some
3702 * special cases. In particular, single-channel texture values are
3703 * returned as red and two-channel texture values are returned as
3706 if ((baseTexFormat
== GL_LUMINANCE
||
3707 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3708 baseTexFormat
== GL_INTENSITY
) ||
3709 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3710 * luminance then we need to return L=tex(R).
3712 ((baseTexFormat
== GL_RGBA
||
3713 baseTexFormat
== GL_RGB
||
3714 baseTexFormat
== GL_RG
) &&
3715 (destBaseFormat
== GL_LUMINANCE
||
3716 destBaseFormat
== GL_LUMINANCE_ALPHA
||
3717 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
3718 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
3719 /* Green and blue must be zero */
3720 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3721 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3724 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3727 /* disable texture unit */
3728 if (!use_glsl_version
)
3729 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3731 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3733 _mesa_meta_end(ctx
);
3735 /* restore fbo bindings */
3736 if (fboDrawSave
== fboReadSave
) {
3737 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3740 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3741 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3743 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, rbSave
);
3748 * This is just a wrapper around _mesa_get_tex_image() and
3749 * decompress_texture_image(). Meta functions should not be directly called
3753 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3754 GLenum format
, GLenum type
, GLvoid
*pixels
,
3755 struct gl_texture_image
*texImage
)
3757 /* We can only use the decompress-with-blit method here if the texels are
3758 * unsigned, normalized values. We could handle signed and unnormalized
3759 * with floating point renderbuffers...
3761 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3762 _mesa_get_format_datatype(texImage
->TexFormat
)
3763 == GL_UNSIGNED_NORMALIZED
) {
3764 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3766 /* Need to unlock the texture here to prevent deadlock... */
3767 _mesa_unlock_texture(ctx
, texObj
);
3768 for (slice
= 0; slice
< texImage
->Depth
; slice
++) {
3770 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
3771 || texImage
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
3772 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3773 * in the decompress_texture_image() function's call to
3774 * glReadPixels but we need to compute the dest slice's address
3775 * here (according to SkipImages and ImageHeight).
3777 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
3778 packing
.SkipPixels
= 0;
3779 packing
.SkipRows
= 0;
3780 dst
= _mesa_image_address3d(&packing
, pixels
, texImage
->Width
,
3781 texImage
->Height
, format
, type
,
3787 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, dst
);
3789 /* ... and relock it */
3790 _mesa_lock_texture(ctx
, texObj
);
3793 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3799 * Meta implementation of ctx->Driver.DrawTex() in terms
3800 * of polygon rendering.
3803 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3804 GLfloat width
, GLfloat height
)
3806 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3808 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3810 struct vertex verts
[4];
3813 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3815 MESA_META_TRANSFORM
|
3817 MESA_META_VIEWPORT
));
3819 if (drawtex
->VAO
== 0) {
3820 /* one-time setup */
3821 GLint active_texture
;
3823 /* create vertex array object */
3824 _mesa_GenVertexArrays(1, &drawtex
->VAO
);
3825 _mesa_BindVertexArray(drawtex
->VAO
);
3827 /* create vertex array buffer */
3828 _mesa_GenBuffers(1, &drawtex
->VBO
);
3829 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3830 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3831 NULL
, GL_DYNAMIC_DRAW_ARB
);
3833 /* client active texture is not part of the array object */
3834 active_texture
= ctx
->Array
.ActiveTexture
;
3836 /* setup vertex arrays */
3837 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3838 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3839 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3840 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
3841 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3842 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3845 /* restore client active texture */
3846 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
3849 _mesa_BindVertexArray(drawtex
->VAO
);
3850 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3853 /* vertex positions, texcoords */
3855 const GLfloat x1
= x
+ width
;
3856 const GLfloat y1
= y
+ height
;
3858 z
= CLAMP(z
, 0.0f
, 1.0f
);
3877 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3878 const struct gl_texture_object
*texObj
;
3879 const struct gl_texture_image
*texImage
;
3880 GLfloat s
, t
, s1
, t1
;
3883 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3885 for (j
= 0; j
< 4; j
++) {
3886 verts
[j
].st
[i
][0] = 0.0f
;
3887 verts
[j
].st
[i
][1] = 0.0f
;
3892 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3893 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3894 tw
= texImage
->Width2
;
3895 th
= texImage
->Height2
;
3897 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3898 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3899 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3900 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3902 verts
[0].st
[i
][0] = s
;
3903 verts
[0].st
[i
][1] = t
;
3905 verts
[1].st
[i
][0] = s1
;
3906 verts
[1].st
[i
][1] = t
;
3908 verts
[2].st
[i
][0] = s1
;
3909 verts
[2].st
[i
][1] = t1
;
3911 verts
[3].st
[i
][0] = s
;
3912 verts
[3].st
[i
][1] = t1
;
3915 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3918 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3920 _mesa_meta_end(ctx
);