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