st/mesa: use _mesa_geometric_width/height() in glDrawPixels code
[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_FRAGMENT_SHADER |
483 CSO_BIT_STREAM_OUTPUTS |
484 CSO_BIT_VERTEX_SHADER |
485 CSO_BIT_TESSCTRL_SHADER |
486 CSO_BIT_TESSEVAL_SHADER |
487 CSO_BIT_GEOMETRY_SHADER |
488 CSO_BIT_VERTEX_ELEMENTS |
489 CSO_BIT_AUX_VERTEX_BUFFER_SLOT);
490 if (write_stencil) {
491 cso_state_mask |= (CSO_BIT_DEPTH_STENCIL_ALPHA |
492 CSO_BIT_BLEND);
493 }
494 cso_save_state(cso, cso_state_mask);
495
496 /* rasterizer state: just scissor */
497 {
498 struct pipe_rasterizer_state rasterizer;
499 memset(&rasterizer, 0, sizeof(rasterizer));
500 rasterizer.clamp_fragment_color = !st->clamp_frag_color_in_shader &&
501 ctx->Color._ClampFragmentColor;
502 rasterizer.half_pixel_center = 1;
503 rasterizer.bottom_edge_rule = 1;
504 rasterizer.depth_clip = !ctx->Transform.DepthClamp;
505 rasterizer.scissor = ctx->Scissor.EnableFlags;
506 cso_set_rasterizer(cso, &rasterizer);
507 }
508
509 if (write_stencil) {
510 /* Stencil writing bypasses the normal fragment pipeline to
511 * disable color writing and set stencil test to always pass.
512 */
513 struct pipe_depth_stencil_alpha_state dsa;
514 struct pipe_blend_state blend;
515
516 /* depth/stencil */
517 memset(&dsa, 0, sizeof(dsa));
518 dsa.stencil[0].enabled = 1;
519 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
520 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
521 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
522 if (write_depth) {
523 /* writing depth+stencil: depth test always passes */
524 dsa.depth.enabled = 1;
525 dsa.depth.writemask = ctx->Depth.Mask;
526 dsa.depth.func = PIPE_FUNC_ALWAYS;
527 }
528 cso_set_depth_stencil_alpha(cso, &dsa);
529
530 /* blend (colormask) */
531 memset(&blend, 0, sizeof(blend));
532 cso_set_blend(cso, &blend);
533 }
534
535 /* fragment shader state: TEX lookup program */
536 cso_set_fragment_shader_handle(cso, driver_fp);
537
538 /* vertex shader state: position + texcoord pass-through */
539 cso_set_vertex_shader_handle(cso, driver_vp);
540
541 /* disable other shaders */
542 cso_set_tessctrl_shader_handle(cso, NULL);
543 cso_set_tesseval_shader_handle(cso, NULL);
544 cso_set_geometry_shader_handle(cso, NULL);
545
546 /* user samplers, plus the drawpix samplers */
547 {
548 struct pipe_sampler_state sampler;
549
550 memset(&sampler, 0, sizeof(sampler));
551 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
552 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
553 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
554 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
555 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
556 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
557 sampler.normalized_coords = normalized;
558
559 if (fpv) {
560 /* drawing a color image */
561 const struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
562 uint num = MAX3(fpv->drawpix_sampler + 1,
563 fpv->pixelmap_sampler + 1,
564 st->state.num_samplers[PIPE_SHADER_FRAGMENT]);
565 uint i;
566
567 for (i = 0; i < st->state.num_samplers[PIPE_SHADER_FRAGMENT]; i++)
568 samplers[i] = &st->state.samplers[PIPE_SHADER_FRAGMENT][i];
569
570 samplers[fpv->drawpix_sampler] = &sampler;
571 if (sv[1])
572 samplers[fpv->pixelmap_sampler] = &sampler;
573
574 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num, samplers);
575 } else {
576 /* drawing a depth/stencil image */
577 const struct pipe_sampler_state *samplers[2] = {&sampler, &sampler};
578
579 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num_sampler_view, samplers);
580 }
581 }
582
583 /* user textures, plus the drawpix textures */
584 if (fpv) {
585 /* drawing a color image */
586 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
587 uint num = MAX3(fpv->drawpix_sampler + 1,
588 fpv->pixelmap_sampler + 1,
589 st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
590
591 memcpy(sampler_views, st->state.sampler_views[PIPE_SHADER_FRAGMENT],
592 sizeof(sampler_views));
593
594 sampler_views[fpv->drawpix_sampler] = sv[0];
595 if (sv[1])
596 sampler_views[fpv->pixelmap_sampler] = sv[1];
597 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num, sampler_views);
598 } else {
599 /* drawing a depth/stencil image */
600 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num_sampler_view, sv);
601 }
602
603 /* viewport state: viewport matching window dims */
604 cso_set_viewport_dims(cso, fb_width, fb_height, TRUE);
605
606 cso_set_vertex_elements(cso, 3, st->util_velems);
607 cso_set_stream_outputs(cso, 0, NULL, NULL);
608
609 /* Compute Gallium window coords (y=0=top) with pixel zoom.
610 * Recall that these coords are transformed by the current
611 * vertex shader and viewport transformation.
612 */
613 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
614 y = fb_height - (int) (y + height * ctx->Pixel.ZoomY);
615 invertTex = !invertTex;
616 }
617
618 x0 = (GLfloat) x;
619 x1 = x + width * ctx->Pixel.ZoomX;
620 y0 = (GLfloat) y;
621 y1 = y + height * ctx->Pixel.ZoomY;
622
623 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
624 z = z * 2.0f - 1.0f;
625
626 {
627 const float clip_x0 = x0 / (float) fb_width * 2.0f - 1.0f;
628 const float clip_y0 = y0 / (float) fb_height * 2.0f - 1.0f;
629 const float clip_x1 = x1 / (float) fb_width * 2.0f - 1.0f;
630 const float clip_y1 = y1 / (float) fb_height * 2.0f - 1.0f;
631 const float maxXcoord = normalized ?
632 ((float) width / sv[0]->texture->width0) : (float) width;
633 const float maxYcoord = normalized
634 ? ((float) height / sv[0]->texture->height0) : (float) height;
635 const float sLeft = 0.0f, sRight = maxXcoord;
636 const float tTop = invertTex ? maxYcoord : 0.0f;
637 const float tBot = invertTex ? 0.0f : maxYcoord;
638
639 if (!st_draw_quad(ctx->st, clip_x0, clip_y0, clip_x1, clip_y1, z,
640 sLeft, tBot, sRight, tTop, color, 0)) {
641 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
642 }
643 }
644
645 /* restore state */
646 cso_restore_state(cso);
647 }
648
649
650 /**
651 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
652 * can't use a fragment shader to write stencil values.
653 */
654 static void
655 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
656 GLsizei width, GLsizei height, GLenum format, GLenum type,
657 const struct gl_pixelstore_attrib *unpack,
658 const GLvoid *pixels)
659 {
660 struct st_context *st = st_context(ctx);
661 struct pipe_context *pipe = st->pipe;
662 struct st_renderbuffer *strb;
663 enum pipe_transfer_usage usage;
664 struct pipe_transfer *pt;
665 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
666 ubyte *stmap;
667 struct gl_pixelstore_attrib clippedUnpack = *unpack;
668 GLubyte *sValues;
669 GLuint *zValues;
670
671 if (!zoom) {
672 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
673 &clippedUnpack)) {
674 /* totally clipped */
675 return;
676 }
677 }
678
679 strb = st_renderbuffer(ctx->DrawBuffer->
680 Attachment[BUFFER_STENCIL].Renderbuffer);
681
682 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
683 y = ctx->DrawBuffer->Height - y - height;
684 }
685
686 if (format == GL_STENCIL_INDEX &&
687 _mesa_is_format_packed_depth_stencil(strb->Base.Format)) {
688 /* writing stencil to a combined depth+stencil buffer */
689 usage = PIPE_TRANSFER_READ_WRITE;
690 }
691 else {
692 usage = PIPE_TRANSFER_WRITE;
693 }
694
695 stmap = pipe_transfer_map(pipe, strb->texture,
696 strb->surface->u.tex.level,
697 strb->surface->u.tex.first_layer,
698 usage, x, y,
699 width, height, &pt);
700
701 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
702 assert(pixels);
703
704 sValues = malloc(width * sizeof(GLubyte));
705 zValues = malloc(width * sizeof(GLuint));
706
707 if (sValues && zValues) {
708 GLint row;
709 for (row = 0; row < height; row++) {
710 GLfloat *zValuesFloat = (GLfloat*)zValues;
711 GLenum destType = GL_UNSIGNED_BYTE;
712 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
713 width, height,
714 format, type,
715 row, 0);
716 _mesa_unpack_stencil_span(ctx, width, destType, sValues,
717 type, source, &clippedUnpack,
718 ctx->_ImageTransferState);
719
720 if (format == GL_DEPTH_STENCIL) {
721 GLenum ztype =
722 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
723 GL_FLOAT : GL_UNSIGNED_INT;
724
725 _mesa_unpack_depth_span(ctx, width, ztype, zValues,
726 (1 << 24) - 1, type, source,
727 &clippedUnpack);
728 }
729
730 if (zoom) {
731 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
732 "zoom not complete");
733 }
734
735 {
736 GLint spanY;
737
738 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
739 spanY = height - row - 1;
740 }
741 else {
742 spanY = row;
743 }
744
745 /* now pack the stencil (and Z) values in the dest format */
746 switch (pt->resource->format) {
747 case PIPE_FORMAT_S8_UINT:
748 {
749 ubyte *dest = stmap + spanY * pt->stride;
750 assert(usage == PIPE_TRANSFER_WRITE);
751 memcpy(dest, sValues, width);
752 }
753 break;
754 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
755 if (format == GL_DEPTH_STENCIL) {
756 uint *dest = (uint *) (stmap + spanY * pt->stride);
757 GLint k;
758 assert(usage == PIPE_TRANSFER_WRITE);
759 for (k = 0; k < width; k++) {
760 dest[k] = zValues[k] | (sValues[k] << 24);
761 }
762 }
763 else {
764 uint *dest = (uint *) (stmap + spanY * pt->stride);
765 GLint k;
766 assert(usage == PIPE_TRANSFER_READ_WRITE);
767 for (k = 0; k < width; k++) {
768 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
769 }
770 }
771 break;
772 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
773 if (format == GL_DEPTH_STENCIL) {
774 uint *dest = (uint *) (stmap + spanY * pt->stride);
775 GLint k;
776 assert(usage == PIPE_TRANSFER_WRITE);
777 for (k = 0; k < width; k++) {
778 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
779 }
780 }
781 else {
782 uint *dest = (uint *) (stmap + spanY * pt->stride);
783 GLint k;
784 assert(usage == PIPE_TRANSFER_READ_WRITE);
785 for (k = 0; k < width; k++) {
786 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
787 }
788 }
789 break;
790 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
791 if (format == GL_DEPTH_STENCIL) {
792 uint *dest = (uint *) (stmap + spanY * pt->stride);
793 GLfloat *destf = (GLfloat*)dest;
794 GLint k;
795 assert(usage == PIPE_TRANSFER_WRITE);
796 for (k = 0; k < width; k++) {
797 destf[k*2] = zValuesFloat[k];
798 dest[k*2+1] = sValues[k] & 0xff;
799 }
800 }
801 else {
802 uint *dest = (uint *) (stmap + spanY * pt->stride);
803 GLint k;
804 assert(usage == PIPE_TRANSFER_READ_WRITE);
805 for (k = 0; k < width; k++) {
806 dest[k*2+1] = sValues[k] & 0xff;
807 }
808 }
809 break;
810 default:
811 assert(0);
812 }
813 }
814 }
815 }
816 else {
817 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels()");
818 }
819
820 free(sValues);
821 free(zValues);
822
823 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
824
825 /* unmap the stencil buffer */
826 pipe_transfer_unmap(pipe, pt);
827 }
828
829
830 /**
831 * Get fragment program variant for a glDrawPixels or glCopyPixels
832 * command for RGBA data.
833 */
834 static struct st_fp_variant *
835 get_color_fp_variant(struct st_context *st)
836 {
837 struct gl_context *ctx = st->ctx;
838 struct st_fp_variant_key key;
839 struct st_fp_variant *fpv;
840
841 memset(&key, 0, sizeof(key));
842
843 key.st = st->has_shareable_shaders ? NULL : st;
844 key.drawpixels = 1;
845 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
846 ctx->Pixel.RedScale != 1.0 ||
847 ctx->Pixel.GreenBias != 0.0 ||
848 ctx->Pixel.GreenScale != 1.0 ||
849 ctx->Pixel.BlueBias != 0.0 ||
850 ctx->Pixel.BlueScale != 1.0 ||
851 ctx->Pixel.AlphaBias != 0.0 ||
852 ctx->Pixel.AlphaScale != 1.0);
853 key.pixelMaps = ctx->Pixel.MapColorFlag;
854 key.clamp_color = st->clamp_frag_color_in_shader &&
855 st->ctx->Color._ClampFragmentColor;
856
857 fpv = st_get_fp_variant(st, st->fp, &key);
858
859 return fpv;
860 }
861
862
863 /**
864 * Clamp glDrawPixels width and height to the maximum texture size.
865 */
866 static void
867 clamp_size(struct pipe_context *pipe, GLsizei *width, GLsizei *height,
868 struct gl_pixelstore_attrib *unpack)
869 {
870 const int maxSize =
871 1 << (pipe->screen->get_param(pipe->screen,
872 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
873
874 if (*width > maxSize) {
875 if (unpack->RowLength == 0)
876 unpack->RowLength = *width;
877 *width = maxSize;
878 }
879 if (*height > maxSize) {
880 *height = maxSize;
881 }
882 }
883
884
885 /**
886 * Search the array of 4 swizzle components for the named component and return
887 * its position.
888 */
889 static unsigned
890 search_swizzle(const unsigned char swizzle[4], unsigned component)
891 {
892 unsigned i;
893 for (i = 0; i < 4; i++) {
894 if (swizzle[i] == component)
895 return i;
896 }
897 assert(!"search_swizzle() failed");
898 return 0;
899 }
900
901
902 /**
903 * Set the sampler view's swizzle terms. This is used to handle RGBA
904 * swizzling when the incoming image format isn't an exact match for
905 * the actual texture format. For example, if we have glDrawPixels(
906 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
907 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
908 * avoid swizzling all the pixels in software in the texstore code.
909 */
910 static void
911 setup_sampler_swizzle(struct pipe_sampler_view *sv, GLenum format, GLenum type)
912 {
913 if ((format == GL_RGBA || format == GL_BGRA) && type == GL_UNSIGNED_BYTE) {
914 const struct util_format_description *desc =
915 util_format_description(sv->texture->format);
916 unsigned c0, c1, c2, c3;
917
918 /* Every gallium driver supports at least one 32-bit packed RGBA format.
919 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
920 */
921 assert(desc->block.bits == 32);
922
923 /* invert the format's swizzle to setup the sampler's swizzle */
924 if (format == GL_RGBA) {
925 c0 = UTIL_FORMAT_SWIZZLE_X;
926 c1 = UTIL_FORMAT_SWIZZLE_Y;
927 c2 = UTIL_FORMAT_SWIZZLE_Z;
928 c3 = UTIL_FORMAT_SWIZZLE_W;
929 }
930 else {
931 assert(format == GL_BGRA);
932 c0 = UTIL_FORMAT_SWIZZLE_Z;
933 c1 = UTIL_FORMAT_SWIZZLE_Y;
934 c2 = UTIL_FORMAT_SWIZZLE_X;
935 c3 = UTIL_FORMAT_SWIZZLE_W;
936 }
937 sv->swizzle_r = search_swizzle(desc->swizzle, c0);
938 sv->swizzle_g = search_swizzle(desc->swizzle, c1);
939 sv->swizzle_b = search_swizzle(desc->swizzle, c2);
940 sv->swizzle_a = search_swizzle(desc->swizzle, c3);
941 }
942 else {
943 /* use the default sampler swizzle */
944 }
945 }
946
947
948 /**
949 * Called via ctx->Driver.DrawPixels()
950 */
951 static void
952 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
953 GLsizei width, GLsizei height,
954 GLenum format, GLenum type,
955 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
956 {
957 void *driver_vp, *driver_fp;
958 struct st_context *st = st_context(ctx);
959 struct pipe_context *pipe = st->pipe;
960 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
961 struct pipe_sampler_view *sv[2] = { NULL };
962 int num_sampler_view = 1;
963 struct gl_pixelstore_attrib clippedUnpack;
964 struct st_fp_variant *fpv = NULL;
965 struct pipe_resource *pt;
966
967 /* Mesa state should be up to date by now */
968 assert(ctx->NewState == 0x0);
969
970 st_flush_bitmap_cache(st);
971
972 st_validate_state(st, ST_PIPELINE_RENDER);
973
974 /* Limit the size of the glDrawPixels to the max texture size.
975 * Strictly speaking, that's not correct but since we don't handle
976 * larger images yet, this is better than crashing.
977 */
978 clippedUnpack = *unpack;
979 unpack = &clippedUnpack;
980 clamp_size(st->pipe, &width, &height, &clippedUnpack);
981
982 if (format == GL_DEPTH_STENCIL)
983 write_stencil = write_depth = GL_TRUE;
984 else if (format == GL_STENCIL_INDEX)
985 write_stencil = GL_TRUE;
986 else if (format == GL_DEPTH_COMPONENT)
987 write_depth = GL_TRUE;
988
989 if (write_stencil &&
990 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
991 /* software fallback */
992 draw_stencil_pixels(ctx, x, y, width, height, format, type,
993 unpack, pixels);
994 return;
995 }
996
997 /*
998 * Get vertex/fragment shaders
999 */
1000 if (write_depth || write_stencil) {
1001 driver_fp = get_drawpix_z_stencil_program(st, write_depth,
1002 write_stencil);
1003 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1004 }
1005 else {
1006 fpv = get_color_fp_variant(st);
1007
1008 driver_fp = fpv->driver_shader;
1009 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1010
1011 if (ctx->Pixel.MapColorFlag) {
1012 pipe_sampler_view_reference(&sv[1],
1013 st->pixel_xfer.pixelmap_sampler_view);
1014 num_sampler_view++;
1015 }
1016
1017 /* compiling a new fragment shader variant added new state constants
1018 * into the constant buffer, we need to update them
1019 */
1020 st_upload_constants(st, st->fp->Base.Base.Parameters,
1021 PIPE_SHADER_FRAGMENT);
1022 }
1023
1024 /* Put glDrawPixels image into a texture */
1025 pt = make_texture(st, width, height, format, type, unpack, pixels);
1026 if (!pt) {
1027 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1028 return;
1029 }
1030
1031 /* create sampler view for the image */
1032 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1033 if (!sv[0]) {
1034 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1035 pipe_resource_reference(&pt, NULL);
1036 return;
1037 }
1038
1039 /* Set up the sampler view's swizzle */
1040 setup_sampler_swizzle(sv[0], format, type);
1041
1042 /* Create a second sampler view to read stencil. The stencil is
1043 * written using the shader stencil export functionality.
1044 */
1045 if (write_stencil) {
1046 enum pipe_format stencil_format =
1047 util_format_stencil_only(pt->format);
1048 /* we should not be doing pixel map/transfer (see above) */
1049 assert(num_sampler_view == 1);
1050 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1051 stencil_format);
1052 if (!sv[1]) {
1053 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1054 pipe_resource_reference(&pt, NULL);
1055 pipe_sampler_view_reference(&sv[0], NULL);
1056 return;
1057 }
1058 num_sampler_view++;
1059 }
1060
1061 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1062 width, height,
1063 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1064 sv,
1065 num_sampler_view,
1066 driver_vp,
1067 driver_fp, fpv,
1068 ctx->Current.RasterColor,
1069 GL_FALSE, write_depth, write_stencil);
1070 pipe_sampler_view_reference(&sv[0], NULL);
1071 if (num_sampler_view > 1)
1072 pipe_sampler_view_reference(&sv[1], NULL);
1073
1074 pipe_resource_reference(&pt, NULL);
1075 }
1076
1077
1078
1079 /**
1080 * Software fallback for glCopyPixels(GL_STENCIL).
1081 */
1082 static void
1083 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1084 GLsizei width, GLsizei height,
1085 GLint dstx, GLint dsty)
1086 {
1087 struct st_renderbuffer *rbDraw;
1088 struct pipe_context *pipe = st_context(ctx)->pipe;
1089 enum pipe_transfer_usage usage;
1090 struct pipe_transfer *ptDraw;
1091 ubyte *drawMap;
1092 ubyte *buffer;
1093 int i;
1094
1095 buffer = malloc(width * height * sizeof(ubyte));
1096 if (!buffer) {
1097 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1098 return;
1099 }
1100
1101 /* Get the dest renderbuffer */
1102 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1103 Attachment[BUFFER_STENCIL].Renderbuffer);
1104
1105 /* this will do stencil pixel transfer ops */
1106 _mesa_readpixels(ctx, srcx, srcy, width, height,
1107 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1108 &ctx->DefaultPacking, buffer);
1109
1110 if (0) {
1111 /* debug code: dump stencil values */
1112 GLint row, col;
1113 for (row = 0; row < height; row++) {
1114 printf("%3d: ", row);
1115 for (col = 0; col < width; col++) {
1116 printf("%02x ", buffer[col + row * width]);
1117 }
1118 printf("\n");
1119 }
1120 }
1121
1122 if (_mesa_is_format_packed_depth_stencil(rbDraw->Base.Format))
1123 usage = PIPE_TRANSFER_READ_WRITE;
1124 else
1125 usage = PIPE_TRANSFER_WRITE;
1126
1127 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1128 dsty = rbDraw->Base.Height - dsty - height;
1129 }
1130
1131 assert(util_format_get_blockwidth(rbDraw->texture->format) == 1);
1132 assert(util_format_get_blockheight(rbDraw->texture->format) == 1);
1133
1134 /* map the stencil buffer */
1135 drawMap = pipe_transfer_map(pipe,
1136 rbDraw->texture,
1137 rbDraw->surface->u.tex.level,
1138 rbDraw->surface->u.tex.first_layer,
1139 usage, dstx, dsty,
1140 width, height, &ptDraw);
1141
1142 /* draw */
1143 /* XXX PixelZoom not handled yet */
1144 for (i = 0; i < height; i++) {
1145 ubyte *dst;
1146 const ubyte *src;
1147 int y;
1148
1149 y = i;
1150
1151 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1152 y = height - y - 1;
1153 }
1154
1155 dst = drawMap + y * ptDraw->stride;
1156 src = buffer + i * width;
1157
1158 _mesa_pack_ubyte_stencil_row(rbDraw->Base.Format, width, src, dst);
1159 }
1160
1161 free(buffer);
1162
1163 /* unmap the stencil buffer */
1164 pipe_transfer_unmap(pipe, ptDraw);
1165 }
1166
1167
1168 /**
1169 * Return renderbuffer to use for reading color pixels for glCopyPixels
1170 */
1171 static struct st_renderbuffer *
1172 st_get_color_read_renderbuffer(struct gl_context *ctx)
1173 {
1174 struct gl_framebuffer *fb = ctx->ReadBuffer;
1175 struct st_renderbuffer *strb =
1176 st_renderbuffer(fb->_ColorReadBuffer);
1177
1178 return strb;
1179 }
1180
1181
1182 /**
1183 * Try to do a glCopyPixels for simple cases with a blit by calling
1184 * pipe->blit().
1185 *
1186 * We can do this when we're copying color pixels (depth/stencil
1187 * eventually) with no pixel zoom, no pixel transfer ops, no
1188 * per-fragment ops, and the src/dest regions don't overlap.
1189 */
1190 static GLboolean
1191 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1192 GLsizei width, GLsizei height,
1193 GLint dstx, GLint dsty, GLenum type)
1194 {
1195 struct st_context *st = st_context(ctx);
1196 struct pipe_context *pipe = st->pipe;
1197 struct pipe_screen *screen = pipe->screen;
1198 struct gl_pixelstore_attrib pack, unpack;
1199 GLint readX, readY, readW, readH, drawX, drawY, drawW, drawH;
1200
1201 if (type == GL_COLOR &&
1202 ctx->Pixel.ZoomX == 1.0 &&
1203 ctx->Pixel.ZoomY == 1.0 &&
1204 ctx->_ImageTransferState == 0x0 &&
1205 !ctx->Color.BlendEnabled &&
1206 !ctx->Color.AlphaEnabled &&
1207 (!ctx->Color.ColorLogicOpEnabled || ctx->Color.LogicOp == GL_COPY) &&
1208 !ctx->Depth.Test &&
1209 !ctx->Fog.Enabled &&
1210 !ctx->Stencil.Enabled &&
1211 !ctx->FragmentProgram.Enabled &&
1212 !ctx->VertexProgram.Enabled &&
1213 !ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT] &&
1214 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1215 !ctx->Query.CondRenderQuery &&
1216 !ctx->Query.CurrentOcclusionObject) {
1217 struct st_renderbuffer *rbRead, *rbDraw;
1218
1219 /*
1220 * Clip the read region against the src buffer bounds.
1221 * We'll still allocate a temporary buffer/texture for the original
1222 * src region size but we'll only read the region which is on-screen.
1223 * This may mean that we draw garbage pixels into the dest region, but
1224 * that's expected.
1225 */
1226 readX = srcx;
1227 readY = srcy;
1228 readW = width;
1229 readH = height;
1230 pack = ctx->DefaultPacking;
1231 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1232 return GL_TRUE; /* all done */
1233
1234 /* clip against dest buffer bounds and scissor box */
1235 drawX = dstx + pack.SkipPixels;
1236 drawY = dsty + pack.SkipRows;
1237 unpack = pack;
1238 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1239 return GL_TRUE; /* all done */
1240
1241 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1242 readY = readY - pack.SkipRows + unpack.SkipRows;
1243
1244 drawW = readW;
1245 drawH = readH;
1246
1247 rbRead = st_get_color_read_renderbuffer(ctx);
1248 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1249
1250 /* Flip src/dst position depending on the orientation of buffers. */
1251 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1252 readY = rbRead->Base.Height - readY;
1253 readH = -readH;
1254 }
1255
1256 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1257 /* We can't flip the destination for pipe->blit, so we only adjust
1258 * its position and flip the source.
1259 */
1260 drawY = rbDraw->Base.Height - drawY - drawH;
1261 readY += readH;
1262 readH = -readH;
1263 }
1264
1265 if (rbRead != rbDraw ||
1266 !_mesa_regions_overlap(readX, readY, readX + readW, readY + readH,
1267 drawX, drawY, drawX + drawW, drawY + drawH)) {
1268 struct pipe_blit_info blit;
1269
1270 memset(&blit, 0, sizeof(blit));
1271 blit.src.resource = rbRead->texture;
1272 blit.src.level = rbRead->surface->u.tex.level;
1273 blit.src.format = rbRead->texture->format;
1274 blit.src.box.x = readX;
1275 blit.src.box.y = readY;
1276 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1277 blit.src.box.width = readW;
1278 blit.src.box.height = readH;
1279 blit.src.box.depth = 1;
1280 blit.dst.resource = rbDraw->texture;
1281 blit.dst.level = rbDraw->surface->u.tex.level;
1282 blit.dst.format = rbDraw->texture->format;
1283 blit.dst.box.x = drawX;
1284 blit.dst.box.y = drawY;
1285 blit.dst.box.z = rbDraw->surface->u.tex.first_layer;
1286 blit.dst.box.width = drawW;
1287 blit.dst.box.height = drawH;
1288 blit.dst.box.depth = 1;
1289 blit.mask = PIPE_MASK_RGBA;
1290 blit.filter = PIPE_TEX_FILTER_NEAREST;
1291
1292 if (screen->is_format_supported(screen, blit.src.format,
1293 blit.src.resource->target,
1294 blit.src.resource->nr_samples,
1295 PIPE_BIND_SAMPLER_VIEW) &&
1296 screen->is_format_supported(screen, blit.dst.format,
1297 blit.dst.resource->target,
1298 blit.dst.resource->nr_samples,
1299 PIPE_BIND_RENDER_TARGET)) {
1300 pipe->blit(pipe, &blit);
1301 return GL_TRUE;
1302 }
1303 }
1304 }
1305
1306 return GL_FALSE;
1307 }
1308
1309
1310 static void
1311 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1312 GLsizei width, GLsizei height,
1313 GLint dstx, GLint dsty, GLenum type)
1314 {
1315 struct st_context *st = st_context(ctx);
1316 struct pipe_context *pipe = st->pipe;
1317 struct pipe_screen *screen = pipe->screen;
1318 struct st_renderbuffer *rbRead;
1319 void *driver_vp, *driver_fp;
1320 struct pipe_resource *pt;
1321 struct pipe_sampler_view *sv[2] = { NULL };
1322 struct st_fp_variant *fpv = NULL;
1323 int num_sampler_view = 1;
1324 enum pipe_format srcFormat;
1325 unsigned srcBind;
1326 GLboolean invertTex = GL_FALSE;
1327 GLint readX, readY, readW, readH;
1328 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1329
1330 st_flush_bitmap_cache(st);
1331
1332 st_validate_state(st, ST_PIPELINE_RENDER);
1333
1334 if (type == GL_DEPTH_STENCIL) {
1335 /* XXX make this more efficient */
1336 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1337 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1338 return;
1339 }
1340
1341 if (type == GL_STENCIL) {
1342 /* can't use texturing to do stencil */
1343 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1344 return;
1345 }
1346
1347 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1348 return;
1349
1350 /*
1351 * The subsequent code implements glCopyPixels by copying the source
1352 * pixels into a temporary texture that's then applied to a textured quad.
1353 * When we draw the textured quad, all the usual per-fragment operations
1354 * are handled.
1355 */
1356
1357
1358 /*
1359 * Get vertex/fragment shaders
1360 */
1361 if (type == GL_COLOR) {
1362 fpv = get_color_fp_variant(st);
1363
1364 rbRead = st_get_color_read_renderbuffer(ctx);
1365
1366 driver_fp = fpv->driver_shader;
1367 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1368
1369 if (ctx->Pixel.MapColorFlag) {
1370 pipe_sampler_view_reference(&sv[1],
1371 st->pixel_xfer.pixelmap_sampler_view);
1372 num_sampler_view++;
1373 }
1374
1375 /* compiling a new fragment shader variant added new state constants
1376 * into the constant buffer, we need to update them
1377 */
1378 st_upload_constants(st, st->fp->Base.Base.Parameters,
1379 PIPE_SHADER_FRAGMENT);
1380 }
1381 else {
1382 assert(type == GL_DEPTH);
1383 rbRead = st_renderbuffer(ctx->ReadBuffer->
1384 Attachment[BUFFER_DEPTH].Renderbuffer);
1385
1386 driver_fp = get_drawpix_z_stencil_program(st, GL_TRUE, GL_FALSE);
1387 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1388 }
1389
1390 /* Choose the format for the temporary texture. */
1391 srcFormat = rbRead->texture->format;
1392 srcBind = PIPE_BIND_SAMPLER_VIEW |
1393 (type == GL_COLOR ? PIPE_BIND_RENDER_TARGET : PIPE_BIND_DEPTH_STENCIL);
1394
1395 if (!screen->is_format_supported(screen, srcFormat, st->internal_target, 0,
1396 srcBind)) {
1397 /* srcFormat is non-renderable. Find a compatible renderable format. */
1398 if (type == GL_DEPTH) {
1399 srcFormat = st_choose_format(st, GL_DEPTH_COMPONENT, GL_NONE,
1400 GL_NONE, st->internal_target, 0,
1401 srcBind, FALSE);
1402 }
1403 else {
1404 assert(type == GL_COLOR);
1405
1406 if (util_format_is_float(srcFormat)) {
1407 srcFormat = st_choose_format(st, GL_RGBA32F, GL_NONE,
1408 GL_NONE, st->internal_target, 0,
1409 srcBind, FALSE);
1410 }
1411 else if (util_format_is_pure_sint(srcFormat)) {
1412 srcFormat = st_choose_format(st, GL_RGBA32I, GL_NONE,
1413 GL_NONE, st->internal_target, 0,
1414 srcBind, FALSE);
1415 }
1416 else if (util_format_is_pure_uint(srcFormat)) {
1417 srcFormat = st_choose_format(st, GL_RGBA32UI, GL_NONE,
1418 GL_NONE, st->internal_target, 0,
1419 srcBind, FALSE);
1420 }
1421 else if (util_format_is_snorm(srcFormat)) {
1422 srcFormat = st_choose_format(st, GL_RGBA16_SNORM, GL_NONE,
1423 GL_NONE, st->internal_target, 0,
1424 srcBind, FALSE);
1425 }
1426 else {
1427 srcFormat = st_choose_format(st, GL_RGBA, GL_NONE,
1428 GL_NONE, st->internal_target, 0,
1429 srcBind, FALSE);
1430 }
1431 }
1432
1433 if (srcFormat == PIPE_FORMAT_NONE) {
1434 assert(0 && "cannot choose a format for src of CopyPixels");
1435 return;
1436 }
1437 }
1438
1439 /* Invert src region if needed */
1440 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1441 srcy = ctx->ReadBuffer->Height - srcy - height;
1442 invertTex = !invertTex;
1443 }
1444
1445 /* Clip the read region against the src buffer bounds.
1446 * We'll still allocate a temporary buffer/texture for the original
1447 * src region size but we'll only read the region which is on-screen.
1448 * This may mean that we draw garbage pixels into the dest region, but
1449 * that's expected.
1450 */
1451 readX = srcx;
1452 readY = srcy;
1453 readW = width;
1454 readH = height;
1455 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1456 /* The source region is completely out of bounds. Do nothing.
1457 * The GL spec says "Results of copies from outside the window,
1458 * or from regions of the window that are not exposed, are
1459 * hardware dependent and undefined."
1460 */
1461 return;
1462 }
1463
1464 readW = MAX2(0, readW);
1465 readH = MAX2(0, readH);
1466
1467 /* Allocate the temporary texture. */
1468 pt = alloc_texture(st, width, height, srcFormat, srcBind);
1469 if (!pt)
1470 return;
1471
1472 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1473 if (!sv[0]) {
1474 pipe_resource_reference(&pt, NULL);
1475 return;
1476 }
1477
1478 /* Copy the src region to the temporary texture. */
1479 {
1480 struct pipe_blit_info blit;
1481
1482 memset(&blit, 0, sizeof(blit));
1483 blit.src.resource = rbRead->texture;
1484 blit.src.level = rbRead->surface->u.tex.level;
1485 blit.src.format = rbRead->texture->format;
1486 blit.src.box.x = readX;
1487 blit.src.box.y = readY;
1488 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1489 blit.src.box.width = readW;
1490 blit.src.box.height = readH;
1491 blit.src.box.depth = 1;
1492 blit.dst.resource = pt;
1493 blit.dst.level = 0;
1494 blit.dst.format = pt->format;
1495 blit.dst.box.x = pack.SkipPixels;
1496 blit.dst.box.y = pack.SkipRows;
1497 blit.dst.box.z = 0;
1498 blit.dst.box.width = readW;
1499 blit.dst.box.height = readH;
1500 blit.dst.box.depth = 1;
1501 blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_S;
1502 blit.filter = PIPE_TEX_FILTER_NEAREST;
1503
1504 pipe->blit(pipe, &blit);
1505 }
1506
1507 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1508 * textured quad with that texture.
1509 */
1510 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1511 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1512 sv,
1513 num_sampler_view,
1514 driver_vp,
1515 driver_fp, fpv,
1516 ctx->Current.Attrib[VERT_ATTRIB_COLOR0],
1517 invertTex, GL_FALSE, GL_FALSE);
1518
1519 pipe_resource_reference(&pt, NULL);
1520 pipe_sampler_view_reference(&sv[0], NULL);
1521 }
1522
1523
1524
1525 void st_init_drawpixels_functions(struct dd_function_table *functions)
1526 {
1527 functions->DrawPixels = st_DrawPixels;
1528 functions->CopyPixels = st_CopyPixels;
1529 }
1530
1531
1532 void
1533 st_destroy_drawpix(struct st_context *st)
1534 {
1535 GLuint i;
1536
1537 for (i = 0; i < ARRAY_SIZE(st->drawpix.zs_shaders); i++) {
1538 if (st->drawpix.zs_shaders[i])
1539 cso_delete_fragment_shader(st->cso_context,
1540 st->drawpix.zs_shaders[i]);
1541 }
1542
1543 if (st->drawpix.vert_shaders[0])
1544 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]);
1545 if (st->drawpix.vert_shaders[1])
1546 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]);
1547 }