st/mesa: use new CSO_BITS_ALL_SHADERS
[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/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/blit.h"
37 #include "main/format_pack.h"
38 #include "main/framebuffer.h"
39 #include "main/macros.h"
40 #include "main/mtypes.h"
41 #include "main/pack.h"
42 #include "main/pbo.h"
43 #include "main/readpix.h"
44 #include "main/texformat.h"
45 #include "main/teximage.h"
46 #include "main/texstore.h"
47 #include "main/glformats.h"
48 #include "program/program.h"
49 #include "program/prog_print.h"
50 #include "program/prog_instruction.h"
51
52 #include "st_atom.h"
53 #include "st_atom_constbuf.h"
54 #include "st_cb_bitmap.h"
55 #include "st_cb_drawpixels.h"
56 #include "st_cb_readpixels.h"
57 #include "st_cb_fbo.h"
58 #include "st_context.h"
59 #include "st_debug.h"
60 #include "st_draw.h"
61 #include "st_format.h"
62 #include "st_program.h"
63 #include "st_texture.h"
64
65 #include "pipe/p_context.h"
66 #include "pipe/p_defines.h"
67 #include "tgsi/tgsi_ureg.h"
68 #include "util/u_format.h"
69 #include "util/u_inlines.h"
70 #include "util/u_math.h"
71 #include "util/u_tile.h"
72 #include "cso_cache/cso_context.h"
73
74
75 /**
76 * Create fragment program that does a TEX() instruction to get a Z and/or
77 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
78 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
79 * Pass fragment color through as-is.
80 *
81 * \return CSO of the fragment shader.
82 */
83 static void *
84 get_drawpix_z_stencil_program(struct st_context *st,
85 GLboolean write_depth,
86 GLboolean write_stencil)
87 {
88 struct ureg_program *ureg;
89 struct ureg_src depth_sampler, stencil_sampler;
90 struct ureg_src texcoord, color;
91 struct ureg_dst out_color, out_depth, out_stencil;
92 const GLuint shaderIndex = write_depth * 2 + write_stencil;
93 void *cso;
94
95 assert(shaderIndex < ARRAY_SIZE(st->drawpix.zs_shaders));
96
97 if (st->drawpix.zs_shaders[shaderIndex]) {
98 /* already have the proper shader */
99 return st->drawpix.zs_shaders[shaderIndex];
100 }
101
102 ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
103 if (ureg == NULL)
104 return NULL;
105
106 ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, TRUE);
107
108 if (write_depth) {
109 color = ureg_DECL_fs_input(ureg, TGSI_SEMANTIC_COLOR, 0,
110 TGSI_INTERPOLATE_COLOR);
111 out_color = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
112
113 depth_sampler = ureg_DECL_sampler(ureg, 0);
114 out_depth = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
115 }
116
117 if (write_stencil) {
118 stencil_sampler = ureg_DECL_sampler(ureg, 1);
119 out_stencil = ureg_DECL_output(ureg, TGSI_SEMANTIC_STENCIL, 0);
120 }
121
122 texcoord = ureg_DECL_fs_input(ureg,
123 st->needs_texcoord_semantic ?
124 TGSI_SEMANTIC_TEXCOORD :
125 TGSI_SEMANTIC_GENERIC,
126 0, TGSI_INTERPOLATE_LINEAR);
127
128 if (write_depth) {
129 ureg_TEX(ureg, ureg_writemask(out_depth, TGSI_WRITEMASK_Z),
130 TGSI_TEXTURE_2D, texcoord, depth_sampler);
131 ureg_MOV(ureg, out_color, color);
132 }
133
134 if (write_stencil)
135 ureg_TEX(ureg, ureg_writemask(out_stencil, TGSI_WRITEMASK_Y),
136 TGSI_TEXTURE_2D, texcoord, stencil_sampler);
137
138 ureg_END(ureg);
139 cso = ureg_create_shader_and_destroy(ureg, st->pipe);
140
141 /* save the new shader */
142 st->drawpix.zs_shaders[shaderIndex] = cso;
143 return cso;
144 }
145
146
147 /**
148 * Create a simple vertex shader that just passes through the
149 * vertex position and texcoord (and optionally, color).
150 */
151 static void *
152 make_passthrough_vertex_shader(struct st_context *st,
153 GLboolean passColor)
154 {
155 const unsigned texcoord_semantic = st->needs_texcoord_semantic ?
156 TGSI_SEMANTIC_TEXCOORD : TGSI_SEMANTIC_GENERIC;
157
158 if (!st->drawpix.vert_shaders[passColor]) {
159 struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
160
161 if (ureg == NULL)
162 return NULL;
163
164 /* MOV result.pos, vertex.pos; */
165 ureg_MOV(ureg,
166 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
167 ureg_DECL_vs_input( ureg, 0 ));
168
169 if (passColor) {
170 /* MOV result.color0, vertex.attr[1]; */
171 ureg_MOV(ureg,
172 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
173 ureg_DECL_vs_input( ureg, 1 ));
174 }
175
176 /* MOV result.texcoord0, vertex.attr[2]; */
177 ureg_MOV(ureg,
178 ureg_DECL_output( ureg, texcoord_semantic, 0 ),
179 ureg_DECL_vs_input( ureg, 2 ));
180
181 ureg_END( ureg );
182
183 st->drawpix.vert_shaders[passColor] =
184 ureg_create_shader_and_destroy( ureg, st->pipe );
185 }
186
187 return st->drawpix.vert_shaders[passColor];
188 }
189
190
191 /**
192 * Return a texture internalFormat for drawing/copying an image
193 * of the given format and type.
194 */
195 static GLenum
196 internal_format(struct gl_context *ctx, GLenum format, GLenum type)
197 {
198 switch (format) {
199 case GL_DEPTH_COMPONENT:
200 switch (type) {
201 case GL_UNSIGNED_SHORT:
202 return GL_DEPTH_COMPONENT16;
203
204 case GL_UNSIGNED_INT:
205 return GL_DEPTH_COMPONENT32;
206
207 case GL_FLOAT:
208 if (ctx->Extensions.ARB_depth_buffer_float)
209 return GL_DEPTH_COMPONENT32F;
210 else
211 return GL_DEPTH_COMPONENT;
212
213 default:
214 return GL_DEPTH_COMPONENT;
215 }
216
217 case GL_DEPTH_STENCIL:
218 switch (type) {
219 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
220 return GL_DEPTH32F_STENCIL8;
221
222 case GL_UNSIGNED_INT_24_8:
223 default:
224 return GL_DEPTH24_STENCIL8;
225 }
226
227 case GL_STENCIL_INDEX:
228 return GL_STENCIL_INDEX;
229
230 default:
231 if (_mesa_is_enum_format_integer(format)) {
232 switch (type) {
233 case GL_BYTE:
234 return GL_RGBA8I;
235 case GL_UNSIGNED_BYTE:
236 return GL_RGBA8UI;
237 case GL_SHORT:
238 return GL_RGBA16I;
239 case GL_UNSIGNED_SHORT:
240 return GL_RGBA16UI;
241 case GL_INT:
242 return GL_RGBA32I;
243 case GL_UNSIGNED_INT:
244 return GL_RGBA32UI;
245 default:
246 assert(0 && "Unexpected type in internal_format()");
247 return GL_RGBA_INTEGER;
248 }
249 }
250 else {
251 switch (type) {
252 case GL_UNSIGNED_BYTE:
253 case GL_UNSIGNED_INT_8_8_8_8:
254 case GL_UNSIGNED_INT_8_8_8_8_REV:
255 default:
256 return GL_RGBA8;
257
258 case GL_UNSIGNED_BYTE_3_3_2:
259 case GL_UNSIGNED_BYTE_2_3_3_REV:
260 return GL_R3_G3_B2;
261
262 case GL_UNSIGNED_SHORT_4_4_4_4:
263 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
264 return GL_RGBA4;
265
266 case GL_UNSIGNED_SHORT_5_6_5:
267 case GL_UNSIGNED_SHORT_5_6_5_REV:
268 return GL_RGB565;
269
270 case GL_UNSIGNED_SHORT_5_5_5_1:
271 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
272 return GL_RGB5_A1;
273
274 case GL_UNSIGNED_INT_10_10_10_2:
275 case GL_UNSIGNED_INT_2_10_10_10_REV:
276 return GL_RGB10_A2;
277
278 case GL_UNSIGNED_SHORT:
279 case GL_UNSIGNED_INT:
280 return GL_RGBA16;
281
282 case GL_BYTE:
283 return
284 ctx->Extensions.EXT_texture_snorm ? GL_RGBA8_SNORM : GL_RGBA8;
285
286 case GL_SHORT:
287 case GL_INT:
288 return
289 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
290
291 case GL_HALF_FLOAT_ARB:
292 return
293 ctx->Extensions.ARB_texture_float ? GL_RGBA16F :
294 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
295
296 case GL_FLOAT:
297 case GL_DOUBLE:
298 return
299 ctx->Extensions.ARB_texture_float ? GL_RGBA32F :
300 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
301
302 case GL_UNSIGNED_INT_5_9_9_9_REV:
303 assert(ctx->Extensions.EXT_texture_shared_exponent);
304 return GL_RGB9_E5;
305
306 case GL_UNSIGNED_INT_10F_11F_11F_REV:
307 assert(ctx->Extensions.EXT_packed_float);
308 return GL_R11F_G11F_B10F;
309 }
310 }
311 }
312 }
313
314
315 /**
316 * Create a temporary texture to hold an image of the given size.
317 * If width, height are not POT and the driver only handles POT textures,
318 * allocate the next larger size of texture that is POT.
319 */
320 static struct pipe_resource *
321 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
322 enum pipe_format texFormat, unsigned bind)
323 {
324 struct pipe_resource *pt;
325
326 pt = st_texture_create(st, st->internal_target, texFormat, 0,
327 width, height, 1, 1, 0, bind);
328
329 return pt;
330 }
331
332
333 /**
334 * Make texture containing an image for glDrawPixels image.
335 * If 'pixels' is NULL, leave the texture image data undefined.
336 */
337 static struct pipe_resource *
338 make_texture(struct st_context *st,
339 GLsizei width, GLsizei height, GLenum format, GLenum type,
340 const struct gl_pixelstore_attrib *unpack,
341 const GLvoid *pixels)
342 {
343 struct gl_context *ctx = st->ctx;
344 struct pipe_context *pipe = st->pipe;
345 mesa_format mformat;
346 struct pipe_resource *pt;
347 enum pipe_format pipeFormat;
348 GLenum baseInternalFormat;
349
350 /* Choose a pixel format for the temp texture which will hold the
351 * image to draw.
352 */
353 pipeFormat = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
354 format, type, unpack->SwapBytes);
355
356 if (pipeFormat == PIPE_FORMAT_NONE) {
357 /* Use the generic approach. */
358 GLenum intFormat = internal_format(ctx, format, type);
359
360 pipeFormat = st_choose_format(st, intFormat, format, type,
361 st->internal_target, 0,
362 PIPE_BIND_SAMPLER_VIEW, FALSE);
363 assert(pipeFormat != PIPE_FORMAT_NONE);
364 }
365
366 mformat = st_pipe_format_to_mesa_format(pipeFormat);
367 baseInternalFormat = _mesa_get_format_base_format(mformat);
368
369 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
370 if (!pixels)
371 return NULL;
372
373 /* alloc temporary texture */
374 pt = alloc_texture(st, width, height, pipeFormat, PIPE_BIND_SAMPLER_VIEW);
375 if (!pt) {
376 _mesa_unmap_pbo_source(ctx, unpack);
377 return NULL;
378 }
379
380 {
381 struct pipe_transfer *transfer;
382 GLboolean success;
383 GLubyte *dest;
384 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
385
386 /* we'll do pixel transfer in a fragment shader */
387 ctx->_ImageTransferState = 0x0;
388
389 /* map texture transfer */
390 dest = pipe_transfer_map(pipe, pt, 0, 0,
391 PIPE_TRANSFER_WRITE, 0, 0,
392 width, height, &transfer);
393
394
395 /* Put image into texture transfer.
396 * Note that the image is actually going to be upside down in
397 * the texture. We deal with that with texcoords.
398 */
399 if ((format == GL_RGBA || format == GL_BGRA)
400 && type == GL_UNSIGNED_BYTE) {
401 /* Use a memcpy-based texstore to avoid software pixel swizzling.
402 * We'll do the necessary swizzling with the pipe_sampler_view to
403 * give much better performance.
404 * XXX in the future, expand this to accomodate more format and
405 * type combinations.
406 */
407 _mesa_memcpy_texture(ctx, 2,
408 mformat, /* mesa_format */
409 transfer->stride, /* dstRowStride, bytes */
410 &dest, /* destSlices */
411 width, height, 1, /* size */
412 format, type, /* src format/type */
413 pixels, /* data source */
414 unpack);
415 success = GL_TRUE;
416 }
417 else {
418 success = _mesa_texstore(ctx, 2, /* dims */
419 baseInternalFormat, /* baseInternalFormat */
420 mformat, /* mesa_format */
421 transfer->stride, /* dstRowStride, bytes */
422 &dest, /* destSlices */
423 width, height, 1, /* size */
424 format, type, /* src format/type */
425 pixels, /* data source */
426 unpack);
427 }
428
429 /* unmap */
430 pipe_transfer_unmap(pipe, transfer);
431
432 assert(success);
433
434 /* restore */
435 ctx->_ImageTransferState = imageTransferStateSave;
436 }
437
438 _mesa_unmap_pbo_source(ctx, unpack);
439
440 return pt;
441 }
442
443
444 static void
445 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
446 GLsizei width, GLsizei height,
447 GLfloat zoomX, GLfloat zoomY,
448 struct pipe_sampler_view **sv,
449 int num_sampler_view,
450 void *driver_vp,
451 void *driver_fp,
452 struct st_fp_variant *fpv,
453 const GLfloat *color,
454 GLboolean invertTex,
455 GLboolean write_depth, GLboolean write_stencil)
456 {
457 struct st_context *st = st_context(ctx);
458 struct pipe_context *pipe = st->pipe;
459 struct cso_context *cso = st->cso_context;
460 const unsigned fb_width = _mesa_geometric_width(ctx->DrawBuffer);
461 const unsigned fb_height = _mesa_geometric_height(ctx->DrawBuffer);
462 GLfloat x0, y0, x1, y1;
463 GLsizei maxSize;
464 boolean normalized = sv[0]->texture->target == PIPE_TEXTURE_2D;
465 unsigned cso_state_mask;
466
467 assert(sv[0]->texture->target == st->internal_target);
468
469 /* limit checks */
470 /* XXX if DrawPixels image is larger than max texture size, break
471 * it up into chunks.
472 */
473 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
474 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
475 assert(width <= maxSize);
476 assert(height <= maxSize);
477
478 cso_state_mask = (CSO_BIT_RASTERIZER |
479 CSO_BIT_VIEWPORT |
480 CSO_BIT_FRAGMENT_SAMPLERS |
481 CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
482 CSO_BIT_STREAM_OUTPUTS |
483 CSO_BIT_VERTEX_ELEMENTS |
484 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
485 CSO_BITS_ALL_SHADERS);
486 if (write_stencil) {
487 cso_state_mask |= (CSO_BIT_DEPTH_STENCIL_ALPHA |
488 CSO_BIT_BLEND);
489 }
490 cso_save_state(cso, cso_state_mask);
491
492 /* rasterizer state: just scissor */
493 {
494 struct pipe_rasterizer_state rasterizer;
495 memset(&rasterizer, 0, sizeof(rasterizer));
496 rasterizer.clamp_fragment_color = !st->clamp_frag_color_in_shader &&
497 ctx->Color._ClampFragmentColor;
498 rasterizer.half_pixel_center = 1;
499 rasterizer.bottom_edge_rule = 1;
500 rasterizer.depth_clip = !ctx->Transform.DepthClamp;
501 rasterizer.scissor = ctx->Scissor.EnableFlags;
502 cso_set_rasterizer(cso, &rasterizer);
503 }
504
505 if (write_stencil) {
506 /* Stencil writing bypasses the normal fragment pipeline to
507 * disable color writing and set stencil test to always pass.
508 */
509 struct pipe_depth_stencil_alpha_state dsa;
510 struct pipe_blend_state blend;
511
512 /* depth/stencil */
513 memset(&dsa, 0, sizeof(dsa));
514 dsa.stencil[0].enabled = 1;
515 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
516 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
517 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
518 if (write_depth) {
519 /* writing depth+stencil: depth test always passes */
520 dsa.depth.enabled = 1;
521 dsa.depth.writemask = ctx->Depth.Mask;
522 dsa.depth.func = PIPE_FUNC_ALWAYS;
523 }
524 cso_set_depth_stencil_alpha(cso, &dsa);
525
526 /* blend (colormask) */
527 memset(&blend, 0, sizeof(blend));
528 cso_set_blend(cso, &blend);
529 }
530
531 /* fragment shader state: TEX lookup program */
532 cso_set_fragment_shader_handle(cso, driver_fp);
533
534 /* vertex shader state: position + texcoord pass-through */
535 cso_set_vertex_shader_handle(cso, driver_vp);
536
537 /* disable other shaders */
538 cso_set_tessctrl_shader_handle(cso, NULL);
539 cso_set_tesseval_shader_handle(cso, NULL);
540 cso_set_geometry_shader_handle(cso, NULL);
541
542 /* user samplers, plus the drawpix samplers */
543 {
544 struct pipe_sampler_state sampler;
545
546 memset(&sampler, 0, sizeof(sampler));
547 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
548 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
549 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
550 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
551 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
552 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
553 sampler.normalized_coords = normalized;
554
555 if (fpv) {
556 /* drawing a color image */
557 const struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
558 uint num = MAX3(fpv->drawpix_sampler + 1,
559 fpv->pixelmap_sampler + 1,
560 st->state.num_samplers[PIPE_SHADER_FRAGMENT]);
561 uint i;
562
563 for (i = 0; i < st->state.num_samplers[PIPE_SHADER_FRAGMENT]; i++)
564 samplers[i] = &st->state.samplers[PIPE_SHADER_FRAGMENT][i];
565
566 samplers[fpv->drawpix_sampler] = &sampler;
567 if (sv[1])
568 samplers[fpv->pixelmap_sampler] = &sampler;
569
570 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num, samplers);
571 } else {
572 /* drawing a depth/stencil image */
573 const struct pipe_sampler_state *samplers[2] = {&sampler, &sampler};
574
575 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num_sampler_view, samplers);
576 }
577 }
578
579 /* user textures, plus the drawpix textures */
580 if (fpv) {
581 /* drawing a color image */
582 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
583 uint num = MAX3(fpv->drawpix_sampler + 1,
584 fpv->pixelmap_sampler + 1,
585 st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
586
587 memcpy(sampler_views, st->state.sampler_views[PIPE_SHADER_FRAGMENT],
588 sizeof(sampler_views));
589
590 sampler_views[fpv->drawpix_sampler] = sv[0];
591 if (sv[1])
592 sampler_views[fpv->pixelmap_sampler] = sv[1];
593 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num, sampler_views);
594 } else {
595 /* drawing a depth/stencil image */
596 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num_sampler_view, sv);
597 }
598
599 /* viewport state: viewport matching window dims */
600 cso_set_viewport_dims(cso, fb_width, fb_height, TRUE);
601
602 cso_set_vertex_elements(cso, 3, st->util_velems);
603 cso_set_stream_outputs(cso, 0, NULL, NULL);
604
605 /* Compute Gallium window coords (y=0=top) with pixel zoom.
606 * Recall that these coords are transformed by the current
607 * vertex shader and viewport transformation.
608 */
609 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
610 y = fb_height - (int) (y + height * ctx->Pixel.ZoomY);
611 invertTex = !invertTex;
612 }
613
614 x0 = (GLfloat) x;
615 x1 = x + width * ctx->Pixel.ZoomX;
616 y0 = (GLfloat) y;
617 y1 = y + height * ctx->Pixel.ZoomY;
618
619 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
620 z = z * 2.0f - 1.0f;
621
622 {
623 const float clip_x0 = x0 / (float) fb_width * 2.0f - 1.0f;
624 const float clip_y0 = y0 / (float) fb_height * 2.0f - 1.0f;
625 const float clip_x1 = x1 / (float) fb_width * 2.0f - 1.0f;
626 const float clip_y1 = y1 / (float) fb_height * 2.0f - 1.0f;
627 const float maxXcoord = normalized ?
628 ((float) width / sv[0]->texture->width0) : (float) width;
629 const float maxYcoord = normalized
630 ? ((float) height / sv[0]->texture->height0) : (float) height;
631 const float sLeft = 0.0f, sRight = maxXcoord;
632 const float tTop = invertTex ? maxYcoord : 0.0f;
633 const float tBot = invertTex ? 0.0f : maxYcoord;
634
635 if (!st_draw_quad(st, clip_x0, clip_y0, clip_x1, clip_y1, z,
636 sLeft, tBot, sRight, tTop, color, 0)) {
637 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
638 }
639 }
640
641 /* restore state */
642 cso_restore_state(cso);
643 }
644
645
646 /**
647 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
648 * can't use a fragment shader to write stencil values.
649 */
650 static void
651 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
652 GLsizei width, GLsizei height, GLenum format, GLenum type,
653 const struct gl_pixelstore_attrib *unpack,
654 const GLvoid *pixels)
655 {
656 struct st_context *st = st_context(ctx);
657 struct pipe_context *pipe = st->pipe;
658 struct st_renderbuffer *strb;
659 enum pipe_transfer_usage usage;
660 struct pipe_transfer *pt;
661 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
662 ubyte *stmap;
663 struct gl_pixelstore_attrib clippedUnpack = *unpack;
664 GLubyte *sValues;
665 GLuint *zValues;
666
667 if (!zoom) {
668 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
669 &clippedUnpack)) {
670 /* totally clipped */
671 return;
672 }
673 }
674
675 strb = st_renderbuffer(ctx->DrawBuffer->
676 Attachment[BUFFER_STENCIL].Renderbuffer);
677
678 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
679 y = ctx->DrawBuffer->Height - y - height;
680 }
681
682 if (format == GL_STENCIL_INDEX &&
683 _mesa_is_format_packed_depth_stencil(strb->Base.Format)) {
684 /* writing stencil to a combined depth+stencil buffer */
685 usage = PIPE_TRANSFER_READ_WRITE;
686 }
687 else {
688 usage = PIPE_TRANSFER_WRITE;
689 }
690
691 stmap = pipe_transfer_map(pipe, strb->texture,
692 strb->surface->u.tex.level,
693 strb->surface->u.tex.first_layer,
694 usage, x, y,
695 width, height, &pt);
696
697 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
698 assert(pixels);
699
700 sValues = malloc(width * sizeof(GLubyte));
701 zValues = malloc(width * sizeof(GLuint));
702
703 if (sValues && zValues) {
704 GLint row;
705 for (row = 0; row < height; row++) {
706 GLfloat *zValuesFloat = (GLfloat*)zValues;
707 GLenum destType = GL_UNSIGNED_BYTE;
708 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
709 width, height,
710 format, type,
711 row, 0);
712 _mesa_unpack_stencil_span(ctx, width, destType, sValues,
713 type, source, &clippedUnpack,
714 ctx->_ImageTransferState);
715
716 if (format == GL_DEPTH_STENCIL) {
717 GLenum ztype =
718 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
719 GL_FLOAT : GL_UNSIGNED_INT;
720
721 _mesa_unpack_depth_span(ctx, width, ztype, zValues,
722 (1 << 24) - 1, type, source,
723 &clippedUnpack);
724 }
725
726 if (zoom) {
727 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
728 "zoom not complete");
729 }
730
731 {
732 GLint spanY;
733
734 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
735 spanY = height - row - 1;
736 }
737 else {
738 spanY = row;
739 }
740
741 /* now pack the stencil (and Z) values in the dest format */
742 switch (pt->resource->format) {
743 case PIPE_FORMAT_S8_UINT:
744 {
745 ubyte *dest = stmap + spanY * pt->stride;
746 assert(usage == PIPE_TRANSFER_WRITE);
747 memcpy(dest, sValues, width);
748 }
749 break;
750 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
751 if (format == GL_DEPTH_STENCIL) {
752 uint *dest = (uint *) (stmap + spanY * pt->stride);
753 GLint k;
754 assert(usage == PIPE_TRANSFER_WRITE);
755 for (k = 0; k < width; k++) {
756 dest[k] = zValues[k] | (sValues[k] << 24);
757 }
758 }
759 else {
760 uint *dest = (uint *) (stmap + spanY * pt->stride);
761 GLint k;
762 assert(usage == PIPE_TRANSFER_READ_WRITE);
763 for (k = 0; k < width; k++) {
764 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
765 }
766 }
767 break;
768 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
769 if (format == GL_DEPTH_STENCIL) {
770 uint *dest = (uint *) (stmap + spanY * pt->stride);
771 GLint k;
772 assert(usage == PIPE_TRANSFER_WRITE);
773 for (k = 0; k < width; k++) {
774 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
775 }
776 }
777 else {
778 uint *dest = (uint *) (stmap + spanY * pt->stride);
779 GLint k;
780 assert(usage == PIPE_TRANSFER_READ_WRITE);
781 for (k = 0; k < width; k++) {
782 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
783 }
784 }
785 break;
786 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
787 if (format == GL_DEPTH_STENCIL) {
788 uint *dest = (uint *) (stmap + spanY * pt->stride);
789 GLfloat *destf = (GLfloat*)dest;
790 GLint k;
791 assert(usage == PIPE_TRANSFER_WRITE);
792 for (k = 0; k < width; k++) {
793 destf[k*2] = zValuesFloat[k];
794 dest[k*2+1] = sValues[k] & 0xff;
795 }
796 }
797 else {
798 uint *dest = (uint *) (stmap + spanY * pt->stride);
799 GLint k;
800 assert(usage == PIPE_TRANSFER_READ_WRITE);
801 for (k = 0; k < width; k++) {
802 dest[k*2+1] = sValues[k] & 0xff;
803 }
804 }
805 break;
806 default:
807 assert(0);
808 }
809 }
810 }
811 }
812 else {
813 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels()");
814 }
815
816 free(sValues);
817 free(zValues);
818
819 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
820
821 /* unmap the stencil buffer */
822 pipe_transfer_unmap(pipe, pt);
823 }
824
825
826 /**
827 * Get fragment program variant for a glDrawPixels or glCopyPixels
828 * command for RGBA data.
829 */
830 static struct st_fp_variant *
831 get_color_fp_variant(struct st_context *st)
832 {
833 struct gl_context *ctx = st->ctx;
834 struct st_fp_variant_key key;
835 struct st_fp_variant *fpv;
836
837 memset(&key, 0, sizeof(key));
838
839 key.st = st->has_shareable_shaders ? NULL : st;
840 key.drawpixels = 1;
841 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
842 ctx->Pixel.RedScale != 1.0 ||
843 ctx->Pixel.GreenBias != 0.0 ||
844 ctx->Pixel.GreenScale != 1.0 ||
845 ctx->Pixel.BlueBias != 0.0 ||
846 ctx->Pixel.BlueScale != 1.0 ||
847 ctx->Pixel.AlphaBias != 0.0 ||
848 ctx->Pixel.AlphaScale != 1.0);
849 key.pixelMaps = ctx->Pixel.MapColorFlag;
850 key.clamp_color = st->clamp_frag_color_in_shader &&
851 ctx->Color._ClampFragmentColor;
852
853 fpv = st_get_fp_variant(st, st->fp, &key);
854
855 return fpv;
856 }
857
858
859 /**
860 * Clamp glDrawPixels width and height to the maximum texture size.
861 */
862 static void
863 clamp_size(struct pipe_context *pipe, GLsizei *width, GLsizei *height,
864 struct gl_pixelstore_attrib *unpack)
865 {
866 const int maxSize =
867 1 << (pipe->screen->get_param(pipe->screen,
868 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
869
870 if (*width > maxSize) {
871 if (unpack->RowLength == 0)
872 unpack->RowLength = *width;
873 *width = maxSize;
874 }
875 if (*height > maxSize) {
876 *height = maxSize;
877 }
878 }
879
880
881 /**
882 * Search the array of 4 swizzle components for the named component and return
883 * its position.
884 */
885 static unsigned
886 search_swizzle(const unsigned char swizzle[4], unsigned component)
887 {
888 unsigned i;
889 for (i = 0; i < 4; i++) {
890 if (swizzle[i] == component)
891 return i;
892 }
893 assert(!"search_swizzle() failed");
894 return 0;
895 }
896
897
898 /**
899 * Set the sampler view's swizzle terms. This is used to handle RGBA
900 * swizzling when the incoming image format isn't an exact match for
901 * the actual texture format. For example, if we have glDrawPixels(
902 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
903 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
904 * avoid swizzling all the pixels in software in the texstore code.
905 */
906 static void
907 setup_sampler_swizzle(struct pipe_sampler_view *sv, GLenum format, GLenum type)
908 {
909 if ((format == GL_RGBA || format == GL_BGRA) && type == GL_UNSIGNED_BYTE) {
910 const struct util_format_description *desc =
911 util_format_description(sv->texture->format);
912 unsigned c0, c1, c2, c3;
913
914 /* Every gallium driver supports at least one 32-bit packed RGBA format.
915 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
916 */
917 assert(desc->block.bits == 32);
918
919 /* invert the format's swizzle to setup the sampler's swizzle */
920 if (format == GL_RGBA) {
921 c0 = UTIL_FORMAT_SWIZZLE_X;
922 c1 = UTIL_FORMAT_SWIZZLE_Y;
923 c2 = UTIL_FORMAT_SWIZZLE_Z;
924 c3 = UTIL_FORMAT_SWIZZLE_W;
925 }
926 else {
927 assert(format == GL_BGRA);
928 c0 = UTIL_FORMAT_SWIZZLE_Z;
929 c1 = UTIL_FORMAT_SWIZZLE_Y;
930 c2 = UTIL_FORMAT_SWIZZLE_X;
931 c3 = UTIL_FORMAT_SWIZZLE_W;
932 }
933 sv->swizzle_r = search_swizzle(desc->swizzle, c0);
934 sv->swizzle_g = search_swizzle(desc->swizzle, c1);
935 sv->swizzle_b = search_swizzle(desc->swizzle, c2);
936 sv->swizzle_a = search_swizzle(desc->swizzle, c3);
937 }
938 else {
939 /* use the default sampler swizzle */
940 }
941 }
942
943
944 /**
945 * Called via ctx->Driver.DrawPixels()
946 */
947 static void
948 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
949 GLsizei width, GLsizei height,
950 GLenum format, GLenum type,
951 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
952 {
953 void *driver_vp, *driver_fp;
954 struct st_context *st = st_context(ctx);
955 struct pipe_context *pipe = st->pipe;
956 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
957 struct pipe_sampler_view *sv[2] = { NULL };
958 int num_sampler_view = 1;
959 struct gl_pixelstore_attrib clippedUnpack;
960 struct st_fp_variant *fpv = NULL;
961 struct pipe_resource *pt;
962
963 /* Mesa state should be up to date by now */
964 assert(ctx->NewState == 0x0);
965
966 st_flush_bitmap_cache(st);
967
968 st_validate_state(st, ST_PIPELINE_RENDER);
969
970 /* Limit the size of the glDrawPixels to the max texture size.
971 * Strictly speaking, that's not correct but since we don't handle
972 * larger images yet, this is better than crashing.
973 */
974 clippedUnpack = *unpack;
975 unpack = &clippedUnpack;
976 clamp_size(st->pipe, &width, &height, &clippedUnpack);
977
978 if (format == GL_DEPTH_STENCIL)
979 write_stencil = write_depth = GL_TRUE;
980 else if (format == GL_STENCIL_INDEX)
981 write_stencil = GL_TRUE;
982 else if (format == GL_DEPTH_COMPONENT)
983 write_depth = GL_TRUE;
984
985 if (write_stencil &&
986 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
987 /* software fallback */
988 draw_stencil_pixels(ctx, x, y, width, height, format, type,
989 unpack, pixels);
990 return;
991 }
992
993 /*
994 * Get vertex/fragment shaders
995 */
996 if (write_depth || write_stencil) {
997 driver_fp = get_drawpix_z_stencil_program(st, write_depth,
998 write_stencil);
999 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1000 }
1001 else {
1002 fpv = get_color_fp_variant(st);
1003
1004 driver_fp = fpv->driver_shader;
1005 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1006
1007 if (ctx->Pixel.MapColorFlag) {
1008 pipe_sampler_view_reference(&sv[1],
1009 st->pixel_xfer.pixelmap_sampler_view);
1010 num_sampler_view++;
1011 }
1012
1013 /* compiling a new fragment shader variant added new state constants
1014 * into the constant buffer, we need to update them
1015 */
1016 st_upload_constants(st, st->fp->Base.Base.Parameters,
1017 PIPE_SHADER_FRAGMENT);
1018 }
1019
1020 /* Put glDrawPixels image into a texture */
1021 pt = make_texture(st, width, height, format, type, unpack, pixels);
1022 if (!pt) {
1023 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1024 return;
1025 }
1026
1027 /* create sampler view for the image */
1028 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1029 if (!sv[0]) {
1030 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1031 pipe_resource_reference(&pt, NULL);
1032 return;
1033 }
1034
1035 /* Set up the sampler view's swizzle */
1036 setup_sampler_swizzle(sv[0], format, type);
1037
1038 /* Create a second sampler view to read stencil. The stencil is
1039 * written using the shader stencil export functionality.
1040 */
1041 if (write_stencil) {
1042 enum pipe_format stencil_format =
1043 util_format_stencil_only(pt->format);
1044 /* we should not be doing pixel map/transfer (see above) */
1045 assert(num_sampler_view == 1);
1046 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1047 stencil_format);
1048 if (!sv[1]) {
1049 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1050 pipe_resource_reference(&pt, NULL);
1051 pipe_sampler_view_reference(&sv[0], NULL);
1052 return;
1053 }
1054 num_sampler_view++;
1055 }
1056
1057 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1058 width, height,
1059 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1060 sv,
1061 num_sampler_view,
1062 driver_vp,
1063 driver_fp, fpv,
1064 ctx->Current.RasterColor,
1065 GL_FALSE, write_depth, write_stencil);
1066 pipe_sampler_view_reference(&sv[0], NULL);
1067 if (num_sampler_view > 1)
1068 pipe_sampler_view_reference(&sv[1], NULL);
1069
1070 pipe_resource_reference(&pt, NULL);
1071 }
1072
1073
1074
1075 /**
1076 * Software fallback for glCopyPixels(GL_STENCIL).
1077 */
1078 static void
1079 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1080 GLsizei width, GLsizei height,
1081 GLint dstx, GLint dsty)
1082 {
1083 struct st_renderbuffer *rbDraw;
1084 struct pipe_context *pipe = st_context(ctx)->pipe;
1085 enum pipe_transfer_usage usage;
1086 struct pipe_transfer *ptDraw;
1087 ubyte *drawMap;
1088 ubyte *buffer;
1089 int i;
1090
1091 buffer = malloc(width * height * sizeof(ubyte));
1092 if (!buffer) {
1093 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1094 return;
1095 }
1096
1097 /* Get the dest renderbuffer */
1098 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1099 Attachment[BUFFER_STENCIL].Renderbuffer);
1100
1101 /* this will do stencil pixel transfer ops */
1102 _mesa_readpixels(ctx, srcx, srcy, width, height,
1103 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1104 &ctx->DefaultPacking, buffer);
1105
1106 if (0) {
1107 /* debug code: dump stencil values */
1108 GLint row, col;
1109 for (row = 0; row < height; row++) {
1110 printf("%3d: ", row);
1111 for (col = 0; col < width; col++) {
1112 printf("%02x ", buffer[col + row * width]);
1113 }
1114 printf("\n");
1115 }
1116 }
1117
1118 if (_mesa_is_format_packed_depth_stencil(rbDraw->Base.Format))
1119 usage = PIPE_TRANSFER_READ_WRITE;
1120 else
1121 usage = PIPE_TRANSFER_WRITE;
1122
1123 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1124 dsty = rbDraw->Base.Height - dsty - height;
1125 }
1126
1127 assert(util_format_get_blockwidth(rbDraw->texture->format) == 1);
1128 assert(util_format_get_blockheight(rbDraw->texture->format) == 1);
1129
1130 /* map the stencil buffer */
1131 drawMap = pipe_transfer_map(pipe,
1132 rbDraw->texture,
1133 rbDraw->surface->u.tex.level,
1134 rbDraw->surface->u.tex.first_layer,
1135 usage, dstx, dsty,
1136 width, height, &ptDraw);
1137
1138 /* draw */
1139 /* XXX PixelZoom not handled yet */
1140 for (i = 0; i < height; i++) {
1141 ubyte *dst;
1142 const ubyte *src;
1143 int y;
1144
1145 y = i;
1146
1147 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1148 y = height - y - 1;
1149 }
1150
1151 dst = drawMap + y * ptDraw->stride;
1152 src = buffer + i * width;
1153
1154 _mesa_pack_ubyte_stencil_row(rbDraw->Base.Format, width, src, dst);
1155 }
1156
1157 free(buffer);
1158
1159 /* unmap the stencil buffer */
1160 pipe_transfer_unmap(pipe, ptDraw);
1161 }
1162
1163
1164 /**
1165 * Return renderbuffer to use for reading color pixels for glCopyPixels
1166 */
1167 static struct st_renderbuffer *
1168 st_get_color_read_renderbuffer(struct gl_context *ctx)
1169 {
1170 struct gl_framebuffer *fb = ctx->ReadBuffer;
1171 struct st_renderbuffer *strb =
1172 st_renderbuffer(fb->_ColorReadBuffer);
1173
1174 return strb;
1175 }
1176
1177
1178 /**
1179 * Try to do a glCopyPixels for simple cases with a blit by calling
1180 * pipe->blit().
1181 *
1182 * We can do this when we're copying color pixels (depth/stencil
1183 * eventually) with no pixel zoom, no pixel transfer ops, no
1184 * per-fragment ops, and the src/dest regions don't overlap.
1185 */
1186 static GLboolean
1187 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1188 GLsizei width, GLsizei height,
1189 GLint dstx, GLint dsty, GLenum type)
1190 {
1191 struct st_context *st = st_context(ctx);
1192 struct pipe_context *pipe = st->pipe;
1193 struct pipe_screen *screen = pipe->screen;
1194 struct gl_pixelstore_attrib pack, unpack;
1195 GLint readX, readY, readW, readH, drawX, drawY, drawW, drawH;
1196
1197 if (type == GL_COLOR &&
1198 ctx->Pixel.ZoomX == 1.0 &&
1199 ctx->Pixel.ZoomY == 1.0 &&
1200 ctx->_ImageTransferState == 0x0 &&
1201 !ctx->Color.BlendEnabled &&
1202 !ctx->Color.AlphaEnabled &&
1203 (!ctx->Color.ColorLogicOpEnabled || ctx->Color.LogicOp == GL_COPY) &&
1204 !ctx->Depth.Test &&
1205 !ctx->Fog.Enabled &&
1206 !ctx->Stencil.Enabled &&
1207 !ctx->FragmentProgram.Enabled &&
1208 !ctx->VertexProgram.Enabled &&
1209 !ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT] &&
1210 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1211 !ctx->Query.CondRenderQuery &&
1212 !ctx->Query.CurrentOcclusionObject) {
1213 struct st_renderbuffer *rbRead, *rbDraw;
1214
1215 /*
1216 * Clip the read region against the src buffer bounds.
1217 * We'll still allocate a temporary buffer/texture for the original
1218 * src region size but we'll only read the region which is on-screen.
1219 * This may mean that we draw garbage pixels into the dest region, but
1220 * that's expected.
1221 */
1222 readX = srcx;
1223 readY = srcy;
1224 readW = width;
1225 readH = height;
1226 pack = ctx->DefaultPacking;
1227 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1228 return GL_TRUE; /* all done */
1229
1230 /* clip against dest buffer bounds and scissor box */
1231 drawX = dstx + pack.SkipPixels;
1232 drawY = dsty + pack.SkipRows;
1233 unpack = pack;
1234 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1235 return GL_TRUE; /* all done */
1236
1237 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1238 readY = readY - pack.SkipRows + unpack.SkipRows;
1239
1240 drawW = readW;
1241 drawH = readH;
1242
1243 rbRead = st_get_color_read_renderbuffer(ctx);
1244 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1245
1246 /* Flip src/dst position depending on the orientation of buffers. */
1247 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1248 readY = rbRead->Base.Height - readY;
1249 readH = -readH;
1250 }
1251
1252 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1253 /* We can't flip the destination for pipe->blit, so we only adjust
1254 * its position and flip the source.
1255 */
1256 drawY = rbDraw->Base.Height - drawY - drawH;
1257 readY += readH;
1258 readH = -readH;
1259 }
1260
1261 if (rbRead != rbDraw ||
1262 !_mesa_regions_overlap(readX, readY, readX + readW, readY + readH,
1263 drawX, drawY, drawX + drawW, drawY + drawH)) {
1264 struct pipe_blit_info blit;
1265
1266 memset(&blit, 0, sizeof(blit));
1267 blit.src.resource = rbRead->texture;
1268 blit.src.level = rbRead->surface->u.tex.level;
1269 blit.src.format = rbRead->texture->format;
1270 blit.src.box.x = readX;
1271 blit.src.box.y = readY;
1272 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1273 blit.src.box.width = readW;
1274 blit.src.box.height = readH;
1275 blit.src.box.depth = 1;
1276 blit.dst.resource = rbDraw->texture;
1277 blit.dst.level = rbDraw->surface->u.tex.level;
1278 blit.dst.format = rbDraw->texture->format;
1279 blit.dst.box.x = drawX;
1280 blit.dst.box.y = drawY;
1281 blit.dst.box.z = rbDraw->surface->u.tex.first_layer;
1282 blit.dst.box.width = drawW;
1283 blit.dst.box.height = drawH;
1284 blit.dst.box.depth = 1;
1285 blit.mask = PIPE_MASK_RGBA;
1286 blit.filter = PIPE_TEX_FILTER_NEAREST;
1287
1288 if (screen->is_format_supported(screen, blit.src.format,
1289 blit.src.resource->target,
1290 blit.src.resource->nr_samples,
1291 PIPE_BIND_SAMPLER_VIEW) &&
1292 screen->is_format_supported(screen, blit.dst.format,
1293 blit.dst.resource->target,
1294 blit.dst.resource->nr_samples,
1295 PIPE_BIND_RENDER_TARGET)) {
1296 pipe->blit(pipe, &blit);
1297 return GL_TRUE;
1298 }
1299 }
1300 }
1301
1302 return GL_FALSE;
1303 }
1304
1305
1306 static void
1307 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1308 GLsizei width, GLsizei height,
1309 GLint dstx, GLint dsty, GLenum type)
1310 {
1311 struct st_context *st = st_context(ctx);
1312 struct pipe_context *pipe = st->pipe;
1313 struct pipe_screen *screen = pipe->screen;
1314 struct st_renderbuffer *rbRead;
1315 void *driver_vp, *driver_fp;
1316 struct pipe_resource *pt;
1317 struct pipe_sampler_view *sv[2] = { NULL };
1318 struct st_fp_variant *fpv = NULL;
1319 int num_sampler_view = 1;
1320 enum pipe_format srcFormat;
1321 unsigned srcBind;
1322 GLboolean invertTex = GL_FALSE;
1323 GLint readX, readY, readW, readH;
1324 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1325
1326 st_flush_bitmap_cache(st);
1327
1328 st_validate_state(st, ST_PIPELINE_RENDER);
1329
1330 if (type == GL_DEPTH_STENCIL) {
1331 /* XXX make this more efficient */
1332 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1333 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1334 return;
1335 }
1336
1337 if (type == GL_STENCIL) {
1338 /* can't use texturing to do stencil */
1339 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1340 return;
1341 }
1342
1343 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1344 return;
1345
1346 /*
1347 * The subsequent code implements glCopyPixels by copying the source
1348 * pixels into a temporary texture that's then applied to a textured quad.
1349 * When we draw the textured quad, all the usual per-fragment operations
1350 * are handled.
1351 */
1352
1353
1354 /*
1355 * Get vertex/fragment shaders
1356 */
1357 if (type == GL_COLOR) {
1358 fpv = get_color_fp_variant(st);
1359
1360 rbRead = st_get_color_read_renderbuffer(ctx);
1361
1362 driver_fp = fpv->driver_shader;
1363 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1364
1365 if (ctx->Pixel.MapColorFlag) {
1366 pipe_sampler_view_reference(&sv[1],
1367 st->pixel_xfer.pixelmap_sampler_view);
1368 num_sampler_view++;
1369 }
1370
1371 /* compiling a new fragment shader variant added new state constants
1372 * into the constant buffer, we need to update them
1373 */
1374 st_upload_constants(st, st->fp->Base.Base.Parameters,
1375 PIPE_SHADER_FRAGMENT);
1376 }
1377 else {
1378 assert(type == GL_DEPTH);
1379 rbRead = st_renderbuffer(ctx->ReadBuffer->
1380 Attachment[BUFFER_DEPTH].Renderbuffer);
1381
1382 driver_fp = get_drawpix_z_stencil_program(st, GL_TRUE, GL_FALSE);
1383 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1384 }
1385
1386 /* Choose the format for the temporary texture. */
1387 srcFormat = rbRead->texture->format;
1388 srcBind = PIPE_BIND_SAMPLER_VIEW |
1389 (type == GL_COLOR ? PIPE_BIND_RENDER_TARGET : PIPE_BIND_DEPTH_STENCIL);
1390
1391 if (!screen->is_format_supported(screen, srcFormat, st->internal_target, 0,
1392 srcBind)) {
1393 /* srcFormat is non-renderable. Find a compatible renderable format. */
1394 if (type == GL_DEPTH) {
1395 srcFormat = st_choose_format(st, GL_DEPTH_COMPONENT, GL_NONE,
1396 GL_NONE, st->internal_target, 0,
1397 srcBind, FALSE);
1398 }
1399 else {
1400 assert(type == GL_COLOR);
1401
1402 if (util_format_is_float(srcFormat)) {
1403 srcFormat = st_choose_format(st, GL_RGBA32F, GL_NONE,
1404 GL_NONE, st->internal_target, 0,
1405 srcBind, FALSE);
1406 }
1407 else if (util_format_is_pure_sint(srcFormat)) {
1408 srcFormat = st_choose_format(st, GL_RGBA32I, GL_NONE,
1409 GL_NONE, st->internal_target, 0,
1410 srcBind, FALSE);
1411 }
1412 else if (util_format_is_pure_uint(srcFormat)) {
1413 srcFormat = st_choose_format(st, GL_RGBA32UI, GL_NONE,
1414 GL_NONE, st->internal_target, 0,
1415 srcBind, FALSE);
1416 }
1417 else if (util_format_is_snorm(srcFormat)) {
1418 srcFormat = st_choose_format(st, GL_RGBA16_SNORM, GL_NONE,
1419 GL_NONE, st->internal_target, 0,
1420 srcBind, FALSE);
1421 }
1422 else {
1423 srcFormat = st_choose_format(st, GL_RGBA, GL_NONE,
1424 GL_NONE, st->internal_target, 0,
1425 srcBind, FALSE);
1426 }
1427 }
1428
1429 if (srcFormat == PIPE_FORMAT_NONE) {
1430 assert(0 && "cannot choose a format for src of CopyPixels");
1431 return;
1432 }
1433 }
1434
1435 /* Invert src region if needed */
1436 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1437 srcy = ctx->ReadBuffer->Height - srcy - height;
1438 invertTex = !invertTex;
1439 }
1440
1441 /* Clip the read region against the src buffer bounds.
1442 * We'll still allocate a temporary buffer/texture for the original
1443 * src region size but we'll only read the region which is on-screen.
1444 * This may mean that we draw garbage pixels into the dest region, but
1445 * that's expected.
1446 */
1447 readX = srcx;
1448 readY = srcy;
1449 readW = width;
1450 readH = height;
1451 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1452 /* The source region is completely out of bounds. Do nothing.
1453 * The GL spec says "Results of copies from outside the window,
1454 * or from regions of the window that are not exposed, are
1455 * hardware dependent and undefined."
1456 */
1457 return;
1458 }
1459
1460 readW = MAX2(0, readW);
1461 readH = MAX2(0, readH);
1462
1463 /* Allocate the temporary texture. */
1464 pt = alloc_texture(st, width, height, srcFormat, srcBind);
1465 if (!pt)
1466 return;
1467
1468 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1469 if (!sv[0]) {
1470 pipe_resource_reference(&pt, NULL);
1471 return;
1472 }
1473
1474 /* Copy the src region to the temporary texture. */
1475 {
1476 struct pipe_blit_info blit;
1477
1478 memset(&blit, 0, sizeof(blit));
1479 blit.src.resource = rbRead->texture;
1480 blit.src.level = rbRead->surface->u.tex.level;
1481 blit.src.format = rbRead->texture->format;
1482 blit.src.box.x = readX;
1483 blit.src.box.y = readY;
1484 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1485 blit.src.box.width = readW;
1486 blit.src.box.height = readH;
1487 blit.src.box.depth = 1;
1488 blit.dst.resource = pt;
1489 blit.dst.level = 0;
1490 blit.dst.format = pt->format;
1491 blit.dst.box.x = pack.SkipPixels;
1492 blit.dst.box.y = pack.SkipRows;
1493 blit.dst.box.z = 0;
1494 blit.dst.box.width = readW;
1495 blit.dst.box.height = readH;
1496 blit.dst.box.depth = 1;
1497 blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_S;
1498 blit.filter = PIPE_TEX_FILTER_NEAREST;
1499
1500 pipe->blit(pipe, &blit);
1501 }
1502
1503 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1504 * textured quad with that texture.
1505 */
1506 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1507 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1508 sv,
1509 num_sampler_view,
1510 driver_vp,
1511 driver_fp, fpv,
1512 ctx->Current.Attrib[VERT_ATTRIB_COLOR0],
1513 invertTex, GL_FALSE, GL_FALSE);
1514
1515 pipe_resource_reference(&pt, NULL);
1516 pipe_sampler_view_reference(&sv[0], NULL);
1517 }
1518
1519
1520
1521 void st_init_drawpixels_functions(struct dd_function_table *functions)
1522 {
1523 functions->DrawPixels = st_DrawPixels;
1524 functions->CopyPixels = st_CopyPixels;
1525 }
1526
1527
1528 void
1529 st_destroy_drawpix(struct st_context *st)
1530 {
1531 GLuint i;
1532
1533 for (i = 0; i < ARRAY_SIZE(st->drawpix.zs_shaders); i++) {
1534 if (st->drawpix.zs_shaders[i])
1535 cso_delete_fragment_shader(st->cso_context,
1536 st->drawpix.zs_shaders[i]);
1537 }
1538
1539 if (st->drawpix.vert_shaders[0])
1540 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]);
1541 if (st->drawpix.vert_shaders[1])
1542 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]);
1543 }