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