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