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