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