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