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