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