mesa/sso: Enable GL_ARB_separate_shader_objects by default
[mesa.git] / src / mesa / drivers / common / meta.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 /**
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.
29 *
30 * \author Brian Paul
31 */
32
33
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/blit.h"
41 #include "main/bufferobj.h"
42 #include "main/buffers.h"
43 #include "main/colortab.h"
44 #include "main/condrender.h"
45 #include "main/depth.h"
46 #include "main/enable.h"
47 #include "main/fbobject.h"
48 #include "main/feedback.h"
49 #include "main/formats.h"
50 #include "main/glformats.h"
51 #include "main/image.h"
52 #include "main/macros.h"
53 #include "main/matrix.h"
54 #include "main/mipmap.h"
55 #include "main/multisample.h"
56 #include "main/objectlabel.h"
57 #include "main/pipelineobj.h"
58 #include "main/pixel.h"
59 #include "main/pbo.h"
60 #include "main/polygon.h"
61 #include "main/queryobj.h"
62 #include "main/readpix.h"
63 #include "main/scissor.h"
64 #include "main/shaderapi.h"
65 #include "main/shaderobj.h"
66 #include "main/state.h"
67 #include "main/stencil.h"
68 #include "main/texobj.h"
69 #include "main/texenv.h"
70 #include "main/texgetimage.h"
71 #include "main/teximage.h"
72 #include "main/texparam.h"
73 #include "main/texstate.h"
74 #include "main/transformfeedback.h"
75 #include "main/uniforms.h"
76 #include "main/varray.h"
77 #include "main/viewport.h"
78 #include "main/samplerobj.h"
79 #include "program/program.h"
80 #include "swrast/swrast.h"
81 #include "drivers/common/meta.h"
82 #include "main/enums.h"
83 #include "main/glformats.h"
84 #include "../glsl/ralloc.h"
85
86 /** Return offset in bytes of the field within a vertex struct */
87 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
88
89 static struct blit_shader *
90 choose_blit_shader(GLenum target, struct blit_shader_table *table);
91
92 static void cleanup_temp_texture(struct temp_texture *tex);
93 static void meta_glsl_clear_cleanup(struct clear_state *clear);
94 static void meta_decompress_cleanup(struct decompress_state *decompress);
95 static void meta_drawpix_cleanup(struct drawpix_state *drawpix);
96
97 void
98 _mesa_meta_bind_fbo_image(GLenum attachment,
99 struct gl_texture_image *texImage, GLuint layer)
100 {
101 struct gl_texture_object *texObj = texImage->TexObject;
102 int level = texImage->Level;
103 GLenum target = texObj->Target;
104
105 switch (target) {
106 case GL_TEXTURE_1D:
107 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER,
108 attachment,
109 target,
110 texObj->Name,
111 level);
112 break;
113 case GL_TEXTURE_1D_ARRAY:
114 case GL_TEXTURE_2D_ARRAY:
115 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
116 case GL_TEXTURE_CUBE_MAP_ARRAY:
117 case GL_TEXTURE_3D:
118 _mesa_FramebufferTextureLayer(GL_FRAMEBUFFER,
119 attachment,
120 texObj->Name,
121 level,
122 layer);
123 break;
124 default: /* 2D / cube */
125 if (target == GL_TEXTURE_CUBE_MAP)
126 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face;
127
128 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER,
129 attachment,
130 target,
131 texObj->Name,
132 level);
133 }
134 }
135
136 GLuint
137 _mesa_meta_compile_shader_with_debug(struct gl_context *ctx, GLenum target,
138 const GLcharARB *source)
139 {
140 GLuint shader;
141 GLint ok, size;
142 GLchar *info;
143
144 shader = _mesa_CreateShader(target);
145 _mesa_ShaderSource(shader, 1, &source, NULL);
146 _mesa_CompileShader(shader);
147
148 _mesa_GetShaderiv(shader, GL_COMPILE_STATUS, &ok);
149 if (ok)
150 return shader;
151
152 _mesa_GetShaderiv(shader, GL_INFO_LOG_LENGTH, &size);
153 if (size == 0) {
154 _mesa_DeleteShader(shader);
155 return 0;
156 }
157
158 info = malloc(size);
159 if (!info) {
160 _mesa_DeleteShader(shader);
161 return 0;
162 }
163
164 _mesa_GetShaderInfoLog(shader, size, NULL, info);
165 _mesa_problem(ctx,
166 "meta program compile failed:\n%s\n"
167 "source:\n%s\n",
168 info, source);
169
170 free(info);
171 _mesa_DeleteShader(shader);
172
173 return 0;
174 }
175
176 GLuint
177 _mesa_meta_link_program_with_debug(struct gl_context *ctx, GLuint program)
178 {
179 GLint ok, size;
180 GLchar *info;
181
182 _mesa_LinkProgram(program);
183
184 _mesa_GetProgramiv(program, GL_LINK_STATUS, &ok);
185 if (ok)
186 return program;
187
188 _mesa_GetProgramiv(program, GL_INFO_LOG_LENGTH, &size);
189 if (size == 0)
190 return 0;
191
192 info = malloc(size);
193 if (!info)
194 return 0;
195
196 _mesa_GetProgramInfoLog(program, size, NULL, info);
197 _mesa_problem(ctx, "meta program link failed:\n%s", info);
198
199 free(info);
200
201 return 0;
202 }
203
204 /**
205 * Generate a generic shader to blit from a texture to a framebuffer
206 *
207 * \param ctx Current GL context
208 * \param texTarget Texture target that will be the source of the blit
209 *
210 * \returns a handle to a shader program on success or zero on failure.
211 */
212 void
213 _mesa_meta_setup_blit_shader(struct gl_context *ctx,
214 GLenum target,
215 struct blit_shader_table *table)
216 {
217 const char *vs_source;
218 char *fs_source;
219 GLuint vs, fs;
220 void *const mem_ctx = ralloc_context(NULL);
221 struct blit_shader *shader = choose_blit_shader(target, table);
222 char *name;
223
224 assert(shader != NULL);
225
226 if (shader->shader_prog != 0) {
227 _mesa_UseProgram(shader->shader_prog);
228 return;
229 }
230
231 if (ctx->Const.GLSLVersion < 130) {
232 vs_source =
233 "attribute vec2 position;\n"
234 "attribute vec4 textureCoords;\n"
235 "varying vec4 texCoords;\n"
236 "void main()\n"
237 "{\n"
238 " texCoords = textureCoords;\n"
239 " gl_Position = vec4(position, 0.0, 1.0);\n"
240 "}\n";
241
242 fs_source = ralloc_asprintf(mem_ctx,
243 "#extension GL_EXT_texture_array : enable\n"
244 "#extension GL_ARB_texture_cube_map_array: enable\n"
245 "uniform %s texSampler;\n"
246 "varying vec4 texCoords;\n"
247 "void main()\n"
248 "{\n"
249 " gl_FragColor = %s(texSampler, %s);\n"
250 " gl_FragDepth = gl_FragColor.x;\n"
251 "}\n",
252 shader->type,
253 shader->func, shader->texcoords);
254 }
255 else {
256 vs_source = ralloc_asprintf(mem_ctx,
257 "#version 130\n"
258 "in vec2 position;\n"
259 "in vec4 textureCoords;\n"
260 "out vec4 texCoords;\n"
261 "void main()\n"
262 "{\n"
263 " texCoords = textureCoords;\n"
264 " gl_Position = vec4(position, 0.0, 1.0);\n"
265 "}\n");
266 fs_source = ralloc_asprintf(mem_ctx,
267 "#version 130\n"
268 "#extension GL_ARB_texture_cube_map_array: enable\n"
269 "uniform %s texSampler;\n"
270 "in vec4 texCoords;\n"
271 "out vec4 out_color;\n"
272 "\n"
273 "void main()\n"
274 "{\n"
275 " out_color = texture(texSampler, %s);\n"
276 " gl_FragDepth = out_color.x;\n"
277 "}\n",
278 shader->type,
279 shader->texcoords);
280 }
281
282 vs = _mesa_meta_compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
283 fs = _mesa_meta_compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
284
285 shader->shader_prog = _mesa_CreateProgram();
286 _mesa_AttachShader(shader->shader_prog, fs);
287 _mesa_DeleteShader(fs);
288 _mesa_AttachShader(shader->shader_prog, vs);
289 _mesa_DeleteShader(vs);
290 _mesa_BindAttribLocation(shader->shader_prog, 0, "position");
291 _mesa_BindAttribLocation(shader->shader_prog, 1, "texcoords");
292 _mesa_meta_link_program_with_debug(ctx, shader->shader_prog);
293 name = ralloc_asprintf(mem_ctx, "%s blit", shader->type);
294 _mesa_ObjectLabel(GL_PROGRAM, shader->shader_prog, -1, name);
295 ralloc_free(mem_ctx);
296
297 _mesa_UseProgram(shader->shader_prog);
298 }
299
300 /**
301 * Configure vertex buffer and vertex array objects for tests
302 *
303 * Regardless of whether a new VAO and new VBO are created, the objects
304 * referenced by \c VAO and \c VBO will be bound into the GL state vector
305 * when this function terminates.
306 *
307 * \param VAO Storage for vertex array object handle. If 0, a new VAO
308 * will be created.
309 * \param VBO Storage for vertex buffer object handle. If 0, a new VBO
310 * will be created. The new VBO will have storage for 4
311 * \c vertex structures.
312 * \param use_generic_attributes Should generic attributes 0 and 1 be used,
313 * or should traditional, fixed-function color and texture
314 * coordinate be used?
315 * \param vertex_size Number of components for attribute 0 / vertex.
316 * \param texcoord_size Number of components for attribute 1 / texture
317 * coordinate. If this is 0, attribute 1 will not be set or
318 * enabled.
319 * \param color_size Number of components for attribute 1 / primary color.
320 * If this is 0, attribute 1 will not be set or enabled.
321 *
322 * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
323 * Use \c texcoord_size instead.
324 */
325 void
326 _mesa_meta_setup_vertex_objects(GLuint *VAO, GLuint *VBO,
327 bool use_generic_attributes,
328 unsigned vertex_size, unsigned texcoord_size,
329 unsigned color_size)
330 {
331 if (*VAO == 0) {
332 assert(*VBO == 0);
333
334 /* create vertex array object */
335 _mesa_GenVertexArrays(1, VAO);
336 _mesa_BindVertexArray(*VAO);
337
338 /* create vertex array buffer */
339 _mesa_GenBuffers(1, VBO);
340 _mesa_BindBuffer(GL_ARRAY_BUFFER, *VBO);
341 _mesa_BufferData(GL_ARRAY_BUFFER, 4 * sizeof(struct vertex), NULL,
342 GL_DYNAMIC_DRAW);
343
344 /* setup vertex arrays */
345 if (use_generic_attributes) {
346 assert(color_size == 0);
347
348 _mesa_VertexAttribPointer(0, vertex_size, GL_FLOAT, GL_FALSE,
349 sizeof(struct vertex), OFFSET(x));
350 _mesa_EnableVertexAttribArray(0);
351
352 if (texcoord_size > 0) {
353 _mesa_VertexAttribPointer(1, texcoord_size, GL_FLOAT, GL_FALSE,
354 sizeof(struct vertex), OFFSET(tex));
355 _mesa_EnableVertexAttribArray(1);
356 }
357 } else {
358 _mesa_VertexPointer(vertex_size, GL_FLOAT, sizeof(struct vertex),
359 OFFSET(x));
360 _mesa_EnableClientState(GL_VERTEX_ARRAY);
361
362 if (texcoord_size > 0) {
363 _mesa_TexCoordPointer(texcoord_size, GL_FLOAT,
364 sizeof(struct vertex), OFFSET(tex));
365 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
366 }
367
368 if (color_size > 0) {
369 _mesa_ColorPointer(color_size, GL_FLOAT,
370 sizeof(struct vertex), OFFSET(r));
371 _mesa_EnableClientState(GL_COLOR_ARRAY);
372 }
373 }
374 } else {
375 _mesa_BindVertexArray(*VAO);
376 _mesa_BindBuffer(GL_ARRAY_BUFFER, *VBO);
377 }
378 }
379
380 /**
381 * Initialize meta-ops for a context.
382 * To be called once during context creation.
383 */
384 void
385 _mesa_meta_init(struct gl_context *ctx)
386 {
387 ASSERT(!ctx->Meta);
388
389 ctx->Meta = CALLOC_STRUCT(gl_meta_state);
390 }
391
392
393 /**
394 * Free context meta-op state.
395 * To be called once during context destruction.
396 */
397 void
398 _mesa_meta_free(struct gl_context *ctx)
399 {
400 GET_CURRENT_CONTEXT(old_context);
401 _mesa_make_current(ctx, NULL, NULL);
402 _mesa_meta_glsl_blit_cleanup(&ctx->Meta->Blit);
403 meta_glsl_clear_cleanup(&ctx->Meta->Clear);
404 _mesa_meta_glsl_generate_mipmap_cleanup(&ctx->Meta->Mipmap);
405 cleanup_temp_texture(&ctx->Meta->TempTex);
406 meta_decompress_cleanup(&ctx->Meta->Decompress);
407 meta_drawpix_cleanup(&ctx->Meta->DrawPix);
408 if (old_context)
409 _mesa_make_current(old_context, old_context->WinSysDrawBuffer, old_context->WinSysReadBuffer);
410 else
411 _mesa_make_current(NULL, NULL, NULL);
412 free(ctx->Meta);
413 ctx->Meta = NULL;
414 }
415
416
417 /**
418 * Enter meta state. This is like a light-weight version of glPushAttrib
419 * but it also resets most GL state back to default values.
420 *
421 * \param state bitmask of MESA_META_* flags indicating which attribute groups
422 * to save and reset to their defaults
423 */
424 void
425 _mesa_meta_begin(struct gl_context *ctx, GLbitfield state)
426 {
427 struct save_state *save;
428
429 /* hope MAX_META_OPS_DEPTH is large enough */
430 assert(ctx->Meta->SaveStackDepth < MAX_META_OPS_DEPTH);
431
432 save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth++];
433 memset(save, 0, sizeof(*save));
434 save->SavedState = state;
435
436 /* We always push into desktop GL mode and pop out at the end. No sense in
437 * writing our shaders varying based on the user's context choice, when
438 * Mesa can handle either.
439 */
440 save->API = ctx->API;
441 ctx->API = API_OPENGL_COMPAT;
442
443 /* Pausing transform feedback needs to be done early, or else we won't be
444 * able to change other state.
445 */
446 save->TransformFeedbackNeedsResume =
447 _mesa_is_xfb_active_and_unpaused(ctx);
448 if (save->TransformFeedbackNeedsResume)
449 _mesa_PauseTransformFeedback();
450
451 /* After saving the current occlusion object, call EndQuery so that no
452 * occlusion querying will be active during the meta-operation.
453 */
454 if (state & MESA_META_OCCLUSION_QUERY) {
455 save->CurrentOcclusionObject = ctx->Query.CurrentOcclusionObject;
456 if (save->CurrentOcclusionObject)
457 _mesa_EndQuery(save->CurrentOcclusionObject->Target);
458 }
459
460 if (state & MESA_META_ALPHA_TEST) {
461 save->AlphaEnabled = ctx->Color.AlphaEnabled;
462 save->AlphaFunc = ctx->Color.AlphaFunc;
463 save->AlphaRef = ctx->Color.AlphaRef;
464 if (ctx->Color.AlphaEnabled)
465 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_FALSE);
466 }
467
468 if (state & MESA_META_BLEND) {
469 save->BlendEnabled = ctx->Color.BlendEnabled;
470 if (ctx->Color.BlendEnabled) {
471 if (ctx->Extensions.EXT_draw_buffers2) {
472 GLuint i;
473 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
474 _mesa_set_enablei(ctx, GL_BLEND, i, GL_FALSE);
475 }
476 }
477 else {
478 _mesa_set_enable(ctx, GL_BLEND, GL_FALSE);
479 }
480 }
481 save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled;
482 if (ctx->Color.ColorLogicOpEnabled)
483 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, GL_FALSE);
484 }
485
486 if (state & MESA_META_COLOR_MASK) {
487 memcpy(save->ColorMask, ctx->Color.ColorMask,
488 sizeof(ctx->Color.ColorMask));
489 if (!ctx->Color.ColorMask[0][0] ||
490 !ctx->Color.ColorMask[0][1] ||
491 !ctx->Color.ColorMask[0][2] ||
492 !ctx->Color.ColorMask[0][3])
493 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
494 }
495
496 if (state & MESA_META_DEPTH_TEST) {
497 save->Depth = ctx->Depth; /* struct copy */
498 if (ctx->Depth.Test)
499 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
500 }
501
502 if (state & MESA_META_FOG) {
503 save->Fog = ctx->Fog.Enabled;
504 if (ctx->Fog.Enabled)
505 _mesa_set_enable(ctx, GL_FOG, GL_FALSE);
506 }
507
508 if (state & MESA_META_PIXEL_STORE) {
509 save->Pack = ctx->Pack;
510 save->Unpack = ctx->Unpack;
511 ctx->Pack = ctx->DefaultPacking;
512 ctx->Unpack = ctx->DefaultPacking;
513 }
514
515 if (state & MESA_META_PIXEL_TRANSFER) {
516 save->RedScale = ctx->Pixel.RedScale;
517 save->RedBias = ctx->Pixel.RedBias;
518 save->GreenScale = ctx->Pixel.GreenScale;
519 save->GreenBias = ctx->Pixel.GreenBias;
520 save->BlueScale = ctx->Pixel.BlueScale;
521 save->BlueBias = ctx->Pixel.BlueBias;
522 save->AlphaScale = ctx->Pixel.AlphaScale;
523 save->AlphaBias = ctx->Pixel.AlphaBias;
524 save->MapColorFlag = ctx->Pixel.MapColorFlag;
525 ctx->Pixel.RedScale = 1.0F;
526 ctx->Pixel.RedBias = 0.0F;
527 ctx->Pixel.GreenScale = 1.0F;
528 ctx->Pixel.GreenBias = 0.0F;
529 ctx->Pixel.BlueScale = 1.0F;
530 ctx->Pixel.BlueBias = 0.0F;
531 ctx->Pixel.AlphaScale = 1.0F;
532 ctx->Pixel.AlphaBias = 0.0F;
533 ctx->Pixel.MapColorFlag = GL_FALSE;
534 /* XXX more state */
535 ctx->NewState |=_NEW_PIXEL;
536 }
537
538 if (state & MESA_META_RASTERIZATION) {
539 save->FrontPolygonMode = ctx->Polygon.FrontMode;
540 save->BackPolygonMode = ctx->Polygon.BackMode;
541 save->PolygonOffset = ctx->Polygon.OffsetFill;
542 save->PolygonSmooth = ctx->Polygon.SmoothFlag;
543 save->PolygonStipple = ctx->Polygon.StippleFlag;
544 save->PolygonCull = ctx->Polygon.CullFlag;
545 _mesa_PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
546 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, GL_FALSE);
547 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, GL_FALSE);
548 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, GL_FALSE);
549 _mesa_set_enable(ctx, GL_CULL_FACE, GL_FALSE);
550 }
551
552 if (state & MESA_META_SCISSOR) {
553 save->Scissor = ctx->Scissor; /* struct copy */
554 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_FALSE);
555 }
556
557 if (state & MESA_META_SHADER) {
558 int i;
559
560 if (ctx->Extensions.ARB_vertex_program) {
561 save->VertexProgramEnabled = ctx->VertexProgram.Enabled;
562 _mesa_reference_vertprog(ctx, &save->VertexProgram,
563 ctx->VertexProgram.Current);
564 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB, GL_FALSE);
565 }
566
567 if (ctx->Extensions.ARB_fragment_program) {
568 save->FragmentProgramEnabled = ctx->FragmentProgram.Enabled;
569 _mesa_reference_fragprog(ctx, &save->FragmentProgram,
570 ctx->FragmentProgram.Current);
571 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_FALSE);
572 }
573
574 if (ctx->Extensions.ATI_fragment_shader) {
575 save->ATIFragmentShaderEnabled = ctx->ATIFragmentShader.Enabled;
576 _mesa_set_enable(ctx, GL_FRAGMENT_SHADER_ATI, GL_FALSE);
577 }
578
579 if (ctx->Pipeline.Current) {
580 _mesa_reference_pipeline_object(ctx, &save->Pipeline,
581 ctx->Pipeline.Current);
582 _mesa_BindProgramPipeline(0);
583 }
584
585 /* Save the shader state from ctx->Shader (instead of ctx->_Shader) so
586 * that we don't have to worry about the current pipeline state.
587 */
588 for (i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
589 _mesa_reference_shader_program(ctx, &save->Shader[i],
590 ctx->Shader.CurrentProgram[i]);
591 }
592 _mesa_reference_shader_program(ctx, &save->ActiveShader,
593 ctx->Shader.ActiveProgram);
594
595 _mesa_UseProgram(0);
596 }
597
598 if (state & MESA_META_STENCIL_TEST) {
599 save->Stencil = ctx->Stencil; /* struct copy */
600 if (ctx->Stencil.Enabled)
601 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE);
602 /* NOTE: other stencil state not reset */
603 }
604
605 if (state & MESA_META_TEXTURE) {
606 GLuint u, tgt;
607
608 save->ActiveUnit = ctx->Texture.CurrentUnit;
609 save->ClientActiveUnit = ctx->Array.ActiveTexture;
610 save->EnvMode = ctx->Texture.Unit[0].EnvMode;
611
612 /* Disable all texture units */
613 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
614 save->TexEnabled[u] = ctx->Texture.Unit[u].Enabled;
615 save->TexGenEnabled[u] = ctx->Texture.Unit[u].TexGenEnabled;
616 if (ctx->Texture.Unit[u].Enabled ||
617 ctx->Texture.Unit[u].TexGenEnabled) {
618 _mesa_ActiveTexture(GL_TEXTURE0 + u);
619 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_FALSE);
620 if (ctx->Extensions.ARB_texture_cube_map)
621 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_FALSE);
622
623 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_FALSE);
624 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_FALSE);
625 if (ctx->Extensions.NV_texture_rectangle)
626 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_FALSE);
627 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_FALSE);
628 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_FALSE);
629 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_FALSE);
630 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
631 }
632 }
633
634 /* save current texture objects for unit[0] only */
635 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
636 _mesa_reference_texobj(&save->CurrentTexture[tgt],
637 ctx->Texture.Unit[0].CurrentTex[tgt]);
638 }
639
640 /* set defaults for unit[0] */
641 _mesa_ActiveTexture(GL_TEXTURE0);
642 _mesa_ClientActiveTexture(GL_TEXTURE0);
643 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
644 }
645
646 if (state & MESA_META_TRANSFORM) {
647 GLuint activeTexture = ctx->Texture.CurrentUnit;
648 memcpy(save->ModelviewMatrix, ctx->ModelviewMatrixStack.Top->m,
649 16 * sizeof(GLfloat));
650 memcpy(save->ProjectionMatrix, ctx->ProjectionMatrixStack.Top->m,
651 16 * sizeof(GLfloat));
652 memcpy(save->TextureMatrix, ctx->TextureMatrixStack[0].Top->m,
653 16 * sizeof(GLfloat));
654 save->MatrixMode = ctx->Transform.MatrixMode;
655 /* set 1:1 vertex:pixel coordinate transform */
656 _mesa_ActiveTexture(GL_TEXTURE0);
657 _mesa_MatrixMode(GL_TEXTURE);
658 _mesa_LoadIdentity();
659 _mesa_ActiveTexture(GL_TEXTURE0 + activeTexture);
660 _mesa_MatrixMode(GL_MODELVIEW);
661 _mesa_LoadIdentity();
662 _mesa_MatrixMode(GL_PROJECTION);
663 _mesa_LoadIdentity();
664
665 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
666 * This can occur when there is no draw buffer.
667 */
668 if (ctx->DrawBuffer->Width != 0 && ctx->DrawBuffer->Height != 0)
669 _mesa_Ortho(0.0, ctx->DrawBuffer->Width,
670 0.0, ctx->DrawBuffer->Height,
671 -1.0, 1.0);
672 }
673
674 if (state & MESA_META_CLIP) {
675 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
676 if (ctx->Transform.ClipPlanesEnabled) {
677 GLuint i;
678 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
679 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
680 }
681 }
682 }
683
684 if (state & MESA_META_VERTEX) {
685 /* save vertex array object state */
686 _mesa_reference_vao(ctx, &save->VAO,
687 ctx->Array.VAO);
688 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj,
689 ctx->Array.ArrayBufferObj);
690 /* set some default state? */
691 }
692
693 if (state & MESA_META_VIEWPORT) {
694 /* save viewport state */
695 save->ViewportX = ctx->ViewportArray[0].X;
696 save->ViewportY = ctx->ViewportArray[0].Y;
697 save->ViewportW = ctx->ViewportArray[0].Width;
698 save->ViewportH = ctx->ViewportArray[0].Height;
699 /* set viewport to match window size */
700 if (ctx->ViewportArray[0].X != 0 ||
701 ctx->ViewportArray[0].Y != 0 ||
702 ctx->ViewportArray[0].Width != (float) ctx->DrawBuffer->Width ||
703 ctx->ViewportArray[0].Height != (float) ctx->DrawBuffer->Height) {
704 _mesa_set_viewport(ctx, 0, 0, 0,
705 ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
706 }
707 /* save depth range state */
708 save->DepthNear = ctx->ViewportArray[0].Near;
709 save->DepthFar = ctx->ViewportArray[0].Far;
710 /* set depth range to default */
711 _mesa_DepthRange(0.0, 1.0);
712 }
713
714 if (state & MESA_META_CLAMP_FRAGMENT_COLOR) {
715 save->ClampFragmentColor = ctx->Color.ClampFragmentColor;
716
717 /* Generally in here we want to do clamping according to whether
718 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
719 * regardless of the internal implementation of the metaops.
720 */
721 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
722 ctx->Extensions.ARB_color_buffer_float)
723 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
724 }
725
726 if (state & MESA_META_CLAMP_VERTEX_COLOR) {
727 save->ClampVertexColor = ctx->Light.ClampVertexColor;
728
729 /* Generally in here we never want vertex color clamping --
730 * result clamping is only dependent on fragment clamping.
731 */
732 if (ctx->Extensions.ARB_color_buffer_float)
733 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
734 }
735
736 if (state & MESA_META_CONDITIONAL_RENDER) {
737 save->CondRenderQuery = ctx->Query.CondRenderQuery;
738 save->CondRenderMode = ctx->Query.CondRenderMode;
739
740 if (ctx->Query.CondRenderQuery)
741 _mesa_EndConditionalRender();
742 }
743
744 if (state & MESA_META_SELECT_FEEDBACK) {
745 save->RenderMode = ctx->RenderMode;
746 if (ctx->RenderMode == GL_SELECT) {
747 save->Select = ctx->Select; /* struct copy */
748 _mesa_RenderMode(GL_RENDER);
749 } else if (ctx->RenderMode == GL_FEEDBACK) {
750 save->Feedback = ctx->Feedback; /* struct copy */
751 _mesa_RenderMode(GL_RENDER);
752 }
753 }
754
755 if (state & MESA_META_MULTISAMPLE) {
756 save->Multisample = ctx->Multisample; /* struct copy */
757
758 if (ctx->Multisample.Enabled)
759 _mesa_set_multisample(ctx, GL_FALSE);
760 if (ctx->Multisample.SampleCoverage)
761 _mesa_set_enable(ctx, GL_SAMPLE_COVERAGE, GL_FALSE);
762 if (ctx->Multisample.SampleAlphaToCoverage)
763 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_COVERAGE, GL_FALSE);
764 if (ctx->Multisample.SampleAlphaToOne)
765 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_ONE, GL_FALSE);
766 if (ctx->Multisample.SampleShading)
767 _mesa_set_enable(ctx, GL_SAMPLE_SHADING, GL_FALSE);
768 if (ctx->Multisample.SampleMask)
769 _mesa_set_enable(ctx, GL_SAMPLE_MASK, GL_FALSE);
770 }
771
772 if (state & MESA_META_FRAMEBUFFER_SRGB) {
773 save->sRGBEnabled = ctx->Color.sRGBEnabled;
774 if (ctx->Color.sRGBEnabled)
775 _mesa_set_framebuffer_srgb(ctx, GL_FALSE);
776 }
777
778 /* misc */
779 {
780 save->Lighting = ctx->Light.Enabled;
781 if (ctx->Light.Enabled)
782 _mesa_set_enable(ctx, GL_LIGHTING, GL_FALSE);
783 save->RasterDiscard = ctx->RasterDiscard;
784 if (ctx->RasterDiscard)
785 _mesa_set_enable(ctx, GL_RASTERIZER_DISCARD, GL_FALSE);
786
787 save->DrawBufferName = ctx->DrawBuffer->Name;
788 save->ReadBufferName = ctx->ReadBuffer->Name;
789 save->RenderbufferName = (ctx->CurrentRenderbuffer ?
790 ctx->CurrentRenderbuffer->Name : 0);
791 }
792 }
793
794
795 /**
796 * Leave meta state. This is like a light-weight version of glPopAttrib().
797 */
798 void
799 _mesa_meta_end(struct gl_context *ctx)
800 {
801 struct save_state *save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth - 1];
802 const GLbitfield state = save->SavedState;
803 int i;
804
805 /* After starting a new occlusion query, initialize the results to the
806 * values saved previously. The driver will then continue to increment
807 * these values.
808 */
809 if (state & MESA_META_OCCLUSION_QUERY) {
810 if (save->CurrentOcclusionObject) {
811 _mesa_BeginQuery(save->CurrentOcclusionObject->Target,
812 save->CurrentOcclusionObject->Id);
813 ctx->Query.CurrentOcclusionObject->Result = save->CurrentOcclusionObject->Result;
814 }
815 }
816
817 if (state & MESA_META_ALPHA_TEST) {
818 if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
819 _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
820 _mesa_AlphaFunc(save->AlphaFunc, save->AlphaRef);
821 }
822
823 if (state & MESA_META_BLEND) {
824 if (ctx->Color.BlendEnabled != save->BlendEnabled) {
825 if (ctx->Extensions.EXT_draw_buffers2) {
826 GLuint i;
827 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
828 _mesa_set_enablei(ctx, GL_BLEND, i, (save->BlendEnabled >> i) & 1);
829 }
830 }
831 else {
832 _mesa_set_enable(ctx, GL_BLEND, (save->BlendEnabled & 1));
833 }
834 }
835 if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled)
836 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled);
837 }
838
839 if (state & MESA_META_COLOR_MASK) {
840 GLuint i;
841 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
842 if (!TEST_EQ_4V(ctx->Color.ColorMask[i], save->ColorMask[i])) {
843 if (i == 0) {
844 _mesa_ColorMask(save->ColorMask[i][0], save->ColorMask[i][1],
845 save->ColorMask[i][2], save->ColorMask[i][3]);
846 }
847 else {
848 _mesa_ColorMaski(i,
849 save->ColorMask[i][0],
850 save->ColorMask[i][1],
851 save->ColorMask[i][2],
852 save->ColorMask[i][3]);
853 }
854 }
855 }
856 }
857
858 if (state & MESA_META_DEPTH_TEST) {
859 if (ctx->Depth.Test != save->Depth.Test)
860 _mesa_set_enable(ctx, GL_DEPTH_TEST, save->Depth.Test);
861 _mesa_DepthFunc(save->Depth.Func);
862 _mesa_DepthMask(save->Depth.Mask);
863 }
864
865 if (state & MESA_META_FOG) {
866 _mesa_set_enable(ctx, GL_FOG, save->Fog);
867 }
868
869 if (state & MESA_META_PIXEL_STORE) {
870 ctx->Pack = save->Pack;
871 ctx->Unpack = save->Unpack;
872 }
873
874 if (state & MESA_META_PIXEL_TRANSFER) {
875 ctx->Pixel.RedScale = save->RedScale;
876 ctx->Pixel.RedBias = save->RedBias;
877 ctx->Pixel.GreenScale = save->GreenScale;
878 ctx->Pixel.GreenBias = save->GreenBias;
879 ctx->Pixel.BlueScale = save->BlueScale;
880 ctx->Pixel.BlueBias = save->BlueBias;
881 ctx->Pixel.AlphaScale = save->AlphaScale;
882 ctx->Pixel.AlphaBias = save->AlphaBias;
883 ctx->Pixel.MapColorFlag = save->MapColorFlag;
884 /* XXX more state */
885 ctx->NewState |=_NEW_PIXEL;
886 }
887
888 if (state & MESA_META_RASTERIZATION) {
889 _mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode);
890 _mesa_PolygonMode(GL_BACK, save->BackPolygonMode);
891 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, save->PolygonStipple);
892 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, save->PolygonSmooth);
893 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, save->PolygonOffset);
894 _mesa_set_enable(ctx, GL_CULL_FACE, save->PolygonCull);
895 }
896
897 if (state & MESA_META_SCISSOR) {
898 unsigned i;
899
900 for (i = 0; i < ctx->Const.MaxViewports; i++) {
901 _mesa_set_scissor(ctx, i,
902 save->Scissor.ScissorArray[i].X,
903 save->Scissor.ScissorArray[i].Y,
904 save->Scissor.ScissorArray[i].Width,
905 save->Scissor.ScissorArray[i].Height);
906 _mesa_set_enablei(ctx, GL_SCISSOR_TEST, i,
907 (save->Scissor.EnableFlags >> i) & 1);
908 }
909 }
910
911 if (state & MESA_META_SHADER) {
912 static const GLenum targets[] = {
913 GL_VERTEX_SHADER,
914 GL_GEOMETRY_SHADER,
915 GL_FRAGMENT_SHADER,
916 };
917
918 bool any_shader;
919
920 if (ctx->Extensions.ARB_vertex_program) {
921 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB,
922 save->VertexProgramEnabled);
923 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
924 save->VertexProgram);
925 _mesa_reference_vertprog(ctx, &save->VertexProgram, NULL);
926 }
927
928 if (ctx->Extensions.ARB_fragment_program) {
929 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB,
930 save->FragmentProgramEnabled);
931 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
932 save->FragmentProgram);
933 _mesa_reference_fragprog(ctx, &save->FragmentProgram, NULL);
934 }
935
936 if (ctx->Extensions.ATI_fragment_shader) {
937 _mesa_set_enable(ctx, GL_FRAGMENT_SHADER_ATI,
938 save->ATIFragmentShaderEnabled);
939 }
940
941 any_shader = false;
942 for (i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
943 /* It is safe to call _mesa_use_shader_program even if the extension
944 * necessary for that program state is not supported. In that case,
945 * the saved program object must be NULL and the currently bound
946 * program object must be NULL. _mesa_use_shader_program is a no-op
947 * in that case.
948 */
949 _mesa_use_shader_program(ctx, targets[i],
950 save->Shader[i],
951 &ctx->Shader);
952
953 /* Do this *before* killing the reference. :)
954 */
955 if (save->Shader[i] != NULL)
956 any_shader = true;
957
958 _mesa_reference_shader_program(ctx, &save->Shader[i], NULL);
959 }
960
961 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram,
962 save->ActiveShader);
963 _mesa_reference_shader_program(ctx, &save->ActiveShader, NULL);
964
965 /* If there were any stages set with programs, use ctx->Shader as the
966 * current shader state. Otherwise, use Pipeline.Default. The pipeline
967 * hasn't been restored yet, and that may modify ctx->_Shader further.
968 */
969 if (any_shader)
970 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
971 &ctx->Shader);
972 else
973 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
974 ctx->Pipeline.Default);
975
976 if (save->Pipeline) {
977 _mesa_bind_pipeline(ctx, save->Pipeline);
978
979 _mesa_reference_pipeline_object(ctx, &save->Pipeline, NULL);
980 }
981 }
982
983 if (state & MESA_META_STENCIL_TEST) {
984 const struct gl_stencil_attrib *stencil = &save->Stencil;
985
986 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
987 _mesa_ClearStencil(stencil->Clear);
988 if (ctx->Extensions.EXT_stencil_two_side) {
989 _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
990 stencil->TestTwoSide);
991 _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
992 ? GL_BACK : GL_FRONT);
993 }
994 /* front state */
995 _mesa_StencilFuncSeparate(GL_FRONT,
996 stencil->Function[0],
997 stencil->Ref[0],
998 stencil->ValueMask[0]);
999 _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
1000 _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
1001 stencil->ZFailFunc[0],
1002 stencil->ZPassFunc[0]);
1003 /* back state */
1004 _mesa_StencilFuncSeparate(GL_BACK,
1005 stencil->Function[1],
1006 stencil->Ref[1],
1007 stencil->ValueMask[1]);
1008 _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
1009 _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
1010 stencil->ZFailFunc[1],
1011 stencil->ZPassFunc[1]);
1012 }
1013
1014 if (state & MESA_META_TEXTURE) {
1015 GLuint u, tgt;
1016
1017 ASSERT(ctx->Texture.CurrentUnit == 0);
1018
1019 /* restore texenv for unit[0] */
1020 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
1021
1022 /* restore texture objects for unit[0] only */
1023 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1024 if (ctx->Texture.Unit[0].CurrentTex[tgt] != save->CurrentTexture[tgt]) {
1025 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1026 _mesa_reference_texobj(&ctx->Texture.Unit[0].CurrentTex[tgt],
1027 save->CurrentTexture[tgt]);
1028 }
1029 _mesa_reference_texobj(&save->CurrentTexture[tgt], NULL);
1030 }
1031
1032 /* Restore fixed function texture enables, texgen */
1033 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
1034 if (ctx->Texture.Unit[u].Enabled != save->TexEnabled[u]) {
1035 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1036 ctx->Texture.Unit[u].Enabled = save->TexEnabled[u];
1037 }
1038
1039 if (ctx->Texture.Unit[u].TexGenEnabled != save->TexGenEnabled[u]) {
1040 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1041 ctx->Texture.Unit[u].TexGenEnabled = save->TexGenEnabled[u];
1042 }
1043 }
1044
1045 /* restore current unit state */
1046 _mesa_ActiveTexture(GL_TEXTURE0 + save->ActiveUnit);
1047 _mesa_ClientActiveTexture(GL_TEXTURE0 + save->ClientActiveUnit);
1048 }
1049
1050 if (state & MESA_META_TRANSFORM) {
1051 GLuint activeTexture = ctx->Texture.CurrentUnit;
1052 _mesa_ActiveTexture(GL_TEXTURE0);
1053 _mesa_MatrixMode(GL_TEXTURE);
1054 _mesa_LoadMatrixf(save->TextureMatrix);
1055 _mesa_ActiveTexture(GL_TEXTURE0 + activeTexture);
1056
1057 _mesa_MatrixMode(GL_MODELVIEW);
1058 _mesa_LoadMatrixf(save->ModelviewMatrix);
1059
1060 _mesa_MatrixMode(GL_PROJECTION);
1061 _mesa_LoadMatrixf(save->ProjectionMatrix);
1062
1063 _mesa_MatrixMode(save->MatrixMode);
1064 }
1065
1066 if (state & MESA_META_CLIP) {
1067 if (save->ClipPlanesEnabled) {
1068 GLuint i;
1069 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
1070 if (save->ClipPlanesEnabled & (1 << i)) {
1071 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
1072 }
1073 }
1074 }
1075 }
1076
1077 if (state & MESA_META_VERTEX) {
1078 /* restore vertex buffer object */
1079 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, save->ArrayBufferObj->Name);
1080 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj, NULL);
1081
1082 /* restore vertex array object */
1083 _mesa_BindVertexArray(save->VAO->Name);
1084 _mesa_reference_vao(ctx, &save->VAO, NULL);
1085 }
1086
1087 if (state & MESA_META_VIEWPORT) {
1088 if (save->ViewportX != ctx->ViewportArray[0].X ||
1089 save->ViewportY != ctx->ViewportArray[0].Y ||
1090 save->ViewportW != ctx->ViewportArray[0].Width ||
1091 save->ViewportH != ctx->ViewportArray[0].Height) {
1092 _mesa_set_viewport(ctx, 0, save->ViewportX, save->ViewportY,
1093 save->ViewportW, save->ViewportH);
1094 }
1095 _mesa_DepthRange(save->DepthNear, save->DepthFar);
1096 }
1097
1098 if (state & MESA_META_CLAMP_FRAGMENT_COLOR &&
1099 ctx->Extensions.ARB_color_buffer_float) {
1100 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, save->ClampFragmentColor);
1101 }
1102
1103 if (state & MESA_META_CLAMP_VERTEX_COLOR &&
1104 ctx->Extensions.ARB_color_buffer_float) {
1105 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR, save->ClampVertexColor);
1106 }
1107
1108 if (state & MESA_META_CONDITIONAL_RENDER) {
1109 if (save->CondRenderQuery)
1110 _mesa_BeginConditionalRender(save->CondRenderQuery->Id,
1111 save->CondRenderMode);
1112 }
1113
1114 if (state & MESA_META_SELECT_FEEDBACK) {
1115 if (save->RenderMode == GL_SELECT) {
1116 _mesa_RenderMode(GL_SELECT);
1117 ctx->Select = save->Select;
1118 } else if (save->RenderMode == GL_FEEDBACK) {
1119 _mesa_RenderMode(GL_FEEDBACK);
1120 ctx->Feedback = save->Feedback;
1121 }
1122 }
1123
1124 if (state & MESA_META_MULTISAMPLE) {
1125 struct gl_multisample_attrib *ctx_ms = &ctx->Multisample;
1126 struct gl_multisample_attrib *save_ms = &save->Multisample;
1127
1128 if (ctx_ms->Enabled != save_ms->Enabled)
1129 _mesa_set_multisample(ctx, save_ms->Enabled);
1130 if (ctx_ms->SampleCoverage != save_ms->SampleCoverage)
1131 _mesa_set_enable(ctx, GL_SAMPLE_COVERAGE, save_ms->SampleCoverage);
1132 if (ctx_ms->SampleAlphaToCoverage != save_ms->SampleAlphaToCoverage)
1133 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_COVERAGE, save_ms->SampleAlphaToCoverage);
1134 if (ctx_ms->SampleAlphaToOne != save_ms->SampleAlphaToOne)
1135 _mesa_set_enable(ctx, GL_SAMPLE_ALPHA_TO_ONE, save_ms->SampleAlphaToOne);
1136 if (ctx_ms->SampleCoverageValue != save_ms->SampleCoverageValue ||
1137 ctx_ms->SampleCoverageInvert != save_ms->SampleCoverageInvert) {
1138 _mesa_SampleCoverage(save_ms->SampleCoverageValue,
1139 save_ms->SampleCoverageInvert);
1140 }
1141 if (ctx_ms->SampleShading != save_ms->SampleShading)
1142 _mesa_set_enable(ctx, GL_SAMPLE_SHADING, save_ms->SampleShading);
1143 if (ctx_ms->SampleMask != save_ms->SampleMask)
1144 _mesa_set_enable(ctx, GL_SAMPLE_MASK, save_ms->SampleMask);
1145 if (ctx_ms->SampleMaskValue != save_ms->SampleMaskValue)
1146 _mesa_SampleMaski(0, save_ms->SampleMaskValue);
1147 if (ctx_ms->MinSampleShadingValue != save_ms->MinSampleShadingValue)
1148 _mesa_MinSampleShading(save_ms->MinSampleShadingValue);
1149 }
1150
1151 if (state & MESA_META_FRAMEBUFFER_SRGB) {
1152 if (ctx->Color.sRGBEnabled != save->sRGBEnabled)
1153 _mesa_set_framebuffer_srgb(ctx, save->sRGBEnabled);
1154 }
1155
1156 /* misc */
1157 if (save->Lighting) {
1158 _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
1159 }
1160 if (save->RasterDiscard) {
1161 _mesa_set_enable(ctx, GL_RASTERIZER_DISCARD, GL_TRUE);
1162 }
1163 if (save->TransformFeedbackNeedsResume)
1164 _mesa_ResumeTransformFeedback();
1165
1166 if (ctx->DrawBuffer->Name != save->DrawBufferName)
1167 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, save->DrawBufferName);
1168
1169 if (ctx->ReadBuffer->Name != save->ReadBufferName)
1170 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, save->ReadBufferName);
1171
1172 if (!ctx->CurrentRenderbuffer ||
1173 ctx->CurrentRenderbuffer->Name != save->RenderbufferName)
1174 _mesa_BindRenderbuffer(GL_RENDERBUFFER, save->RenderbufferName);
1175
1176 ctx->Meta->SaveStackDepth--;
1177
1178 ctx->API = save->API;
1179 }
1180
1181
1182 /**
1183 * Determine whether Mesa is currently in a meta state.
1184 */
1185 GLboolean
1186 _mesa_meta_in_progress(struct gl_context *ctx)
1187 {
1188 return ctx->Meta->SaveStackDepth != 0;
1189 }
1190
1191
1192 /**
1193 * Convert Z from a normalized value in the range [0, 1] to an object-space
1194 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1195 * default/identity ortho projection results in the original Z value.
1196 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1197 * value comes from the clear value or raster position.
1198 */
1199 static INLINE GLfloat
1200 invert_z(GLfloat normZ)
1201 {
1202 GLfloat objZ = 1.0f - 2.0f * normZ;
1203 return objZ;
1204 }
1205
1206
1207 /**
1208 * One-time init for a temp_texture object.
1209 * Choose tex target, compute max tex size, etc.
1210 */
1211 static void
1212 init_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
1213 {
1214 /* prefer texture rectangle */
1215 if (_mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle) {
1216 tex->Target = GL_TEXTURE_RECTANGLE;
1217 tex->MaxSize = ctx->Const.MaxTextureRectSize;
1218 tex->NPOT = GL_TRUE;
1219 }
1220 else {
1221 /* use 2D texture, NPOT if possible */
1222 tex->Target = GL_TEXTURE_2D;
1223 tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1224 tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
1225 }
1226 tex->MinSize = 16; /* 16 x 16 at least */
1227 assert(tex->MaxSize > 0);
1228
1229 _mesa_GenTextures(1, &tex->TexObj);
1230 }
1231
1232 static void
1233 cleanup_temp_texture(struct temp_texture *tex)
1234 {
1235 if (!tex->TexObj)
1236 return;
1237 _mesa_DeleteTextures(1, &tex->TexObj);
1238 tex->TexObj = 0;
1239 }
1240
1241
1242 /**
1243 * Return pointer to temp_texture info for non-bitmap ops.
1244 * This does some one-time init if needed.
1245 */
1246 struct temp_texture *
1247 _mesa_meta_get_temp_texture(struct gl_context *ctx)
1248 {
1249 struct temp_texture *tex = &ctx->Meta->TempTex;
1250
1251 if (!tex->TexObj) {
1252 init_temp_texture(ctx, tex);
1253 }
1254
1255 return tex;
1256 }
1257
1258
1259 /**
1260 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1261 * We use a separate texture for bitmaps to reduce texture
1262 * allocation/deallocation.
1263 */
1264 static struct temp_texture *
1265 get_bitmap_temp_texture(struct gl_context *ctx)
1266 {
1267 struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
1268
1269 if (!tex->TexObj) {
1270 init_temp_texture(ctx, tex);
1271 }
1272
1273 return tex;
1274 }
1275
1276 /**
1277 * Return pointer to depth temp_texture.
1278 * This does some one-time init if needed.
1279 */
1280 struct temp_texture *
1281 _mesa_meta_get_temp_depth_texture(struct gl_context *ctx)
1282 {
1283 struct temp_texture *tex = &ctx->Meta->Blit.depthTex;
1284
1285 if (!tex->TexObj) {
1286 init_temp_texture(ctx, tex);
1287 }
1288
1289 return tex;
1290 }
1291
1292 /**
1293 * Compute the width/height of texture needed to draw an image of the
1294 * given size. Return a flag indicating whether the current texture
1295 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1296 * allocated (glTexImage2D).
1297 * Also, compute s/t texcoords for drawing.
1298 *
1299 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1300 */
1301 GLboolean
1302 _mesa_meta_alloc_texture(struct temp_texture *tex,
1303 GLsizei width, GLsizei height, GLenum intFormat)
1304 {
1305 GLboolean newTex = GL_FALSE;
1306
1307 ASSERT(width <= tex->MaxSize);
1308 ASSERT(height <= tex->MaxSize);
1309
1310 if (width > tex->Width ||
1311 height > tex->Height ||
1312 intFormat != tex->IntFormat) {
1313 /* alloc new texture (larger or different format) */
1314
1315 if (tex->NPOT) {
1316 /* use non-power of two size */
1317 tex->Width = MAX2(tex->MinSize, width);
1318 tex->Height = MAX2(tex->MinSize, height);
1319 }
1320 else {
1321 /* find power of two size */
1322 GLsizei w, h;
1323 w = h = tex->MinSize;
1324 while (w < width)
1325 w *= 2;
1326 while (h < height)
1327 h *= 2;
1328 tex->Width = w;
1329 tex->Height = h;
1330 }
1331
1332 tex->IntFormat = intFormat;
1333
1334 newTex = GL_TRUE;
1335 }
1336
1337 /* compute texcoords */
1338 if (tex->Target == GL_TEXTURE_RECTANGLE) {
1339 tex->Sright = (GLfloat) width;
1340 tex->Ttop = (GLfloat) height;
1341 }
1342 else {
1343 tex->Sright = (GLfloat) width / tex->Width;
1344 tex->Ttop = (GLfloat) height / tex->Height;
1345 }
1346
1347 return newTex;
1348 }
1349
1350
1351 /**
1352 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1353 */
1354 void
1355 _mesa_meta_setup_copypix_texture(struct gl_context *ctx,
1356 struct temp_texture *tex,
1357 GLint srcX, GLint srcY,
1358 GLsizei width, GLsizei height,
1359 GLenum intFormat,
1360 GLenum filter)
1361 {
1362 bool newTex;
1363
1364 _mesa_BindTexture(tex->Target, tex->TexObj);
1365 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter);
1366 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter);
1367 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1368
1369 newTex = _mesa_meta_alloc_texture(tex, width, height, intFormat);
1370
1371 /* copy framebuffer image to texture */
1372 if (newTex) {
1373 /* create new tex image */
1374 if (tex->Width == width && tex->Height == height) {
1375 /* create new tex with framebuffer data */
1376 _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
1377 srcX, srcY, width, height, 0);
1378 }
1379 else {
1380 /* create empty texture */
1381 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1382 tex->Width, tex->Height, 0,
1383 intFormat, GL_UNSIGNED_BYTE, NULL);
1384 /* load image */
1385 _mesa_CopyTexSubImage2D(tex->Target, 0,
1386 0, 0, srcX, srcY, width, height);
1387 }
1388 }
1389 else {
1390 /* replace existing tex image */
1391 _mesa_CopyTexSubImage2D(tex->Target, 0,
1392 0, 0, srcX, srcY, width, height);
1393 }
1394 }
1395
1396
1397 /**
1398 * Setup/load texture for glDrawPixels.
1399 */
1400 void
1401 _mesa_meta_setup_drawpix_texture(struct gl_context *ctx,
1402 struct temp_texture *tex,
1403 GLboolean newTex,
1404 GLsizei width, GLsizei height,
1405 GLenum format, GLenum type,
1406 const GLvoid *pixels)
1407 {
1408 _mesa_BindTexture(tex->Target, tex->TexObj);
1409 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1410 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1411 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1412
1413 /* copy pixel data to texture */
1414 if (newTex) {
1415 /* create new tex image */
1416 if (tex->Width == width && tex->Height == height) {
1417 /* create new tex and load image data */
1418 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1419 tex->Width, tex->Height, 0, format, type, pixels);
1420 }
1421 else {
1422 struct gl_buffer_object *save_unpack_obj = NULL;
1423
1424 _mesa_reference_buffer_object(ctx, &save_unpack_obj,
1425 ctx->Unpack.BufferObj);
1426 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
1427 /* create empty texture */
1428 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1429 tex->Width, tex->Height, 0, format, type, NULL);
1430 if (save_unpack_obj != NULL)
1431 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,
1432 save_unpack_obj->Name);
1433 /* load image */
1434 _mesa_TexSubImage2D(tex->Target, 0,
1435 0, 0, width, height, format, type, pixels);
1436 }
1437 }
1438 else {
1439 /* replace existing tex image */
1440 _mesa_TexSubImage2D(tex->Target, 0,
1441 0, 0, width, height, format, type, pixels);
1442 }
1443 }
1444
1445 void
1446 _mesa_meta_setup_ff_tnl_for_blit(GLuint *VAO, GLuint *VBO,
1447 unsigned texcoord_size)
1448 {
1449 _mesa_meta_setup_vertex_objects(VAO, VBO, false, 2, texcoord_size, 0);
1450
1451 /* setup projection matrix */
1452 _mesa_MatrixMode(GL_PROJECTION);
1453 _mesa_LoadIdentity();
1454 }
1455
1456 /**
1457 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1458 */
1459 void
1460 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
1461 {
1462 struct clear_state *clear = &ctx->Meta->Clear;
1463 struct vertex verts[4];
1464 /* save all state but scissor, pixel pack/unpack */
1465 GLbitfield metaSave = (MESA_META_ALL -
1466 MESA_META_SCISSOR -
1467 MESA_META_PIXEL_STORE -
1468 MESA_META_CONDITIONAL_RENDER -
1469 MESA_META_FRAMEBUFFER_SRGB);
1470 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1471
1472 if (buffers & BUFFER_BITS_COLOR) {
1473 /* if clearing color buffers, don't save/restore colormask */
1474 metaSave -= MESA_META_COLOR_MASK;
1475 }
1476
1477 _mesa_meta_begin(ctx, metaSave);
1478
1479 _mesa_meta_setup_vertex_objects(&clear->VAO, &clear->VBO, false, 3, 0, 4);
1480
1481 /* GL_COLOR_BUFFER_BIT */
1482 if (buffers & BUFFER_BITS_COLOR) {
1483 /* leave colormask, glDrawBuffer state as-is */
1484
1485 /* Clears never have the color clamped. */
1486 if (ctx->Extensions.ARB_color_buffer_float)
1487 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1488 }
1489 else {
1490 ASSERT(metaSave & MESA_META_COLOR_MASK);
1491 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1492 }
1493
1494 /* GL_DEPTH_BUFFER_BIT */
1495 if (buffers & BUFFER_BIT_DEPTH) {
1496 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1497 _mesa_DepthFunc(GL_ALWAYS);
1498 _mesa_DepthMask(GL_TRUE);
1499 }
1500 else {
1501 assert(!ctx->Depth.Test);
1502 }
1503
1504 /* GL_STENCIL_BUFFER_BIT */
1505 if (buffers & BUFFER_BIT_STENCIL) {
1506 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1507 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1508 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1509 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1510 ctx->Stencil.Clear & stencilMax,
1511 ctx->Stencil.WriteMask[0]);
1512 }
1513 else {
1514 assert(!ctx->Stencil.Enabled);
1515 }
1516
1517 /* vertex positions/colors */
1518 {
1519 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
1520 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
1521 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
1522 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
1523 const GLfloat z = invert_z(ctx->Depth.Clear);
1524 GLuint i;
1525
1526 verts[0].x = x0;
1527 verts[0].y = y0;
1528 verts[0].z = z;
1529 verts[1].x = x1;
1530 verts[1].y = y0;
1531 verts[1].z = z;
1532 verts[2].x = x1;
1533 verts[2].y = y1;
1534 verts[2].z = z;
1535 verts[3].x = x0;
1536 verts[3].y = y1;
1537 verts[3].z = z;
1538
1539 /* vertex colors */
1540 for (i = 0; i < 4; i++) {
1541 verts[i].r = ctx->Color.ClearColor.f[0];
1542 verts[i].g = ctx->Color.ClearColor.f[1];
1543 verts[i].b = ctx->Color.ClearColor.f[2];
1544 verts[i].a = ctx->Color.ClearColor.f[3];
1545 }
1546
1547 /* upload new vertex data */
1548 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1549 GL_DYNAMIC_DRAW_ARB);
1550 }
1551
1552 /* draw quad */
1553 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1554
1555 _mesa_meta_end(ctx);
1556 }
1557
1558 static void
1559 meta_glsl_clear_init(struct gl_context *ctx, struct clear_state *clear)
1560 {
1561 const char *vs_source =
1562 "attribute vec4 position;\n"
1563 "void main()\n"
1564 "{\n"
1565 " gl_Position = position;\n"
1566 "}\n";
1567 const char *gs_source =
1568 "#version 150\n"
1569 "layout(triangles) in;\n"
1570 "layout(triangle_strip, max_vertices = 4) out;\n"
1571 "uniform int layer;\n"
1572 "void main()\n"
1573 "{\n"
1574 " for (int i = 0; i < 3; i++) {\n"
1575 " gl_Layer = layer;\n"
1576 " gl_Position = gl_in[i].gl_Position;\n"
1577 " EmitVertex();\n"
1578 " }\n"
1579 "}\n";
1580 const char *fs_source =
1581 "uniform vec4 color;\n"
1582 "void main()\n"
1583 "{\n"
1584 " gl_FragColor = color;\n"
1585 "}\n";
1586 GLuint vs, gs = 0, fs;
1587 bool has_integer_textures;
1588
1589 _mesa_meta_setup_vertex_objects(&clear->VAO, &clear->VBO, true, 3, 0, 0);
1590
1591 if (clear->ShaderProg != 0)
1592 return;
1593
1594 vs = _mesa_CreateShader(GL_VERTEX_SHADER);
1595 _mesa_ShaderSource(vs, 1, &vs_source, NULL);
1596 _mesa_CompileShader(vs);
1597
1598 if (_mesa_has_geometry_shaders(ctx)) {
1599 gs = _mesa_CreateShader(GL_GEOMETRY_SHADER);
1600 _mesa_ShaderSource(gs, 1, &gs_source, NULL);
1601 _mesa_CompileShader(gs);
1602 }
1603
1604 fs = _mesa_CreateShader(GL_FRAGMENT_SHADER);
1605 _mesa_ShaderSource(fs, 1, &fs_source, NULL);
1606 _mesa_CompileShader(fs);
1607
1608 clear->ShaderProg = _mesa_CreateProgram();
1609 _mesa_AttachShader(clear->ShaderProg, fs);
1610 _mesa_DeleteShader(fs);
1611 if (gs != 0)
1612 _mesa_AttachShader(clear->ShaderProg, gs);
1613 _mesa_AttachShader(clear->ShaderProg, vs);
1614 _mesa_DeleteShader(vs);
1615 _mesa_BindAttribLocation(clear->ShaderProg, 0, "position");
1616 _mesa_LinkProgram(clear->ShaderProg);
1617
1618 clear->ColorLocation = _mesa_GetUniformLocation(clear->ShaderProg,
1619 "color");
1620 if (gs != 0) {
1621 clear->LayerLocation = _mesa_GetUniformLocation(clear->ShaderProg,
1622 "layer");
1623 }
1624
1625 has_integer_textures = _mesa_is_gles3(ctx) ||
1626 (_mesa_is_desktop_gl(ctx) && ctx->Const.GLSLVersion >= 130);
1627
1628 if (has_integer_textures) {
1629 void *shader_source_mem_ctx = ralloc_context(NULL);
1630 const char *vs_int_source =
1631 ralloc_asprintf(shader_source_mem_ctx,
1632 "#version 130\n"
1633 "in vec4 position;\n"
1634 "void main()\n"
1635 "{\n"
1636 " gl_Position = position;\n"
1637 "}\n");
1638 const char *fs_int_source =
1639 ralloc_asprintf(shader_source_mem_ctx,
1640 "#version 130\n"
1641 "uniform ivec4 color;\n"
1642 "out ivec4 out_color;\n"
1643 "\n"
1644 "void main()\n"
1645 "{\n"
1646 " out_color = color;\n"
1647 "}\n");
1648
1649 vs = _mesa_meta_compile_shader_with_debug(ctx, GL_VERTEX_SHADER,
1650 vs_int_source);
1651 fs = _mesa_meta_compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER,
1652 fs_int_source);
1653 ralloc_free(shader_source_mem_ctx);
1654
1655 clear->IntegerShaderProg = _mesa_CreateProgram();
1656 _mesa_AttachShader(clear->IntegerShaderProg, fs);
1657 _mesa_DeleteShader(fs);
1658 if (gs != 0)
1659 _mesa_AttachShader(clear->IntegerShaderProg, gs);
1660 _mesa_AttachShader(clear->IntegerShaderProg, vs);
1661 _mesa_DeleteShader(vs);
1662 _mesa_BindAttribLocation(clear->IntegerShaderProg, 0, "position");
1663
1664 /* Note that user-defined out attributes get automatically assigned
1665 * locations starting from 0, so we don't need to explicitly
1666 * BindFragDataLocation to 0.
1667 */
1668
1669 _mesa_ObjectLabel(GL_PROGRAM, clear->IntegerShaderProg, -1,
1670 "integer clear");
1671 _mesa_meta_link_program_with_debug(ctx, clear->IntegerShaderProg);
1672
1673 clear->IntegerColorLocation =
1674 _mesa_GetUniformLocation(clear->IntegerShaderProg, "color");
1675 if (gs != 0) {
1676 clear->IntegerLayerLocation =
1677 _mesa_GetUniformLocation(clear->IntegerShaderProg, "layer");
1678 }
1679 }
1680 if (gs != 0)
1681 _mesa_DeleteShader(gs);
1682 }
1683
1684 static void
1685 meta_glsl_clear_cleanup(struct clear_state *clear)
1686 {
1687 if (clear->VAO == 0)
1688 return;
1689 _mesa_DeleteVertexArrays(1, &clear->VAO);
1690 clear->VAO = 0;
1691 _mesa_DeleteBuffers(1, &clear->VBO);
1692 clear->VBO = 0;
1693 _mesa_DeleteProgram(clear->ShaderProg);
1694 clear->ShaderProg = 0;
1695
1696 if (clear->IntegerShaderProg) {
1697 _mesa_DeleteProgram(clear->IntegerShaderProg);
1698 clear->IntegerShaderProg = 0;
1699 }
1700 }
1701
1702 /**
1703 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1704 */
1705 void
1706 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
1707 {
1708 struct clear_state *clear = &ctx->Meta->Clear;
1709 GLbitfield metaSave;
1710 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1711 struct gl_framebuffer *fb = ctx->DrawBuffer;
1712 const float x0 = ((float)fb->_Xmin / fb->Width) * 2.0f - 1.0f;
1713 const float y0 = ((float)fb->_Ymin / fb->Height) * 2.0f - 1.0f;
1714 const float x1 = ((float)fb->_Xmax / fb->Width) * 2.0f - 1.0f;
1715 const float y1 = ((float)fb->_Ymax / fb->Height) * 2.0f - 1.0f;
1716 const float z = -invert_z(ctx->Depth.Clear);
1717 struct vertex verts[4];
1718
1719 metaSave = (MESA_META_ALPHA_TEST |
1720 MESA_META_BLEND |
1721 MESA_META_DEPTH_TEST |
1722 MESA_META_RASTERIZATION |
1723 MESA_META_SHADER |
1724 MESA_META_STENCIL_TEST |
1725 MESA_META_VERTEX |
1726 MESA_META_VIEWPORT |
1727 MESA_META_CLIP |
1728 MESA_META_CLAMP_FRAGMENT_COLOR |
1729 MESA_META_MULTISAMPLE |
1730 MESA_META_OCCLUSION_QUERY);
1731
1732 if (!(buffers & BUFFER_BITS_COLOR)) {
1733 /* We'll use colormask to disable color writes. Otherwise,
1734 * respect color mask
1735 */
1736 metaSave |= MESA_META_COLOR_MASK;
1737 }
1738
1739 _mesa_meta_begin(ctx, metaSave);
1740
1741 meta_glsl_clear_init(ctx, clear);
1742
1743 if (fb->_IntegerColor) {
1744 _mesa_UseProgram(clear->IntegerShaderProg);
1745 _mesa_Uniform4iv(clear->IntegerColorLocation, 1,
1746 ctx->Color.ClearColor.i);
1747 } else {
1748 _mesa_UseProgram(clear->ShaderProg);
1749 _mesa_Uniform4fv(clear->ColorLocation, 1,
1750 ctx->Color.ClearColor.f);
1751 }
1752
1753 /* GL_COLOR_BUFFER_BIT */
1754 if (buffers & BUFFER_BITS_COLOR) {
1755 /* leave colormask, glDrawBuffer state as-is */
1756
1757 /* Clears never have the color clamped. */
1758 if (ctx->Extensions.ARB_color_buffer_float)
1759 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1760 }
1761 else {
1762 ASSERT(metaSave & MESA_META_COLOR_MASK);
1763 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1764 }
1765
1766 /* GL_DEPTH_BUFFER_BIT */
1767 if (buffers & BUFFER_BIT_DEPTH) {
1768 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1769 _mesa_DepthFunc(GL_ALWAYS);
1770 _mesa_DepthMask(GL_TRUE);
1771 }
1772 else {
1773 assert(!ctx->Depth.Test);
1774 }
1775
1776 /* GL_STENCIL_BUFFER_BIT */
1777 if (buffers & BUFFER_BIT_STENCIL) {
1778 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1779 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1780 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1781 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1782 ctx->Stencil.Clear & stencilMax,
1783 ctx->Stencil.WriteMask[0]);
1784 }
1785 else {
1786 assert(!ctx->Stencil.Enabled);
1787 }
1788
1789 /* vertex positions */
1790 verts[0].x = x0;
1791 verts[0].y = y0;
1792 verts[0].z = z;
1793 verts[1].x = x1;
1794 verts[1].y = y0;
1795 verts[1].z = z;
1796 verts[2].x = x1;
1797 verts[2].y = y1;
1798 verts[2].z = z;
1799 verts[3].x = x0;
1800 verts[3].y = y1;
1801 verts[3].z = z;
1802
1803 /* upload new vertex data */
1804 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1805 GL_DYNAMIC_DRAW_ARB);
1806
1807 /* draw quad(s) */
1808 if (fb->MaxNumLayers > 0) {
1809 unsigned layer;
1810 for (layer = 0; layer < fb->MaxNumLayers; layer++) {
1811 if (fb->_IntegerColor)
1812 _mesa_Uniform1i(clear->IntegerLayerLocation, layer);
1813 else
1814 _mesa_Uniform1i(clear->LayerLocation, layer);
1815 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1816 }
1817 } else {
1818 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1819 }
1820
1821 _mesa_meta_end(ctx);
1822 }
1823
1824 /**
1825 * Meta implementation of ctx->Driver.CopyPixels() in terms
1826 * of texture mapping and polygon rendering and GLSL shaders.
1827 */
1828 void
1829 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
1830 GLsizei width, GLsizei height,
1831 GLint dstX, GLint dstY, GLenum type)
1832 {
1833 struct copypix_state *copypix = &ctx->Meta->CopyPix;
1834 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
1835 struct vertex verts[4];
1836
1837 if (type != GL_COLOR ||
1838 ctx->_ImageTransferState ||
1839 ctx->Fog.Enabled ||
1840 width > tex->MaxSize ||
1841 height > tex->MaxSize) {
1842 /* XXX avoid this fallback */
1843 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1844 return;
1845 }
1846
1847 /* Most GL state applies to glCopyPixels, but a there's a few things
1848 * we need to override:
1849 */
1850 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
1851 MESA_META_SHADER |
1852 MESA_META_TEXTURE |
1853 MESA_META_TRANSFORM |
1854 MESA_META_CLIP |
1855 MESA_META_VERTEX |
1856 MESA_META_VIEWPORT));
1857
1858 _mesa_meta_setup_vertex_objects(&copypix->VAO, &copypix->VBO, false,
1859 3, 2, 0);
1860
1861 /* Silence valgrind warnings about reading uninitialized stack. */
1862 memset(verts, 0, sizeof(verts));
1863
1864 /* Alloc/setup texture */
1865 _mesa_meta_setup_copypix_texture(ctx, tex, srcX, srcY, width, height,
1866 GL_RGBA, GL_NEAREST);
1867
1868 /* vertex positions, texcoords (after texture allocation!) */
1869 {
1870 const GLfloat dstX0 = (GLfloat) dstX;
1871 const GLfloat dstY0 = (GLfloat) dstY;
1872 const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
1873 const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
1874 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
1875
1876 verts[0].x = dstX0;
1877 verts[0].y = dstY0;
1878 verts[0].z = z;
1879 verts[0].tex[0] = 0.0F;
1880 verts[0].tex[1] = 0.0F;
1881 verts[1].x = dstX1;
1882 verts[1].y = dstY0;
1883 verts[1].z = z;
1884 verts[1].tex[0] = tex->Sright;
1885 verts[1].tex[1] = 0.0F;
1886 verts[2].x = dstX1;
1887 verts[2].y = dstY1;
1888 verts[2].z = z;
1889 verts[2].tex[0] = tex->Sright;
1890 verts[2].tex[1] = tex->Ttop;
1891 verts[3].x = dstX0;
1892 verts[3].y = dstY1;
1893 verts[3].z = z;
1894 verts[3].tex[0] = 0.0F;
1895 verts[3].tex[1] = tex->Ttop;
1896
1897 /* upload new vertex data */
1898 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1899 }
1900
1901 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1902
1903 /* draw textured quad */
1904 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1905
1906 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1907
1908 _mesa_meta_end(ctx);
1909 }
1910
1911 static void
1912 meta_drawpix_cleanup(struct drawpix_state *drawpix)
1913 {
1914 if (drawpix->VAO != 0) {
1915 _mesa_DeleteVertexArrays(1, &drawpix->VAO);
1916 drawpix->VAO = 0;
1917
1918 _mesa_DeleteBuffers(1, &drawpix->VBO);
1919 drawpix->VBO = 0;
1920 }
1921
1922 if (drawpix->StencilFP != 0) {
1923 _mesa_DeleteProgramsARB(1, &drawpix->StencilFP);
1924 drawpix->StencilFP = 0;
1925 }
1926
1927 if (drawpix->DepthFP != 0) {
1928 _mesa_DeleteProgramsARB(1, &drawpix->DepthFP);
1929 drawpix->DepthFP = 0;
1930 }
1931 }
1932
1933 /**
1934 * When the glDrawPixels() image size is greater than the max rectangle
1935 * texture size we use this function to break the glDrawPixels() image
1936 * into tiles which fit into the max texture size.
1937 */
1938 static void
1939 tiled_draw_pixels(struct gl_context *ctx,
1940 GLint tileSize,
1941 GLint x, GLint y, GLsizei width, GLsizei height,
1942 GLenum format, GLenum type,
1943 const struct gl_pixelstore_attrib *unpack,
1944 const GLvoid *pixels)
1945 {
1946 struct gl_pixelstore_attrib tileUnpack = *unpack;
1947 GLint i, j;
1948
1949 if (tileUnpack.RowLength == 0)
1950 tileUnpack.RowLength = width;
1951
1952 for (i = 0; i < width; i += tileSize) {
1953 const GLint tileWidth = MIN2(tileSize, width - i);
1954 const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
1955
1956 tileUnpack.SkipPixels = unpack->SkipPixels + i;
1957
1958 for (j = 0; j < height; j += tileSize) {
1959 const GLint tileHeight = MIN2(tileSize, height - j);
1960 const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
1961
1962 tileUnpack.SkipRows = unpack->SkipRows + j;
1963
1964 _mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight,
1965 format, type, &tileUnpack, pixels);
1966 }
1967 }
1968 }
1969
1970
1971 /**
1972 * One-time init for drawing stencil pixels.
1973 */
1974 static void
1975 init_draw_stencil_pixels(struct gl_context *ctx)
1976 {
1977 /* This program is run eight times, once for each stencil bit.
1978 * The stencil values to draw are found in an 8-bit alpha texture.
1979 * We read the texture/stencil value and test if bit 'b' is set.
1980 * If the bit is not set, use KIL to kill the fragment.
1981 * Finally, we use the stencil test to update the stencil buffer.
1982 *
1983 * The basic algorithm for checking if a bit is set is:
1984 * if (is_odd(value / (1 << bit)))
1985 * result is one (or non-zero).
1986 * else
1987 * result is zero.
1988 * The program parameter contains three values:
1989 * parm.x = 255 / (1 << bit)
1990 * parm.y = 0.5
1991 * parm.z = 0.0
1992 */
1993 static const char *program =
1994 "!!ARBfp1.0\n"
1995 "PARAM parm = program.local[0]; \n"
1996 "TEMP t; \n"
1997 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1998 "# t = t * 255 / bit \n"
1999 "MUL t.x, t.a, parm.x; \n"
2000 "# t = (int) t \n"
2001 "FRC t.y, t.x; \n"
2002 "SUB t.x, t.x, t.y; \n"
2003 "# t = t * 0.5 \n"
2004 "MUL t.x, t.x, parm.y; \n"
2005 "# t = fract(t.x) \n"
2006 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2007 "# t.x = (t.x == 0 ? 1 : 0) \n"
2008 "SGE t.x, -t.x, parm.z; \n"
2009 "KIL -t.x; \n"
2010 "# for debug only \n"
2011 "#MOV result.color, t.x; \n"
2012 "END \n";
2013 char program2[1000];
2014 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2015 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2016 const char *texTarget;
2017
2018 assert(drawpix->StencilFP == 0);
2019
2020 /* replace %s with "RECT" or "2D" */
2021 assert(strlen(program) + 4 < sizeof(program2));
2022 if (tex->Target == GL_TEXTURE_RECTANGLE)
2023 texTarget = "RECT";
2024 else
2025 texTarget = "2D";
2026 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
2027
2028 _mesa_GenProgramsARB(1, &drawpix->StencilFP);
2029 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2030 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2031 strlen(program2), (const GLubyte *) program2);
2032 }
2033
2034
2035 /**
2036 * One-time init for drawing depth pixels.
2037 */
2038 static void
2039 init_draw_depth_pixels(struct gl_context *ctx)
2040 {
2041 static const char *program =
2042 "!!ARBfp1.0\n"
2043 "PARAM color = program.local[0]; \n"
2044 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2045 "MOV result.color, color; \n"
2046 "END \n";
2047 char program2[200];
2048 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2049 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2050 const char *texTarget;
2051
2052 assert(drawpix->DepthFP == 0);
2053
2054 /* replace %s with "RECT" or "2D" */
2055 assert(strlen(program) + 4 < sizeof(program2));
2056 if (tex->Target == GL_TEXTURE_RECTANGLE)
2057 texTarget = "RECT";
2058 else
2059 texTarget = "2D";
2060 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
2061
2062 _mesa_GenProgramsARB(1, &drawpix->DepthFP);
2063 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2064 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2065 strlen(program2), (const GLubyte *) program2);
2066 }
2067
2068
2069 /**
2070 * Meta implementation of ctx->Driver.DrawPixels() in terms
2071 * of texture mapping and polygon rendering.
2072 */
2073 void
2074 _mesa_meta_DrawPixels(struct gl_context *ctx,
2075 GLint x, GLint y, GLsizei width, GLsizei height,
2076 GLenum format, GLenum type,
2077 const struct gl_pixelstore_attrib *unpack,
2078 const GLvoid *pixels)
2079 {
2080 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2081 struct temp_texture *tex = _mesa_meta_get_temp_texture(ctx);
2082 const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
2083 const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
2084 struct vertex verts[4];
2085 GLenum texIntFormat;
2086 GLboolean fallback, newTex;
2087 GLbitfield metaExtraSave = 0x0;
2088
2089 /*
2090 * Determine if we can do the glDrawPixels with texture mapping.
2091 */
2092 fallback = GL_FALSE;
2093 if (ctx->Fog.Enabled) {
2094 fallback = GL_TRUE;
2095 }
2096
2097 if (_mesa_is_color_format(format)) {
2098 /* use more compact format when possible */
2099 /* XXX disable special case for GL_LUMINANCE for now to work around
2100 * apparent i965 driver bug (see bug #23670).
2101 */
2102 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
2103 texIntFormat = format;
2104 else
2105 texIntFormat = GL_RGBA;
2106
2107 /* If we're not supposed to clamp the resulting color, then just
2108 * promote our texture to fully float. We could do better by
2109 * just going for the matching set of channels, in floating
2110 * point.
2111 */
2112 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
2113 ctx->Extensions.ARB_texture_float)
2114 texIntFormat = GL_RGBA32F;
2115 }
2116 else if (_mesa_is_stencil_format(format)) {
2117 if (ctx->Extensions.ARB_fragment_program &&
2118 ctx->Pixel.IndexShift == 0 &&
2119 ctx->Pixel.IndexOffset == 0 &&
2120 type == GL_UNSIGNED_BYTE) {
2121 /* We'll store stencil as alpha. This only works for GLubyte
2122 * image data because of how incoming values are mapped to alpha
2123 * in [0,1].
2124 */
2125 texIntFormat = GL_ALPHA;
2126 metaExtraSave = (MESA_META_COLOR_MASK |
2127 MESA_META_DEPTH_TEST |
2128 MESA_META_PIXEL_TRANSFER |
2129 MESA_META_SHADER |
2130 MESA_META_STENCIL_TEST);
2131 }
2132 else {
2133 fallback = GL_TRUE;
2134 }
2135 }
2136 else if (_mesa_is_depth_format(format)) {
2137 if (ctx->Extensions.ARB_depth_texture &&
2138 ctx->Extensions.ARB_fragment_program) {
2139 texIntFormat = GL_DEPTH_COMPONENT;
2140 metaExtraSave = (MESA_META_SHADER);
2141 }
2142 else {
2143 fallback = GL_TRUE;
2144 }
2145 }
2146 else {
2147 fallback = GL_TRUE;
2148 }
2149
2150 if (fallback) {
2151 _swrast_DrawPixels(ctx, x, y, width, height,
2152 format, type, unpack, pixels);
2153 return;
2154 }
2155
2156 /*
2157 * Check image size against max texture size, draw as tiles if needed.
2158 */
2159 if (width > tex->MaxSize || height > tex->MaxSize) {
2160 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
2161 format, type, unpack, pixels);
2162 return;
2163 }
2164
2165 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2166 * but a there's a few things we need to override:
2167 */
2168 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
2169 MESA_META_SHADER |
2170 MESA_META_TEXTURE |
2171 MESA_META_TRANSFORM |
2172 MESA_META_CLIP |
2173 MESA_META_VERTEX |
2174 MESA_META_VIEWPORT |
2175 metaExtraSave));
2176
2177 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2178
2179 _mesa_meta_setup_vertex_objects(&drawpix->VAO, &drawpix->VBO, false,
2180 3, 2, 0);
2181
2182 /* Silence valgrind warnings about reading uninitialized stack. */
2183 memset(verts, 0, sizeof(verts));
2184
2185 /* vertex positions, texcoords (after texture allocation!) */
2186 {
2187 const GLfloat x0 = (GLfloat) x;
2188 const GLfloat y0 = (GLfloat) y;
2189 const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
2190 const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
2191 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2192
2193 verts[0].x = x0;
2194 verts[0].y = y0;
2195 verts[0].z = z;
2196 verts[0].tex[0] = 0.0F;
2197 verts[0].tex[1] = 0.0F;
2198 verts[1].x = x1;
2199 verts[1].y = y0;
2200 verts[1].z = z;
2201 verts[1].tex[0] = tex->Sright;
2202 verts[1].tex[1] = 0.0F;
2203 verts[2].x = x1;
2204 verts[2].y = y1;
2205 verts[2].z = z;
2206 verts[2].tex[0] = tex->Sright;
2207 verts[2].tex[1] = tex->Ttop;
2208 verts[3].x = x0;
2209 verts[3].y = y1;
2210 verts[3].z = z;
2211 verts[3].tex[0] = 0.0F;
2212 verts[3].tex[1] = tex->Ttop;
2213 }
2214
2215 /* upload new vertex data */
2216 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2217 verts, GL_DYNAMIC_DRAW_ARB);
2218
2219 /* set given unpack params */
2220 ctx->Unpack = *unpack;
2221
2222 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2223
2224 if (_mesa_is_stencil_format(format)) {
2225 /* Drawing stencil */
2226 GLint bit;
2227
2228 if (!drawpix->StencilFP)
2229 init_draw_stencil_pixels(ctx);
2230
2231 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2232 GL_ALPHA, type, pixels);
2233
2234 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2235
2236 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
2237
2238 /* set all stencil bits to 0 */
2239 _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2240 _mesa_StencilFunc(GL_ALWAYS, 0, 255);
2241 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2242
2243 /* set stencil bits to 1 where needed */
2244 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2245
2246 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2247 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2248
2249 for (bit = 0; bit < ctx->DrawBuffer->Visual.stencilBits; bit++) {
2250 const GLuint mask = 1 << bit;
2251 if (mask & origStencilMask) {
2252 _mesa_StencilFunc(GL_ALWAYS, mask, mask);
2253 _mesa_StencilMask(mask);
2254
2255 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2256 255.0f / mask, 0.5f, 0.0f, 0.0f);
2257
2258 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2259 }
2260 }
2261 }
2262 else if (_mesa_is_depth_format(format)) {
2263 /* Drawing depth */
2264 if (!drawpix->DepthFP)
2265 init_draw_depth_pixels(ctx);
2266
2267 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2268 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2269
2270 /* polygon color = current raster color */
2271 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2272 ctx->Current.RasterColor);
2273
2274 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2275 format, type, pixels);
2276
2277 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2278 }
2279 else {
2280 /* Drawing RGBA */
2281 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2282 format, type, pixels);
2283 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2284 }
2285
2286 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2287
2288 /* restore unpack params */
2289 ctx->Unpack = unpackSave;
2290
2291 _mesa_meta_end(ctx);
2292 }
2293
2294 static GLboolean
2295 alpha_test_raster_color(struct gl_context *ctx)
2296 {
2297 GLfloat alpha = ctx->Current.RasterColor[ACOMP];
2298 GLfloat ref = ctx->Color.AlphaRef;
2299
2300 switch (ctx->Color.AlphaFunc) {
2301 case GL_NEVER:
2302 return GL_FALSE;
2303 case GL_LESS:
2304 return alpha < ref;
2305 case GL_EQUAL:
2306 return alpha == ref;
2307 case GL_LEQUAL:
2308 return alpha <= ref;
2309 case GL_GREATER:
2310 return alpha > ref;
2311 case GL_NOTEQUAL:
2312 return alpha != ref;
2313 case GL_GEQUAL:
2314 return alpha >= ref;
2315 case GL_ALWAYS:
2316 return GL_TRUE;
2317 default:
2318 assert(0);
2319 return GL_FALSE;
2320 }
2321 }
2322
2323 /**
2324 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2325 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2326 * tracker would improve performance a lot.
2327 */
2328 void
2329 _mesa_meta_Bitmap(struct gl_context *ctx,
2330 GLint x, GLint y, GLsizei width, GLsizei height,
2331 const struct gl_pixelstore_attrib *unpack,
2332 const GLubyte *bitmap1)
2333 {
2334 struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
2335 struct temp_texture *tex = get_bitmap_temp_texture(ctx);
2336 const GLenum texIntFormat = GL_ALPHA;
2337 const struct gl_pixelstore_attrib unpackSave = *unpack;
2338 GLubyte fg, bg;
2339 struct vertex verts[4];
2340 GLboolean newTex;
2341 GLubyte *bitmap8;
2342
2343 /*
2344 * Check if swrast fallback is needed.
2345 */
2346 if (ctx->_ImageTransferState ||
2347 ctx->FragmentProgram._Enabled ||
2348 ctx->Fog.Enabled ||
2349 ctx->Texture._MaxEnabledTexImageUnit != -1 ||
2350 width > tex->MaxSize ||
2351 height > tex->MaxSize) {
2352 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
2353 return;
2354 }
2355
2356 if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
2357 return;
2358
2359 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2360 * but a there's a few things we need to override:
2361 */
2362 _mesa_meta_begin(ctx, (MESA_META_ALPHA_TEST |
2363 MESA_META_PIXEL_STORE |
2364 MESA_META_RASTERIZATION |
2365 MESA_META_SHADER |
2366 MESA_META_TEXTURE |
2367 MESA_META_TRANSFORM |
2368 MESA_META_CLIP |
2369 MESA_META_VERTEX |
2370 MESA_META_VIEWPORT));
2371
2372 _mesa_meta_setup_vertex_objects(&bitmap->VAO, &bitmap->VBO, false, 3, 2, 4);
2373
2374 newTex = _mesa_meta_alloc_texture(tex, width, height, texIntFormat);
2375
2376 /* Silence valgrind warnings about reading uninitialized stack. */
2377 memset(verts, 0, sizeof(verts));
2378
2379 /* vertex positions, texcoords, colors (after texture allocation!) */
2380 {
2381 const GLfloat x0 = (GLfloat) x;
2382 const GLfloat y0 = (GLfloat) y;
2383 const GLfloat x1 = (GLfloat) (x + width);
2384 const GLfloat y1 = (GLfloat) (y + height);
2385 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2386 GLuint i;
2387
2388 verts[0].x = x0;
2389 verts[0].y = y0;
2390 verts[0].z = z;
2391 verts[0].tex[0] = 0.0F;
2392 verts[0].tex[1] = 0.0F;
2393 verts[1].x = x1;
2394 verts[1].y = y0;
2395 verts[1].z = z;
2396 verts[1].tex[0] = tex->Sright;
2397 verts[1].tex[1] = 0.0F;
2398 verts[2].x = x1;
2399 verts[2].y = y1;
2400 verts[2].z = z;
2401 verts[2].tex[0] = tex->Sright;
2402 verts[2].tex[1] = tex->Ttop;
2403 verts[3].x = x0;
2404 verts[3].y = y1;
2405 verts[3].z = z;
2406 verts[3].tex[0] = 0.0F;
2407 verts[3].tex[1] = tex->Ttop;
2408
2409 for (i = 0; i < 4; i++) {
2410 verts[i].r = ctx->Current.RasterColor[0];
2411 verts[i].g = ctx->Current.RasterColor[1];
2412 verts[i].b = ctx->Current.RasterColor[2];
2413 verts[i].a = ctx->Current.RasterColor[3];
2414 }
2415
2416 /* upload new vertex data */
2417 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2418 }
2419
2420 /* choose different foreground/background alpha values */
2421 CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
2422 bg = (fg > 127 ? 0 : 255);
2423
2424 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
2425 if (!bitmap1) {
2426 _mesa_meta_end(ctx);
2427 return;
2428 }
2429
2430 bitmap8 = malloc(width * height);
2431 if (bitmap8) {
2432 memset(bitmap8, bg, width * height);
2433 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
2434 bitmap8, width, fg);
2435
2436 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2437
2438 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
2439 _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
2440
2441 _mesa_meta_setup_drawpix_texture(ctx, tex, newTex, width, height,
2442 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
2443
2444 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2445
2446 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2447
2448 free(bitmap8);
2449 }
2450
2451 _mesa_unmap_pbo_source(ctx, &unpackSave);
2452
2453 _mesa_meta_end(ctx);
2454 }
2455
2456 /**
2457 * Compute the texture coordinates for the four vertices of a quad for
2458 * drawing a 2D texture image or slice of a cube/3D texture.
2459 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2460 * \param slice slice of a 1D/2D array texture or 3D texture
2461 * \param width width of the texture image
2462 * \param height height of the texture image
2463 * \param coords0/1/2/3 returns the computed texcoords
2464 */
2465 void
2466 _mesa_meta_setup_texture_coords(GLenum faceTarget,
2467 GLint slice,
2468 GLint width,
2469 GLint height,
2470 GLint depth,
2471 GLfloat coords0[4],
2472 GLfloat coords1[4],
2473 GLfloat coords2[4],
2474 GLfloat coords3[4])
2475 {
2476 static const GLfloat st[4][2] = {
2477 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
2478 };
2479 GLuint i;
2480 GLfloat r;
2481
2482 if (faceTarget == GL_TEXTURE_CUBE_MAP_ARRAY)
2483 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice % 6;
2484
2485 /* Currently all texture targets want the W component to be 1.0.
2486 */
2487 coords0[3] = 1.0F;
2488 coords1[3] = 1.0F;
2489 coords2[3] = 1.0F;
2490 coords3[3] = 1.0F;
2491
2492 switch (faceTarget) {
2493 case GL_TEXTURE_1D:
2494 case GL_TEXTURE_2D:
2495 case GL_TEXTURE_3D:
2496 case GL_TEXTURE_2D_ARRAY:
2497 if (faceTarget == GL_TEXTURE_3D) {
2498 assert(slice < depth);
2499 assert(depth >= 1);
2500 r = (slice + 0.5f) / depth;
2501 }
2502 else if (faceTarget == GL_TEXTURE_2D_ARRAY)
2503 r = (float) slice;
2504 else
2505 r = 0.0F;
2506 coords0[0] = 0.0F; /* s */
2507 coords0[1] = 0.0F; /* t */
2508 coords0[2] = r; /* r */
2509 coords1[0] = 1.0F;
2510 coords1[1] = 0.0F;
2511 coords1[2] = r;
2512 coords2[0] = 1.0F;
2513 coords2[1] = 1.0F;
2514 coords2[2] = r;
2515 coords3[0] = 0.0F;
2516 coords3[1] = 1.0F;
2517 coords3[2] = r;
2518 break;
2519 case GL_TEXTURE_RECTANGLE_ARB:
2520 coords0[0] = 0.0F; /* s */
2521 coords0[1] = 0.0F; /* t */
2522 coords0[2] = 0.0F; /* r */
2523 coords1[0] = (float) width;
2524 coords1[1] = 0.0F;
2525 coords1[2] = 0.0F;
2526 coords2[0] = (float) width;
2527 coords2[1] = (float) height;
2528 coords2[2] = 0.0F;
2529 coords3[0] = 0.0F;
2530 coords3[1] = (float) height;
2531 coords3[2] = 0.0F;
2532 break;
2533 case GL_TEXTURE_1D_ARRAY:
2534 coords0[0] = 0.0F; /* s */
2535 coords0[1] = (float) slice; /* t */
2536 coords0[2] = 0.0F; /* r */
2537 coords1[0] = 1.0f;
2538 coords1[1] = (float) slice;
2539 coords1[2] = 0.0F;
2540 coords2[0] = 1.0F;
2541 coords2[1] = (float) slice;
2542 coords2[2] = 0.0F;
2543 coords3[0] = 0.0F;
2544 coords3[1] = (float) slice;
2545 coords3[2] = 0.0F;
2546 break;
2547
2548 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2549 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2550 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2551 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2552 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2553 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2554 /* loop over quad verts */
2555 for (i = 0; i < 4; i++) {
2556 /* Compute sc = +/-scale and tc = +/-scale.
2557 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2558 * though that can still sometimes happen with this scale factor...
2559 */
2560 const GLfloat scale = 0.9999f;
2561 const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
2562 const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
2563 GLfloat *coord;
2564
2565 switch (i) {
2566 case 0:
2567 coord = coords0;
2568 break;
2569 case 1:
2570 coord = coords1;
2571 break;
2572 case 2:
2573 coord = coords2;
2574 break;
2575 case 3:
2576 coord = coords3;
2577 break;
2578 default:
2579 assert(0);
2580 }
2581
2582 coord[3] = (float) (slice / 6);
2583
2584 switch (faceTarget) {
2585 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2586 coord[0] = 1.0f;
2587 coord[1] = -tc;
2588 coord[2] = -sc;
2589 break;
2590 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2591 coord[0] = -1.0f;
2592 coord[1] = -tc;
2593 coord[2] = sc;
2594 break;
2595 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2596 coord[0] = sc;
2597 coord[1] = 1.0f;
2598 coord[2] = tc;
2599 break;
2600 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2601 coord[0] = sc;
2602 coord[1] = -1.0f;
2603 coord[2] = -tc;
2604 break;
2605 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2606 coord[0] = sc;
2607 coord[1] = -tc;
2608 coord[2] = 1.0f;
2609 break;
2610 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2611 coord[0] = -sc;
2612 coord[1] = -tc;
2613 coord[2] = -1.0f;
2614 break;
2615 default:
2616 assert(0);
2617 }
2618 }
2619 break;
2620 default:
2621 assert(!"unexpected target in _mesa_meta_setup_texture_coords()");
2622 }
2623 }
2624
2625 static struct blit_shader *
2626 choose_blit_shader(GLenum target, struct blit_shader_table *table)
2627 {
2628 switch(target) {
2629 case GL_TEXTURE_1D:
2630 table->sampler_1d.type = "sampler1D";
2631 table->sampler_1d.func = "texture1D";
2632 table->sampler_1d.texcoords = "texCoords.x";
2633 return &table->sampler_1d;
2634 case GL_TEXTURE_2D:
2635 table->sampler_2d.type = "sampler2D";
2636 table->sampler_2d.func = "texture2D";
2637 table->sampler_2d.texcoords = "texCoords.xy";
2638 return &table->sampler_2d;
2639 case GL_TEXTURE_RECTANGLE:
2640 table->sampler_rect.type = "sampler2DRect";
2641 table->sampler_rect.func = "texture2DRect";
2642 table->sampler_rect.texcoords = "texCoords.xy";
2643 return &table->sampler_rect;
2644 case GL_TEXTURE_3D:
2645 /* Code for mipmap generation with 3D textures is not used yet.
2646 * It's a sw fallback.
2647 */
2648 table->sampler_3d.type = "sampler3D";
2649 table->sampler_3d.func = "texture3D";
2650 table->sampler_3d.texcoords = "texCoords.xyz";
2651 return &table->sampler_3d;
2652 case GL_TEXTURE_CUBE_MAP:
2653 table->sampler_cubemap.type = "samplerCube";
2654 table->sampler_cubemap.func = "textureCube";
2655 table->sampler_cubemap.texcoords = "texCoords.xyz";
2656 return &table->sampler_cubemap;
2657 case GL_TEXTURE_1D_ARRAY:
2658 table->sampler_1d_array.type = "sampler1DArray";
2659 table->sampler_1d_array.func = "texture1DArray";
2660 table->sampler_1d_array.texcoords = "texCoords.xy";
2661 return &table->sampler_1d_array;
2662 case GL_TEXTURE_2D_ARRAY:
2663 table->sampler_2d_array.type = "sampler2DArray";
2664 table->sampler_2d_array.func = "texture2DArray";
2665 table->sampler_2d_array.texcoords = "texCoords.xyz";
2666 return &table->sampler_2d_array;
2667 case GL_TEXTURE_CUBE_MAP_ARRAY:
2668 table->sampler_cubemap_array.type = "samplerCubeArray";
2669 table->sampler_cubemap_array.func = "textureCubeArray";
2670 table->sampler_cubemap_array.texcoords = "texCoords.xyzw";
2671 return &table->sampler_cubemap_array;
2672 default:
2673 _mesa_problem(NULL, "Unexpected texture target 0x%x in"
2674 " setup_texture_sampler()\n", target);
2675 return NULL;
2676 }
2677 }
2678
2679 void
2680 _mesa_meta_blit_shader_table_cleanup(struct blit_shader_table *table)
2681 {
2682 _mesa_DeleteProgram(table->sampler_1d.shader_prog);
2683 _mesa_DeleteProgram(table->sampler_2d.shader_prog);
2684 _mesa_DeleteProgram(table->sampler_3d.shader_prog);
2685 _mesa_DeleteProgram(table->sampler_rect.shader_prog);
2686 _mesa_DeleteProgram(table->sampler_cubemap.shader_prog);
2687 _mesa_DeleteProgram(table->sampler_1d_array.shader_prog);
2688 _mesa_DeleteProgram(table->sampler_2d_array.shader_prog);
2689 _mesa_DeleteProgram(table->sampler_cubemap_array.shader_prog);
2690
2691 table->sampler_1d.shader_prog = 0;
2692 table->sampler_2d.shader_prog = 0;
2693 table->sampler_3d.shader_prog = 0;
2694 table->sampler_rect.shader_prog = 0;
2695 table->sampler_cubemap.shader_prog = 0;
2696 table->sampler_1d_array.shader_prog = 0;
2697 table->sampler_2d_array.shader_prog = 0;
2698 table->sampler_cubemap_array.shader_prog = 0;
2699 }
2700
2701 /**
2702 * Determine the GL data type to use for the temporary image read with
2703 * ReadPixels() and passed to Tex[Sub]Image().
2704 */
2705 static GLenum
2706 get_temp_image_type(struct gl_context *ctx, mesa_format format)
2707 {
2708 GLenum baseFormat;
2709
2710 baseFormat = _mesa_get_format_base_format(format);
2711
2712 switch (baseFormat) {
2713 case GL_RGBA:
2714 case GL_RGB:
2715 case GL_RG:
2716 case GL_RED:
2717 case GL_ALPHA:
2718 case GL_LUMINANCE:
2719 case GL_LUMINANCE_ALPHA:
2720 case GL_INTENSITY:
2721 if (ctx->DrawBuffer->Visual.redBits <= 8) {
2722 return GL_UNSIGNED_BYTE;
2723 } else if (ctx->DrawBuffer->Visual.redBits <= 16) {
2724 return GL_UNSIGNED_SHORT;
2725 } else {
2726 GLenum datatype = _mesa_get_format_datatype(format);
2727 if (datatype == GL_INT || datatype == GL_UNSIGNED_INT)
2728 return datatype;
2729 return GL_FLOAT;
2730 }
2731 case GL_DEPTH_COMPONENT: {
2732 GLenum datatype = _mesa_get_format_datatype(format);
2733 if (datatype == GL_FLOAT)
2734 return GL_FLOAT;
2735 else
2736 return GL_UNSIGNED_INT;
2737 }
2738 case GL_DEPTH_STENCIL: {
2739 GLenum datatype = _mesa_get_format_datatype(format);
2740 if (datatype == GL_FLOAT)
2741 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
2742 else
2743 return GL_UNSIGNED_INT_24_8;
2744 }
2745 default:
2746 _mesa_problem(ctx, "Unexpected format %d in get_temp_image_type()",
2747 baseFormat);
2748 return 0;
2749 }
2750 }
2751
2752 /**
2753 * Attempts to wrap the destination texture in an FBO and use
2754 * glBlitFramebuffer() to implement glCopyTexSubImage().
2755 */
2756 static bool
2757 copytexsubimage_using_blit_framebuffer(struct gl_context *ctx, GLuint dims,
2758 struct gl_texture_image *texImage,
2759 GLint xoffset,
2760 GLint yoffset,
2761 GLint zoffset,
2762 struct gl_renderbuffer *rb,
2763 GLint x, GLint y,
2764 GLsizei width, GLsizei height)
2765 {
2766 struct gl_texture_object *texObj = texImage->TexObject;
2767 GLuint fbo;
2768 bool success = false;
2769 GLbitfield mask;
2770 GLenum status;
2771
2772 if (!ctx->Extensions.ARB_framebuffer_object)
2773 return false;
2774
2775 _mesa_unlock_texture(ctx, texObj);
2776
2777 _mesa_meta_begin(ctx, MESA_META_ALL);
2778
2779 _mesa_GenFramebuffers(1, &fbo);
2780 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
2781
2782 if (rb->_BaseFormat == GL_DEPTH_STENCIL ||
2783 rb->_BaseFormat == GL_DEPTH_COMPONENT) {
2784 _mesa_meta_bind_fbo_image(GL_DEPTH_ATTACHMENT, texImage, zoffset);
2785 mask = GL_DEPTH_BUFFER_BIT;
2786
2787 if (rb->_BaseFormat == GL_DEPTH_STENCIL &&
2788 texImage->_BaseFormat == GL_DEPTH_STENCIL) {
2789 _mesa_meta_bind_fbo_image(GL_STENCIL_ATTACHMENT, texImage, zoffset);
2790 mask |= GL_STENCIL_BUFFER_BIT;
2791 }
2792 _mesa_DrawBuffer(GL_NONE);
2793 } else {
2794 _mesa_meta_bind_fbo_image(GL_COLOR_ATTACHMENT0, texImage, zoffset);
2795 mask = GL_COLOR_BUFFER_BIT;
2796 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
2797 }
2798
2799 status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
2800 if (status != GL_FRAMEBUFFER_COMPLETE)
2801 goto out;
2802
2803 ctx->Meta->Blit.no_ctsi_fallback = true;
2804
2805 /* Since we've bound a new draw framebuffer, we need to update
2806 * its derived state -- _Xmin, etc -- for BlitFramebuffer's clipping to
2807 * be correct.
2808 */
2809 _mesa_update_state(ctx);
2810
2811 /* We skip the core BlitFramebuffer checks for format consistency, which
2812 * are too strict for CopyTexImage. We know meta will be fine with format
2813 * changes.
2814 */
2815 _mesa_meta_BlitFramebuffer(ctx, x, y,
2816 x + width, y + height,
2817 xoffset, yoffset,
2818 xoffset + width, yoffset + height,
2819 mask, GL_NEAREST);
2820 ctx->Meta->Blit.no_ctsi_fallback = false;
2821 success = true;
2822
2823 out:
2824 _mesa_lock_texture(ctx, texObj);
2825 _mesa_DeleteFramebuffers(1, &fbo);
2826 _mesa_meta_end(ctx);
2827 return success;
2828 }
2829
2830 /**
2831 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2832 * Have to be careful with locking and meta state for pixel transfer.
2833 */
2834 void
2835 _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2836 struct gl_texture_image *texImage,
2837 GLint xoffset, GLint yoffset, GLint zoffset,
2838 struct gl_renderbuffer *rb,
2839 GLint x, GLint y,
2840 GLsizei width, GLsizei height)
2841 {
2842 struct gl_texture_object *texObj = texImage->TexObject;
2843 GLenum format, type;
2844 GLint bpp;
2845 void *buf;
2846
2847 if (copytexsubimage_using_blit_framebuffer(ctx, dims,
2848 texImage,
2849 xoffset, yoffset, zoffset,
2850 rb,
2851 x, y,
2852 width, height)) {
2853 return;
2854 }
2855
2856 /* Choose format/type for temporary image buffer */
2857 format = _mesa_get_format_base_format(texImage->TexFormat);
2858 if (format == GL_LUMINANCE ||
2859 format == GL_LUMINANCE_ALPHA ||
2860 format == GL_INTENSITY) {
2861 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
2862 * temp image buffer because glReadPixels will do L=R+G+B which is
2863 * not what we want (should be L=R).
2864 */
2865 format = GL_RGBA;
2866 }
2867
2868 type = get_temp_image_type(ctx, texImage->TexFormat);
2869 if (_mesa_is_format_integer_color(texImage->TexFormat)) {
2870 format = _mesa_base_format_to_integer_format(format);
2871 }
2872 bpp = _mesa_bytes_per_pixel(format, type);
2873 if (bpp <= 0) {
2874 _mesa_problem(ctx, "Bad bpp in _mesa_meta_CopyTexSubImage()");
2875 return;
2876 }
2877
2878 /*
2879 * Alloc image buffer (XXX could use a PBO)
2880 */
2881 buf = malloc(width * height * bpp);
2882 if (!buf) {
2883 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
2884 return;
2885 }
2886
2887 _mesa_unlock_texture(ctx, texObj); /* need to unlock first */
2888
2889 /*
2890 * Read image from framebuffer (disable pixel transfer ops)
2891 */
2892 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE | MESA_META_PIXEL_TRANSFER);
2893 ctx->Driver.ReadPixels(ctx, x, y, width, height,
2894 format, type, &ctx->Pack, buf);
2895 _mesa_meta_end(ctx);
2896
2897 _mesa_update_state(ctx); /* to update pixel transfer state */
2898
2899 /*
2900 * Store texture data (with pixel transfer ops)
2901 */
2902 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE);
2903
2904 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
2905 assert(yoffset == 0);
2906 ctx->Driver.TexSubImage(ctx, dims, texImage,
2907 xoffset, zoffset, 0, width, 1, 1,
2908 format, type, buf, &ctx->Unpack);
2909 } else {
2910 ctx->Driver.TexSubImage(ctx, dims, texImage,
2911 xoffset, yoffset, zoffset, width, height, 1,
2912 format, type, buf, &ctx->Unpack);
2913 }
2914
2915 _mesa_meta_end(ctx);
2916
2917 _mesa_lock_texture(ctx, texObj); /* re-lock */
2918
2919 free(buf);
2920 }
2921
2922
2923 static void
2924 meta_decompress_cleanup(struct decompress_state *decompress)
2925 {
2926 if (decompress->FBO != 0) {
2927 _mesa_DeleteFramebuffers(1, &decompress->FBO);
2928 _mesa_DeleteRenderbuffers(1, &decompress->RBO);
2929 }
2930
2931 if (decompress->VAO != 0) {
2932 _mesa_DeleteVertexArrays(1, &decompress->VAO);
2933 _mesa_DeleteBuffers(1, &decompress->VBO);
2934 }
2935
2936 if (decompress->Sampler != 0)
2937 _mesa_DeleteSamplers(1, &decompress->Sampler);
2938
2939 memset(decompress, 0, sizeof(*decompress));
2940 }
2941
2942 /**
2943 * Decompress a texture image by drawing a quad with the compressed
2944 * texture and reading the pixels out of the color buffer.
2945 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
2946 * \param destFormat format, ala glReadPixels
2947 * \param destType type, ala glReadPixels
2948 * \param dest destination buffer
2949 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
2950 */
2951 static void
2952 decompress_texture_image(struct gl_context *ctx,
2953 struct gl_texture_image *texImage,
2954 GLuint slice,
2955 GLenum destFormat, GLenum destType,
2956 GLvoid *dest)
2957 {
2958 struct decompress_state *decompress = &ctx->Meta->Decompress;
2959 struct gl_texture_object *texObj = texImage->TexObject;
2960 const GLint width = texImage->Width;
2961 const GLint height = texImage->Height;
2962 const GLint depth = texImage->Height;
2963 const GLenum target = texObj->Target;
2964 GLenum faceTarget;
2965 struct vertex verts[4];
2966 GLuint samplerSave;
2967 const bool use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
2968 ctx->Extensions.ARB_fragment_shader;
2969
2970 if (slice > 0) {
2971 assert(target == GL_TEXTURE_3D ||
2972 target == GL_TEXTURE_2D_ARRAY ||
2973 target == GL_TEXTURE_CUBE_MAP_ARRAY);
2974 }
2975
2976 switch (target) {
2977 case GL_TEXTURE_1D:
2978 case GL_TEXTURE_1D_ARRAY:
2979 assert(!"No compressed 1D textures.");
2980 return;
2981
2982 case GL_TEXTURE_3D:
2983 assert(!"No compressed 3D textures.");
2984 return;
2985
2986 case GL_TEXTURE_CUBE_MAP_ARRAY:
2987 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + (slice % 6);
2988 break;
2989
2990 case GL_TEXTURE_CUBE_MAP:
2991 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face;
2992 break;
2993
2994 default:
2995 faceTarget = target;
2996 break;
2997 }
2998
2999 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_PIXEL_STORE);
3000
3001 samplerSave = ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
3002 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
3003
3004 /* Create/bind FBO/renderbuffer */
3005 if (decompress->FBO == 0) {
3006 _mesa_GenFramebuffers(1, &decompress->FBO);
3007 _mesa_GenRenderbuffers(1, &decompress->RBO);
3008 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, decompress->FBO);
3009 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, decompress->RBO);
3010 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT,
3011 GL_COLOR_ATTACHMENT0_EXT,
3012 GL_RENDERBUFFER_EXT,
3013 decompress->RBO);
3014 }
3015 else {
3016 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, decompress->FBO);
3017 }
3018
3019 /* alloc dest surface */
3020 if (width > decompress->Width || height > decompress->Height) {
3021 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, decompress->RBO);
3022 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT, GL_RGBA,
3023 width, height);
3024 decompress->Width = width;
3025 decompress->Height = height;
3026 }
3027
3028 if (use_glsl_version) {
3029 _mesa_meta_setup_vertex_objects(&decompress->VAO, &decompress->VBO, true,
3030 2, 4, 0);
3031
3032 _mesa_meta_setup_blit_shader(ctx, target, &decompress->shaders);
3033 } else {
3034 _mesa_meta_setup_ff_tnl_for_blit(&decompress->VAO, &decompress->VBO, 3);
3035 }
3036
3037 if (!decompress->Sampler) {
3038 _mesa_GenSamplers(1, &decompress->Sampler);
3039 _mesa_BindSampler(ctx->Texture.CurrentUnit, decompress->Sampler);
3040 /* nearest filtering */
3041 _mesa_SamplerParameteri(decompress->Sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3042 _mesa_SamplerParameteri(decompress->Sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3043 /* No sRGB decode or encode.*/
3044 if (ctx->Extensions.EXT_texture_sRGB_decode) {
3045 _mesa_SamplerParameteri(decompress->Sampler, GL_TEXTURE_SRGB_DECODE_EXT,
3046 GL_SKIP_DECODE_EXT);
3047 }
3048
3049 } else {
3050 _mesa_BindSampler(ctx->Texture.CurrentUnit, decompress->Sampler);
3051 }
3052
3053 /* Silence valgrind warnings about reading uninitialized stack. */
3054 memset(verts, 0, sizeof(verts));
3055
3056 _mesa_meta_setup_texture_coords(faceTarget, slice, width, height, depth,
3057 verts[0].tex,
3058 verts[1].tex,
3059 verts[2].tex,
3060 verts[3].tex);
3061
3062 /* setup vertex positions */
3063 verts[0].x = -1.0F;
3064 verts[0].y = -1.0F;
3065 verts[1].x = 1.0F;
3066 verts[1].y = -1.0F;
3067 verts[2].x = 1.0F;
3068 verts[2].y = 1.0F;
3069 verts[3].x = -1.0F;
3070 verts[3].y = 1.0F;
3071
3072 _mesa_set_viewport(ctx, 0, 0, 0, width, height);
3073
3074 /* upload new vertex data */
3075 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
3076
3077 /* setup texture state */
3078 _mesa_BindTexture(target, texObj->Name);
3079
3080 if (!use_glsl_version)
3081 _mesa_set_enable(ctx, target, GL_TRUE);
3082
3083 {
3084 /* save texture object state */
3085 const GLint baseLevelSave = texObj->BaseLevel;
3086 const GLint maxLevelSave = texObj->MaxLevel;
3087
3088 /* restrict sampling to the texture level of interest */
3089 if (target != GL_TEXTURE_RECTANGLE_ARB) {
3090 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, texImage->Level);
3091 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, texImage->Level);
3092 }
3093
3094 /* render quad w/ texture into renderbuffer */
3095 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3096
3097 /* Restore texture object state, the texture binding will
3098 * be restored by _mesa_meta_end().
3099 */
3100 if (target != GL_TEXTURE_RECTANGLE_ARB) {
3101 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave);
3102 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
3103 }
3104
3105 }
3106
3107 /* read pixels from renderbuffer */
3108 {
3109 GLenum baseTexFormat = texImage->_BaseFormat;
3110 GLenum destBaseFormat = _mesa_base_tex_format(ctx, destFormat);
3111
3112 /* The pixel transfer state will be set to default values at this point
3113 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3114 * turned off (as required by glGetTexImage) but we need to handle some
3115 * special cases. In particular, single-channel texture values are
3116 * returned as red and two-channel texture values are returned as
3117 * red/alpha.
3118 */
3119 if ((baseTexFormat == GL_LUMINANCE ||
3120 baseTexFormat == GL_LUMINANCE_ALPHA ||
3121 baseTexFormat == GL_INTENSITY) ||
3122 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3123 * luminance then we need to return L=tex(R).
3124 */
3125 ((baseTexFormat == GL_RGBA ||
3126 baseTexFormat == GL_RGB ||
3127 baseTexFormat == GL_RG) &&
3128 (destBaseFormat == GL_LUMINANCE ||
3129 destBaseFormat == GL_LUMINANCE_ALPHA ||
3130 destBaseFormat == GL_LUMINANCE_INTEGER_EXT ||
3131 destBaseFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT))) {
3132 /* Green and blue must be zero */
3133 _mesa_PixelTransferf(GL_GREEN_SCALE, 0.0f);
3134 _mesa_PixelTransferf(GL_BLUE_SCALE, 0.0f);
3135 }
3136
3137 _mesa_ReadPixels(0, 0, width, height, destFormat, destType, dest);
3138 }
3139
3140 /* disable texture unit */
3141 if (!use_glsl_version)
3142 _mesa_set_enable(ctx, target, GL_FALSE);
3143
3144 _mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
3145
3146 _mesa_meta_end(ctx);
3147 }
3148
3149
3150 /**
3151 * This is just a wrapper around _mesa_get_tex_image() and
3152 * decompress_texture_image(). Meta functions should not be directly called
3153 * from core Mesa.
3154 */
3155 void
3156 _mesa_meta_GetTexImage(struct gl_context *ctx,
3157 GLenum format, GLenum type, GLvoid *pixels,
3158 struct gl_texture_image *texImage)
3159 {
3160 /* We can only use the decompress-with-blit method here if the texels are
3161 * unsigned, normalized values. We could handle signed and unnormalized
3162 * with floating point renderbuffers...
3163 */
3164 if (_mesa_is_format_compressed(texImage->TexFormat) &&
3165 _mesa_get_format_datatype(texImage->TexFormat)
3166 == GL_UNSIGNED_NORMALIZED) {
3167 struct gl_texture_object *texObj = texImage->TexObject;
3168 GLuint slice;
3169 /* Need to unlock the texture here to prevent deadlock... */
3170 _mesa_unlock_texture(ctx, texObj);
3171 for (slice = 0; slice < texImage->Depth; slice++) {
3172 void *dst;
3173 if (texImage->TexObject->Target == GL_TEXTURE_2D_ARRAY
3174 || texImage->TexObject->Target == GL_TEXTURE_CUBE_MAP_ARRAY) {
3175 /* Setup pixel packing. SkipPixels and SkipRows will be applied
3176 * in the decompress_texture_image() function's call to
3177 * glReadPixels but we need to compute the dest slice's address
3178 * here (according to SkipImages and ImageHeight).
3179 */
3180 struct gl_pixelstore_attrib packing = ctx->Pack;
3181 packing.SkipPixels = 0;
3182 packing.SkipRows = 0;
3183 dst = _mesa_image_address3d(&packing, pixels, texImage->Width,
3184 texImage->Height, format, type,
3185 slice, 0, 0);
3186 }
3187 else {
3188 dst = pixels;
3189 }
3190 decompress_texture_image(ctx, texImage, slice, format, type, dst);
3191 }
3192 /* ... and relock it */
3193 _mesa_lock_texture(ctx, texObj);
3194 }
3195 else {
3196 _mesa_get_teximage(ctx, format, type, pixels, texImage);
3197 }
3198 }
3199
3200
3201 /**
3202 * Meta implementation of ctx->Driver.DrawTex() in terms
3203 * of polygon rendering.
3204 */
3205 void
3206 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
3207 GLfloat width, GLfloat height)
3208 {
3209 struct drawtex_state *drawtex = &ctx->Meta->DrawTex;
3210 struct vertex {
3211 GLfloat x, y, z, st[MAX_TEXTURE_UNITS][2];
3212 };
3213 struct vertex verts[4];
3214 GLuint i;
3215
3216 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
3217 MESA_META_SHADER |
3218 MESA_META_TRANSFORM |
3219 MESA_META_VERTEX |
3220 MESA_META_VIEWPORT));
3221
3222 if (drawtex->VAO == 0) {
3223 /* one-time setup */
3224 GLint active_texture;
3225
3226 /* create vertex array object */
3227 _mesa_GenVertexArrays(1, &drawtex->VAO);
3228 _mesa_BindVertexArray(drawtex->VAO);
3229
3230 /* create vertex array buffer */
3231 _mesa_GenBuffers(1, &drawtex->VBO);
3232 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, drawtex->VBO);
3233 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
3234 NULL, GL_DYNAMIC_DRAW_ARB);
3235
3236 /* client active texture is not part of the array object */
3237 active_texture = ctx->Array.ActiveTexture;
3238
3239 /* setup vertex arrays */
3240 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
3241 _mesa_EnableClientState(GL_VERTEX_ARRAY);
3242 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3243 _mesa_ClientActiveTexture(GL_TEXTURE0 + i);
3244 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(st[i]));
3245 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
3246 }
3247
3248 /* restore client active texture */
3249 _mesa_ClientActiveTexture(GL_TEXTURE0 + active_texture);
3250 }
3251 else {
3252 _mesa_BindVertexArray(drawtex->VAO);
3253 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, drawtex->VBO);
3254 }
3255
3256 /* vertex positions, texcoords */
3257 {
3258 const GLfloat x1 = x + width;
3259 const GLfloat y1 = y + height;
3260
3261 z = CLAMP(z, 0.0f, 1.0f);
3262 z = invert_z(z);
3263
3264 verts[0].x = x;
3265 verts[0].y = y;
3266 verts[0].z = z;
3267
3268 verts[1].x = x1;
3269 verts[1].y = y;
3270 verts[1].z = z;
3271
3272 verts[2].x = x1;
3273 verts[2].y = y1;
3274 verts[2].z = z;
3275
3276 verts[3].x = x;
3277 verts[3].y = y1;
3278 verts[3].z = z;
3279
3280 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3281 const struct gl_texture_object *texObj;
3282 const struct gl_texture_image *texImage;
3283 GLfloat s, t, s1, t1;
3284 GLuint tw, th;
3285
3286 if (!ctx->Texture.Unit[i]._Current) {
3287 GLuint j;
3288 for (j = 0; j < 4; j++) {
3289 verts[j].st[i][0] = 0.0f;
3290 verts[j].st[i][1] = 0.0f;
3291 }
3292 continue;
3293 }
3294
3295 texObj = ctx->Texture.Unit[i]._Current;
3296 texImage = texObj->Image[0][texObj->BaseLevel];
3297 tw = texImage->Width2;
3298 th = texImage->Height2;
3299
3300 s = (GLfloat) texObj->CropRect[0] / tw;
3301 t = (GLfloat) texObj->CropRect[1] / th;
3302 s1 = (GLfloat) (texObj->CropRect[0] + texObj->CropRect[2]) / tw;
3303 t1 = (GLfloat) (texObj->CropRect[1] + texObj->CropRect[3]) / th;
3304
3305 verts[0].st[i][0] = s;
3306 verts[0].st[i][1] = t;
3307
3308 verts[1].st[i][0] = s1;
3309 verts[1].st[i][1] = t;
3310
3311 verts[2].st[i][0] = s1;
3312 verts[2].st[i][1] = t1;
3313
3314 verts[3].st[i][0] = s;
3315 verts[3].st[i][1] = t1;
3316 }
3317
3318 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
3319 }
3320
3321 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3322
3323 _mesa_meta_end(ctx);
3324 }