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