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