[OPENGL]
[reactos.git] / reactos / dll / opengl / mesa / src / mesa / state_tracker / st_cb_drawpixels.c
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/macros.h"
37 #include "main/mfeatures.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 "program/program.h"
46 #include "program/prog_print.h"
47 #include "program/prog_instruction.h"
48
49 #include "st_atom.h"
50 #include "st_atom_constbuf.h"
51 #include "st_cb_drawpixels.h"
52 #include "st_cb_readpixels.h"
53 #include "st_cb_fbo.h"
54 #include "st_context.h"
55 #include "st_debug.h"
56 #include "st_format.h"
57 #include "st_program.h"
58 #include "st_texture.h"
59
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "tgsi/tgsi_ureg.h"
63 #include "util/u_draw_quad.h"
64 #include "util/u_format.h"
65 #include "util/u_inlines.h"
66 #include "util/u_math.h"
67 #include "util/u_tile.h"
68 #include "cso_cache/cso_context.h"
69
70
71 #if FEATURE_drawpix
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 == FRAG_ATTRIB_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 = FRAG_ATTRIB_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 = FRAG_ATTRIB_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 = FRAG_ATTRIB_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 = FRAG_BIT_TEX0 | FRAG_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 if (!st->drawpix.vert_shaders[passColor]) {
297 struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
298
299 if (ureg == NULL)
300 return NULL;
301
302 /* MOV result.pos, vertex.pos; */
303 ureg_MOV(ureg,
304 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
305 ureg_DECL_vs_input( ureg, 0 ));
306
307 /* MOV result.texcoord0, vertex.attr[1]; */
308 ureg_MOV(ureg,
309 ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ),
310 ureg_DECL_vs_input( ureg, 1 ));
311
312 if (passColor) {
313 /* MOV result.color0, vertex.attr[2]; */
314 ureg_MOV(ureg,
315 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
316 ureg_DECL_vs_input( ureg, 2 ));
317 }
318
319 ureg_END( ureg );
320
321 st->drawpix.vert_shaders[passColor] =
322 ureg_create_shader_and_destroy( ureg, st->pipe );
323 }
324
325 return st->drawpix.vert_shaders[passColor];
326 }
327
328
329 /**
330 * Return a texture internalFormat for drawing/copying an image
331 * of the given format and type.
332 */
333 static GLenum
334 internal_format(struct gl_context *ctx, GLenum format, GLenum type)
335 {
336 switch (format) {
337 case GL_DEPTH_COMPONENT:
338 switch (type) {
339 case GL_UNSIGNED_SHORT:
340 return GL_DEPTH_COMPONENT16;
341
342 case GL_UNSIGNED_INT:
343 return GL_DEPTH_COMPONENT32;
344
345 case GL_FLOAT:
346 if (ctx->Extensions.ARB_depth_buffer_float)
347 return GL_DEPTH_COMPONENT32F;
348 else
349 return GL_DEPTH_COMPONENT;
350
351 default:
352 return GL_DEPTH_COMPONENT;
353 }
354
355 case GL_DEPTH_STENCIL:
356 switch (type) {
357 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
358 return GL_DEPTH32F_STENCIL8;
359
360 case GL_UNSIGNED_INT_24_8:
361 default:
362 return GL_DEPTH24_STENCIL8;
363 }
364
365 case GL_STENCIL_INDEX:
366 return GL_STENCIL_INDEX;
367
368 default:
369 if (_mesa_is_integer_format(format)) {
370 switch (type) {
371 case GL_BYTE:
372 return GL_RGBA8I;
373 case GL_UNSIGNED_BYTE:
374 return GL_RGBA8UI;
375 case GL_SHORT:
376 return GL_RGBA16I;
377 case GL_UNSIGNED_SHORT:
378 return GL_RGBA16UI;
379 case GL_INT:
380 return GL_RGBA32I;
381 case GL_UNSIGNED_INT:
382 return GL_RGBA32UI;
383 default:
384 assert(0 && "Unexpected type in internal_format()");
385 return GL_RGBA_INTEGER;
386 }
387 }
388 else {
389 switch (type) {
390 case GL_UNSIGNED_BYTE:
391 case GL_UNSIGNED_INT_8_8_8_8:
392 case GL_UNSIGNED_INT_8_8_8_8_REV:
393 default:
394 return GL_RGBA8;
395
396 case GL_UNSIGNED_BYTE_3_3_2:
397 case GL_UNSIGNED_BYTE_2_3_3_REV:
398 case GL_UNSIGNED_SHORT_4_4_4_4:
399 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
400 return GL_RGBA4;
401
402 case GL_UNSIGNED_SHORT_5_6_5:
403 case GL_UNSIGNED_SHORT_5_6_5_REV:
404 case GL_UNSIGNED_SHORT_5_5_5_1:
405 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
406 return GL_RGB5_A1;
407
408 case GL_UNSIGNED_INT_10_10_10_2:
409 case GL_UNSIGNED_INT_2_10_10_10_REV:
410 return GL_RGB10_A2;
411
412 case GL_UNSIGNED_SHORT:
413 case GL_UNSIGNED_INT:
414 return GL_RGBA16;
415
416 case GL_BYTE:
417 return
418 ctx->Extensions.EXT_texture_snorm ? GL_RGBA8_SNORM : GL_RGBA8;
419
420 case GL_SHORT:
421 case GL_INT:
422 return
423 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
424
425 case GL_HALF_FLOAT_ARB:
426 return
427 ctx->Extensions.ARB_texture_float ? GL_RGBA16F :
428 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
429
430 case GL_FLOAT:
431 case GL_DOUBLE:
432 return
433 ctx->Extensions.ARB_texture_float ? GL_RGBA32F :
434 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
435
436 case GL_UNSIGNED_INT_5_9_9_9_REV:
437 assert(ctx->Extensions.EXT_texture_shared_exponent);
438 return GL_RGB9_E5;
439
440 case GL_UNSIGNED_INT_10F_11F_11F_REV:
441 assert(ctx->Extensions.EXT_packed_float);
442 return GL_R11F_G11F_B10F;
443 }
444 }
445 }
446 }
447
448
449 /**
450 * Create a temporary texture to hold an image of the given size.
451 * If width, height are not POT and the driver only handles POT textures,
452 * allocate the next larger size of texture that is POT.
453 */
454 static struct pipe_resource *
455 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
456 enum pipe_format texFormat)
457 {
458 struct pipe_resource *pt;
459
460 pt = st_texture_create(st, st->internal_target, texFormat, 0,
461 width, height, 1, 1, PIPE_BIND_SAMPLER_VIEW);
462
463 return pt;
464 }
465
466
467 /**
468 * Make texture containing an image for glDrawPixels image.
469 * If 'pixels' is NULL, leave the texture image data undefined.
470 */
471 static struct pipe_resource *
472 make_texture(struct st_context *st,
473 GLsizei width, GLsizei height, GLenum format, GLenum type,
474 const struct gl_pixelstore_attrib *unpack,
475 const GLvoid *pixels)
476 {
477 struct gl_context *ctx = st->ctx;
478 struct pipe_context *pipe = st->pipe;
479 gl_format mformat;
480 struct pipe_resource *pt;
481 enum pipe_format pipeFormat;
482 GLenum baseInternalFormat, intFormat;
483
484 intFormat = internal_format(ctx, format, type);
485 baseInternalFormat = _mesa_base_tex_format(ctx, intFormat);
486
487 mformat = st_ChooseTextureFormat_renderable(ctx, intFormat,
488 format, type, GL_FALSE);
489 assert(mformat);
490
491 pipeFormat = st_mesa_format_to_pipe_format(mformat);
492 assert(pipeFormat);
493
494 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
495 if (!pixels)
496 return NULL;
497
498 /* alloc temporary texture */
499 pt = alloc_texture(st, width, height, pipeFormat);
500 if (!pt) {
501 _mesa_unmap_pbo_source(ctx, unpack);
502 return NULL;
503 }
504
505 {
506 struct pipe_transfer *transfer;
507 GLboolean success;
508 GLubyte *dest;
509 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
510
511 /* we'll do pixel transfer in a fragment shader */
512 ctx->_ImageTransferState = 0x0;
513
514 transfer = pipe_get_transfer(st->pipe, pt, 0, 0,
515 PIPE_TRANSFER_WRITE, 0, 0,
516 width, height);
517
518 /* map texture transfer */
519 dest = pipe_transfer_map(pipe, transfer);
520
521
522 /* Put image into texture transfer.
523 * Note that the image is actually going to be upside down in
524 * the texture. We deal with that with texcoords.
525 */
526 success = _mesa_texstore(ctx, 2, /* dims */
527 baseInternalFormat, /* baseInternalFormat */
528 mformat, /* gl_format */
529 transfer->stride, /* dstRowStride, bytes */
530 &dest, /* destSlices */
531 width, height, 1, /* size */
532 format, type, /* src format/type */
533 pixels, /* data source */
534 unpack);
535
536 /* unmap */
537 pipe_transfer_unmap(pipe, transfer);
538 pipe->transfer_destroy(pipe, transfer);
539
540 assert(success);
541
542 /* restore */
543 ctx->_ImageTransferState = imageTransferStateSave;
544 }
545
546 _mesa_unmap_pbo_source(ctx, unpack);
547
548 return pt;
549 }
550
551
552 /**
553 * Draw quad with texcoords and optional color.
554 * Coords are gallium window coords with y=0=top.
555 * \param color may be null
556 * \param invertTex if true, flip texcoords vertically
557 */
558 static void
559 draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
560 GLfloat x1, GLfloat y1, const GLfloat *color,
561 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
562 {
563 struct st_context *st = st_context(ctx);
564 struct pipe_context *pipe = st->pipe;
565 GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
566
567 /* setup vertex data */
568 {
569 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
570 const GLfloat fb_width = (GLfloat) fb->Width;
571 const GLfloat fb_height = (GLfloat) fb->Height;
572 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
573 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
574 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
575 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
576 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
577 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
578 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
579 GLuint i;
580
581 /* upper-left */
582 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
583 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
584
585 /* upper-right */
586 verts[1][0][0] = clip_x1;
587 verts[1][0][1] = clip_y0;
588
589 /* lower-right */
590 verts[2][0][0] = clip_x1;
591 verts[2][0][1] = clip_y1;
592
593 /* lower-left */
594 verts[3][0][0] = clip_x0;
595 verts[3][0][1] = clip_y1;
596
597 verts[0][1][0] = sLeft; /* v[0].attr[1].S */
598 verts[0][1][1] = tTop; /* v[0].attr[1].T */
599 verts[1][1][0] = sRight;
600 verts[1][1][1] = tTop;
601 verts[2][1][0] = sRight;
602 verts[2][1][1] = tBot;
603 verts[3][1][0] = sLeft;
604 verts[3][1][1] = tBot;
605
606 /* same for all verts: */
607 if (color) {
608 for (i = 0; i < 4; i++) {
609 verts[i][0][2] = z; /* v[i].attr[0].z */
610 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
611 verts[i][2][0] = color[0]; /* v[i].attr[2].r */
612 verts[i][2][1] = color[1]; /* v[i].attr[2].g */
613 verts[i][2][2] = color[2]; /* v[i].attr[2].b */
614 verts[i][2][3] = color[3]; /* v[i].attr[2].a */
615 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
616 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
617 }
618 }
619 else {
620 for (i = 0; i < 4; i++) {
621 verts[i][0][2] = z; /*Z*/
622 verts[i][0][3] = 1.0f; /*W*/
623 verts[i][1][2] = 0.0f; /*R*/
624 verts[i][1][3] = 1.0f; /*Q*/
625 }
626 }
627 }
628
629 {
630 struct pipe_resource *buf;
631
632 /* allocate/load buffer object with vertex data */
633 buf = pipe_buffer_create(pipe->screen,
634 PIPE_BIND_VERTEX_BUFFER,
635 PIPE_USAGE_STATIC,
636 sizeof(verts));
637 pipe_buffer_write(st->pipe, buf, 0, sizeof(verts), verts);
638
639 util_draw_vertex_buffer(pipe, st->cso_context, buf, 0,
640 PIPE_PRIM_QUADS,
641 4, /* verts */
642 3); /* attribs/vert */
643 pipe_resource_reference(&buf, NULL);
644 }
645 }
646
647
648
649 static void
650 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
651 GLsizei width, GLsizei height,
652 GLfloat zoomX, GLfloat zoomY,
653 struct pipe_sampler_view **sv,
654 int num_sampler_view,
655 void *driver_vp,
656 void *driver_fp,
657 const GLfloat *color,
658 GLboolean invertTex,
659 GLboolean write_depth, GLboolean write_stencil)
660 {
661 struct st_context *st = st_context(ctx);
662 struct pipe_context *pipe = st->pipe;
663 struct cso_context *cso = st->cso_context;
664 GLfloat x0, y0, x1, y1;
665 GLsizei maxSize;
666 boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
667
668 /* limit checks */
669 /* XXX if DrawPixels image is larger than max texture size, break
670 * it up into chunks.
671 */
672 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
673 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
674 assert(width <= maxSize);
675 assert(height <= maxSize);
676
677 cso_save_rasterizer(cso);
678 cso_save_viewport(cso);
679 cso_save_samplers(cso);
680 cso_save_fragment_sampler_views(cso);
681 cso_save_fragment_shader(cso);
682 cso_save_stream_outputs(cso);
683 cso_save_vertex_shader(cso);
684 cso_save_geometry_shader(cso);
685 cso_save_vertex_elements(cso);
686 cso_save_vertex_buffers(cso);
687 if (write_stencil) {
688 cso_save_depth_stencil_alpha(cso);
689 cso_save_blend(cso);
690 }
691
692 /* rasterizer state: just scissor */
693 {
694 struct pipe_rasterizer_state rasterizer;
695 memset(&rasterizer, 0, sizeof(rasterizer));
696 rasterizer.clamp_fragment_color = ctx->Color._ClampFragmentColor;
697 rasterizer.gl_rasterization_rules = 1;
698 rasterizer.depth_clip = !ctx->Transform.DepthClamp;
699 rasterizer.scissor = ctx->Scissor.Enabled;
700 cso_set_rasterizer(cso, &rasterizer);
701 }
702
703 if (write_stencil) {
704 /* Stencil writing bypasses the normal fragment pipeline to
705 * disable color writing and set stencil test to always pass.
706 */
707 struct pipe_depth_stencil_alpha_state dsa;
708 struct pipe_blend_state blend;
709
710 /* depth/stencil */
711 memset(&dsa, 0, sizeof(dsa));
712 dsa.stencil[0].enabled = 1;
713 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
714 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
715 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
716 if (write_depth) {
717 /* writing depth+stencil: depth test always passes */
718 dsa.depth.enabled = 1;
719 dsa.depth.writemask = ctx->Depth.Mask;
720 dsa.depth.func = PIPE_FUNC_ALWAYS;
721 }
722 cso_set_depth_stencil_alpha(cso, &dsa);
723
724 /* blend (colormask) */
725 memset(&blend, 0, sizeof(blend));
726 cso_set_blend(cso, &blend);
727 }
728
729 /* fragment shader state: TEX lookup program */
730 cso_set_fragment_shader_handle(cso, driver_fp);
731
732 /* vertex shader state: position + texcoord pass-through */
733 cso_set_vertex_shader_handle(cso, driver_vp);
734
735 /* geometry shader state: disabled */
736 cso_set_geometry_shader_handle(cso, NULL);
737
738 /* texture sampling state: */
739 {
740 struct pipe_sampler_state sampler;
741 memset(&sampler, 0, sizeof(sampler));
742 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
743 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
744 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
745 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
746 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
747 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
748 sampler.normalized_coords = normalized;
749
750 cso_single_sampler(cso, 0, &sampler);
751 if (num_sampler_view > 1) {
752 cso_single_sampler(cso, 1, &sampler);
753 }
754 cso_single_sampler_done(cso);
755 }
756
757 /* viewport state: viewport matching window dims */
758 {
759 const float w = (float) ctx->DrawBuffer->Width;
760 const float h = (float) ctx->DrawBuffer->Height;
761 struct pipe_viewport_state vp;
762 vp.scale[0] = 0.5f * w;
763 vp.scale[1] = -0.5f * h;
764 vp.scale[2] = 0.5f;
765 vp.scale[3] = 1.0f;
766 vp.translate[0] = 0.5f * w;
767 vp.translate[1] = 0.5f * h;
768 vp.translate[2] = 0.5f;
769 vp.translate[3] = 0.0f;
770 cso_set_viewport(cso, &vp);
771 }
772
773 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
774 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
775
776 /* texture state: */
777 cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
778
779 /* Compute Gallium window coords (y=0=top) with pixel zoom.
780 * Recall that these coords are transformed by the current
781 * vertex shader and viewport transformation.
782 */
783 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
784 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
785 invertTex = !invertTex;
786 }
787
788 x0 = (GLfloat) x;
789 x1 = x + width * ctx->Pixel.ZoomX;
790 y0 = (GLfloat) y;
791 y1 = y + height * ctx->Pixel.ZoomY;
792
793 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
794 z = z * 2.0 - 1.0;
795
796 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
797 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
798 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
799
800 /* restore state */
801 cso_restore_rasterizer(cso);
802 cso_restore_viewport(cso);
803 cso_restore_samplers(cso);
804 cso_restore_fragment_sampler_views(cso);
805 cso_restore_fragment_shader(cso);
806 cso_restore_vertex_shader(cso);
807 cso_restore_geometry_shader(cso);
808 cso_restore_vertex_elements(cso);
809 cso_restore_vertex_buffers(cso);
810 cso_restore_stream_outputs(cso);
811 if (write_stencil) {
812 cso_restore_depth_stencil_alpha(cso);
813 cso_restore_blend(cso);
814 }
815 }
816
817
818 /**
819 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
820 * can't use a fragment shader to write stencil values.
821 */
822 static void
823 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
824 GLsizei width, GLsizei height, GLenum format, GLenum type,
825 const struct gl_pixelstore_attrib *unpack,
826 const GLvoid *pixels)
827 {
828 struct st_context *st = st_context(ctx);
829 struct pipe_context *pipe = st->pipe;
830 struct st_renderbuffer *strb;
831 enum pipe_transfer_usage usage;
832 struct pipe_transfer *pt;
833 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
834 GLint skipPixels;
835 ubyte *stmap;
836 struct gl_pixelstore_attrib clippedUnpack = *unpack;
837
838 if (!zoom) {
839 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
840 &clippedUnpack)) {
841 /* totally clipped */
842 return;
843 }
844 }
845
846 strb = st_renderbuffer(ctx->DrawBuffer->
847 Attachment[BUFFER_STENCIL].Renderbuffer);
848
849 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
850 y = ctx->DrawBuffer->Height - y - height;
851 }
852
853 if(format != GL_DEPTH_STENCIL &&
854 util_format_get_component_bits(strb->format,
855 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
856 usage = PIPE_TRANSFER_READ_WRITE;
857 else
858 usage = PIPE_TRANSFER_WRITE;
859
860 pt = pipe_get_transfer(pipe, strb->texture,
861 strb->rtt_level, strb->rtt_face + strb->rtt_slice,
862 usage, x, y,
863 width, height);
864
865 stmap = pipe_transfer_map(pipe, pt);
866
867 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
868 assert(pixels);
869
870 /* if width > MAX_WIDTH, have to process image in chunks */
871 skipPixels = 0;
872 while (skipPixels < width) {
873 const GLint spanX = skipPixels;
874 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
875 GLint row;
876 for (row = 0; row < height; row++) {
877 GLubyte sValues[MAX_WIDTH];
878 GLuint zValues[MAX_WIDTH];
879 GLfloat *zValuesFloat = (GLfloat*)zValues;
880 GLenum destType = GL_UNSIGNED_BYTE;
881 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
882 width, height,
883 format, type,
884 row, skipPixels);
885 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
886 type, source, &clippedUnpack,
887 ctx->_ImageTransferState);
888
889 if (format == GL_DEPTH_STENCIL) {
890 GLenum ztype =
891 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
892 GL_FLOAT : GL_UNSIGNED_INT;
893
894 _mesa_unpack_depth_span(ctx, spanWidth, ztype, zValues,
895 (1 << 24) - 1, type, source,
896 &clippedUnpack);
897 }
898
899 if (zoom) {
900 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
901 "zoom not complete");
902 }
903
904 {
905 GLint spanY;
906
907 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
908 spanY = height - row - 1;
909 }
910 else {
911 spanY = row;
912 }
913
914 /* now pack the stencil (and Z) values in the dest format */
915 switch (pt->resource->format) {
916 case PIPE_FORMAT_S8_UINT:
917 {
918 ubyte *dest = stmap + spanY * pt->stride + spanX;
919 assert(usage == PIPE_TRANSFER_WRITE);
920 memcpy(dest, sValues, spanWidth);
921 }
922 break;
923 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
924 if (format == GL_DEPTH_STENCIL) {
925 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
926 GLint k;
927 assert(usage == PIPE_TRANSFER_WRITE);
928 for (k = 0; k < spanWidth; k++) {
929 dest[k] = zValues[k] | (sValues[k] << 24);
930 }
931 }
932 else {
933 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
934 GLint k;
935 assert(usage == PIPE_TRANSFER_READ_WRITE);
936 for (k = 0; k < spanWidth; k++) {
937 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
938 }
939 }
940 break;
941 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
942 if (format == GL_DEPTH_STENCIL) {
943 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
944 GLint k;
945 assert(usage == PIPE_TRANSFER_WRITE);
946 for (k = 0; k < spanWidth; k++) {
947 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
948 }
949 }
950 else {
951 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
952 GLint k;
953 assert(usage == PIPE_TRANSFER_READ_WRITE);
954 for (k = 0; k < spanWidth; k++) {
955 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
956 }
957 }
958 break;
959 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
960 if (format == GL_DEPTH_STENCIL) {
961 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
962 GLfloat *destf = (GLfloat*)dest;
963 GLint k;
964 assert(usage == PIPE_TRANSFER_WRITE);
965 for (k = 0; k < spanWidth; k++) {
966 destf[k*2] = zValuesFloat[k];
967 dest[k*2+1] = sValues[k] & 0xff;
968 }
969 }
970 else {
971 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
972 GLint k;
973 assert(usage == PIPE_TRANSFER_READ_WRITE);
974 for (k = 0; k < spanWidth; k++) {
975 dest[k*2+1] = sValues[k] & 0xff;
976 }
977 }
978 break;
979 default:
980 assert(0);
981 }
982 }
983 }
984 skipPixels += spanWidth;
985 }
986
987 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
988
989 /* unmap the stencil buffer */
990 pipe_transfer_unmap(pipe, pt);
991 pipe->transfer_destroy(pipe, pt);
992 }
993
994
995 /**
996 * Get fragment program variant for a glDrawPixels or glCopyPixels
997 * command for RGBA data.
998 */
999 static struct st_fp_variant *
1000 get_color_fp_variant(struct st_context *st)
1001 {
1002 struct gl_context *ctx = st->ctx;
1003 struct st_fp_variant_key key;
1004 struct st_fp_variant *fpv;
1005
1006 memset(&key, 0, sizeof(key));
1007
1008 key.st = st;
1009 key.drawpixels = 1;
1010 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
1011 ctx->Pixel.RedScale != 1.0 ||
1012 ctx->Pixel.GreenBias != 0.0 ||
1013 ctx->Pixel.GreenScale != 1.0 ||
1014 ctx->Pixel.BlueBias != 0.0 ||
1015 ctx->Pixel.BlueScale != 1.0 ||
1016 ctx->Pixel.AlphaBias != 0.0 ||
1017 ctx->Pixel.AlphaScale != 1.0);
1018 key.pixelMaps = ctx->Pixel.MapColorFlag;
1019
1020 fpv = st_get_fp_variant(st, st->fp, &key);
1021
1022 return fpv;
1023 }
1024
1025
1026 /**
1027 * Get fragment program variant for a glDrawPixels or glCopyPixels
1028 * command for depth/stencil data.
1029 */
1030 static struct st_fp_variant *
1031 get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
1032 GLboolean write_stencil)
1033 {
1034 struct st_fp_variant_key key;
1035 struct st_fp_variant *fpv;
1036
1037 memset(&key, 0, sizeof(key));
1038
1039 key.st = st;
1040 key.drawpixels = 1;
1041 key.drawpixels_z = write_depth;
1042 key.drawpixels_stencil = write_stencil;
1043
1044 fpv = st_get_fp_variant(st, st->fp, &key);
1045
1046 return fpv;
1047 }
1048
1049
1050 /**
1051 * Called via ctx->Driver.DrawPixels()
1052 */
1053 static void
1054 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
1055 GLsizei width, GLsizei height,
1056 GLenum format, GLenum type,
1057 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
1058 {
1059 void *driver_vp, *driver_fp;
1060 struct st_context *st = st_context(ctx);
1061 const GLfloat *color;
1062 struct pipe_context *pipe = st->pipe;
1063 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
1064 struct pipe_sampler_view *sv[2];
1065 int num_sampler_view = 1;
1066 struct st_fp_variant *fpv;
1067
1068 if (format == GL_DEPTH_STENCIL)
1069 write_stencil = write_depth = GL_TRUE;
1070 else if (format == GL_STENCIL_INDEX)
1071 write_stencil = GL_TRUE;
1072 else if (format == GL_DEPTH_COMPONENT)
1073 write_depth = GL_TRUE;
1074
1075 if (write_stencil &&
1076 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
1077 /* software fallback */
1078 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1079 unpack, pixels);
1080 return;
1081 }
1082
1083 /* Mesa state should be up to date by now */
1084 assert(ctx->NewState == 0x0);
1085
1086 st_validate_state(st);
1087
1088 /*
1089 * Get vertex/fragment shaders
1090 */
1091 if (write_depth || write_stencil) {
1092 fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
1093
1094 driver_fp = fpv->driver_shader;
1095
1096 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1097
1098 color = ctx->Current.RasterColor;
1099 }
1100 else {
1101 fpv = get_color_fp_variant(st);
1102
1103 driver_fp = fpv->driver_shader;
1104
1105 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1106
1107 color = NULL;
1108 if (st->pixel_xfer.pixelmap_enabled) {
1109 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1110 num_sampler_view++;
1111 }
1112 }
1113
1114 /* update fragment program constants */
1115 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1116
1117 /* draw with textured quad */
1118 {
1119 struct pipe_resource *pt
1120 = make_texture(st, width, height, format, type, unpack, pixels);
1121 if (pt) {
1122 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1123
1124 if (sv[0]) {
1125 /* Create a second sampler view to read stencil.
1126 * The stencil is written using the shader stencil export
1127 * functionality. */
1128 if (write_stencil) {
1129 enum pipe_format stencil_format = PIPE_FORMAT_NONE;
1130
1131 switch (pt->format) {
1132 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1133 case PIPE_FORMAT_X24S8_UINT:
1134 stencil_format = PIPE_FORMAT_X24S8_UINT;
1135 break;
1136 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1137 case PIPE_FORMAT_S8X24_UINT:
1138 stencil_format = PIPE_FORMAT_S8X24_UINT;
1139 break;
1140 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1141 case PIPE_FORMAT_X32_S8X24_UINT:
1142 stencil_format = PIPE_FORMAT_X32_S8X24_UINT;
1143 break;
1144 case PIPE_FORMAT_S8_UINT:
1145 stencil_format = PIPE_FORMAT_S8_UINT;
1146 break;
1147 default:
1148 assert(0);
1149 }
1150
1151 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1152 stencil_format);
1153 num_sampler_view++;
1154 }
1155
1156 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1157 width, height,
1158 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1159 sv,
1160 num_sampler_view,
1161 driver_vp,
1162 driver_fp,
1163 color, GL_FALSE, write_depth, write_stencil);
1164 pipe_sampler_view_reference(&sv[0], NULL);
1165 if (num_sampler_view > 1)
1166 pipe_sampler_view_reference(&sv[1], NULL);
1167 }
1168 pipe_resource_reference(&pt, NULL);
1169 }
1170 }
1171 }
1172
1173
1174
1175 /**
1176 * Software fallback for glCopyPixels(GL_STENCIL).
1177 */
1178 static void
1179 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1180 GLsizei width, GLsizei height,
1181 GLint dstx, GLint dsty)
1182 {
1183 struct st_renderbuffer *rbDraw;
1184 struct pipe_context *pipe = st_context(ctx)->pipe;
1185 enum pipe_transfer_usage usage;
1186 struct pipe_transfer *ptDraw;
1187 ubyte *drawMap;
1188 ubyte *buffer;
1189 int i;
1190
1191 buffer = malloc(width * height * sizeof(ubyte));
1192 if (!buffer) {
1193 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1194 return;
1195 }
1196
1197 /* Get the dest renderbuffer */
1198 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1199 Attachment[BUFFER_STENCIL].Renderbuffer);
1200
1201 /* this will do stencil pixel transfer ops */
1202 _mesa_readpixels(ctx, srcx, srcy, width, height,
1203 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1204 &ctx->DefaultPacking, buffer);
1205
1206 if (0) {
1207 /* debug code: dump stencil values */
1208 GLint row, col;
1209 for (row = 0; row < height; row++) {
1210 printf("%3d: ", row);
1211 for (col = 0; col < width; col++) {
1212 printf("%02x ", buffer[col + row * width]);
1213 }
1214 printf("\n");
1215 }
1216 }
1217
1218 if (util_format_get_component_bits(rbDraw->format,
1219 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
1220 usage = PIPE_TRANSFER_READ_WRITE;
1221 else
1222 usage = PIPE_TRANSFER_WRITE;
1223
1224 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1225 dsty = rbDraw->Base.Height - dsty - height;
1226 }
1227
1228 ptDraw = pipe_get_transfer(pipe,
1229 rbDraw->texture,
1230 rbDraw->rtt_level,
1231 rbDraw->rtt_face + rbDraw->rtt_slice,
1232 usage, dstx, dsty,
1233 width, height);
1234
1235 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
1236 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
1237
1238 /* map the stencil buffer */
1239 drawMap = pipe_transfer_map(pipe, ptDraw);
1240
1241 /* draw */
1242 /* XXX PixelZoom not handled yet */
1243 for (i = 0; i < height; i++) {
1244 ubyte *dst;
1245 const ubyte *src;
1246 int y;
1247
1248 y = i;
1249
1250 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1251 y = height - y - 1;
1252 }
1253
1254 dst = drawMap + y * ptDraw->stride;
1255 src = buffer + i * width;
1256
1257 switch (ptDraw->resource->format) {
1258 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1259 {
1260 uint *dst4 = (uint *) dst;
1261 int j;
1262 assert(usage == PIPE_TRANSFER_READ_WRITE);
1263 for (j = 0; j < width; j++) {
1264 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
1265 dst4++;
1266 }
1267 }
1268 break;
1269 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1270 {
1271 uint *dst4 = (uint *) dst;
1272 int j;
1273 assert(usage == PIPE_TRANSFER_READ_WRITE);
1274 for (j = 0; j < width; j++) {
1275 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
1276 dst4++;
1277 }
1278 }
1279 break;
1280 case PIPE_FORMAT_S8_UINT:
1281 assert(usage == PIPE_TRANSFER_WRITE);
1282 memcpy(dst, src, width);
1283 break;
1284 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1285 {
1286 uint *dst4 = (uint *) dst;
1287 int j;
1288 dst4++;
1289 assert(usage == PIPE_TRANSFER_READ_WRITE);
1290 for (j = 0; j < width; j++) {
1291 *dst4 = src[j] & 0xff;
1292 dst4 += 2;
1293 }
1294 }
1295 break;
1296 default:
1297 assert(0);
1298 }
1299 }
1300
1301 free(buffer);
1302
1303 /* unmap the stencil buffer */
1304 pipe_transfer_unmap(pipe, ptDraw);
1305 pipe->transfer_destroy(pipe, ptDraw);
1306 }
1307
1308
1309 /**
1310 * Return renderbuffer to use for reading color pixels for glCopyPixels
1311 */
1312 static struct st_renderbuffer *
1313 st_get_color_read_renderbuffer(struct gl_context *ctx)
1314 {
1315 struct gl_framebuffer *fb = ctx->ReadBuffer;
1316 struct st_renderbuffer *strb =
1317 st_renderbuffer(fb->_ColorReadBuffer);
1318
1319 return strb;
1320 }
1321
1322
1323 /** Do the src/dest regions overlap? */
1324 static GLboolean
1325 regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY,
1326 GLsizei width, GLsizei height)
1327 {
1328 if (srcX + width <= dstX ||
1329 dstX + width <= srcX ||
1330 srcY + height <= dstY ||
1331 dstY + height <= srcY)
1332 return GL_FALSE;
1333 else
1334 return GL_TRUE;
1335 }
1336
1337
1338 /**
1339 * Try to do a glCopyPixels for simple cases with a blit by calling
1340 * pipe->resource_copy_region().
1341 *
1342 * We can do this when we're copying color pixels (depth/stencil
1343 * eventually) with no pixel zoom, no pixel transfer ops, no
1344 * per-fragment ops, the src/dest regions don't overlap and the
1345 * src/dest pixel formats are the same.
1346 */
1347 static GLboolean
1348 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1349 GLsizei width, GLsizei height,
1350 GLint dstx, GLint dsty, GLenum type)
1351 {
1352 struct st_context *st = st_context(ctx);
1353 struct pipe_context *pipe = st->pipe;
1354 struct gl_pixelstore_attrib pack, unpack;
1355 GLint readX, readY, readW, readH;
1356
1357 if (type == GL_COLOR &&
1358 ctx->Pixel.ZoomX == 1.0 &&
1359 ctx->Pixel.ZoomY == 1.0 &&
1360 ctx->_ImageTransferState == 0x0 &&
1361 !ctx->Color.BlendEnabled &&
1362 !ctx->Color.AlphaEnabled &&
1363 !ctx->Depth.Test &&
1364 !ctx->Fog.Enabled &&
1365 !ctx->Stencil.Enabled &&
1366 !ctx->FragmentProgram.Enabled &&
1367 !ctx->VertexProgram.Enabled &&
1368 !ctx->Shader.CurrentFragmentProgram &&
1369 st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) &&
1370 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1371 !ctx->Query.CondRenderQuery) {
1372 struct st_renderbuffer *rbRead, *rbDraw;
1373 GLint drawX, drawY;
1374
1375 /*
1376 * Clip the read region against the src buffer bounds.
1377 * We'll still allocate a temporary buffer/texture for the original
1378 * src region size but we'll only read the region which is on-screen.
1379 * This may mean that we draw garbage pixels into the dest region, but
1380 * that's expected.
1381 */
1382 readX = srcx;
1383 readY = srcy;
1384 readW = width;
1385 readH = height;
1386 pack = ctx->DefaultPacking;
1387 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1388 return GL_TRUE; /* all done */
1389
1390 /* clip against dest buffer bounds and scissor box */
1391 drawX = dstx + pack.SkipPixels;
1392 drawY = dsty + pack.SkipRows;
1393 unpack = pack;
1394 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1395 return GL_TRUE; /* all done */
1396
1397 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1398 readY = readY - pack.SkipRows + unpack.SkipRows;
1399
1400 rbRead = st_get_color_read_renderbuffer(ctx);
1401 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1402
1403 if ((rbRead != rbDraw ||
1404 !regions_overlap(readX, readY, drawX, drawY, readW, readH)) &&
1405 rbRead->Base.Format == rbDraw->Base.Format) {
1406 struct pipe_box srcBox;
1407
1408 /* flip src/dst position if needed */
1409 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1410 /* both buffers will have the same orientation */
1411 readY = ctx->ReadBuffer->Height - readY - readH;
1412 drawY = ctx->DrawBuffer->Height - drawY - readH;
1413 }
1414
1415 u_box_2d(readX, readY, readW, readH, &srcBox);
1416
1417 pipe->resource_copy_region(pipe,
1418 rbDraw->texture,
1419 rbDraw->rtt_level, drawX, drawY, 0,
1420 rbRead->texture,
1421 rbRead->rtt_level, &srcBox);
1422 return GL_TRUE;
1423 }
1424 }
1425
1426 return GL_FALSE;
1427 }
1428
1429
1430 static void
1431 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1432 GLsizei width, GLsizei height,
1433 GLint dstx, GLint dsty, GLenum type)
1434 {
1435 struct st_context *st = st_context(ctx);
1436 struct pipe_context *pipe = st->pipe;
1437 struct pipe_screen *screen = pipe->screen;
1438 struct st_renderbuffer *rbRead;
1439 void *driver_vp, *driver_fp;
1440 struct pipe_resource *pt;
1441 struct pipe_sampler_view *sv[2];
1442 int num_sampler_view = 1;
1443 GLfloat *color;
1444 enum pipe_format srcFormat, texFormat;
1445 GLboolean invertTex = GL_FALSE;
1446 GLint readX, readY, readW, readH;
1447 GLuint sample_count;
1448 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1449 struct st_fp_variant *fpv;
1450
1451 st_validate_state(st);
1452
1453 if (type == GL_DEPTH_STENCIL) {
1454 /* XXX make this more efficient */
1455 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1456 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1457 return;
1458 }
1459
1460 if (type == GL_STENCIL) {
1461 /* can't use texturing to do stencil */
1462 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1463 return;
1464 }
1465
1466 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1467 return;
1468
1469 /*
1470 * The subsequent code implements glCopyPixels by copying the source
1471 * pixels into a temporary texture that's then applied to a textured quad.
1472 * When we draw the textured quad, all the usual per-fragment operations
1473 * are handled.
1474 */
1475
1476
1477 /*
1478 * Get vertex/fragment shaders
1479 */
1480 if (type == GL_COLOR) {
1481 rbRead = st_get_color_read_renderbuffer(ctx);
1482 color = NULL;
1483
1484 fpv = get_color_fp_variant(st);
1485 driver_fp = fpv->driver_shader;
1486
1487 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1488
1489 if (st->pixel_xfer.pixelmap_enabled) {
1490 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1491 num_sampler_view++;
1492 }
1493 }
1494 else {
1495 assert(type == GL_DEPTH);
1496 rbRead = st_renderbuffer(ctx->ReadBuffer->
1497 Attachment[BUFFER_DEPTH].Renderbuffer);
1498 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1499
1500 fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
1501 driver_fp = fpv->driver_shader;
1502
1503 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1504 }
1505
1506 /* update fragment program constants */
1507 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1508
1509 sample_count = rbRead->texture->nr_samples;
1510 /* I believe this would be legal, presumably would need to do a resolve
1511 for color, and for depth/stencil spec says to just use one of the
1512 depth/stencil samples per pixel? Need some transfer clarifications. */
1513 assert(sample_count < 2);
1514
1515 srcFormat = rbRead->texture->format;
1516
1517 if (screen->is_format_supported(screen, srcFormat, st->internal_target,
1518 sample_count,
1519 PIPE_BIND_SAMPLER_VIEW)) {
1520 texFormat = srcFormat;
1521 }
1522 else {
1523 /* srcFormat can't be used as a texture format */
1524 if (type == GL_DEPTH) {
1525 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1526 GL_NONE, GL_NONE, st->internal_target,
1527 sample_count, PIPE_BIND_DEPTH_STENCIL);
1528 assert(texFormat != PIPE_FORMAT_NONE);
1529 }
1530 else {
1531 /* default color format */
1532 texFormat = st_choose_format(screen, GL_RGBA,
1533 GL_NONE, GL_NONE, st->internal_target,
1534 sample_count, PIPE_BIND_SAMPLER_VIEW);
1535 assert(texFormat != PIPE_FORMAT_NONE);
1536 }
1537 }
1538
1539 /* Invert src region if needed */
1540 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1541 srcy = ctx->ReadBuffer->Height - srcy - height;
1542 invertTex = !invertTex;
1543 }
1544
1545 /* Clip the read region against the src buffer bounds.
1546 * We'll still allocate a temporary buffer/texture for the original
1547 * src region size but we'll only read the region which is on-screen.
1548 * This may mean that we draw garbage pixels into the dest region, but
1549 * that's expected.
1550 */
1551 readX = srcx;
1552 readY = srcy;
1553 readW = width;
1554 readH = height;
1555 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1556 /* The source region is completely out of bounds. Do nothing.
1557 * The GL spec says "Results of copies from outside the window,
1558 * or from regions of the window that are not exposed, are
1559 * hardware dependent and undefined."
1560 */
1561 return;
1562 }
1563
1564 readW = MAX2(0, readW);
1565 readH = MAX2(0, readH);
1566
1567 /* alloc temporary texture */
1568 pt = alloc_texture(st, width, height, texFormat);
1569 if (!pt)
1570 return;
1571
1572 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1573 if (!sv[0]) {
1574 pipe_resource_reference(&pt, NULL);
1575 return;
1576 }
1577
1578 /* Make temporary texture which is a copy of the src region.
1579 */
1580 if (srcFormat == texFormat) {
1581 struct pipe_box src_box;
1582 u_box_2d(readX, readY, readW, readH, &src_box);
1583 /* copy source framebuffer surface into mipmap/texture */
1584 pipe->resource_copy_region(pipe,
1585 pt, /* dest tex */
1586 0, /* dest lvl */
1587 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1588 rbRead->texture, /* src tex */
1589 rbRead->rtt_level, /* src lvl */
1590 &src_box);
1591
1592 }
1593 else {
1594 /* CPU-based fallback/conversion */
1595 struct pipe_transfer *ptRead =
1596 pipe_get_transfer(st->pipe, rbRead->texture,
1597 rbRead->rtt_level,
1598 rbRead->rtt_face + rbRead->rtt_slice,
1599 PIPE_TRANSFER_READ,
1600 readX, readY, readW, readH);
1601 struct pipe_transfer *ptTex;
1602 enum pipe_transfer_usage transfer_usage;
1603
1604 if (ST_DEBUG & DEBUG_FALLBACK)
1605 debug_printf("%s: fallback processing\n", __FUNCTION__);
1606
1607 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1608 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1609 else
1610 transfer_usage = PIPE_TRANSFER_WRITE;
1611
1612 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
1613 0, 0, width, height);
1614
1615 /* copy image from ptRead surface to ptTex surface */
1616 if (type == GL_COLOR) {
1617 /* alternate path using get/put_tile() */
1618 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1619 enum pipe_format readFormat, drawFormat;
1620 readFormat = util_format_linear(rbRead->texture->format);
1621 drawFormat = util_format_linear(pt->format);
1622 pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH,
1623 readFormat, buf);
1624 pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1625 readW, readH, drawFormat, buf);
1626 free(buf);
1627 }
1628 else {
1629 /* GL_DEPTH */
1630 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1631 pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf);
1632 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1633 readW, readH, buf);
1634 free(buf);
1635 }
1636
1637 pipe->transfer_destroy(pipe, ptRead);
1638 pipe->transfer_destroy(pipe, ptTex);
1639 }
1640
1641 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1642 * textured quad with that texture.
1643 */
1644 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1645 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1646 sv,
1647 num_sampler_view,
1648 driver_vp,
1649 driver_fp,
1650 color, invertTex, GL_FALSE, GL_FALSE);
1651
1652 pipe_resource_reference(&pt, NULL);
1653 pipe_sampler_view_reference(&sv[0], NULL);
1654 }
1655
1656
1657
1658 void st_init_drawpixels_functions(struct dd_function_table *functions)
1659 {
1660 functions->DrawPixels = st_DrawPixels;
1661 functions->CopyPixels = st_CopyPixels;
1662 }
1663
1664
1665 void
1666 st_destroy_drawpix(struct st_context *st)
1667 {
1668 GLuint i;
1669
1670 for (i = 0; i < Elements(st->drawpix.shaders); i++) {
1671 if (st->drawpix.shaders[i])
1672 _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
1673 }
1674
1675 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1676 if (st->drawpix.vert_shaders[0])
1677 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]);
1678 if (st->drawpix.vert_shaders[1])
1679 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]);
1680 }
1681
1682 #endif /* FEATURE_drawpix */