st/mesa: Fix GL_MAP_COLOR with glDrawPixels GL_COLOR_INDEX
[mesa.git] / src / mesa / state_tracker / st_cb_drawpixels.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Brian Paul
31 */
32
33 #include "main/errors.h"
34 #include "main/imports.h"
35 #include "main/image.h"
36 #include "main/bufferobj.h"
37 #include "main/blit.h"
38 #include "main/format_pack.h"
39 #include "main/framebuffer.h"
40 #include "main/macros.h"
41 #include "main/mtypes.h"
42 #include "main/pack.h"
43 #include "main/pbo.h"
44 #include "main/readpix.h"
45 #include "main/state.h"
46 #include "main/texformat.h"
47 #include "main/teximage.h"
48 #include "main/texstore.h"
49 #include "main/glformats.h"
50 #include "program/program.h"
51 #include "program/prog_print.h"
52 #include "program/prog_instruction.h"
53
54 #include "st_atom.h"
55 #include "st_atom_constbuf.h"
56 #include "st_cb_bitmap.h"
57 #include "st_cb_drawpixels.h"
58 #include "st_cb_readpixels.h"
59 #include "st_cb_fbo.h"
60 #include "st_context.h"
61 #include "st_debug.h"
62 #include "st_draw.h"
63 #include "st_format.h"
64 #include "st_program.h"
65 #include "st_sampler_view.h"
66 #include "st_scissor.h"
67 #include "st_texture.h"
68 #include "st_util.h"
69 #include "st_nir.h"
70
71 #include "pipe/p_context.h"
72 #include "pipe/p_defines.h"
73 #include "tgsi/tgsi_ureg.h"
74 #include "util/u_format.h"
75 #include "util/u_inlines.h"
76 #include "util/u_math.h"
77 #include "util/u_simple_shaders.h"
78 #include "util/u_tile.h"
79 #include "cso_cache/cso_context.h"
80
81 #include "compiler/nir/nir_builder.h"
82
83 /**
84 * We have a simple glDrawPixels cache to try to optimize the case where the
85 * same image is drawn over and over again. It basically works as follows:
86 *
87 * 1. After we construct a texture map with the image and draw it, we do
88 * not discard the texture. We keep it around, plus we note the
89 * glDrawPixels width, height, format, etc. parameters and keep a copy
90 * of the image in a malloc'd buffer.
91 *
92 * 2. On the next glDrawPixels we check if the parameters match the previous
93 * call. If those match, we check if the image matches the previous image
94 * via a memcmp() call. If everything matches, we re-use the previous
95 * texture, thereby avoiding the cost creating a new texture and copying
96 * the image to it.
97 *
98 * The effectiveness of this cache depends upon:
99 * 1. If the memcmp() finds a difference, it happens relatively quickly.
100 Hopefully, not just the last pixels differ!
101 * 2. If the memcmp() finds no difference, doing that check is faster than
102 * creating and loading a texture.
103 *
104 * Notes:
105 * 1. We don't support any pixel unpacking parameters.
106 * 2. We don't try to cache images in Pixel Buffer Objects.
107 * 3. Instead of saving the whole image, perhaps some sort of reliable
108 * checksum function could be used instead.
109 */
110 #define USE_DRAWPIXELS_CACHE 1
111
112 static nir_ssa_def *
113 sample_via_nir(nir_builder *b, nir_variable *texcoord,
114 const char *name, int sampler)
115 {
116 const struct glsl_type *sampler2D =
117 glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
118
119 nir_variable *var =
120 nir_variable_create(b->shader, nir_var_uniform, sampler2D, name);
121 var->data.binding = sampler;
122 var->data.explicit_binding = true;
123
124 nir_deref_instr *deref = nir_build_deref_var(b, var);
125
126 nir_tex_instr *tex = nir_tex_instr_create(b->shader, 3);
127 tex->op = nir_texop_tex;
128 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
129 tex->coord_components = 2;
130 tex->dest_type = nir_type_float;
131 tex->src[0].src_type = nir_tex_src_texture_deref;
132 tex->src[0].src = nir_src_for_ssa(&deref->dest.ssa);
133 tex->src[1].src_type = nir_tex_src_sampler_deref;
134 tex->src[1].src = nir_src_for_ssa(&deref->dest.ssa);
135 tex->src[2].src_type = nir_tex_src_coord;
136 tex->src[2].src =
137 nir_src_for_ssa(nir_channels(b, nir_load_var(b, texcoord),
138 (1 << tex->coord_components) - 1));
139
140 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
141 nir_builder_instr_insert(b, &tex->instr);
142 return nir_channel(b, &tex->dest.ssa, 0);
143 }
144
145 static void *
146 make_drawpix_z_stencil_program_nir(struct st_context *st,
147 bool write_depth,
148 bool write_stencil)
149 {
150 struct nir_builder b;
151 const nir_shader_compiler_options *options =
152 st->ctx->Const.ShaderCompilerOptions[MESA_SHADER_FRAGMENT].NirOptions;
153
154 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, options);
155
156 nir_variable *texcoord =
157 nir_variable_create(b.shader, nir_var_shader_in, glsl_vec_type(2),
158 "texcoord");
159 texcoord->data.location = VARYING_SLOT_TEX0;
160
161 if (write_depth) {
162 nir_variable *out =
163 nir_variable_create(b.shader, nir_var_shader_out, glsl_float_type(),
164 "gl_FragDepth");
165 out->data.location = FRAG_RESULT_DEPTH;
166 nir_ssa_def *depth = sample_via_nir(&b, texcoord, "depth", 0);
167 nir_store_var(&b, out, depth, 0x1);
168
169 /* Also copy color */
170 nir_variable *color_in =
171 nir_variable_create(b.shader, nir_var_shader_in, glsl_vec_type(4),
172 "v_color");
173 color_in->data.location = VARYING_SLOT_COL0;
174
175 nir_variable *color_out =
176 nir_variable_create(b.shader, nir_var_shader_out, glsl_vec_type(4),
177 "gl_FragColor");
178 color_out->data.location = FRAG_RESULT_COLOR;
179 nir_copy_var(&b, color_out, color_in);
180 }
181
182 if (write_stencil) {
183 nir_variable *out =
184 nir_variable_create(b.shader, nir_var_shader_out, glsl_uint_type(),
185 "gl_FragStencilRefARB");
186 out->data.location = FRAG_RESULT_STENCIL;
187 nir_ssa_def *stencil = sample_via_nir(&b, texcoord, "stencil", 1);
188 nir_store_var(&b, out, stencil, 0x1);
189 }
190
191 char name[14];
192 snprintf(name, 14, "drawpixels %s%s",
193 write_depth ? "Z" : "", write_stencil ? "S" : "");
194
195 return st_nir_finish_builtin_shader(st, b.shader, name);
196 }
197
198
199 static void *
200 make_drawpix_z_stencil_program_tgsi(struct st_context *st,
201 bool write_depth,
202 bool write_stencil)
203 {
204 struct ureg_program *ureg;
205 struct ureg_src depth_sampler, stencil_sampler;
206 struct ureg_src texcoord, color;
207 struct ureg_dst out_color, out_depth, out_stencil;
208
209 ureg = ureg_create(PIPE_SHADER_FRAGMENT);
210 if (ureg == NULL)
211 return NULL;
212
213 ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, TRUE);
214
215 if (write_depth) {
216 color = ureg_DECL_fs_input(ureg, TGSI_SEMANTIC_COLOR, 0,
217 TGSI_INTERPOLATE_COLOR);
218 out_color = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
219
220 depth_sampler = ureg_DECL_sampler(ureg, 0);
221 ureg_DECL_sampler_view(ureg, 0, TGSI_TEXTURE_2D,
222 TGSI_RETURN_TYPE_FLOAT,
223 TGSI_RETURN_TYPE_FLOAT,
224 TGSI_RETURN_TYPE_FLOAT,
225 TGSI_RETURN_TYPE_FLOAT);
226 out_depth = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
227 }
228
229 if (write_stencil) {
230 stencil_sampler = ureg_DECL_sampler(ureg, 1);
231 ureg_DECL_sampler_view(ureg, 1, TGSI_TEXTURE_2D,
232 TGSI_RETURN_TYPE_UINT,
233 TGSI_RETURN_TYPE_UINT,
234 TGSI_RETURN_TYPE_UINT,
235 TGSI_RETURN_TYPE_UINT);
236 out_stencil = ureg_DECL_output(ureg, TGSI_SEMANTIC_STENCIL, 0);
237 }
238
239 texcoord = ureg_DECL_fs_input(ureg,
240 st->needs_texcoord_semantic ?
241 TGSI_SEMANTIC_TEXCOORD :
242 TGSI_SEMANTIC_GENERIC,
243 0, TGSI_INTERPOLATE_LINEAR);
244
245 if (write_depth) {
246 ureg_TEX(ureg, ureg_writemask(out_depth, TGSI_WRITEMASK_Z),
247 TGSI_TEXTURE_2D, texcoord, depth_sampler);
248 ureg_MOV(ureg, out_color, color);
249 }
250
251 if (write_stencil)
252 ureg_TEX(ureg, ureg_writemask(out_stencil, TGSI_WRITEMASK_Y),
253 TGSI_TEXTURE_2D, texcoord, stencil_sampler);
254
255 ureg_END(ureg);
256 return ureg_create_shader_and_destroy(ureg, st->pipe);
257 }
258
259
260 /**
261 * Create fragment program that does a TEX() instruction to get a Z and/or
262 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
263 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
264 * Pass fragment color through as-is.
265 *
266 * \return CSO of the fragment shader.
267 */
268 static void *
269 get_drawpix_z_stencil_program(struct st_context *st,
270 bool write_depth,
271 bool write_stencil)
272 {
273 struct pipe_screen *pscreen = st->pipe->screen;
274 const GLuint shaderIndex = write_depth * 2 + write_stencil;
275 void *cso;
276
277 assert(shaderIndex < ARRAY_SIZE(st->drawpix.zs_shaders));
278
279 if (st->drawpix.zs_shaders[shaderIndex]) {
280 /* already have the proper shader */
281 return st->drawpix.zs_shaders[shaderIndex];
282 }
283
284 enum pipe_shader_ir preferred_ir =
285 pscreen->get_shader_param(pscreen, PIPE_SHADER_FRAGMENT,
286 PIPE_SHADER_CAP_PREFERRED_IR);
287
288 if (preferred_ir == PIPE_SHADER_IR_NIR)
289 cso = make_drawpix_z_stencil_program_nir(st, write_depth, write_stencil);
290 else
291 cso = make_drawpix_z_stencil_program_tgsi(st, write_depth, write_stencil);
292
293 /* save the new shader */
294 st->drawpix.zs_shaders[shaderIndex] = cso;
295 return cso;
296 }
297
298
299 /**
300 * Create a simple vertex shader that just passes through the
301 * vertex position, texcoord, and color.
302 */
303 void
304 st_make_passthrough_vertex_shader(struct st_context *st)
305 {
306 struct pipe_context *pipe = st->pipe;
307 struct pipe_screen *screen = pipe->screen;
308
309 if (st->passthrough_vs)
310 return;
311
312 enum pipe_shader_ir preferred_ir =
313 screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
314 PIPE_SHADER_CAP_PREFERRED_IR);
315
316 if (preferred_ir == PIPE_SHADER_IR_NIR) {
317 unsigned inputs[] =
318 { VERT_ATTRIB_POS, VERT_ATTRIB_COLOR0, VERT_ATTRIB_GENERIC0 };
319 unsigned outputs[] =
320 { VARYING_SLOT_POS, VARYING_SLOT_COL0, VARYING_SLOT_TEX0 };
321
322 st->passthrough_vs =
323 st_nir_make_passthrough_shader(st, "drawpixels VS",
324 MESA_SHADER_VERTEX, 3,
325 inputs, outputs, NULL, 0);
326 } else {
327 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
328 TGSI_SEMANTIC_COLOR,
329 st->needs_texcoord_semantic ? TGSI_SEMANTIC_TEXCOORD :
330 TGSI_SEMANTIC_GENERIC };
331 const uint semantic_indexes[] = { 0, 0, 0 };
332
333 st->passthrough_vs =
334 util_make_vertex_passthrough_shader(st->pipe, 3, semantic_names,
335 semantic_indexes, false);
336 }
337 }
338
339
340 /**
341 * Return a texture internalFormat for drawing/copying an image
342 * of the given format and type.
343 */
344 static GLenum
345 internal_format(struct gl_context *ctx, GLenum format, GLenum type)
346 {
347 switch (format) {
348 case GL_DEPTH_COMPONENT:
349 switch (type) {
350 case GL_UNSIGNED_SHORT:
351 return GL_DEPTH_COMPONENT16;
352
353 case GL_UNSIGNED_INT:
354 return GL_DEPTH_COMPONENT32;
355
356 case GL_FLOAT:
357 if (ctx->Extensions.ARB_depth_buffer_float)
358 return GL_DEPTH_COMPONENT32F;
359 else
360 return GL_DEPTH_COMPONENT;
361
362 default:
363 return GL_DEPTH_COMPONENT;
364 }
365
366 case GL_DEPTH_STENCIL:
367 switch (type) {
368 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
369 return GL_DEPTH32F_STENCIL8;
370
371 case GL_UNSIGNED_INT_24_8:
372 default:
373 return GL_DEPTH24_STENCIL8;
374 }
375
376 case GL_STENCIL_INDEX:
377 return GL_STENCIL_INDEX;
378
379 default:
380 if (_mesa_is_enum_format_integer(format)) {
381 switch (type) {
382 case GL_BYTE:
383 return GL_RGBA8I;
384 case GL_UNSIGNED_BYTE:
385 return GL_RGBA8UI;
386 case GL_SHORT:
387 return GL_RGBA16I;
388 case GL_UNSIGNED_SHORT:
389 return GL_RGBA16UI;
390 case GL_INT:
391 return GL_RGBA32I;
392 case GL_UNSIGNED_INT:
393 return GL_RGBA32UI;
394 default:
395 assert(0 && "Unexpected type in internal_format()");
396 return GL_RGBA_INTEGER;
397 }
398 }
399 else {
400 switch (type) {
401 case GL_UNSIGNED_BYTE:
402 case GL_UNSIGNED_INT_8_8_8_8:
403 case GL_UNSIGNED_INT_8_8_8_8_REV:
404 default:
405 return GL_RGBA8;
406
407 case GL_UNSIGNED_BYTE_3_3_2:
408 case GL_UNSIGNED_BYTE_2_3_3_REV:
409 return GL_R3_G3_B2;
410
411 case GL_UNSIGNED_SHORT_4_4_4_4:
412 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
413 return GL_RGBA4;
414
415 case GL_UNSIGNED_SHORT_5_6_5:
416 case GL_UNSIGNED_SHORT_5_6_5_REV:
417 return GL_RGB565;
418
419 case GL_UNSIGNED_SHORT_5_5_5_1:
420 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
421 return GL_RGB5_A1;
422
423 case GL_UNSIGNED_INT_10_10_10_2:
424 case GL_UNSIGNED_INT_2_10_10_10_REV:
425 return GL_RGB10_A2;
426
427 case GL_UNSIGNED_SHORT:
428 case GL_UNSIGNED_INT:
429 return GL_RGBA16;
430
431 case GL_BYTE:
432 return
433 ctx->Extensions.EXT_texture_snorm ? GL_RGBA8_SNORM : GL_RGBA8;
434
435 case GL_SHORT:
436 case GL_INT:
437 return
438 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
439
440 case GL_HALF_FLOAT_ARB:
441 return
442 ctx->Extensions.ARB_texture_float ? GL_RGBA16F :
443 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
444
445 case GL_FLOAT:
446 case GL_DOUBLE:
447 return
448 ctx->Extensions.ARB_texture_float ? GL_RGBA32F :
449 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
450
451 case GL_UNSIGNED_INT_5_9_9_9_REV:
452 assert(ctx->Extensions.EXT_texture_shared_exponent);
453 return GL_RGB9_E5;
454
455 case GL_UNSIGNED_INT_10F_11F_11F_REV:
456 assert(ctx->Extensions.EXT_packed_float);
457 return GL_R11F_G11F_B10F;
458 }
459 }
460 }
461 }
462
463
464 /**
465 * Create a temporary texture to hold an image of the given size.
466 * If width, height are not POT and the driver only handles POT textures,
467 * allocate the next larger size of texture that is POT.
468 */
469 static struct pipe_resource *
470 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
471 enum pipe_format texFormat, unsigned bind)
472 {
473 struct pipe_resource *pt;
474
475 pt = st_texture_create(st, st->internal_target, texFormat, 0,
476 width, height, 1, 1, 0, bind);
477
478 return pt;
479 }
480
481
482 /**
483 * Search the cache for an image which matches the given parameters.
484 * \return pipe_resource pointer if found, NULL if not found.
485 */
486 static struct pipe_resource *
487 search_drawpixels_cache(struct st_context *st,
488 GLsizei width, GLsizei height,
489 GLenum format, GLenum type,
490 const struct gl_pixelstore_attrib *unpack,
491 const void *pixels)
492 {
493 struct pipe_resource *pt = NULL;
494 const GLint bpp = _mesa_bytes_per_pixel(format, type);
495 unsigned i;
496
497 if ((unpack->RowLength != 0 && unpack->RowLength != width) ||
498 unpack->SkipPixels != 0 ||
499 unpack->SkipRows != 0 ||
500 unpack->SwapBytes ||
501 _mesa_is_bufferobj(unpack->BufferObj)) {
502 /* we don't allow non-default pixel unpacking values */
503 return NULL;
504 }
505
506 /* Search cache entries for a match */
507 for (i = 0; i < ARRAY_SIZE(st->drawpix_cache.entries); i++) {
508 struct drawpix_cache_entry *entry = &st->drawpix_cache.entries[i];
509
510 if (width == entry->width &&
511 height == entry->height &&
512 format == entry->format &&
513 type == entry->type &&
514 pixels == entry->user_pointer &&
515 entry->image) {
516 assert(entry->texture);
517
518 /* check if the pixel data is the same */
519 if (memcmp(pixels, entry->image, width * height * bpp) == 0) {
520 /* Success - found a cache match */
521 pipe_resource_reference(&pt, entry->texture);
522 /* refcount of returned texture should be at least two here. One
523 * reference for the cache to hold on to, one for the caller (which
524 * it will release), and possibly more held by the driver.
525 */
526 assert(pt->reference.count >= 2);
527
528 /* update the age of this entry */
529 entry->age = ++st->drawpix_cache.age;
530
531 return pt;
532 }
533 }
534 }
535
536 /* no cache match found */
537 return NULL;
538 }
539
540
541 /**
542 * Find the oldest entry in the glDrawPixels cache. We'll replace this
543 * one when we need to store a new image.
544 */
545 static struct drawpix_cache_entry *
546 find_oldest_drawpixels_cache_entry(struct st_context *st)
547 {
548 unsigned oldest_age = ~0u, oldest_index = ~0u;
549 unsigned i;
550
551 /* Find entry with oldest (lowest) age */
552 for (i = 0; i < ARRAY_SIZE(st->drawpix_cache.entries); i++) {
553 const struct drawpix_cache_entry *entry = &st->drawpix_cache.entries[i];
554 if (entry->age < oldest_age) {
555 oldest_age = entry->age;
556 oldest_index = i;
557 }
558 }
559
560 assert(oldest_index != ~0u);
561
562 return &st->drawpix_cache.entries[oldest_index];
563 }
564
565
566 /**
567 * Try to save the given glDrawPixels image in the cache.
568 */
569 static void
570 cache_drawpixels_image(struct st_context *st,
571 GLsizei width, GLsizei height,
572 GLenum format, GLenum type,
573 const struct gl_pixelstore_attrib *unpack,
574 const void *pixels,
575 struct pipe_resource *pt)
576 {
577 if ((unpack->RowLength == 0 || unpack->RowLength == width) &&
578 unpack->SkipPixels == 0 &&
579 unpack->SkipRows == 0) {
580 const GLint bpp = _mesa_bytes_per_pixel(format, type);
581 struct drawpix_cache_entry *entry =
582 find_oldest_drawpixels_cache_entry(st);
583 assert(entry);
584 entry->width = width;
585 entry->height = height;
586 entry->format = format;
587 entry->type = type;
588 entry->user_pointer = pixels;
589 free(entry->image);
590 entry->image = malloc(width * height * bpp);
591 if (entry->image) {
592 memcpy(entry->image, pixels, width * height * bpp);
593 pipe_resource_reference(&entry->texture, pt);
594 entry->age = ++st->drawpix_cache.age;
595 }
596 else {
597 /* out of memory, free/disable cached texture */
598 entry->width = 0;
599 entry->height = 0;
600 pipe_resource_reference(&entry->texture, NULL);
601 }
602 }
603 }
604
605
606 /**
607 * Make texture containing an image for glDrawPixels image.
608 * If 'pixels' is NULL, leave the texture image data undefined.
609 */
610 static struct pipe_resource *
611 make_texture(struct st_context *st,
612 GLsizei width, GLsizei height, GLenum format, GLenum type,
613 const struct gl_pixelstore_attrib *unpack,
614 const void *pixels)
615 {
616 struct gl_context *ctx = st->ctx;
617 struct pipe_context *pipe = st->pipe;
618 mesa_format mformat;
619 struct pipe_resource *pt = NULL;
620 enum pipe_format pipeFormat;
621 GLenum baseInternalFormat;
622
623 #if USE_DRAWPIXELS_CACHE
624 pt = search_drawpixels_cache(st, width, height, format, type,
625 unpack, pixels);
626 if (pt) {
627 return pt;
628 }
629 #endif
630
631 /* Choose a pixel format for the temp texture which will hold the
632 * image to draw.
633 */
634 pipeFormat = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
635 format, type, unpack->SwapBytes);
636
637 if (pipeFormat == PIPE_FORMAT_NONE) {
638 /* Use the generic approach. */
639 GLenum intFormat = internal_format(ctx, format, type);
640
641 pipeFormat = st_choose_format(st, intFormat, format, type,
642 st->internal_target, 0, 0,
643 PIPE_BIND_SAMPLER_VIEW, FALSE);
644 assert(pipeFormat != PIPE_FORMAT_NONE);
645 }
646
647 mformat = st_pipe_format_to_mesa_format(pipeFormat);
648 baseInternalFormat = _mesa_get_format_base_format(mformat);
649
650 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
651 if (!pixels)
652 return NULL;
653
654 /* alloc temporary texture */
655 pt = alloc_texture(st, width, height, pipeFormat, PIPE_BIND_SAMPLER_VIEW);
656 if (!pt) {
657 _mesa_unmap_pbo_source(ctx, unpack);
658 return NULL;
659 }
660
661 {
662 struct pipe_transfer *transfer;
663 GLubyte *dest;
664 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
665
666 /* we'll do pixel transfer in a fragment shader */
667 ctx->_ImageTransferState = 0x0;
668
669 /* map texture transfer */
670 dest = pipe_transfer_map(pipe, pt, 0, 0,
671 PIPE_TRANSFER_WRITE, 0, 0,
672 width, height, &transfer);
673 if (!dest) {
674 pipe_resource_reference(&pt, NULL);
675 _mesa_unmap_pbo_source(ctx, unpack);
676 return NULL;
677 }
678
679 /* Put image into texture transfer.
680 * Note that the image is actually going to be upside down in
681 * the texture. We deal with that with texcoords.
682 */
683 if ((format == GL_RGBA || format == GL_BGRA)
684 && type == GL_UNSIGNED_BYTE) {
685 /* Use a memcpy-based texstore to avoid software pixel swizzling.
686 * We'll do the necessary swizzling with the pipe_sampler_view to
687 * give much better performance.
688 * XXX in the future, expand this to accomodate more format and
689 * type combinations.
690 */
691 _mesa_memcpy_texture(ctx, 2,
692 mformat, /* mesa_format */
693 transfer->stride, /* dstRowStride, bytes */
694 &dest, /* destSlices */
695 width, height, 1, /* size */
696 format, type, /* src format/type */
697 pixels, /* data source */
698 unpack);
699 }
700 else {
701 bool MAYBE_UNUSED success;
702 success = _mesa_texstore(ctx, 2, /* dims */
703 baseInternalFormat, /* baseInternalFormat */
704 mformat, /* mesa_format */
705 transfer->stride, /* dstRowStride, bytes */
706 &dest, /* destSlices */
707 width, height, 1, /* size */
708 format, type, /* src format/type */
709 pixels, /* data source */
710 unpack);
711
712 assert(success);
713 }
714
715 /* unmap */
716 pipe_transfer_unmap(pipe, transfer);
717
718 /* restore */
719 ctx->_ImageTransferState = imageTransferStateSave;
720 }
721
722 _mesa_unmap_pbo_source(ctx, unpack);
723
724 #if USE_DRAWPIXELS_CACHE
725 cache_drawpixels_image(st, width, height, format, type, unpack, pixels, pt);
726 #endif
727
728 return pt;
729 }
730
731
732 static void
733 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
734 GLsizei width, GLsizei height,
735 GLfloat zoomX, GLfloat zoomY,
736 struct pipe_sampler_view **sv,
737 int num_sampler_view,
738 void *driver_vp,
739 void *driver_fp,
740 struct st_fp_variant *fpv,
741 const GLfloat *color,
742 GLboolean invertTex,
743 GLboolean write_depth, GLboolean write_stencil)
744 {
745 struct st_context *st = st_context(ctx);
746 struct pipe_context *pipe = st->pipe;
747 struct cso_context *cso = st->cso_context;
748 const unsigned fb_width = _mesa_geometric_width(ctx->DrawBuffer);
749 const unsigned fb_height = _mesa_geometric_height(ctx->DrawBuffer);
750 GLfloat x0, y0, x1, y1;
751 GLsizei MAYBE_UNUSED maxSize;
752 boolean normalized = sv[0]->texture->target == PIPE_TEXTURE_2D;
753 unsigned cso_state_mask;
754
755 assert(sv[0]->texture->target == st->internal_target);
756
757 /* limit checks */
758 /* XXX if DrawPixels image is larger than max texture size, break
759 * it up into chunks.
760 */
761 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
762 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
763 assert(width <= maxSize);
764 assert(height <= maxSize);
765
766 cso_state_mask = (CSO_BIT_RASTERIZER |
767 CSO_BIT_VIEWPORT |
768 CSO_BIT_FRAGMENT_SAMPLERS |
769 CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
770 CSO_BIT_STREAM_OUTPUTS |
771 CSO_BIT_VERTEX_ELEMENTS |
772 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
773 CSO_BITS_ALL_SHADERS);
774 if (write_stencil) {
775 cso_state_mask |= (CSO_BIT_DEPTH_STENCIL_ALPHA |
776 CSO_BIT_BLEND);
777 }
778 cso_save_state(cso, cso_state_mask);
779
780 /* rasterizer state: just scissor */
781 {
782 struct pipe_rasterizer_state rasterizer;
783 memset(&rasterizer, 0, sizeof(rasterizer));
784 rasterizer.clamp_fragment_color = !st->clamp_frag_color_in_shader &&
785 ctx->Color._ClampFragmentColor;
786 rasterizer.half_pixel_center = 1;
787 rasterizer.bottom_edge_rule = 1;
788 rasterizer.depth_clip_near = !ctx->Transform.DepthClampNear;
789 rasterizer.depth_clip_far = !ctx->Transform.DepthClampFar;
790 rasterizer.scissor = ctx->Scissor.EnableFlags;
791 cso_set_rasterizer(cso, &rasterizer);
792 }
793
794 if (write_stencil) {
795 /* Stencil writing bypasses the normal fragment pipeline to
796 * disable color writing and set stencil test to always pass.
797 */
798 struct pipe_depth_stencil_alpha_state dsa;
799 struct pipe_blend_state blend;
800
801 /* depth/stencil */
802 memset(&dsa, 0, sizeof(dsa));
803 dsa.stencil[0].enabled = 1;
804 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
805 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
806 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
807 if (write_depth) {
808 /* writing depth+stencil: depth test always passes */
809 dsa.depth.enabled = 1;
810 dsa.depth.writemask = ctx->Depth.Mask;
811 dsa.depth.func = PIPE_FUNC_ALWAYS;
812 }
813 cso_set_depth_stencil_alpha(cso, &dsa);
814
815 /* blend (colormask) */
816 memset(&blend, 0, sizeof(blend));
817 cso_set_blend(cso, &blend);
818 }
819
820 /* fragment shader state: TEX lookup program */
821 cso_set_fragment_shader_handle(cso, driver_fp);
822
823 /* vertex shader state: position + texcoord pass-through */
824 cso_set_vertex_shader_handle(cso, driver_vp);
825
826 /* disable other shaders */
827 cso_set_tessctrl_shader_handle(cso, NULL);
828 cso_set_tesseval_shader_handle(cso, NULL);
829 cso_set_geometry_shader_handle(cso, NULL);
830
831 /* user samplers, plus the drawpix samplers */
832 {
833 struct pipe_sampler_state sampler;
834
835 memset(&sampler, 0, sizeof(sampler));
836 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
837 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
838 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
839 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
840 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
841 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
842 sampler.normalized_coords = normalized;
843
844 if (fpv) {
845 /* drawing a color image */
846 const struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
847 uint num = MAX3(fpv->drawpix_sampler + 1,
848 fpv->pixelmap_sampler + 1,
849 st->state.num_frag_samplers);
850 uint i;
851
852 for (i = 0; i < st->state.num_frag_samplers; i++)
853 samplers[i] = &st->state.frag_samplers[i];
854
855 samplers[fpv->drawpix_sampler] = &sampler;
856 if (sv[1])
857 samplers[fpv->pixelmap_sampler] = &sampler;
858
859 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num, samplers);
860 } else {
861 /* drawing a depth/stencil image */
862 const struct pipe_sampler_state *samplers[2] = {&sampler, &sampler};
863
864 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num_sampler_view, samplers);
865 }
866 }
867
868 /* user textures, plus the drawpix textures */
869 if (fpv) {
870 /* drawing a color image */
871 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
872 uint num = MAX3(fpv->drawpix_sampler + 1,
873 fpv->pixelmap_sampler + 1,
874 st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
875
876 memcpy(sampler_views, st->state.frag_sampler_views,
877 sizeof(sampler_views));
878
879 sampler_views[fpv->drawpix_sampler] = sv[0];
880 if (sv[1])
881 sampler_views[fpv->pixelmap_sampler] = sv[1];
882 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num, sampler_views);
883 } else {
884 /* drawing a depth/stencil image */
885 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num_sampler_view, sv);
886 }
887
888 /* viewport state: viewport matching window dims */
889 cso_set_viewport_dims(cso, fb_width, fb_height, TRUE);
890
891 cso_set_vertex_elements(cso, 3, st->util_velems);
892 cso_set_stream_outputs(cso, 0, NULL, NULL);
893
894 /* Compute Gallium window coords (y=0=top) with pixel zoom.
895 * Recall that these coords are transformed by the current
896 * vertex shader and viewport transformation.
897 */
898 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
899 y = fb_height - (int) (y + height * ctx->Pixel.ZoomY);
900 invertTex = !invertTex;
901 }
902
903 x0 = (GLfloat) x;
904 x1 = x + width * ctx->Pixel.ZoomX;
905 y0 = (GLfloat) y;
906 y1 = y + height * ctx->Pixel.ZoomY;
907
908 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
909 z = z * 2.0f - 1.0f;
910
911 {
912 const float clip_x0 = x0 / (float) fb_width * 2.0f - 1.0f;
913 const float clip_y0 = y0 / (float) fb_height * 2.0f - 1.0f;
914 const float clip_x1 = x1 / (float) fb_width * 2.0f - 1.0f;
915 const float clip_y1 = y1 / (float) fb_height * 2.0f - 1.0f;
916 const float maxXcoord = normalized ?
917 ((float) width / sv[0]->texture->width0) : (float) width;
918 const float maxYcoord = normalized
919 ? ((float) height / sv[0]->texture->height0) : (float) height;
920 const float sLeft = 0.0f, sRight = maxXcoord;
921 const float tTop = invertTex ? maxYcoord : 0.0f;
922 const float tBot = invertTex ? 0.0f : maxYcoord;
923
924 if (!st_draw_quad(st, clip_x0, clip_y0, clip_x1, clip_y1, z,
925 sLeft, tBot, sRight, tTop, color, 0)) {
926 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
927 }
928 }
929
930 /* restore state */
931 cso_restore_state(cso);
932 }
933
934
935 /**
936 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
937 * can't use a fragment shader to write stencil values.
938 */
939 static void
940 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
941 GLsizei width, GLsizei height, GLenum format, GLenum type,
942 const struct gl_pixelstore_attrib *unpack,
943 const void *pixels)
944 {
945 struct st_context *st = st_context(ctx);
946 struct pipe_context *pipe = st->pipe;
947 struct st_renderbuffer *strb;
948 enum pipe_transfer_usage usage;
949 struct pipe_transfer *pt;
950 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
951 ubyte *stmap;
952 struct gl_pixelstore_attrib clippedUnpack = *unpack;
953 GLubyte *sValues;
954 GLuint *zValues;
955
956 if (!zoom) {
957 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
958 &clippedUnpack)) {
959 /* totally clipped */
960 return;
961 }
962 }
963
964 strb = st_renderbuffer(ctx->DrawBuffer->
965 Attachment[BUFFER_STENCIL].Renderbuffer);
966
967 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
968 y = ctx->DrawBuffer->Height - y - height;
969 }
970
971 if (format == GL_STENCIL_INDEX &&
972 _mesa_is_format_packed_depth_stencil(strb->Base.Format)) {
973 /* writing stencil to a combined depth+stencil buffer */
974 usage = PIPE_TRANSFER_READ_WRITE;
975 }
976 else {
977 usage = PIPE_TRANSFER_WRITE;
978 }
979
980 stmap = pipe_transfer_map(pipe, strb->texture,
981 strb->surface->u.tex.level,
982 strb->surface->u.tex.first_layer,
983 usage, x, y,
984 width, height, &pt);
985
986 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
987 assert(pixels);
988
989 sValues = malloc(width * sizeof(GLubyte));
990 zValues = malloc(width * sizeof(GLuint));
991
992 if (sValues && zValues) {
993 GLint row;
994 for (row = 0; row < height; row++) {
995 GLfloat *zValuesFloat = (GLfloat*)zValues;
996 GLenum destType = GL_UNSIGNED_BYTE;
997 const void *source = _mesa_image_address2d(&clippedUnpack, pixels,
998 width, height,
999 format, type,
1000 row, 0);
1001 _mesa_unpack_stencil_span(ctx, width, destType, sValues,
1002 type, source, &clippedUnpack,
1003 ctx->_ImageTransferState);
1004
1005 if (format == GL_DEPTH_STENCIL) {
1006 GLenum ztype =
1007 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
1008 GL_FLOAT : GL_UNSIGNED_INT;
1009
1010 _mesa_unpack_depth_span(ctx, width, ztype, zValues,
1011 (1 << 24) - 1, type, source,
1012 &clippedUnpack);
1013 }
1014
1015 if (zoom) {
1016 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
1017 "zoom not complete");
1018 }
1019
1020 {
1021 GLint spanY;
1022
1023 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1024 spanY = height - row - 1;
1025 }
1026 else {
1027 spanY = row;
1028 }
1029
1030 /* now pack the stencil (and Z) values in the dest format */
1031 switch (pt->resource->format) {
1032 case PIPE_FORMAT_S8_UINT:
1033 {
1034 ubyte *dest = stmap + spanY * pt->stride;
1035 assert(usage == PIPE_TRANSFER_WRITE);
1036 memcpy(dest, sValues, width);
1037 }
1038 break;
1039 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1040 if (format == GL_DEPTH_STENCIL) {
1041 uint *dest = (uint *) (stmap + spanY * pt->stride);
1042 GLint k;
1043 assert(usage == PIPE_TRANSFER_WRITE);
1044 for (k = 0; k < width; k++) {
1045 dest[k] = zValues[k] | (sValues[k] << 24);
1046 }
1047 }
1048 else {
1049 uint *dest = (uint *) (stmap + spanY * pt->stride);
1050 GLint k;
1051 assert(usage == PIPE_TRANSFER_READ_WRITE);
1052 for (k = 0; k < width; k++) {
1053 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
1054 }
1055 }
1056 break;
1057 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1058 if (format == GL_DEPTH_STENCIL) {
1059 uint *dest = (uint *) (stmap + spanY * pt->stride);
1060 GLint k;
1061 assert(usage == PIPE_TRANSFER_WRITE);
1062 for (k = 0; k < width; k++) {
1063 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
1064 }
1065 }
1066 else {
1067 uint *dest = (uint *) (stmap + spanY * pt->stride);
1068 GLint k;
1069 assert(usage == PIPE_TRANSFER_READ_WRITE);
1070 for (k = 0; k < width; k++) {
1071 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
1072 }
1073 }
1074 break;
1075 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1076 if (format == GL_DEPTH_STENCIL) {
1077 uint *dest = (uint *) (stmap + spanY * pt->stride);
1078 GLfloat *destf = (GLfloat*)dest;
1079 GLint k;
1080 assert(usage == PIPE_TRANSFER_WRITE);
1081 for (k = 0; k < width; k++) {
1082 destf[k*2] = zValuesFloat[k];
1083 dest[k*2+1] = sValues[k] & 0xff;
1084 }
1085 }
1086 else {
1087 uint *dest = (uint *) (stmap + spanY * pt->stride);
1088 GLint k;
1089 assert(usage == PIPE_TRANSFER_READ_WRITE);
1090 for (k = 0; k < width; k++) {
1091 dest[k*2+1] = sValues[k] & 0xff;
1092 }
1093 }
1094 break;
1095 default:
1096 assert(0);
1097 }
1098 }
1099 }
1100 }
1101 else {
1102 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels()");
1103 }
1104
1105 free(sValues);
1106 free(zValues);
1107
1108 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
1109
1110 /* unmap the stencil buffer */
1111 pipe_transfer_unmap(pipe, pt);
1112 }
1113
1114
1115 /**
1116 * Get fragment program variant for a glDrawPixels or glCopyPixels
1117 * command for RGBA data.
1118 */
1119 static struct st_fp_variant *
1120 get_color_fp_variant(struct st_context *st)
1121 {
1122 struct gl_context *ctx = st->ctx;
1123 struct st_fp_variant_key key;
1124 struct st_fp_variant *fpv;
1125
1126 memset(&key, 0, sizeof(key));
1127
1128 key.st = st->has_shareable_shaders ? NULL : st;
1129 key.drawpixels = 1;
1130 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
1131 ctx->Pixel.RedScale != 1.0 ||
1132 ctx->Pixel.GreenBias != 0.0 ||
1133 ctx->Pixel.GreenScale != 1.0 ||
1134 ctx->Pixel.BlueBias != 0.0 ||
1135 ctx->Pixel.BlueScale != 1.0 ||
1136 ctx->Pixel.AlphaBias != 0.0 ||
1137 ctx->Pixel.AlphaScale != 1.0);
1138 key.pixelMaps = ctx->Pixel.MapColorFlag;
1139 key.clamp_color = st->clamp_frag_color_in_shader &&
1140 ctx->Color._ClampFragmentColor;
1141
1142 fpv = st_get_fp_variant(st, st->fp, &key);
1143
1144 return fpv;
1145 }
1146
1147 /**
1148 * Get fragment program variant for a glDrawPixels command
1149 * for COLOR_INDEX data
1150 */
1151 static struct st_fp_variant *
1152 get_color_index_fp_variant(struct st_context *st)
1153 {
1154 struct gl_context *ctx = st->ctx;
1155 struct st_fp_variant_key key;
1156 struct st_fp_variant *fpv;
1157
1158 memset(&key, 0, sizeof(key));
1159
1160 key.st = st->has_shareable_shaders ? NULL : st;
1161 key.drawpixels = 1;
1162 /* Since GL is always in RGBA mode MapColorFlag does not
1163 * affect GL_COLOR_INDEX format.
1164 * Scale and bias also never affect GL_COLOR_INDEX format.
1165 */
1166 key.scaleAndBias = 0;
1167 key.pixelMaps = 0;
1168 key.clamp_color = st->clamp_frag_color_in_shader &&
1169 ctx->Color._ClampFragmentColor;
1170
1171 fpv = st_get_fp_variant(st, st->fp, &key);
1172
1173 return fpv;
1174 }
1175
1176
1177 /**
1178 * Clamp glDrawPixels width and height to the maximum texture size.
1179 */
1180 static void
1181 clamp_size(struct pipe_context *pipe, GLsizei *width, GLsizei *height,
1182 struct gl_pixelstore_attrib *unpack)
1183 {
1184 const int maxSize =
1185 1 << (pipe->screen->get_param(pipe->screen,
1186 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
1187
1188 if (*width > maxSize) {
1189 if (unpack->RowLength == 0)
1190 unpack->RowLength = *width;
1191 *width = maxSize;
1192 }
1193 if (*height > maxSize) {
1194 *height = maxSize;
1195 }
1196 }
1197
1198
1199 /**
1200 * Search the array of 4 swizzle components for the named component and return
1201 * its position.
1202 */
1203 static unsigned
1204 search_swizzle(const unsigned char swizzle[4], unsigned component)
1205 {
1206 unsigned i;
1207 for (i = 0; i < 4; i++) {
1208 if (swizzle[i] == component)
1209 return i;
1210 }
1211 assert(!"search_swizzle() failed");
1212 return 0;
1213 }
1214
1215
1216 /**
1217 * Set the sampler view's swizzle terms. This is used to handle RGBA
1218 * swizzling when the incoming image format isn't an exact match for
1219 * the actual texture format. For example, if we have glDrawPixels(
1220 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
1221 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
1222 * avoid swizzling all the pixels in software in the texstore code.
1223 */
1224 static void
1225 setup_sampler_swizzle(struct pipe_sampler_view *sv, GLenum format, GLenum type)
1226 {
1227 if ((format == GL_RGBA || format == GL_BGRA) && type == GL_UNSIGNED_BYTE) {
1228 const struct util_format_description *desc =
1229 util_format_description(sv->texture->format);
1230 unsigned c0, c1, c2, c3;
1231
1232 /* Every gallium driver supports at least one 32-bit packed RGBA format.
1233 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
1234 */
1235 assert(desc->block.bits == 32);
1236
1237 /* invert the format's swizzle to setup the sampler's swizzle */
1238 if (format == GL_RGBA) {
1239 c0 = PIPE_SWIZZLE_X;
1240 c1 = PIPE_SWIZZLE_Y;
1241 c2 = PIPE_SWIZZLE_Z;
1242 c3 = PIPE_SWIZZLE_W;
1243 }
1244 else {
1245 assert(format == GL_BGRA);
1246 c0 = PIPE_SWIZZLE_Z;
1247 c1 = PIPE_SWIZZLE_Y;
1248 c2 = PIPE_SWIZZLE_X;
1249 c3 = PIPE_SWIZZLE_W;
1250 }
1251 sv->swizzle_r = search_swizzle(desc->swizzle, c0);
1252 sv->swizzle_g = search_swizzle(desc->swizzle, c1);
1253 sv->swizzle_b = search_swizzle(desc->swizzle, c2);
1254 sv->swizzle_a = search_swizzle(desc->swizzle, c3);
1255 }
1256 else {
1257 /* use the default sampler swizzle */
1258 }
1259 }
1260
1261
1262 /**
1263 * Called via ctx->Driver.DrawPixels()
1264 */
1265 static void
1266 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
1267 GLsizei width, GLsizei height,
1268 GLenum format, GLenum type,
1269 const struct gl_pixelstore_attrib *unpack, const void *pixels)
1270 {
1271 void *driver_fp;
1272 struct st_context *st = st_context(ctx);
1273 struct pipe_context *pipe = st->pipe;
1274 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
1275 struct pipe_sampler_view *sv[2] = { NULL };
1276 int num_sampler_view = 1;
1277 struct gl_pixelstore_attrib clippedUnpack;
1278 struct st_fp_variant *fpv = NULL;
1279 struct pipe_resource *pt;
1280
1281 /* Mesa state should be up to date by now */
1282 assert(ctx->NewState == 0x0);
1283
1284 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
1285
1286 st_flush_bitmap_cache(st);
1287 st_invalidate_readpix_cache(st);
1288
1289 st_validate_state(st, ST_PIPELINE_META);
1290
1291 /* Limit the size of the glDrawPixels to the max texture size.
1292 * Strictly speaking, that's not correct but since we don't handle
1293 * larger images yet, this is better than crashing.
1294 */
1295 clippedUnpack = *unpack;
1296 unpack = &clippedUnpack;
1297 clamp_size(st->pipe, &width, &height, &clippedUnpack);
1298
1299 if (format == GL_DEPTH_STENCIL)
1300 write_stencil = write_depth = GL_TRUE;
1301 else if (format == GL_STENCIL_INDEX)
1302 write_stencil = GL_TRUE;
1303 else if (format == GL_DEPTH_COMPONENT)
1304 write_depth = GL_TRUE;
1305
1306 if (write_stencil &&
1307 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
1308 /* software fallback */
1309 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1310 unpack, pixels);
1311 return;
1312 }
1313
1314 /* Put glDrawPixels image into a texture */
1315 pt = make_texture(st, width, height, format, type, unpack, pixels);
1316 if (!pt) {
1317 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1318 return;
1319 }
1320
1321 st_make_passthrough_vertex_shader(st);
1322
1323 /*
1324 * Get vertex/fragment shaders
1325 */
1326 if (write_depth || write_stencil) {
1327 driver_fp = get_drawpix_z_stencil_program(st, write_depth,
1328 write_stencil);
1329 }
1330 else {
1331 fpv = (format != GL_COLOR_INDEX) ? get_color_fp_variant(st) :
1332 get_color_index_fp_variant(st);
1333
1334 driver_fp = fpv->driver_shader;
1335
1336 if (ctx->Pixel.MapColorFlag && format != GL_COLOR_INDEX) {
1337 pipe_sampler_view_reference(&sv[1],
1338 st->pixel_xfer.pixelmap_sampler_view);
1339 num_sampler_view++;
1340 }
1341
1342 /* compiling a new fragment shader variant added new state constants
1343 * into the constant buffer, we need to update them
1344 */
1345 st_upload_constants(st, &st->fp->Base);
1346 }
1347
1348 /* create sampler view for the image */
1349 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1350 if (!sv[0]) {
1351 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1352 pipe_resource_reference(&pt, NULL);
1353 return;
1354 }
1355
1356 /* Set up the sampler view's swizzle */
1357 setup_sampler_swizzle(sv[0], format, type);
1358
1359 /* Create a second sampler view to read stencil. The stencil is
1360 * written using the shader stencil export functionality.
1361 */
1362 if (write_stencil) {
1363 enum pipe_format stencil_format =
1364 util_format_stencil_only(pt->format);
1365 /* we should not be doing pixel map/transfer (see above) */
1366 assert(num_sampler_view == 1);
1367 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1368 stencil_format);
1369 if (!sv[1]) {
1370 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1371 pipe_resource_reference(&pt, NULL);
1372 pipe_sampler_view_reference(&sv[0], NULL);
1373 return;
1374 }
1375 num_sampler_view++;
1376 }
1377
1378 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1379 width, height,
1380 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1381 sv,
1382 num_sampler_view,
1383 st->passthrough_vs,
1384 driver_fp, fpv,
1385 ctx->Current.RasterColor,
1386 GL_FALSE, write_depth, write_stencil);
1387 pipe_sampler_view_reference(&sv[0], NULL);
1388 if (num_sampler_view > 1)
1389 pipe_sampler_view_reference(&sv[1], NULL);
1390
1391 /* free the texture (but may persist in the cache) */
1392 pipe_resource_reference(&pt, NULL);
1393 }
1394
1395
1396
1397 /**
1398 * Software fallback for glCopyPixels(GL_STENCIL).
1399 */
1400 static void
1401 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1402 GLsizei width, GLsizei height,
1403 GLint dstx, GLint dsty)
1404 {
1405 struct st_renderbuffer *rbDraw;
1406 struct pipe_context *pipe = st_context(ctx)->pipe;
1407 enum pipe_transfer_usage usage;
1408 struct pipe_transfer *ptDraw;
1409 ubyte *drawMap;
1410 ubyte *buffer;
1411 int i;
1412
1413 buffer = malloc(width * height * sizeof(ubyte));
1414 if (!buffer) {
1415 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1416 return;
1417 }
1418
1419 /* Get the dest renderbuffer */
1420 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1421 Attachment[BUFFER_STENCIL].Renderbuffer);
1422
1423 /* this will do stencil pixel transfer ops */
1424 _mesa_readpixels(ctx, srcx, srcy, width, height,
1425 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1426 &ctx->DefaultPacking, buffer);
1427
1428 if (0) {
1429 /* debug code: dump stencil values */
1430 GLint row, col;
1431 for (row = 0; row < height; row++) {
1432 printf("%3d: ", row);
1433 for (col = 0; col < width; col++) {
1434 printf("%02x ", buffer[col + row * width]);
1435 }
1436 printf("\n");
1437 }
1438 }
1439
1440 if (_mesa_is_format_packed_depth_stencil(rbDraw->Base.Format))
1441 usage = PIPE_TRANSFER_READ_WRITE;
1442 else
1443 usage = PIPE_TRANSFER_WRITE;
1444
1445 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1446 dsty = rbDraw->Base.Height - dsty - height;
1447 }
1448
1449 assert(util_format_get_blockwidth(rbDraw->texture->format) == 1);
1450 assert(util_format_get_blockheight(rbDraw->texture->format) == 1);
1451
1452 /* map the stencil buffer */
1453 drawMap = pipe_transfer_map(pipe,
1454 rbDraw->texture,
1455 rbDraw->surface->u.tex.level,
1456 rbDraw->surface->u.tex.first_layer,
1457 usage, dstx, dsty,
1458 width, height, &ptDraw);
1459
1460 /* draw */
1461 /* XXX PixelZoom not handled yet */
1462 for (i = 0; i < height; i++) {
1463 ubyte *dst;
1464 const ubyte *src;
1465 int y;
1466
1467 y = i;
1468
1469 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1470 y = height - y - 1;
1471 }
1472
1473 dst = drawMap + y * ptDraw->stride;
1474 src = buffer + i * width;
1475
1476 _mesa_pack_ubyte_stencil_row(rbDraw->Base.Format, width, src, dst);
1477 }
1478
1479 free(buffer);
1480
1481 /* unmap the stencil buffer */
1482 pipe_transfer_unmap(pipe, ptDraw);
1483 }
1484
1485
1486 /**
1487 * Return renderbuffer to use for reading color pixels for glCopyPixels
1488 */
1489 static struct st_renderbuffer *
1490 st_get_color_read_renderbuffer(struct gl_context *ctx)
1491 {
1492 struct gl_framebuffer *fb = ctx->ReadBuffer;
1493 struct st_renderbuffer *strb =
1494 st_renderbuffer(fb->_ColorReadBuffer);
1495
1496 return strb;
1497 }
1498
1499
1500 /**
1501 * Try to do a glCopyPixels for simple cases with a blit by calling
1502 * pipe->blit().
1503 *
1504 * We can do this when we're copying color pixels (depth/stencil
1505 * eventually) with no pixel zoom, no pixel transfer ops, no
1506 * per-fragment ops, and the src/dest regions don't overlap.
1507 */
1508 static GLboolean
1509 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1510 GLsizei width, GLsizei height,
1511 GLint dstx, GLint dsty, GLenum type)
1512 {
1513 struct st_context *st = st_context(ctx);
1514 struct pipe_context *pipe = st->pipe;
1515 struct pipe_screen *screen = pipe->screen;
1516 struct gl_pixelstore_attrib pack, unpack;
1517 GLint readX, readY, readW, readH, drawX, drawY, drawW, drawH;
1518
1519 if (type == GL_COLOR &&
1520 ctx->Pixel.ZoomX == 1.0 &&
1521 ctx->Pixel.ZoomY == 1.0 &&
1522 ctx->_ImageTransferState == 0x0 &&
1523 !ctx->Color.BlendEnabled &&
1524 !ctx->Color.AlphaEnabled &&
1525 (!ctx->Color.ColorLogicOpEnabled || ctx->Color.LogicOp == GL_COPY) &&
1526 !ctx->Depth.Test &&
1527 !ctx->Fog.Enabled &&
1528 !ctx->Stencil.Enabled &&
1529 !ctx->FragmentProgram.Enabled &&
1530 !ctx->VertexProgram.Enabled &&
1531 !ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT] &&
1532 !_mesa_ati_fragment_shader_enabled(ctx) &&
1533 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1534 !ctx->Query.CondRenderQuery &&
1535 !ctx->Query.CurrentOcclusionObject) {
1536 struct st_renderbuffer *rbRead, *rbDraw;
1537
1538 /*
1539 * Clip the read region against the src buffer bounds.
1540 * We'll still allocate a temporary buffer/texture for the original
1541 * src region size but we'll only read the region which is on-screen.
1542 * This may mean that we draw garbage pixels into the dest region, but
1543 * that's expected.
1544 */
1545 readX = srcx;
1546 readY = srcy;
1547 readW = width;
1548 readH = height;
1549 pack = ctx->DefaultPacking;
1550 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1551 return GL_TRUE; /* all done */
1552
1553 /* clip against dest buffer bounds and scissor box */
1554 drawX = dstx + pack.SkipPixels;
1555 drawY = dsty + pack.SkipRows;
1556 unpack = pack;
1557 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1558 return GL_TRUE; /* all done */
1559
1560 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1561 readY = readY - pack.SkipRows + unpack.SkipRows;
1562
1563 drawW = readW;
1564 drawH = readH;
1565
1566 rbRead = st_get_color_read_renderbuffer(ctx);
1567 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1568
1569 /* Flip src/dst position depending on the orientation of buffers. */
1570 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1571 readY = rbRead->Base.Height - readY;
1572 readH = -readH;
1573 }
1574
1575 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1576 /* We can't flip the destination for pipe->blit, so we only adjust
1577 * its position and flip the source.
1578 */
1579 drawY = rbDraw->Base.Height - drawY - drawH;
1580 readY += readH;
1581 readH = -readH;
1582 }
1583
1584 if (rbRead != rbDraw ||
1585 !_mesa_regions_overlap(readX, readY, readX + readW, readY + readH,
1586 drawX, drawY, drawX + drawW, drawY + drawH)) {
1587 struct pipe_blit_info blit;
1588
1589 memset(&blit, 0, sizeof(blit));
1590 blit.src.resource = rbRead->texture;
1591 blit.src.level = rbRead->surface->u.tex.level;
1592 blit.src.format = rbRead->texture->format;
1593 blit.src.box.x = readX;
1594 blit.src.box.y = readY;
1595 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1596 blit.src.box.width = readW;
1597 blit.src.box.height = readH;
1598 blit.src.box.depth = 1;
1599 blit.dst.resource = rbDraw->texture;
1600 blit.dst.level = rbDraw->surface->u.tex.level;
1601 blit.dst.format = rbDraw->texture->format;
1602 blit.dst.box.x = drawX;
1603 blit.dst.box.y = drawY;
1604 blit.dst.box.z = rbDraw->surface->u.tex.first_layer;
1605 blit.dst.box.width = drawW;
1606 blit.dst.box.height = drawH;
1607 blit.dst.box.depth = 1;
1608 blit.mask = PIPE_MASK_RGBA;
1609 blit.filter = PIPE_TEX_FILTER_NEAREST;
1610
1611 if (ctx->DrawBuffer != ctx->WinSysDrawBuffer)
1612 st_window_rectangles_to_blit(ctx, &blit);
1613
1614 if (screen->is_format_supported(screen, blit.src.format,
1615 blit.src.resource->target,
1616 blit.src.resource->nr_samples,
1617 blit.src.resource->nr_storage_samples,
1618 PIPE_BIND_SAMPLER_VIEW) &&
1619 screen->is_format_supported(screen, blit.dst.format,
1620 blit.dst.resource->target,
1621 blit.dst.resource->nr_samples,
1622 blit.dst.resource->nr_storage_samples,
1623 PIPE_BIND_RENDER_TARGET)) {
1624 pipe->blit(pipe, &blit);
1625 return GL_TRUE;
1626 }
1627 }
1628 }
1629
1630 return GL_FALSE;
1631 }
1632
1633
1634 static void
1635 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1636 GLsizei width, GLsizei height,
1637 GLint dstx, GLint dsty, GLenum type)
1638 {
1639 struct st_context *st = st_context(ctx);
1640 struct pipe_context *pipe = st->pipe;
1641 struct pipe_screen *screen = pipe->screen;
1642 struct st_renderbuffer *rbRead;
1643 void *driver_fp;
1644 struct pipe_resource *pt;
1645 struct pipe_sampler_view *sv[2] = { NULL };
1646 struct st_fp_variant *fpv = NULL;
1647 int num_sampler_view = 1;
1648 enum pipe_format srcFormat;
1649 unsigned srcBind;
1650 GLboolean invertTex = GL_FALSE;
1651 GLint readX, readY, readW, readH;
1652 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1653
1654 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
1655
1656 st_flush_bitmap_cache(st);
1657 st_invalidate_readpix_cache(st);
1658
1659 st_validate_state(st, ST_PIPELINE_META);
1660
1661 if (type == GL_DEPTH_STENCIL) {
1662 /* XXX make this more efficient */
1663 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1664 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1665 return;
1666 }
1667
1668 if (type == GL_STENCIL) {
1669 /* can't use texturing to do stencil */
1670 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1671 return;
1672 }
1673
1674 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1675 return;
1676
1677 /*
1678 * The subsequent code implements glCopyPixels by copying the source
1679 * pixels into a temporary texture that's then applied to a textured quad.
1680 * When we draw the textured quad, all the usual per-fragment operations
1681 * are handled.
1682 */
1683
1684 st_make_passthrough_vertex_shader(st);
1685
1686 /*
1687 * Get vertex/fragment shaders
1688 */
1689 if (type == GL_COLOR) {
1690 fpv = get_color_fp_variant(st);
1691
1692 rbRead = st_get_color_read_renderbuffer(ctx);
1693
1694 driver_fp = fpv->driver_shader;
1695
1696 if (ctx->Pixel.MapColorFlag) {
1697 pipe_sampler_view_reference(&sv[1],
1698 st->pixel_xfer.pixelmap_sampler_view);
1699 num_sampler_view++;
1700 }
1701
1702 /* compiling a new fragment shader variant added new state constants
1703 * into the constant buffer, we need to update them
1704 */
1705 st_upload_constants(st, &st->fp->Base);
1706 }
1707 else {
1708 assert(type == GL_DEPTH);
1709 rbRead = st_renderbuffer(ctx->ReadBuffer->
1710 Attachment[BUFFER_DEPTH].Renderbuffer);
1711
1712 driver_fp = get_drawpix_z_stencil_program(st, GL_TRUE, GL_FALSE);
1713 }
1714
1715 /* Choose the format for the temporary texture. */
1716 srcFormat = rbRead->texture->format;
1717 srcBind = PIPE_BIND_SAMPLER_VIEW |
1718 (type == GL_COLOR ? PIPE_BIND_RENDER_TARGET : PIPE_BIND_DEPTH_STENCIL);
1719
1720 if (!screen->is_format_supported(screen, srcFormat, st->internal_target, 0,
1721 0, srcBind)) {
1722 /* srcFormat is non-renderable. Find a compatible renderable format. */
1723 if (type == GL_DEPTH) {
1724 srcFormat = st_choose_format(st, GL_DEPTH_COMPONENT, GL_NONE,
1725 GL_NONE, st->internal_target, 0, 0,
1726 srcBind, FALSE);
1727 }
1728 else {
1729 assert(type == GL_COLOR);
1730
1731 if (util_format_is_float(srcFormat)) {
1732 srcFormat = st_choose_format(st, GL_RGBA32F, GL_NONE,
1733 GL_NONE, st->internal_target, 0, 0,
1734 srcBind, FALSE);
1735 }
1736 else if (util_format_is_pure_sint(srcFormat)) {
1737 srcFormat = st_choose_format(st, GL_RGBA32I, GL_NONE,
1738 GL_NONE, st->internal_target, 0, 0,
1739 srcBind, FALSE);
1740 }
1741 else if (util_format_is_pure_uint(srcFormat)) {
1742 srcFormat = st_choose_format(st, GL_RGBA32UI, GL_NONE,
1743 GL_NONE, st->internal_target, 0, 0,
1744 srcBind, FALSE);
1745 }
1746 else if (util_format_is_snorm(srcFormat)) {
1747 srcFormat = st_choose_format(st, GL_RGBA16_SNORM, GL_NONE,
1748 GL_NONE, st->internal_target, 0, 0,
1749 srcBind, FALSE);
1750 }
1751 else {
1752 srcFormat = st_choose_format(st, GL_RGBA, GL_NONE,
1753 GL_NONE, st->internal_target, 0, 0,
1754 srcBind, FALSE);
1755 }
1756 }
1757
1758 if (srcFormat == PIPE_FORMAT_NONE) {
1759 assert(0 && "cannot choose a format for src of CopyPixels");
1760 return;
1761 }
1762 }
1763
1764 /* Invert src region if needed */
1765 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1766 srcy = ctx->ReadBuffer->Height - srcy - height;
1767 invertTex = !invertTex;
1768 }
1769
1770 /* Clip the read region against the src buffer bounds.
1771 * We'll still allocate a temporary buffer/texture for the original
1772 * src region size but we'll only read the region which is on-screen.
1773 * This may mean that we draw garbage pixels into the dest region, but
1774 * that's expected.
1775 */
1776 readX = srcx;
1777 readY = srcy;
1778 readW = width;
1779 readH = height;
1780 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1781 /* The source region is completely out of bounds. Do nothing.
1782 * The GL spec says "Results of copies from outside the window,
1783 * or from regions of the window that are not exposed, are
1784 * hardware dependent and undefined."
1785 */
1786 return;
1787 }
1788
1789 readW = MAX2(0, readW);
1790 readH = MAX2(0, readH);
1791
1792 /* Allocate the temporary texture. */
1793 pt = alloc_texture(st, width, height, srcFormat, srcBind);
1794 if (!pt)
1795 return;
1796
1797 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1798 if (!sv[0]) {
1799 pipe_resource_reference(&pt, NULL);
1800 return;
1801 }
1802
1803 /* Copy the src region to the temporary texture. */
1804 {
1805 struct pipe_blit_info blit;
1806
1807 memset(&blit, 0, sizeof(blit));
1808 blit.src.resource = rbRead->texture;
1809 blit.src.level = rbRead->surface->u.tex.level;
1810 blit.src.format = rbRead->texture->format;
1811 blit.src.box.x = readX;
1812 blit.src.box.y = readY;
1813 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1814 blit.src.box.width = readW;
1815 blit.src.box.height = readH;
1816 blit.src.box.depth = 1;
1817 blit.dst.resource = pt;
1818 blit.dst.level = 0;
1819 blit.dst.format = pt->format;
1820 blit.dst.box.x = pack.SkipPixels;
1821 blit.dst.box.y = pack.SkipRows;
1822 blit.dst.box.z = 0;
1823 blit.dst.box.width = readW;
1824 blit.dst.box.height = readH;
1825 blit.dst.box.depth = 1;
1826 blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_S;
1827 blit.filter = PIPE_TEX_FILTER_NEAREST;
1828
1829 pipe->blit(pipe, &blit);
1830 }
1831
1832 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1833 * textured quad with that texture.
1834 */
1835 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1836 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1837 sv,
1838 num_sampler_view,
1839 st->passthrough_vs,
1840 driver_fp, fpv,
1841 ctx->Current.Attrib[VERT_ATTRIB_COLOR0],
1842 invertTex, GL_FALSE, GL_FALSE);
1843
1844 pipe_resource_reference(&pt, NULL);
1845 pipe_sampler_view_reference(&sv[0], NULL);
1846 }
1847
1848
1849
1850 void st_init_drawpixels_functions(struct dd_function_table *functions)
1851 {
1852 functions->DrawPixels = st_DrawPixels;
1853 functions->CopyPixels = st_CopyPixels;
1854 }
1855
1856
1857 void
1858 st_destroy_drawpix(struct st_context *st)
1859 {
1860 GLuint i;
1861
1862 for (i = 0; i < ARRAY_SIZE(st->drawpix.zs_shaders); i++) {
1863 if (st->drawpix.zs_shaders[i])
1864 cso_delete_fragment_shader(st->cso_context,
1865 st->drawpix.zs_shaders[i]);
1866 }
1867
1868 if (st->passthrough_vs)
1869 cso_delete_vertex_shader(st->cso_context, st->passthrough_vs);
1870
1871 /* Free cache data */
1872 for (i = 0; i < ARRAY_SIZE(st->drawpix_cache.entries); i++) {
1873 struct drawpix_cache_entry *entry = &st->drawpix_cache.entries[i];
1874 free(entry->image);
1875 pipe_resource_reference(&entry->texture, NULL);
1876 }
1877 }