st/mesa: check for out-of-memory in st_DrawPixels()
[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 struct pipe_resource *pt;
979
980 /* Mesa state should be up to date by now */
981 assert(ctx->NewState == 0x0);
982
983 st_validate_state(st);
984
985 /* Limit the size of the glDrawPixels to the max texture size.
986 * Strictly speaking, that's not correct but since we don't handle
987 * larger images yet, this is better than crashing.
988 */
989 clippedUnpack = *unpack;
990 unpack = &clippedUnpack;
991 clamp_size(st->pipe, &width, &height, &clippedUnpack);
992
993 if (format == GL_DEPTH_STENCIL)
994 write_stencil = write_depth = GL_TRUE;
995 else if (format == GL_STENCIL_INDEX)
996 write_stencil = GL_TRUE;
997 else if (format == GL_DEPTH_COMPONENT)
998 write_depth = GL_TRUE;
999
1000 if (write_stencil &&
1001 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
1002 /* software fallback */
1003 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1004 unpack, pixels);
1005 return;
1006 }
1007
1008 /*
1009 * Get vertex/fragment shaders
1010 */
1011 if (write_depth || write_stencil) {
1012 driver_fp = get_drawpix_z_stencil_program(st, write_depth,
1013 write_stencil);
1014 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1015 color = ctx->Current.RasterColor;
1016 }
1017 else {
1018 fpv = get_color_fp_variant(st);
1019
1020 driver_fp = fpv->driver_shader;
1021 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1022
1023 color = NULL;
1024 if (ctx->Pixel.MapColorFlag) {
1025 pipe_sampler_view_reference(&sv[1],
1026 st->pixel_xfer.pixelmap_sampler_view);
1027 num_sampler_view++;
1028 }
1029
1030 /* update fragment program constants */
1031 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1032 }
1033
1034 /* Put glDrawPixels image into a texture */
1035 pt = make_texture(st, width, height, format, type, unpack, pixels);
1036 if (!pt) {
1037 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1038 return;
1039 }
1040
1041 /* create sampler view for the image */
1042 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1043 if (!sv[0]) {
1044 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1045 pipe_resource_reference(&pt, NULL);
1046 return;
1047 }
1048
1049 /* Create a second sampler view to read stencil. The stencil is
1050 * written using the shader stencil export functionality.
1051 */
1052 if (write_stencil) {
1053 enum pipe_format stencil_format =
1054 util_format_stencil_only(pt->format);
1055 /* we should not be doing pixel map/transfer (see above) */
1056 assert(num_sampler_view == 1);
1057 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1058 stencil_format);
1059 if (!sv[1]) {
1060 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
1061 pipe_resource_reference(&pt, NULL);
1062 pipe_sampler_view_reference(&sv[0], NULL);
1063 return;
1064 }
1065 num_sampler_view++;
1066 }
1067
1068 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1069 width, height,
1070 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1071 sv,
1072 num_sampler_view,
1073 driver_vp,
1074 driver_fp, fpv,
1075 color, GL_FALSE, write_depth, write_stencil);
1076 pipe_sampler_view_reference(&sv[0], NULL);
1077 if (num_sampler_view > 1)
1078 pipe_sampler_view_reference(&sv[1], NULL);
1079
1080 pipe_resource_reference(&pt, NULL);
1081 }
1082
1083
1084
1085 /**
1086 * Software fallback for glCopyPixels(GL_STENCIL).
1087 */
1088 static void
1089 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1090 GLsizei width, GLsizei height,
1091 GLint dstx, GLint dsty)
1092 {
1093 struct st_renderbuffer *rbDraw;
1094 struct pipe_context *pipe = st_context(ctx)->pipe;
1095 enum pipe_transfer_usage usage;
1096 struct pipe_transfer *ptDraw;
1097 ubyte *drawMap;
1098 ubyte *buffer;
1099 int i;
1100
1101 buffer = malloc(width * height * sizeof(ubyte));
1102 if (!buffer) {
1103 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1104 return;
1105 }
1106
1107 /* Get the dest renderbuffer */
1108 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1109 Attachment[BUFFER_STENCIL].Renderbuffer);
1110
1111 /* this will do stencil pixel transfer ops */
1112 _mesa_readpixels(ctx, srcx, srcy, width, height,
1113 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1114 &ctx->DefaultPacking, buffer);
1115
1116 if (0) {
1117 /* debug code: dump stencil values */
1118 GLint row, col;
1119 for (row = 0; row < height; row++) {
1120 printf("%3d: ", row);
1121 for (col = 0; col < width; col++) {
1122 printf("%02x ", buffer[col + row * width]);
1123 }
1124 printf("\n");
1125 }
1126 }
1127
1128 if (_mesa_is_format_packed_depth_stencil(rbDraw->Base.Format))
1129 usage = PIPE_TRANSFER_READ_WRITE;
1130 else
1131 usage = PIPE_TRANSFER_WRITE;
1132
1133 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1134 dsty = rbDraw->Base.Height - dsty - height;
1135 }
1136
1137 assert(util_format_get_blockwidth(rbDraw->texture->format) == 1);
1138 assert(util_format_get_blockheight(rbDraw->texture->format) == 1);
1139
1140 /* map the stencil buffer */
1141 drawMap = pipe_transfer_map(pipe,
1142 rbDraw->texture,
1143 rbDraw->surface->u.tex.level,
1144 rbDraw->surface->u.tex.first_layer,
1145 usage, dstx, dsty,
1146 width, height, &ptDraw);
1147
1148 /* draw */
1149 /* XXX PixelZoom not handled yet */
1150 for (i = 0; i < height; i++) {
1151 ubyte *dst;
1152 const ubyte *src;
1153 int y;
1154
1155 y = i;
1156
1157 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1158 y = height - y - 1;
1159 }
1160
1161 dst = drawMap + y * ptDraw->stride;
1162 src = buffer + i * width;
1163
1164 _mesa_pack_ubyte_stencil_row(rbDraw->Base.Format, width, src, dst);
1165 }
1166
1167 free(buffer);
1168
1169 /* unmap the stencil buffer */
1170 pipe_transfer_unmap(pipe, ptDraw);
1171 }
1172
1173
1174 /**
1175 * Return renderbuffer to use for reading color pixels for glCopyPixels
1176 */
1177 static struct st_renderbuffer *
1178 st_get_color_read_renderbuffer(struct gl_context *ctx)
1179 {
1180 struct gl_framebuffer *fb = ctx->ReadBuffer;
1181 struct st_renderbuffer *strb =
1182 st_renderbuffer(fb->_ColorReadBuffer);
1183
1184 return strb;
1185 }
1186
1187
1188 /**
1189 * Try to do a glCopyPixels for simple cases with a blit by calling
1190 * pipe->blit().
1191 *
1192 * We can do this when we're copying color pixels (depth/stencil
1193 * eventually) with no pixel zoom, no pixel transfer ops, no
1194 * per-fragment ops, and the src/dest regions don't overlap.
1195 */
1196 static GLboolean
1197 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1198 GLsizei width, GLsizei height,
1199 GLint dstx, GLint dsty, GLenum type)
1200 {
1201 struct st_context *st = st_context(ctx);
1202 struct pipe_context *pipe = st->pipe;
1203 struct pipe_screen *screen = pipe->screen;
1204 struct gl_pixelstore_attrib pack, unpack;
1205 GLint readX, readY, readW, readH, drawX, drawY, drawW, drawH;
1206
1207 if (type == GL_COLOR &&
1208 ctx->Pixel.ZoomX == 1.0 &&
1209 ctx->Pixel.ZoomY == 1.0 &&
1210 ctx->_ImageTransferState == 0x0 &&
1211 !ctx->Color.BlendEnabled &&
1212 !ctx->Color.AlphaEnabled &&
1213 !ctx->Depth.Test &&
1214 !ctx->Fog.Enabled &&
1215 !ctx->Stencil.Enabled &&
1216 !ctx->FragmentProgram.Enabled &&
1217 !ctx->VertexProgram.Enabled &&
1218 !ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT] &&
1219 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1220 !ctx->Query.CondRenderQuery &&
1221 !ctx->Query.CurrentOcclusionObject) {
1222 struct st_renderbuffer *rbRead, *rbDraw;
1223
1224 /*
1225 * Clip the read region against the src buffer bounds.
1226 * We'll still allocate a temporary buffer/texture for the original
1227 * src region size but we'll only read the region which is on-screen.
1228 * This may mean that we draw garbage pixels into the dest region, but
1229 * that's expected.
1230 */
1231 readX = srcx;
1232 readY = srcy;
1233 readW = width;
1234 readH = height;
1235 pack = ctx->DefaultPacking;
1236 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1237 return GL_TRUE; /* all done */
1238
1239 /* clip against dest buffer bounds and scissor box */
1240 drawX = dstx + pack.SkipPixels;
1241 drawY = dsty + pack.SkipRows;
1242 unpack = pack;
1243 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1244 return GL_TRUE; /* all done */
1245
1246 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1247 readY = readY - pack.SkipRows + unpack.SkipRows;
1248
1249 drawW = readW;
1250 drawH = readH;
1251
1252 rbRead = st_get_color_read_renderbuffer(ctx);
1253 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1254
1255 /* Flip src/dst position depending on the orientation of buffers. */
1256 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1257 readY = rbRead->Base.Height - readY;
1258 readH = -readH;
1259 }
1260
1261 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1262 /* We can't flip the destination for pipe->blit, so we only adjust
1263 * its position and flip the source.
1264 */
1265 drawY = rbDraw->Base.Height - drawY - drawH;
1266 readY += readH;
1267 readH = -readH;
1268 }
1269
1270 if (rbRead != rbDraw ||
1271 !_mesa_regions_overlap(readX, readY, readX + readW, readY + readH,
1272 drawX, drawY, drawX + drawW, drawY + drawH)) {
1273 struct pipe_blit_info blit;
1274
1275 memset(&blit, 0, sizeof(blit));
1276 blit.src.resource = rbRead->texture;
1277 blit.src.level = rbRead->surface->u.tex.level;
1278 blit.src.format = rbRead->texture->format;
1279 blit.src.box.x = readX;
1280 blit.src.box.y = readY;
1281 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1282 blit.src.box.width = readW;
1283 blit.src.box.height = readH;
1284 blit.src.box.depth = 1;
1285 blit.dst.resource = rbDraw->texture;
1286 blit.dst.level = rbDraw->surface->u.tex.level;
1287 blit.dst.format = rbDraw->texture->format;
1288 blit.dst.box.x = drawX;
1289 blit.dst.box.y = drawY;
1290 blit.dst.box.z = rbDraw->surface->u.tex.first_layer;
1291 blit.dst.box.width = drawW;
1292 blit.dst.box.height = drawH;
1293 blit.dst.box.depth = 1;
1294 blit.mask = PIPE_MASK_RGBA;
1295 blit.filter = PIPE_TEX_FILTER_NEAREST;
1296
1297 if (screen->is_format_supported(screen, blit.src.format,
1298 blit.src.resource->target,
1299 blit.src.resource->nr_samples,
1300 PIPE_BIND_SAMPLER_VIEW) &&
1301 screen->is_format_supported(screen, blit.dst.format,
1302 blit.dst.resource->target,
1303 blit.dst.resource->nr_samples,
1304 PIPE_BIND_RENDER_TARGET)) {
1305 pipe->blit(pipe, &blit);
1306 return GL_TRUE;
1307 }
1308 }
1309 }
1310
1311 return GL_FALSE;
1312 }
1313
1314
1315 static void
1316 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1317 GLsizei width, GLsizei height,
1318 GLint dstx, GLint dsty, GLenum type)
1319 {
1320 struct st_context *st = st_context(ctx);
1321 struct pipe_context *pipe = st->pipe;
1322 struct pipe_screen *screen = pipe->screen;
1323 struct st_renderbuffer *rbRead;
1324 void *driver_vp, *driver_fp;
1325 struct pipe_resource *pt;
1326 struct pipe_sampler_view *sv[2] = { NULL };
1327 struct st_fp_variant *fpv = NULL;
1328 int num_sampler_view = 1;
1329 GLfloat *color;
1330 enum pipe_format srcFormat;
1331 unsigned srcBind;
1332 GLboolean invertTex = GL_FALSE;
1333 GLint readX, readY, readW, readH;
1334 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1335
1336 st_validate_state(st);
1337
1338 if (type == GL_DEPTH_STENCIL) {
1339 /* XXX make this more efficient */
1340 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1341 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1342 return;
1343 }
1344
1345 if (type == GL_STENCIL) {
1346 /* can't use texturing to do stencil */
1347 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1348 return;
1349 }
1350
1351 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1352 return;
1353
1354 /*
1355 * The subsequent code implements glCopyPixels by copying the source
1356 * pixels into a temporary texture that's then applied to a textured quad.
1357 * When we draw the textured quad, all the usual per-fragment operations
1358 * are handled.
1359 */
1360
1361
1362 /*
1363 * Get vertex/fragment shaders
1364 */
1365 if (type == GL_COLOR) {
1366 fpv = get_color_fp_variant(st);
1367
1368 rbRead = st_get_color_read_renderbuffer(ctx);
1369 color = NULL;
1370
1371 driver_fp = fpv->driver_shader;
1372 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1373
1374 if (ctx->Pixel.MapColorFlag) {
1375 pipe_sampler_view_reference(&sv[1],
1376 st->pixel_xfer.pixelmap_sampler_view);
1377 num_sampler_view++;
1378 }
1379
1380 /* update fragment program constants */
1381 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1382 }
1383 else {
1384 assert(type == GL_DEPTH);
1385 rbRead = st_renderbuffer(ctx->ReadBuffer->
1386 Attachment[BUFFER_DEPTH].Renderbuffer);
1387 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1388
1389 driver_fp = get_drawpix_z_stencil_program(st, GL_TRUE, GL_FALSE);
1390 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1391 }
1392
1393 /* Choose the format for the temporary texture. */
1394 srcFormat = rbRead->texture->format;
1395 srcBind = PIPE_BIND_SAMPLER_VIEW |
1396 (type == GL_COLOR ? PIPE_BIND_RENDER_TARGET : PIPE_BIND_DEPTH_STENCIL);
1397
1398 if (!screen->is_format_supported(screen, srcFormat, st->internal_target, 0,
1399 srcBind)) {
1400 /* srcFormat is non-renderable. Find a compatible renderable format. */
1401 if (type == GL_DEPTH) {
1402 srcFormat = st_choose_format(st, GL_DEPTH_COMPONENT, GL_NONE,
1403 GL_NONE, st->internal_target, 0,
1404 srcBind, FALSE);
1405 }
1406 else {
1407 assert(type == GL_COLOR);
1408
1409 if (util_format_is_float(srcFormat)) {
1410 srcFormat = st_choose_format(st, GL_RGBA32F, GL_NONE,
1411 GL_NONE, st->internal_target, 0,
1412 srcBind, FALSE);
1413 }
1414 else if (util_format_is_pure_sint(srcFormat)) {
1415 srcFormat = st_choose_format(st, GL_RGBA32I, GL_NONE,
1416 GL_NONE, st->internal_target, 0,
1417 srcBind, FALSE);
1418 }
1419 else if (util_format_is_pure_uint(srcFormat)) {
1420 srcFormat = st_choose_format(st, GL_RGBA32UI, GL_NONE,
1421 GL_NONE, st->internal_target, 0,
1422 srcBind, FALSE);
1423 }
1424 else if (util_format_is_snorm(srcFormat)) {
1425 srcFormat = st_choose_format(st, GL_RGBA16_SNORM, GL_NONE,
1426 GL_NONE, st->internal_target, 0,
1427 srcBind, FALSE);
1428 }
1429 else {
1430 srcFormat = st_choose_format(st, GL_RGBA, GL_NONE,
1431 GL_NONE, st->internal_target, 0,
1432 srcBind, FALSE);
1433 }
1434 }
1435
1436 if (srcFormat == PIPE_FORMAT_NONE) {
1437 assert(0 && "cannot choose a format for src of CopyPixels");
1438 return;
1439 }
1440 }
1441
1442 /* Invert src region if needed */
1443 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1444 srcy = ctx->ReadBuffer->Height - srcy - height;
1445 invertTex = !invertTex;
1446 }
1447
1448 /* Clip the read region against the src buffer bounds.
1449 * We'll still allocate a temporary buffer/texture for the original
1450 * src region size but we'll only read the region which is on-screen.
1451 * This may mean that we draw garbage pixels into the dest region, but
1452 * that's expected.
1453 */
1454 readX = srcx;
1455 readY = srcy;
1456 readW = width;
1457 readH = height;
1458 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1459 /* The source region is completely out of bounds. Do nothing.
1460 * The GL spec says "Results of copies from outside the window,
1461 * or from regions of the window that are not exposed, are
1462 * hardware dependent and undefined."
1463 */
1464 return;
1465 }
1466
1467 readW = MAX2(0, readW);
1468 readH = MAX2(0, readH);
1469
1470 /* Allocate the temporary texture. */
1471 pt = alloc_texture(st, width, height, srcFormat, srcBind);
1472 if (!pt)
1473 return;
1474
1475 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1476 if (!sv[0]) {
1477 pipe_resource_reference(&pt, NULL);
1478 return;
1479 }
1480
1481 /* Copy the src region to the temporary texture. */
1482 {
1483 struct pipe_blit_info blit;
1484
1485 memset(&blit, 0, sizeof(blit));
1486 blit.src.resource = rbRead->texture;
1487 blit.src.level = rbRead->surface->u.tex.level;
1488 blit.src.format = rbRead->texture->format;
1489 blit.src.box.x = readX;
1490 blit.src.box.y = readY;
1491 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1492 blit.src.box.width = readW;
1493 blit.src.box.height = readH;
1494 blit.src.box.depth = 1;
1495 blit.dst.resource = pt;
1496 blit.dst.level = 0;
1497 blit.dst.format = pt->format;
1498 blit.dst.box.x = pack.SkipPixels;
1499 blit.dst.box.y = pack.SkipRows;
1500 blit.dst.box.z = 0;
1501 blit.dst.box.width = readW;
1502 blit.dst.box.height = readH;
1503 blit.dst.box.depth = 1;
1504 blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_S;
1505 blit.filter = PIPE_TEX_FILTER_NEAREST;
1506
1507 pipe->blit(pipe, &blit);
1508 }
1509
1510 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1511 * textured quad with that texture.
1512 */
1513 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1514 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1515 sv,
1516 num_sampler_view,
1517 driver_vp,
1518 driver_fp, fpv,
1519 color, invertTex, GL_FALSE, GL_FALSE);
1520
1521 pipe_resource_reference(&pt, NULL);
1522 pipe_sampler_view_reference(&sv[0], NULL);
1523 }
1524
1525
1526
1527 void st_init_drawpixels_functions(struct dd_function_table *functions)
1528 {
1529 functions->DrawPixels = st_DrawPixels;
1530 functions->CopyPixels = st_CopyPixels;
1531 }
1532
1533
1534 void
1535 st_destroy_drawpix(struct st_context *st)
1536 {
1537 GLuint i;
1538
1539 for (i = 0; i < ARRAY_SIZE(st->drawpix.zs_shaders); i++) {
1540 if (st->drawpix.zs_shaders[i])
1541 cso_delete_fragment_shader(st->cso_context,
1542 st->drawpix.zs_shaders[i]);
1543 }
1544
1545 if (st->drawpix.vert_shaders[0])
1546 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]);
1547 if (st->drawpix.vert_shaders[1])
1548 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]);
1549 }