Merge branch 'texformat-rework'
[mesa.git] / 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/texformat.h"
38 #include "main/texstore.h"
39 #include "main/state.h"
40 #include "shader/program.h"
41 #include "shader/prog_parameter.h"
42 #include "shader/prog_print.h"
43
44 #include "st_debug.h"
45 #include "st_context.h"
46 #include "st_atom.h"
47 #include "st_atom_constbuf.h"
48 #include "st_draw.h"
49 #include "st_program.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_cb_readpixels.h"
52 #include "st_cb_fbo.h"
53 #include "st_cb_texture.h"
54 #include "st_draw.h"
55 #include "st_format.h"
56 #include "st_mesa_to_tgsi.h"
57 #include "st_texture.h"
58 #include "st_inlines.h"
59
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "pipe/p_inlines.h"
63 #include "util/u_tile.h"
64 #include "util/u_draw_quad.h"
65 #include "util/u_math.h"
66 #include "util/u_rect.h"
67 #include "shader/prog_instruction.h"
68 #include "cso_cache/cso_context.h"
69
70
71 /**
72 * Check if the given program is:
73 * 0: MOVE result.color, fragment.color;
74 * 1: END;
75 */
76 static GLboolean
77 is_passthrough_program(const struct gl_fragment_program *prog)
78 {
79 if (prog->Base.NumInstructions == 2) {
80 const struct prog_instruction *inst = prog->Base.Instructions;
81 if (inst[0].Opcode == OPCODE_MOV &&
82 inst[1].Opcode == OPCODE_END &&
83 inst[0].DstReg.File == PROGRAM_OUTPUT &&
84 inst[0].DstReg.Index == FRAG_RESULT_COLOR &&
85 inst[0].DstReg.WriteMask == WRITEMASK_XYZW &&
86 inst[0].SrcReg[0].File == PROGRAM_INPUT &&
87 inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 &&
88 inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) {
89 return GL_TRUE;
90 }
91 }
92 return GL_FALSE;
93 }
94
95
96
97 /**
98 * Make fragment shader for glDraw/CopyPixels. This shader is made
99 * by combining the pixel transfer shader with the user-defined shader.
100 */
101 static struct st_fragment_program *
102 combined_drawpix_fragment_program(GLcontext *ctx)
103 {
104 struct st_context *st = st_context(ctx);
105 struct st_fragment_program *stfp;
106
107 if (st->pixel_xfer.program->serialNo == st->pixel_xfer.xfer_prog_sn
108 && st->fp->serialNo == st->pixel_xfer.user_prog_sn) {
109 /* the pixel tranfer program has not changed and the user-defined
110 * program has not changed, so re-use the combined program.
111 */
112 stfp = st->pixel_xfer.combined_prog;
113 }
114 else {
115 /* Concatenate the pixel transfer program with the current user-
116 * defined program.
117 */
118 if (is_passthrough_program(&st->fp->Base)) {
119 stfp = (struct st_fragment_program *)
120 _mesa_clone_program(ctx, &st->pixel_xfer.program->Base.Base);
121 }
122 else {
123 #if 0
124 printf("Base program:\n");
125 _mesa_print_program(&st->fp->Base.Base);
126 printf("DrawPix program:\n");
127 _mesa_print_program(&st->pixel_xfer.program->Base.Base);
128 #endif
129 stfp = (struct st_fragment_program *)
130 _mesa_combine_programs(ctx,
131 &st->pixel_xfer.program->Base.Base,
132 &st->fp->Base.Base);
133 }
134
135 #if 0
136 {
137 struct gl_program *p = &stfp->Base.Base;
138 printf("Combined DrawPixels program:\n");
139 _mesa_print_program(p);
140 printf("InputsRead: 0x%x\n", p->InputsRead);
141 printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
142 _mesa_print_parameter_list(p->Parameters);
143 }
144 #endif
145
146 /* translate to TGSI tokens */
147 st_translate_fragment_program(st, stfp, NULL);
148
149 /* save new program, update serial numbers */
150 st->pixel_xfer.xfer_prog_sn = st->pixel_xfer.program->serialNo;
151 st->pixel_xfer.user_prog_sn = st->fp->serialNo;
152 st->pixel_xfer.combined_prog_sn = stfp->serialNo;
153 /* can't reference new program directly, already have a reference on it */
154 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
155 st->pixel_xfer.combined_prog = stfp;
156 }
157
158 /* Ideally we'd have updated the pipe constants during the normal
159 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
160 */
161 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
162
163 return stfp;
164 }
165
166
167 /**
168 * Create fragment shader that does a TEX() instruction to get a Z
169 * value, then writes to FRAG_RESULT_DEPTH.
170 * Pass fragment color through as-is.
171 */
172 static struct st_fragment_program *
173 make_fragment_shader_z(struct st_context *st)
174 {
175 GLcontext *ctx = st->ctx;
176 struct gl_program *p;
177 GLuint ic = 0;
178
179 if (st->drawpix.z_shader) {
180 return st->drawpix.z_shader;
181 }
182
183 /*
184 * Create shader now
185 */
186 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
187 if (!p)
188 return NULL;
189
190 p->NumInstructions = 3;
191
192 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
193 if (!p->Instructions) {
194 ctx->Driver.DeleteProgram(ctx, p);
195 return NULL;
196 }
197 _mesa_init_instructions(p->Instructions, p->NumInstructions);
198
199 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
200 p->Instructions[ic].Opcode = OPCODE_TEX;
201 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
202 p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
203 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
204 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
205 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
206 p->Instructions[ic].TexSrcUnit = 0;
207 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
208 ic++;
209
210 /* MOV result.color, fragment.color */
211 p->Instructions[ic].Opcode = OPCODE_MOV;
212 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
213 p->Instructions[ic].DstReg.Index = FRAG_RESULT_COLOR;
214 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
215 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_COL0;
216 ic++;
217
218 /* END; */
219 p->Instructions[ic++].Opcode = OPCODE_END;
220
221 assert(ic == p->NumInstructions);
222
223 p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
224 p->OutputsWritten = (1 << FRAG_RESULT_COLOR) | (1 << FRAG_RESULT_DEPTH);
225 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
226
227 st->drawpix.z_shader = (struct st_fragment_program *) p;
228 st_translate_fragment_program(st, st->drawpix.z_shader, NULL);
229
230 return st->drawpix.z_shader;
231 }
232
233
234
235 /**
236 * Create a simple vertex shader that just passes through the
237 * vertex position and texcoord (and optionally, color).
238 */
239 static struct st_vertex_program *
240 st_make_passthrough_vertex_shader(struct st_context *st, GLboolean passColor)
241 {
242 GLcontext *ctx = st->ctx;
243 struct st_vertex_program *stvp;
244 struct gl_program *p;
245 GLuint ic = 0;
246
247 if (st->drawpix.vert_shaders[passColor])
248 return st->drawpix.vert_shaders[passColor];
249
250 /*
251 * Create shader now
252 */
253 p = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
254 if (!p)
255 return NULL;
256
257 if (passColor)
258 p->NumInstructions = 4;
259 else
260 p->NumInstructions = 3;
261
262 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
263 if (!p->Instructions) {
264 ctx->Driver.DeleteProgram(ctx, p);
265 return NULL;
266 }
267 _mesa_init_instructions(p->Instructions, p->NumInstructions);
268 /* MOV result.pos, vertex.pos; */
269 p->Instructions[0].Opcode = OPCODE_MOV;
270 p->Instructions[0].DstReg.File = PROGRAM_OUTPUT;
271 p->Instructions[0].DstReg.Index = VERT_RESULT_HPOS;
272 p->Instructions[0].SrcReg[0].File = PROGRAM_INPUT;
273 p->Instructions[0].SrcReg[0].Index = VERT_ATTRIB_POS;
274 /* MOV result.texcoord0, vertex.texcoord0; */
275 p->Instructions[1].Opcode = OPCODE_MOV;
276 p->Instructions[1].DstReg.File = PROGRAM_OUTPUT;
277 p->Instructions[1].DstReg.Index = VERT_RESULT_TEX0;
278 p->Instructions[1].SrcReg[0].File = PROGRAM_INPUT;
279 p->Instructions[1].SrcReg[0].Index = VERT_ATTRIB_TEX0;
280 ic = 2;
281 if (passColor) {
282 /* MOV result.color0, vertex.color0; */
283 p->Instructions[ic].Opcode = OPCODE_MOV;
284 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
285 p->Instructions[ic].DstReg.Index = VERT_RESULT_COL0;
286 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
287 p->Instructions[ic].SrcReg[0].Index = VERT_ATTRIB_COLOR0;
288 ic++;
289 }
290
291 /* END; */
292 p->Instructions[ic].Opcode = OPCODE_END;
293 ic++;
294
295 assert(ic == p->NumInstructions);
296
297 p->InputsRead = VERT_BIT_POS | VERT_BIT_TEX0;
298 p->OutputsWritten = ((1 << VERT_RESULT_TEX0) |
299 (1 << VERT_RESULT_HPOS));
300 if (passColor) {
301 p->InputsRead |= VERT_BIT_COLOR0;
302 p->OutputsWritten |= (1 << VERT_RESULT_COL0);
303 }
304
305 stvp = (struct st_vertex_program *) p;
306 st_translate_vertex_program(st, stvp, NULL, NULL, NULL);
307
308 st->drawpix.vert_shaders[passColor] = stvp;
309
310 return stvp;
311 }
312
313
314 static GLenum
315 _mesa_base_format(GLenum format)
316 {
317 switch (format) {
318 case GL_DEPTH_COMPONENT:
319 return GL_DEPTH_COMPONENT;
320 case GL_DEPTH_STENCIL:
321 return GL_DEPTH_STENCIL;
322 case GL_STENCIL_INDEX:
323 return GL_STENCIL_INDEX;
324 default:
325 return GL_RGBA;
326 }
327 }
328
329
330 /**
331 * Make texture containing an image for glDrawPixels image.
332 * If 'pixels' is NULL, leave the texture image data undefined.
333 */
334 static struct pipe_texture *
335 make_texture(struct st_context *st,
336 GLsizei width, GLsizei height, GLenum format, GLenum type,
337 const struct gl_pixelstore_attrib *unpack,
338 const GLvoid *pixels)
339 {
340 GLcontext *ctx = st->ctx;
341 struct pipe_context *pipe = st->pipe;
342 struct pipe_screen *screen = pipe->screen;
343 gl_format mformat;
344 struct pipe_texture *pt;
345 enum pipe_format pipeFormat;
346 GLuint cpp;
347 GLenum baseFormat;
348 int ptw, pth;
349
350 baseFormat = _mesa_base_format(format);
351
352 mformat = st_ChooseTextureFormat(ctx, baseFormat, format, type);
353 assert(mformat);
354
355 pipeFormat = st_mesa_format_to_pipe_format(mformat);
356 assert(pipeFormat);
357 cpp = st_sizeof_format(pipeFormat);
358
359 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
360 if (!pixels)
361 return NULL;
362
363 /* Need to use POT texture? */
364 ptw = width;
365 pth = height;
366 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES)) {
367 int l2pt, maxSize;
368
369 l2pt = util_logbase2(width);
370 if (1<<l2pt != width) {
371 ptw = 1<<(l2pt+1);
372 }
373 l2pt = util_logbase2(height);
374 if (1<<l2pt != height) {
375 pth = 1<<(l2pt+1);
376 }
377
378 /* Check against maximum texture size */
379 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
380 assert(ptw <= maxSize);
381 assert(pth <= maxSize);
382 }
383
384 pt = st_texture_create(st, PIPE_TEXTURE_2D, pipeFormat, 0, ptw, pth, 1,
385 PIPE_TEXTURE_USAGE_SAMPLER);
386 if (!pt) {
387 _mesa_unmap_pbo_source(ctx, unpack);
388 return NULL;
389 }
390
391 {
392 struct pipe_transfer *transfer;
393 static const GLuint dstImageOffsets = 0;
394 GLboolean success;
395 GLubyte *dest;
396 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
397
398 /* we'll do pixel transfer in a fragment shader */
399 ctx->_ImageTransferState = 0x0;
400
401 transfer = st_no_flush_get_tex_transfer(st, pt, 0, 0, 0,
402 PIPE_TRANSFER_WRITE, 0, 0,
403 width, height);
404
405 /* map texture transfer */
406 dest = screen->transfer_map(screen, transfer);
407
408
409 /* Put image into texture transfer.
410 * Note that the image is actually going to be upside down in
411 * the texture. We deal with that with texcoords.
412 */
413 success = _mesa_texstore(ctx, 2, /* dims */
414 baseFormat, /* baseInternalFormat */
415 mformat, /* gl_format */
416 dest, /* dest */
417 0, 0, 0, /* dstX/Y/Zoffset */
418 transfer->stride, /* dstRowStride, bytes */
419 &dstImageOffsets, /* dstImageOffsets */
420 width, height, 1, /* size */
421 format, type, /* src format/type */
422 pixels, /* data source */
423 unpack);
424
425 /* unmap */
426 screen->transfer_unmap(screen, transfer);
427 screen->tex_transfer_destroy(transfer);
428
429 assert(success);
430
431 /* restore */
432 ctx->_ImageTransferState = imageTransferStateSave;
433 }
434
435 _mesa_unmap_pbo_source(ctx, unpack);
436
437 return pt;
438 }
439
440
441 /**
442 * Draw quad with texcoords and optional color.
443 * Coords are window coords with y=0=bottom.
444 * \param color may be null
445 * \param invertTex if true, flip texcoords vertically
446 */
447 static void
448 draw_quad(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
449 GLfloat x1, GLfloat y1, const GLfloat *color,
450 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
451 {
452 struct st_context *st = st_context(ctx);
453 struct pipe_context *pipe = st->pipe;
454 GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
455
456 /* setup vertex data */
457 {
458 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
459 const GLfloat fb_width = (GLfloat) fb->Width;
460 const GLfloat fb_height = (GLfloat) fb->Height;
461 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
462 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
463 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
464 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
465 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
466 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
467 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
468 GLuint tex, i;
469
470 /* upper-left */
471 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
472 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
473
474 /* upper-right */
475 verts[1][0][0] = clip_x1;
476 verts[1][0][1] = clip_y0;
477
478 /* lower-right */
479 verts[2][0][0] = clip_x1;
480 verts[2][0][1] = clip_y1;
481
482 /* lower-left */
483 verts[3][0][0] = clip_x0;
484 verts[3][0][1] = clip_y1;
485
486 tex = color ? 2 : 1;
487 verts[0][tex][0] = sLeft; /* v[0].attr[tex].s */
488 verts[0][tex][1] = tTop; /* v[0].attr[tex].t */
489 verts[1][tex][0] = sRight;
490 verts[1][tex][1] = tTop;
491 verts[2][tex][0] = sRight;
492 verts[2][tex][1] = tBot;
493 verts[3][tex][0] = sLeft;
494 verts[3][tex][1] = tBot;
495
496 /* same for all verts: */
497 if (color) {
498 for (i = 0; i < 4; i++) {
499 verts[i][0][2] = z; /*Z*/
500 verts[i][0][3] = 1.0f; /*W*/
501 verts[i][1][0] = color[0];
502 verts[i][1][1] = color[1];
503 verts[i][1][2] = color[2];
504 verts[i][1][3] = color[3];
505 verts[i][2][2] = 0.0f; /*R*/
506 verts[i][2][3] = 1.0f; /*Q*/
507 }
508 }
509 else {
510 for (i = 0; i < 4; i++) {
511 verts[i][0][2] = z; /*Z*/
512 verts[i][0][3] = 1.0f; /*W*/
513 verts[i][1][2] = 0.0f; /*R*/
514 verts[i][1][3] = 1.0f; /*Q*/
515 }
516 }
517 }
518
519 {
520 struct pipe_buffer *buf;
521
522 /* allocate/load buffer object with vertex data */
523 buf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
524 sizeof(verts));
525 st_no_flush_pipe_buffer_write(st, buf, 0, sizeof(verts), verts);
526
527 util_draw_vertex_buffer(pipe, buf, 0,
528 PIPE_PRIM_QUADS,
529 4, /* verts */
530 3); /* attribs/vert */
531 pipe_buffer_reference(&buf, NULL);
532 }
533 }
534
535
536
537 static void
538 draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
539 GLsizei width, GLsizei height,
540 GLfloat zoomX, GLfloat zoomY,
541 struct pipe_texture *pt,
542 struct st_vertex_program *stvp,
543 struct st_fragment_program *stfp,
544 const GLfloat *color,
545 GLboolean invertTex)
546 {
547 struct st_context *st = st_context(ctx);
548 struct pipe_context *pipe = st->pipe;
549 struct cso_context *cso = st->cso_context;
550 GLfloat x0, y0, x1, y1;
551 GLsizei maxSize;
552
553 /* limit checks */
554 /* XXX if DrawPixels image is larger than max texture size, break
555 * it up into chunks.
556 */
557 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
558 assert(width <= maxSize);
559 assert(height <= maxSize);
560
561 cso_save_rasterizer(cso);
562 cso_save_viewport(cso);
563 cso_save_samplers(cso);
564 cso_save_sampler_textures(cso);
565 cso_save_fragment_shader(cso);
566 cso_save_vertex_shader(cso);
567
568 /* rasterizer state: just scissor */
569 {
570 struct pipe_rasterizer_state rasterizer;
571 memset(&rasterizer, 0, sizeof(rasterizer));
572 rasterizer.gl_rasterization_rules = 1;
573 rasterizer.scissor = ctx->Scissor.Enabled;
574 cso_set_rasterizer(cso, &rasterizer);
575 }
576
577 /* fragment shader state: TEX lookup program */
578 cso_set_fragment_shader_handle(cso, stfp->driver_shader);
579
580 /* vertex shader state: position + texcoord pass-through */
581 cso_set_vertex_shader_handle(cso, stvp->driver_shader);
582
583
584 /* texture sampling state: */
585 {
586 struct pipe_sampler_state sampler;
587 memset(&sampler, 0, sizeof(sampler));
588 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
589 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
590 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
591 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
592 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
593 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
594 sampler.normalized_coords = 1;
595
596 cso_single_sampler(cso, 0, &sampler);
597 if (st->pixel_xfer.pixelmap_enabled) {
598 cso_single_sampler(cso, 1, &sampler);
599 }
600 cso_single_sampler_done(cso);
601 }
602
603 /* viewport state: viewport matching window dims */
604 {
605 const float w = (float) ctx->DrawBuffer->Width;
606 const float h = (float) ctx->DrawBuffer->Height;
607 struct pipe_viewport_state vp;
608 vp.scale[0] = 0.5f * w;
609 vp.scale[1] = -0.5f * h;
610 vp.scale[2] = 1.0f;
611 vp.scale[3] = 1.0f;
612 vp.translate[0] = 0.5f * w;
613 vp.translate[1] = 0.5f * h;
614 vp.translate[2] = 0.0f;
615 vp.translate[3] = 0.0f;
616 cso_set_viewport(cso, &vp);
617 }
618
619 /* texture state: */
620 if (st->pixel_xfer.pixelmap_enabled) {
621 struct pipe_texture *textures[2];
622 textures[0] = pt;
623 textures[1] = st->pixel_xfer.pixelmap_texture;
624 pipe->set_sampler_textures(pipe, 2, textures);
625 }
626 else {
627 pipe->set_sampler_textures(pipe, 1, &pt);
628 }
629
630 /* Compute window coords (y=0=bottom) with pixel zoom.
631 * Recall that these coords are transformed by the current
632 * vertex shader and viewport transformation.
633 */
634 x0 = (GLfloat) x;
635 x1 = x + width * ctx->Pixel.ZoomX;
636 y0 = (GLfloat) y;
637 y1 = y + height * ctx->Pixel.ZoomY;
638
639 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
640 (GLfloat) width / pt->width[0],
641 (GLfloat) height / pt->height[0]);
642
643 /* restore state */
644 cso_restore_rasterizer(cso);
645 cso_restore_viewport(cso);
646 cso_restore_samplers(cso);
647 cso_restore_sampler_textures(cso);
648 cso_restore_fragment_shader(cso);
649 cso_restore_vertex_shader(cso);
650 }
651
652
653 static void
654 draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
655 GLsizei width, GLsizei height, GLenum format, GLenum type,
656 const struct gl_pixelstore_attrib *unpack,
657 const GLvoid *pixels)
658 {
659 struct st_context *st = st_context(ctx);
660 struct pipe_context *pipe = st->pipe;
661 struct pipe_screen *screen = pipe->screen;
662 struct st_renderbuffer *strb;
663 enum pipe_transfer_usage usage;
664 struct pipe_transfer *pt;
665 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
666 GLint skipPixels;
667 ubyte *stmap;
668
669 strb = st_renderbuffer(ctx->DrawBuffer->
670 Attachment[BUFFER_STENCIL].Renderbuffer);
671
672 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
673 y = ctx->DrawBuffer->Height - y - height;
674 }
675
676 if(format != GL_DEPTH_STENCIL &&
677 pf_get_component_bits( strb->format, PIPE_FORMAT_COMP_Z ) != 0)
678 usage = PIPE_TRANSFER_READ_WRITE;
679 else
680 usage = PIPE_TRANSFER_WRITE;
681
682 pt = st_cond_flush_get_tex_transfer(st_context(ctx), strb->texture, 0, 0, 0,
683 usage, x, y,
684 width, height);
685
686 stmap = screen->transfer_map(screen, pt);
687
688 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
689 assert(pixels);
690
691 /* if width > MAX_WIDTH, have to process image in chunks */
692 skipPixels = 0;
693 while (skipPixels < width) {
694 const GLint spanX = skipPixels;
695 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
696 GLint row;
697 for (row = 0; row < height; row++) {
698 GLubyte sValues[MAX_WIDTH];
699 GLuint zValues[MAX_WIDTH];
700 GLenum destType = GL_UNSIGNED_BYTE;
701 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
702 width, height,
703 format, type,
704 row, skipPixels);
705 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
706 type, source, unpack,
707 ctx->_ImageTransferState);
708
709 if (format == GL_DEPTH_STENCIL) {
710 _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues,
711 (1 << 24) - 1, type, source, unpack);
712 }
713
714 if (zoom) {
715 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
716 "zoom not complete");
717 }
718
719 {
720 GLint spanY;
721
722 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
723 spanY = height - row - 1;
724 }
725 else {
726 spanY = row;
727 }
728
729 /* now pack the stencil (and Z) values in the dest format */
730 switch (pt->format) {
731 case PIPE_FORMAT_S8_UNORM:
732 {
733 ubyte *dest = stmap + spanY * pt->stride + spanX;
734 assert(usage == PIPE_TRANSFER_WRITE);
735 memcpy(dest, sValues, spanWidth);
736 }
737 break;
738 case PIPE_FORMAT_S8Z24_UNORM:
739 if (format == GL_DEPTH_STENCIL) {
740 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
741 GLint k;
742 assert(usage == PIPE_TRANSFER_WRITE);
743 for (k = 0; k < spanWidth; k++) {
744 dest[k] = zValues[k] | (sValues[k] << 24);
745 }
746 }
747 else {
748 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
749 GLint k;
750 assert(usage == PIPE_TRANSFER_READ_WRITE);
751 for (k = 0; k < spanWidth; k++) {
752 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
753 }
754 }
755 break;
756 case PIPE_FORMAT_Z24S8_UNORM:
757 if (format == GL_DEPTH_STENCIL) {
758 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
759 GLint k;
760 assert(usage == PIPE_TRANSFER_WRITE);
761 for (k = 0; k < spanWidth; k++) {
762 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
763 }
764 }
765 else {
766 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
767 GLint k;
768 assert(usage == PIPE_TRANSFER_READ_WRITE);
769 for (k = 0; k < spanWidth; k++) {
770 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
771 }
772 }
773 break;
774 default:
775 assert(0);
776 }
777 }
778 }
779 skipPixels += spanWidth;
780 }
781
782 _mesa_unmap_pbo_source(ctx, unpack);
783
784 /* unmap the stencil buffer */
785 screen->transfer_unmap(screen, pt);
786 screen->tex_transfer_destroy(pt);
787 }
788
789
790 /**
791 * Called via ctx->Driver.DrawPixels()
792 */
793 static void
794 st_DrawPixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
795 GLenum format, GLenum type,
796 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
797 {
798 struct st_fragment_program *stfp;
799 struct st_vertex_program *stvp;
800 struct st_context *st = st_context(ctx);
801 struct pipe_surface *ps;
802 const GLfloat *color;
803
804 if (format == GL_STENCIL_INDEX ||
805 format == GL_DEPTH_STENCIL) {
806 draw_stencil_pixels(ctx, x, y, width, height, format, type,
807 unpack, pixels);
808 return;
809 }
810
811 /* Mesa state should be up to date by now */
812 assert(ctx->NewState == 0x0);
813
814 st_validate_state(st);
815
816 if (format == GL_DEPTH_COMPONENT) {
817 ps = st->state.framebuffer.zsbuf;
818 stfp = make_fragment_shader_z(st);
819 stvp = st_make_passthrough_vertex_shader(st, GL_TRUE);
820 color = ctx->Current.RasterColor;
821 }
822 else {
823 ps = st->state.framebuffer.cbufs[0];
824 stfp = combined_drawpix_fragment_program(ctx);
825 stvp = st_make_passthrough_vertex_shader(st, GL_FALSE);
826 color = NULL;
827 }
828
829 /* draw with textured quad */
830 {
831 struct pipe_texture *pt
832 = make_texture(st, width, height, format, type, unpack, pixels);
833 if (pt) {
834 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
835 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
836 pt, stvp, stfp, color, GL_FALSE);
837 pipe_texture_reference(&pt, NULL);
838 }
839 }
840 }
841
842
843
844 static void
845 copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
846 GLsizei width, GLsizei height,
847 GLint dstx, GLint dsty)
848 {
849 struct st_renderbuffer *rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
850 struct pipe_screen *screen = ctx->st->pipe->screen;
851 enum pipe_transfer_usage usage;
852 struct pipe_transfer *ptDraw;
853 ubyte *drawMap;
854 ubyte *buffer;
855 int i;
856
857 buffer = _mesa_malloc(width * height * sizeof(ubyte));
858 if (!buffer) {
859 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
860 return;
861 }
862
863 /* this will do stencil pixel transfer ops */
864 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
865 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
866 &ctx->DefaultPacking, buffer);
867
868 if(pf_get_component_bits( rbDraw->format, PIPE_FORMAT_COMP_Z ) != 0)
869 usage = PIPE_TRANSFER_READ_WRITE;
870 else
871 usage = PIPE_TRANSFER_WRITE;
872
873 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
874 dsty = rbDraw->Base.Height - dsty - height;
875 }
876
877 ptDraw = st_cond_flush_get_tex_transfer(st_context(ctx),
878 rbDraw->texture, 0, 0, 0,
879 usage, dstx, dsty,
880 width, height);
881
882 assert(ptDraw->block.width == 1);
883 assert(ptDraw->block.height == 1);
884
885 /* map the stencil buffer */
886 drawMap = screen->transfer_map(screen, ptDraw);
887
888 /* draw */
889 /* XXX PixelZoom not handled yet */
890 for (i = 0; i < height; i++) {
891 ubyte *dst;
892 const ubyte *src;
893 int y;
894
895 y = i;
896
897 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
898 y = height - y - 1;
899 }
900
901 dst = drawMap + y * ptDraw->stride;
902 src = buffer + i * width;
903
904 switch (ptDraw->format) {
905 case PIPE_FORMAT_S8Z24_UNORM:
906 {
907 uint *dst4 = (uint *) dst;
908 int j;
909 assert(usage == PIPE_TRANSFER_READ_WRITE);
910 for (j = 0; j < width; j++) {
911 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
912 dst4++;
913 }
914 }
915 break;
916 case PIPE_FORMAT_Z24S8_UNORM:
917 {
918 uint *dst4 = (uint *) dst;
919 int j;
920 assert(usage == PIPE_TRANSFER_READ_WRITE);
921 for (j = 0; j < width; j++) {
922 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
923 dst4++;
924 }
925 }
926 break;
927 case PIPE_FORMAT_S8_UNORM:
928 assert(usage == PIPE_TRANSFER_WRITE);
929 memcpy(dst, src, width);
930 break;
931 default:
932 assert(0);
933 }
934 }
935
936 _mesa_free(buffer);
937
938 /* unmap the stencil buffer */
939 screen->transfer_unmap(screen, ptDraw);
940 screen->tex_transfer_destroy(ptDraw);
941 }
942
943
944 static void
945 st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
946 GLsizei width, GLsizei height,
947 GLint dstx, GLint dsty, GLenum type)
948 {
949 struct st_context *st = st_context(ctx);
950 struct pipe_context *pipe = st->pipe;
951 struct pipe_screen *screen = pipe->screen;
952 struct st_renderbuffer *rbRead;
953 struct st_vertex_program *stvp;
954 struct st_fragment_program *stfp;
955 struct pipe_texture *pt;
956 GLfloat *color;
957 enum pipe_format srcFormat, texFormat;
958 int ptw, pth;
959
960 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
961
962 st_validate_state(st);
963
964 if (srcx < 0) {
965 width -= -srcx;
966 dstx += -srcx;
967 srcx = 0;
968 }
969
970 if (srcy < 0) {
971 height -= -srcy;
972 dsty += -srcy;
973 srcy = 0;
974 }
975
976 if (dstx < 0) {
977 width -= -dstx;
978 srcx += -dstx;
979 dstx = 0;
980 }
981
982 if (dsty < 0) {
983 height -= -dsty;
984 srcy += -dsty;
985 dsty = 0;
986 }
987
988 if (width < 0 || height < 0)
989 return;
990
991
992 if (type == GL_STENCIL) {
993 /* can't use texturing to do stencil */
994 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
995 return;
996 }
997
998 if (type == GL_COLOR) {
999 rbRead = st_get_color_read_renderbuffer(ctx);
1000 color = NULL;
1001 stfp = combined_drawpix_fragment_program(ctx);
1002 stvp = st_make_passthrough_vertex_shader(st, GL_FALSE);
1003 }
1004 else {
1005 assert(type == GL_DEPTH);
1006 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
1007 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1008 stfp = make_fragment_shader_z(st);
1009 stvp = st_make_passthrough_vertex_shader(st, GL_TRUE);
1010 }
1011
1012 srcFormat = rbRead->texture->format;
1013
1014 if (screen->is_format_supported(screen, srcFormat, PIPE_TEXTURE_2D,
1015 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
1016 texFormat = srcFormat;
1017 }
1018 else {
1019 /* srcFormat can't be used as a texture format */
1020 if (type == GL_DEPTH) {
1021 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1022 PIPE_TEXTURE_2D,
1023 PIPE_TEXTURE_USAGE_DEPTH_STENCIL);
1024 assert(texFormat != PIPE_FORMAT_NONE); /* XXX no depth texture formats??? */
1025 }
1026 else {
1027 /* default color format */
1028 texFormat = st_choose_format(screen, GL_RGBA, PIPE_TEXTURE_2D,
1029 PIPE_TEXTURE_USAGE_SAMPLER);
1030 assert(texFormat != PIPE_FORMAT_NONE);
1031 }
1032 }
1033
1034 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1035 srcy = ctx->DrawBuffer->Height - srcy - height;
1036
1037 if (srcy < 0) {
1038 height -= -srcy;
1039 srcy = 0;
1040 }
1041
1042 if (height < 0)
1043 return;
1044 }
1045
1046 /* Need to use POT texture? */
1047 ptw = width;
1048 pth = height;
1049 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES)) {
1050 int l2pt, maxSize;
1051
1052 l2pt = util_logbase2(width);
1053 if (1<<l2pt != width) {
1054 ptw = 1<<(l2pt+1);
1055 }
1056 l2pt = util_logbase2(height);
1057 if (1<<l2pt != height) {
1058 pth = 1<<(l2pt+1);
1059 }
1060
1061 /* Check against maximum texture size */
1062 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
1063 assert(ptw <= maxSize);
1064 assert(pth <= maxSize);
1065 }
1066
1067 pt = st_texture_create(st, PIPE_TEXTURE_2D, texFormat, 0,
1068 ptw, pth, 1,
1069 PIPE_TEXTURE_USAGE_SAMPLER);
1070 if (!pt)
1071 return;
1072
1073
1074 if (srcFormat == texFormat) {
1075 /* copy source framebuffer surface into mipmap/texture */
1076 struct pipe_surface *psRead = screen->get_tex_surface(screen,
1077 rbRead->texture, 0, 0, 0,
1078 PIPE_BUFFER_USAGE_GPU_READ);
1079 struct pipe_surface *psTex = screen->get_tex_surface(screen, pt, 0, 0, 0,
1080 PIPE_BUFFER_USAGE_GPU_WRITE );
1081 if (pipe->surface_copy) {
1082 pipe->surface_copy(pipe,
1083 psTex, /* dest */
1084 0, 0, /* destx/y */
1085 psRead,
1086 srcx, srcy, width, height);
1087 } else {
1088 util_surface_copy(pipe, FALSE,
1089 psTex,
1090 0, 0,
1091 psRead,
1092 srcx, srcy, width, height);
1093 }
1094 pipe_surface_reference(&psRead, NULL);
1095 pipe_surface_reference(&psTex, NULL);
1096 }
1097 else {
1098 /* CPU-based fallback/conversion */
1099 struct pipe_transfer *ptRead =
1100 st_cond_flush_get_tex_transfer(st, rbRead->texture, 0, 0, 0,
1101 PIPE_TRANSFER_READ, srcx, srcy, width,
1102 height);
1103 struct pipe_transfer *ptTex;
1104 enum pipe_transfer_usage transfer_usage;
1105
1106 if (ST_DEBUG & DEBUG_FALLBACK)
1107 debug_printf("%s: fallback processing\n", __FUNCTION__);
1108
1109 if (type == GL_DEPTH && pf_is_depth_and_stencil(pt->format))
1110 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1111 else
1112 transfer_usage = PIPE_TRANSFER_WRITE;
1113
1114 ptTex = st_cond_flush_get_tex_transfer(st, pt, 0, 0, 0, transfer_usage,
1115 0, 0, width, height);
1116
1117 if (type == GL_COLOR) {
1118 /* alternate path using get/put_tile() */
1119 GLfloat *buf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
1120
1121 pipe_get_tile_rgba(ptRead, 0, 0, width, height, buf);
1122 pipe_put_tile_rgba(ptTex, 0, 0, width, height, buf);
1123
1124 _mesa_free(buf);
1125 }
1126 else {
1127 /* GL_DEPTH */
1128 GLuint *buf = (GLuint *) _mesa_malloc(width * height * sizeof(GLuint));
1129 pipe_get_tile_z(ptRead, 0, 0, width, height, buf);
1130 pipe_put_tile_z(ptTex, 0, 0, width, height, buf);
1131 _mesa_free(buf);
1132 }
1133
1134 screen->tex_transfer_destroy(ptRead);
1135 screen->tex_transfer_destroy(ptTex);
1136 }
1137
1138 /* draw textured quad */
1139 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1140 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1141 pt, stvp, stfp, color, GL_TRUE);
1142
1143 pipe_texture_reference(&pt, NULL);
1144 }
1145
1146
1147
1148 void st_init_drawpixels_functions(struct dd_function_table *functions)
1149 {
1150 functions->DrawPixels = st_DrawPixels;
1151 functions->CopyPixels = st_CopyPixels;
1152 }
1153
1154
1155 void
1156 st_destroy_drawpix(struct st_context *st)
1157 {
1158 st_reference_fragprog(st, &st->drawpix.z_shader, NULL);
1159 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1160 st_reference_vertprog(st, &st->drawpix.vert_shaders[0], NULL);
1161 st_reference_vertprog(st, &st->drawpix.vert_shaders[1], NULL);
1162 }