Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 = MAX2(MAX2(fpv->drawpix_sampler, fpv->pixelmap_sampler) + 1,
671 st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
672
673 memcpy(sampler_views, st->state.sampler_views[PIPE_SHADER_FRAGMENT],
674 sizeof(sampler_views));
675
676 sampler_views[fpv->drawpix_sampler] = sv[0];
677 if (sv[1])
678 sampler_views[fpv->pixelmap_sampler] = sv[1];
679 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num, sampler_views);
680 } else
681 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num_sampler_view, sv);
682
683 /* Compute Gallium window coords (y=0=top) with pixel zoom.
684 * Recall that these coords are transformed by the current
685 * vertex shader and viewport transformation.
686 */
687 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
688 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
689 invertTex = !invertTex;
690 }
691
692 x0 = (GLfloat) x;
693 x1 = x + width * ctx->Pixel.ZoomX;
694 y0 = (GLfloat) y;
695 y1 = y + height * ctx->Pixel.ZoomY;
696
697 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
698 z = z * 2.0f - 1.0f;
699
700 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
701 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
702 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
703
704 /* restore state */
705 cso_restore_rasterizer(cso);
706 cso_restore_viewport(cso);
707 cso_restore_fragment_samplers(cso);
708 cso_restore_fragment_sampler_views(cso);
709 cso_restore_fragment_shader(cso);
710 cso_restore_vertex_shader(cso);
711 cso_restore_tessctrl_shader(cso);
712 cso_restore_tesseval_shader(cso);
713 cso_restore_geometry_shader(cso);
714 cso_restore_vertex_elements(cso);
715 cso_restore_aux_vertex_buffer_slot(cso);
716 cso_restore_stream_outputs(cso);
717 if (write_stencil) {
718 cso_restore_depth_stencil_alpha(cso);
719 cso_restore_blend(cso);
720 }
721 }
722
723
724 /**
725 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
726 * can't use a fragment shader to write stencil values.
727 */
728 static void
729 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
730 GLsizei width, GLsizei height, GLenum format, GLenum type,
731 const struct gl_pixelstore_attrib *unpack,
732 const GLvoid *pixels)
733 {
734 struct st_context *st = st_context(ctx);
735 struct pipe_context *pipe = st->pipe;
736 struct st_renderbuffer *strb;
737 enum pipe_transfer_usage usage;
738 struct pipe_transfer *pt;
739 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
740 ubyte *stmap;
741 struct gl_pixelstore_attrib clippedUnpack = *unpack;
742 GLubyte *sValues;
743 GLuint *zValues;
744
745 if (!zoom) {
746 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
747 &clippedUnpack)) {
748 /* totally clipped */
749 return;
750 }
751 }
752
753 strb = st_renderbuffer(ctx->DrawBuffer->
754 Attachment[BUFFER_STENCIL].Renderbuffer);
755
756 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
757 y = ctx->DrawBuffer->Height - y - height;
758 }
759
760 if (format == GL_STENCIL_INDEX &&
761 _mesa_is_format_packed_depth_stencil(strb->Base.Format)) {
762 /* writing stencil to a combined depth+stencil buffer */
763 usage = PIPE_TRANSFER_READ_WRITE;
764 }
765 else {
766 usage = PIPE_TRANSFER_WRITE;
767 }
768
769 stmap = pipe_transfer_map(pipe, strb->texture,
770 strb->surface->u.tex.level,
771 strb->surface->u.tex.first_layer,
772 usage, x, y,
773 width, height, &pt);
774
775 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
776 assert(pixels);
777
778 sValues = malloc(width * sizeof(GLubyte));
779 zValues = malloc(width * sizeof(GLuint));
780
781 if (sValues && zValues) {
782 GLint row;
783 for (row = 0; row < height; row++) {
784 GLfloat *zValuesFloat = (GLfloat*)zValues;
785 GLenum destType = GL_UNSIGNED_BYTE;
786 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
787 width, height,
788 format, type,
789 row, 0);
790 _mesa_unpack_stencil_span(ctx, width, destType, sValues,
791 type, source, &clippedUnpack,
792 ctx->_ImageTransferState);
793
794 if (format == GL_DEPTH_STENCIL) {
795 GLenum ztype =
796 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
797 GL_FLOAT : GL_UNSIGNED_INT;
798
799 _mesa_unpack_depth_span(ctx, width, ztype, zValues,
800 (1 << 24) - 1, type, source,
801 &clippedUnpack);
802 }
803
804 if (zoom) {
805 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
806 "zoom not complete");
807 }
808
809 {
810 GLint spanY;
811
812 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
813 spanY = height - row - 1;
814 }
815 else {
816 spanY = row;
817 }
818
819 /* now pack the stencil (and Z) values in the dest format */
820 switch (pt->resource->format) {
821 case PIPE_FORMAT_S8_UINT:
822 {
823 ubyte *dest = stmap + spanY * pt->stride;
824 assert(usage == PIPE_TRANSFER_WRITE);
825 memcpy(dest, sValues, width);
826 }
827 break;
828 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
829 if (format == GL_DEPTH_STENCIL) {
830 uint *dest = (uint *) (stmap + spanY * pt->stride);
831 GLint k;
832 assert(usage == PIPE_TRANSFER_WRITE);
833 for (k = 0; k < width; k++) {
834 dest[k] = zValues[k] | (sValues[k] << 24);
835 }
836 }
837 else {
838 uint *dest = (uint *) (stmap + spanY * pt->stride);
839 GLint k;
840 assert(usage == PIPE_TRANSFER_READ_WRITE);
841 for (k = 0; k < width; k++) {
842 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
843 }
844 }
845 break;
846 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
847 if (format == GL_DEPTH_STENCIL) {
848 uint *dest = (uint *) (stmap + spanY * pt->stride);
849 GLint k;
850 assert(usage == PIPE_TRANSFER_WRITE);
851 for (k = 0; k < width; k++) {
852 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
853 }
854 }
855 else {
856 uint *dest = (uint *) (stmap + spanY * pt->stride);
857 GLint k;
858 assert(usage == PIPE_TRANSFER_READ_WRITE);
859 for (k = 0; k < width; k++) {
860 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
861 }
862 }
863 break;
864 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
865 if (format == GL_DEPTH_STENCIL) {
866 uint *dest = (uint *) (stmap + spanY * pt->stride);
867 GLfloat *destf = (GLfloat*)dest;
868 GLint k;
869 assert(usage == PIPE_TRANSFER_WRITE);
870 for (k = 0; k < width; k++) {
871 destf[k*2] = zValuesFloat[k];
872 dest[k*2+1] = sValues[k] & 0xff;
873 }
874 }
875 else {
876 uint *dest = (uint *) (stmap + spanY * pt->stride);
877 GLint k;
878 assert(usage == PIPE_TRANSFER_READ_WRITE);
879 for (k = 0; k < width; k++) {
880 dest[k*2+1] = sValues[k] & 0xff;
881 }
882 }
883 break;
884 default:
885 assert(0);
886 }
887 }
888 }
889 }
890 else {
891 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels()");
892 }
893
894 free(sValues);
895 free(zValues);
896
897 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
898
899 /* unmap the stencil buffer */
900 pipe_transfer_unmap(pipe, pt);
901 }
902
903
904 /**
905 * Get fragment program variant for a glDrawPixels or glCopyPixels
906 * command for RGBA data.
907 */
908 static struct st_fp_variant *
909 get_color_fp_variant(struct st_context *st)
910 {
911 struct gl_context *ctx = st->ctx;
912 struct st_fp_variant_key key;
913 struct st_fp_variant *fpv;
914
915 memset(&key, 0, sizeof(key));
916
917 key.st = st;
918 key.drawpixels = 1;
919 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
920 ctx->Pixel.RedScale != 1.0 ||
921 ctx->Pixel.GreenBias != 0.0 ||
922 ctx->Pixel.GreenScale != 1.0 ||
923 ctx->Pixel.BlueBias != 0.0 ||
924 ctx->Pixel.BlueScale != 1.0 ||
925 ctx->Pixel.AlphaBias != 0.0 ||
926 ctx->Pixel.AlphaScale != 1.0);
927 key.pixelMaps = ctx->Pixel.MapColorFlag;
928 key.clamp_color = st->clamp_frag_color_in_shader &&
929 st->ctx->Color._ClampFragmentColor;
930
931 fpv = st_get_fp_variant(st, st->fp, &key);
932
933 return fpv;
934 }
935
936
937 /**
938 * Clamp glDrawPixels width and height to the maximum texture size.
939 */
940 static void
941 clamp_size(struct pipe_context *pipe, GLsizei *width, GLsizei *height,
942 struct gl_pixelstore_attrib *unpack)
943 {
944 const int maxSize =
945 1 << (pipe->screen->get_param(pipe->screen,
946 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
947
948 if (*width > maxSize) {
949 if (unpack->RowLength == 0)
950 unpack->RowLength = *width;
951 *width = maxSize;
952 }
953 if (*height > maxSize) {
954 *height = maxSize;
955 }
956 }
957
958
959 /**
960 * Called via ctx->Driver.DrawPixels()
961 */
962 static void
963 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
964 GLsizei width, GLsizei height,
965 GLenum format, GLenum type,
966 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
967 {
968 void *driver_vp, *driver_fp;
969 struct st_context *st = st_context(ctx);
970 const GLfloat *color;
971 struct pipe_context *pipe = st->pipe;
972 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
973 struct pipe_sampler_view *sv[2] = { NULL };
974 int num_sampler_view = 1;
975 struct gl_pixelstore_attrib clippedUnpack;
976 struct st_fp_variant *fpv = NULL;
977
978 /* Mesa state should be up to date by now */
979 assert(ctx->NewState == 0x0);
980
981 st_validate_state(st);
982
983 /* Limit the size of the glDrawPixels to the max texture size.
984 * Strictly speaking, that's not correct but since we don't handle
985 * larger images yet, this is better than crashing.
986 */
987 clippedUnpack = *unpack;
988 unpack = &clippedUnpack;
989 clamp_size(st->pipe, &width, &height, &clippedUnpack);
990
991 if (format == GL_DEPTH_STENCIL)
992 write_stencil = write_depth = GL_TRUE;
993 else if (format == GL_STENCIL_INDEX)
994 write_stencil = GL_TRUE;
995 else if (format == GL_DEPTH_COMPONENT)
996 write_depth = GL_TRUE;
997
998 if (write_stencil &&
999 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
1000 /* software fallback */
1001 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1002 unpack, pixels);
1003 return;
1004 }
1005
1006 /*
1007 * Get vertex/fragment shaders
1008 */
1009 if (write_depth || write_stencil) {
1010 driver_fp = get_drawpix_z_stencil_program(st, write_depth,
1011 write_stencil);
1012 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1013 color = ctx->Current.RasterColor;
1014 }
1015 else {
1016 fpv = get_color_fp_variant(st);
1017
1018 driver_fp = fpv->driver_shader;
1019 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1020
1021 color = NULL;
1022 if (ctx->Pixel.MapColorFlag) {
1023 pipe_sampler_view_reference(&sv[1],
1024 st->pixel_xfer.pixelmap_sampler_view);
1025 num_sampler_view++;
1026 }
1027
1028 /* update fragment program constants */
1029 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1030 }
1031
1032 /* draw with textured quad */
1033 {
1034 struct pipe_resource *pt
1035 = make_texture(st, width, height, format, type, unpack, pixels);
1036 if (pt) {
1037 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1038
1039 if (sv[0]) {
1040 /* Create a second sampler view to read stencil.
1041 * The stencil is written using the shader stencil export
1042 * functionality. */
1043 if (write_stencil) {
1044 enum pipe_format stencil_format =
1045 util_format_stencil_only(pt->format);
1046 /* we should not be doing pixel map/transfer (see above) */
1047 assert(num_sampler_view == 1);
1048 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1049 stencil_format);
1050 num_sampler_view++;
1051 }
1052
1053 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1054 width, height,
1055 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1056 sv,
1057 num_sampler_view,
1058 driver_vp,
1059 driver_fp, fpv,
1060 color, GL_FALSE, write_depth, write_stencil);
1061 pipe_sampler_view_reference(&sv[0], NULL);
1062 if (num_sampler_view > 1)
1063 pipe_sampler_view_reference(&sv[1], NULL);
1064 }
1065 pipe_resource_reference(&pt, NULL);
1066 }
1067 }
1068 }
1069
1070
1071
1072 /**
1073 * Software fallback for glCopyPixels(GL_STENCIL).
1074 */
1075 static void
1076 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1077 GLsizei width, GLsizei height,
1078 GLint dstx, GLint dsty)
1079 {
1080 struct st_renderbuffer *rbDraw;
1081 struct pipe_context *pipe = st_context(ctx)->pipe;
1082 enum pipe_transfer_usage usage;
1083 struct pipe_transfer *ptDraw;
1084 ubyte *drawMap;
1085 ubyte *buffer;
1086 int i;
1087
1088 buffer = malloc(width * height * sizeof(ubyte));
1089 if (!buffer) {
1090 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1091 return;
1092 }
1093
1094 /* Get the dest renderbuffer */
1095 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1096 Attachment[BUFFER_STENCIL].Renderbuffer);
1097
1098 /* this will do stencil pixel transfer ops */
1099 _mesa_readpixels(ctx, srcx, srcy, width, height,
1100 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1101 &ctx->DefaultPacking, buffer);
1102
1103 if (0) {
1104 /* debug code: dump stencil values */
1105 GLint row, col;
1106 for (row = 0; row < height; row++) {
1107 printf("%3d: ", row);
1108 for (col = 0; col < width; col++) {
1109 printf("%02x ", buffer[col + row * width]);
1110 }
1111 printf("\n");
1112 }
1113 }
1114
1115 if (_mesa_is_format_packed_depth_stencil(rbDraw->Base.Format))
1116 usage = PIPE_TRANSFER_READ_WRITE;
1117 else
1118 usage = PIPE_TRANSFER_WRITE;
1119
1120 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1121 dsty = rbDraw->Base.Height - dsty - height;
1122 }
1123
1124 assert(util_format_get_blockwidth(rbDraw->texture->format) == 1);
1125 assert(util_format_get_blockheight(rbDraw->texture->format) == 1);
1126
1127 /* map the stencil buffer */
1128 drawMap = pipe_transfer_map(pipe,
1129 rbDraw->texture,
1130 rbDraw->surface->u.tex.level,
1131 rbDraw->surface->u.tex.first_layer,
1132 usage, dstx, dsty,
1133 width, height, &ptDraw);
1134
1135 /* draw */
1136 /* XXX PixelZoom not handled yet */
1137 for (i = 0; i < height; i++) {
1138 ubyte *dst;
1139 const ubyte *src;
1140 int y;
1141
1142 y = i;
1143
1144 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1145 y = height - y - 1;
1146 }
1147
1148 dst = drawMap + y * ptDraw->stride;
1149 src = buffer + i * width;
1150
1151 _mesa_pack_ubyte_stencil_row(rbDraw->Base.Format, width, src, dst);
1152 }
1153
1154 free(buffer);
1155
1156 /* unmap the stencil buffer */
1157 pipe_transfer_unmap(pipe, ptDraw);
1158 }
1159
1160
1161 /**
1162 * Return renderbuffer to use for reading color pixels for glCopyPixels
1163 */
1164 static struct st_renderbuffer *
1165 st_get_color_read_renderbuffer(struct gl_context *ctx)
1166 {
1167 struct gl_framebuffer *fb = ctx->ReadBuffer;
1168 struct st_renderbuffer *strb =
1169 st_renderbuffer(fb->_ColorReadBuffer);
1170
1171 return strb;
1172 }
1173
1174
1175 /**
1176 * Try to do a glCopyPixels for simple cases with a blit by calling
1177 * pipe->blit().
1178 *
1179 * We can do this when we're copying color pixels (depth/stencil
1180 * eventually) with no pixel zoom, no pixel transfer ops, no
1181 * per-fragment ops, and the src/dest regions don't overlap.
1182 */
1183 static GLboolean
1184 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1185 GLsizei width, GLsizei height,
1186 GLint dstx, GLint dsty, GLenum type)
1187 {
1188 struct st_context *st = st_context(ctx);
1189 struct pipe_context *pipe = st->pipe;
1190 struct pipe_screen *screen = pipe->screen;
1191 struct gl_pixelstore_attrib pack, unpack;
1192 GLint readX, readY, readW, readH, drawX, drawY, drawW, drawH;
1193
1194 if (type == GL_COLOR &&
1195 ctx->Pixel.ZoomX == 1.0 &&
1196 ctx->Pixel.ZoomY == 1.0 &&
1197 ctx->_ImageTransferState == 0x0 &&
1198 !ctx->Color.BlendEnabled &&
1199 !ctx->Color.AlphaEnabled &&
1200 !ctx->Depth.Test &&
1201 !ctx->Fog.Enabled &&
1202 !ctx->Stencil.Enabled &&
1203 !ctx->FragmentProgram.Enabled &&
1204 !ctx->VertexProgram.Enabled &&
1205 !ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT] &&
1206 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1207 !ctx->Query.CondRenderQuery &&
1208 !ctx->Query.CurrentOcclusionObject) {
1209 struct st_renderbuffer *rbRead, *rbDraw;
1210
1211 /*
1212 * Clip the read region against the src buffer bounds.
1213 * We'll still allocate a temporary buffer/texture for the original
1214 * src region size but we'll only read the region which is on-screen.
1215 * This may mean that we draw garbage pixels into the dest region, but
1216 * that's expected.
1217 */
1218 readX = srcx;
1219 readY = srcy;
1220 readW = width;
1221 readH = height;
1222 pack = ctx->DefaultPacking;
1223 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1224 return GL_TRUE; /* all done */
1225
1226 /* clip against dest buffer bounds and scissor box */
1227 drawX = dstx + pack.SkipPixels;
1228 drawY = dsty + pack.SkipRows;
1229 unpack = pack;
1230 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1231 return GL_TRUE; /* all done */
1232
1233 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1234 readY = readY - pack.SkipRows + unpack.SkipRows;
1235
1236 drawW = readW;
1237 drawH = readH;
1238
1239 rbRead = st_get_color_read_renderbuffer(ctx);
1240 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1241
1242 /* Flip src/dst position depending on the orientation of buffers. */
1243 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1244 readY = rbRead->Base.Height - readY;
1245 readH = -readH;
1246 }
1247
1248 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1249 /* We can't flip the destination for pipe->blit, so we only adjust
1250 * its position and flip the source.
1251 */
1252 drawY = rbDraw->Base.Height - drawY - drawH;
1253 readY += readH;
1254 readH = -readH;
1255 }
1256
1257 if (rbRead != rbDraw ||
1258 !_mesa_regions_overlap(readX, readY, readX + readW, readY + readH,
1259 drawX, drawY, drawX + drawW, drawY + drawH)) {
1260 struct pipe_blit_info blit;
1261
1262 memset(&blit, 0, sizeof(blit));
1263 blit.src.resource = rbRead->texture;
1264 blit.src.level = rbRead->surface->u.tex.level;
1265 blit.src.format = rbRead->texture->format;
1266 blit.src.box.x = readX;
1267 blit.src.box.y = readY;
1268 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1269 blit.src.box.width = readW;
1270 blit.src.box.height = readH;
1271 blit.src.box.depth = 1;
1272 blit.dst.resource = rbDraw->texture;
1273 blit.dst.level = rbDraw->surface->u.tex.level;
1274 blit.dst.format = rbDraw->texture->format;
1275 blit.dst.box.x = drawX;
1276 blit.dst.box.y = drawY;
1277 blit.dst.box.z = rbDraw->surface->u.tex.first_layer;
1278 blit.dst.box.width = drawW;
1279 blit.dst.box.height = drawH;
1280 blit.dst.box.depth = 1;
1281 blit.mask = PIPE_MASK_RGBA;
1282 blit.filter = PIPE_TEX_FILTER_NEAREST;
1283
1284 if (screen->is_format_supported(screen, blit.src.format,
1285 blit.src.resource->target,
1286 blit.src.resource->nr_samples,
1287 PIPE_BIND_SAMPLER_VIEW) &&
1288 screen->is_format_supported(screen, blit.dst.format,
1289 blit.dst.resource->target,
1290 blit.dst.resource->nr_samples,
1291 PIPE_BIND_RENDER_TARGET)) {
1292 pipe->blit(pipe, &blit);
1293 return GL_TRUE;
1294 }
1295 }
1296 }
1297
1298 return GL_FALSE;
1299 }
1300
1301
1302 static void
1303 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1304 GLsizei width, GLsizei height,
1305 GLint dstx, GLint dsty, GLenum type)
1306 {
1307 struct st_context *st = st_context(ctx);
1308 struct pipe_context *pipe = st->pipe;
1309 struct pipe_screen *screen = pipe->screen;
1310 struct st_renderbuffer *rbRead;
1311 void *driver_vp, *driver_fp;
1312 struct pipe_resource *pt;
1313 struct pipe_sampler_view *sv[2] = { NULL };
1314 struct st_fp_variant *fpv = NULL;
1315 int num_sampler_view = 1;
1316 GLfloat *color;
1317 enum pipe_format srcFormat;
1318 unsigned srcBind;
1319 GLboolean invertTex = GL_FALSE;
1320 GLint readX, readY, readW, readH;
1321 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1322
1323 st_validate_state(st);
1324
1325 if (type == GL_DEPTH_STENCIL) {
1326 /* XXX make this more efficient */
1327 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1328 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1329 return;
1330 }
1331
1332 if (type == GL_STENCIL) {
1333 /* can't use texturing to do stencil */
1334 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1335 return;
1336 }
1337
1338 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1339 return;
1340
1341 /*
1342 * The subsequent code implements glCopyPixels by copying the source
1343 * pixels into a temporary texture that's then applied to a textured quad.
1344 * When we draw the textured quad, all the usual per-fragment operations
1345 * are handled.
1346 */
1347
1348
1349 /*
1350 * Get vertex/fragment shaders
1351 */
1352 if (type == GL_COLOR) {
1353 fpv = get_color_fp_variant(st);
1354
1355 rbRead = st_get_color_read_renderbuffer(ctx);
1356 color = NULL;
1357
1358 driver_fp = fpv->driver_shader;
1359 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1360
1361 if (ctx->Pixel.MapColorFlag) {
1362 pipe_sampler_view_reference(&sv[1],
1363 st->pixel_xfer.pixelmap_sampler_view);
1364 num_sampler_view++;
1365 }
1366
1367 /* update fragment program constants */
1368 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1369 }
1370 else {
1371 assert(type == GL_DEPTH);
1372 rbRead = st_renderbuffer(ctx->ReadBuffer->
1373 Attachment[BUFFER_DEPTH].Renderbuffer);
1374 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1375
1376 driver_fp = get_drawpix_z_stencil_program(st, GL_TRUE, GL_FALSE);
1377 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1378 }
1379
1380 /* Choose the format for the temporary texture. */
1381 srcFormat = rbRead->texture->format;
1382 srcBind = PIPE_BIND_SAMPLER_VIEW |
1383 (type == GL_COLOR ? PIPE_BIND_RENDER_TARGET : PIPE_BIND_DEPTH_STENCIL);
1384
1385 if (!screen->is_format_supported(screen, srcFormat, st->internal_target, 0,
1386 srcBind)) {
1387 /* srcFormat is non-renderable. Find a compatible renderable format. */
1388 if (type == GL_DEPTH) {
1389 srcFormat = st_choose_format(st, GL_DEPTH_COMPONENT, GL_NONE,
1390 GL_NONE, st->internal_target, 0,
1391 srcBind, FALSE);
1392 }
1393 else {
1394 assert(type == GL_COLOR);
1395
1396 if (util_format_is_float(srcFormat)) {
1397 srcFormat = st_choose_format(st, GL_RGBA32F, GL_NONE,
1398 GL_NONE, st->internal_target, 0,
1399 srcBind, FALSE);
1400 }
1401 else if (util_format_is_pure_sint(srcFormat)) {
1402 srcFormat = st_choose_format(st, GL_RGBA32I, GL_NONE,
1403 GL_NONE, st->internal_target, 0,
1404 srcBind, FALSE);
1405 }
1406 else if (util_format_is_pure_uint(srcFormat)) {
1407 srcFormat = st_choose_format(st, GL_RGBA32UI, GL_NONE,
1408 GL_NONE, st->internal_target, 0,
1409 srcBind, FALSE);
1410 }
1411 else if (util_format_is_snorm(srcFormat)) {
1412 srcFormat = st_choose_format(st, GL_RGBA16_SNORM, GL_NONE,
1413 GL_NONE, st->internal_target, 0,
1414 srcBind, FALSE);
1415 }
1416 else {
1417 srcFormat = st_choose_format(st, GL_RGBA, GL_NONE,
1418 GL_NONE, st->internal_target, 0,
1419 srcBind, FALSE);
1420 }
1421 }
1422
1423 if (srcFormat == PIPE_FORMAT_NONE) {
1424 assert(0 && "cannot choose a format for src of CopyPixels");
1425 return;
1426 }
1427 }
1428
1429 /* Invert src region if needed */
1430 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1431 srcy = ctx->ReadBuffer->Height - srcy - height;
1432 invertTex = !invertTex;
1433 }
1434
1435 /* Clip the read region against the src buffer bounds.
1436 * We'll still allocate a temporary buffer/texture for the original
1437 * src region size but we'll only read the region which is on-screen.
1438 * This may mean that we draw garbage pixels into the dest region, but
1439 * that's expected.
1440 */
1441 readX = srcx;
1442 readY = srcy;
1443 readW = width;
1444 readH = height;
1445 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1446 /* The source region is completely out of bounds. Do nothing.
1447 * The GL spec says "Results of copies from outside the window,
1448 * or from regions of the window that are not exposed, are
1449 * hardware dependent and undefined."
1450 */
1451 return;
1452 }
1453
1454 readW = MAX2(0, readW);
1455 readH = MAX2(0, readH);
1456
1457 /* Allocate the temporary texture. */
1458 pt = alloc_texture(st, width, height, srcFormat, srcBind);
1459 if (!pt)
1460 return;
1461
1462 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1463 if (!sv[0]) {
1464 pipe_resource_reference(&pt, NULL);
1465 return;
1466 }
1467
1468 /* Copy the src region to the temporary texture. */
1469 {
1470 struct pipe_blit_info blit;
1471
1472 memset(&blit, 0, sizeof(blit));
1473 blit.src.resource = rbRead->texture;
1474 blit.src.level = rbRead->surface->u.tex.level;
1475 blit.src.format = rbRead->texture->format;
1476 blit.src.box.x = readX;
1477 blit.src.box.y = readY;
1478 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1479 blit.src.box.width = readW;
1480 blit.src.box.height = readH;
1481 blit.src.box.depth = 1;
1482 blit.dst.resource = pt;
1483 blit.dst.level = 0;
1484 blit.dst.format = pt->format;
1485 blit.dst.box.x = pack.SkipPixels;
1486 blit.dst.box.y = pack.SkipRows;
1487 blit.dst.box.z = 0;
1488 blit.dst.box.width = readW;
1489 blit.dst.box.height = readH;
1490 blit.dst.box.depth = 1;
1491 blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_S;
1492 blit.filter = PIPE_TEX_FILTER_NEAREST;
1493
1494 pipe->blit(pipe, &blit);
1495 }
1496
1497 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1498 * textured quad with that texture.
1499 */
1500 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1501 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1502 sv,
1503 num_sampler_view,
1504 driver_vp,
1505 driver_fp, fpv,
1506 color, invertTex, GL_FALSE, GL_FALSE);
1507
1508 pipe_resource_reference(&pt, NULL);
1509 pipe_sampler_view_reference(&sv[0], NULL);
1510 }
1511
1512
1513
1514 void st_init_drawpixels_functions(struct dd_function_table *functions)
1515 {
1516 functions->DrawPixels = st_DrawPixels;
1517 functions->CopyPixels = st_CopyPixels;
1518 }
1519
1520
1521 void
1522 st_destroy_drawpix(struct st_context *st)
1523 {
1524 GLuint i;
1525
1526 for (i = 0; i < ARRAY_SIZE(st->drawpix.zs_shaders); i++) {
1527 if (st->drawpix.zs_shaders[i])
1528 cso_delete_fragment_shader(st->cso_context,
1529 st->drawpix.zs_shaders[i]);
1530 }
1531
1532 if (st->drawpix.vert_shaders[0])
1533 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]);
1534 if (st->drawpix.vert_shaders[1])
1535 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]);
1536 }