st/mesa: use _mesa_get_texstore_func()
[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_context.h"
45 #include "st_atom.h"
46 #include "st_atom_constbuf.h"
47 #include "st_draw.h"
48 #include "st_program.h"
49 #include "st_cb_drawpixels.h"
50 #include "st_cb_readpixels.h"
51 #include "st_cb_fbo.h"
52 #include "st_cb_texture.h"
53 #include "st_draw.h"
54 #include "st_format.h"
55 #include "st_mesa_to_tgsi.h"
56 #include "st_texture.h"
57 #include "st_inlines.h"
58
59 #include "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "pipe/p_inlines.h"
62 #include "util/u_tile.h"
63 #include "util/u_draw_quad.h"
64 #include "util/u_math.h"
65 #include "shader/prog_instruction.h"
66 #include "cso_cache/cso_context.h"
67
68
69 /**
70 * Check if the given program is:
71 * 0: MOVE result.color, fragment.color;
72 * 1: END;
73 */
74 static GLboolean
75 is_passthrough_program(const struct gl_fragment_program *prog)
76 {
77 if (prog->Base.NumInstructions == 2) {
78 const struct prog_instruction *inst = prog->Base.Instructions;
79 if (inst[0].Opcode == OPCODE_MOV &&
80 inst[1].Opcode == OPCODE_END &&
81 inst[0].DstReg.File == PROGRAM_OUTPUT &&
82 inst[0].DstReg.Index == FRAG_RESULT_COLOR &&
83 inst[0].DstReg.WriteMask == WRITEMASK_XYZW &&
84 inst[0].SrcReg[0].File == PROGRAM_INPUT &&
85 inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 &&
86 inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) {
87 return GL_TRUE;
88 }
89 }
90 return GL_FALSE;
91 }
92
93
94
95 /**
96 * Make fragment shader for glDraw/CopyPixels. This shader is made
97 * by combining the pixel transfer shader with the user-defined shader.
98 */
99 static struct st_fragment_program *
100 combined_drawpix_fragment_program(GLcontext *ctx)
101 {
102 struct st_context *st = st_context(ctx);
103 struct st_fragment_program *stfp;
104
105 if (st->pixel_xfer.program->serialNo == st->pixel_xfer.xfer_prog_sn
106 && st->fp->serialNo == st->pixel_xfer.user_prog_sn) {
107 /* the pixel tranfer program has not changed and the user-defined
108 * program has not changed, so re-use the combined program.
109 */
110 stfp = st->pixel_xfer.combined_prog;
111 }
112 else {
113 /* Concatenate the pixel transfer program with the current user-
114 * defined program.
115 */
116 if (is_passthrough_program(&st->fp->Base)) {
117 stfp = (struct st_fragment_program *)
118 _mesa_clone_program(ctx, &st->pixel_xfer.program->Base.Base);
119 }
120 else {
121 #if 0
122 printf("Base program:\n");
123 _mesa_print_program(&st->fp->Base.Base);
124 printf("DrawPix program:\n");
125 _mesa_print_program(&st->pixel_xfer.program->Base.Base);
126 #endif
127 stfp = (struct st_fragment_program *)
128 _mesa_combine_programs(ctx,
129 &st->pixel_xfer.program->Base.Base,
130 &st->fp->Base.Base);
131 }
132
133 #if 0
134 {
135 struct gl_program *p = &stfp->Base.Base;
136 printf("Combined DrawPixels program:\n");
137 _mesa_print_program(p);
138 printf("InputsRead: 0x%x\n", p->InputsRead);
139 printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
140 _mesa_print_parameter_list(p->Parameters);
141 }
142 #endif
143
144 /* translate to TGSI tokens */
145 st_translate_fragment_program(st, stfp, NULL);
146
147 /* save new program, update serial numbers */
148 st->pixel_xfer.xfer_prog_sn = st->pixel_xfer.program->serialNo;
149 st->pixel_xfer.user_prog_sn = st->fp->serialNo;
150 st->pixel_xfer.combined_prog_sn = stfp->serialNo;
151 /* can't reference new program directly, already have a reference on it */
152 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
153 st->pixel_xfer.combined_prog = stfp;
154 }
155
156 /* Ideally we'd have updated the pipe constants during the normal
157 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
158 */
159 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
160
161 return stfp;
162 }
163
164
165 /**
166 * Create fragment shader that does a TEX() instruction to get a Z
167 * value, then writes to FRAG_RESULT_DEPTH.
168 * Pass fragment color through as-is.
169 */
170 static struct st_fragment_program *
171 make_fragment_shader_z(struct st_context *st)
172 {
173 GLcontext *ctx = st->ctx;
174 struct gl_program *p;
175 GLuint ic = 0;
176
177 if (st->drawpix.z_shader) {
178 return st->drawpix.z_shader;
179 }
180
181 /*
182 * Create shader now
183 */
184 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
185 if (!p)
186 return NULL;
187
188 p->NumInstructions = 3;
189
190 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
191 if (!p->Instructions) {
192 ctx->Driver.DeleteProgram(ctx, p);
193 return NULL;
194 }
195 _mesa_init_instructions(p->Instructions, p->NumInstructions);
196
197 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
198 p->Instructions[ic].Opcode = OPCODE_TEX;
199 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
200 p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
201 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
202 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
203 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
204 p->Instructions[ic].TexSrcUnit = 0;
205 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
206 ic++;
207
208 /* MOV result.color, fragment.color */
209 p->Instructions[ic].Opcode = OPCODE_MOV;
210 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
211 p->Instructions[ic].DstReg.Index = FRAG_RESULT_COLOR;
212 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
213 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_COL0;
214 ic++;
215
216 /* END; */
217 p->Instructions[ic++].Opcode = OPCODE_END;
218
219 assert(ic == p->NumInstructions);
220
221 p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
222 p->OutputsWritten = (1 << FRAG_RESULT_COLOR) | (1 << FRAG_RESULT_DEPTH);
223 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
224
225 st->drawpix.z_shader = (struct st_fragment_program *) p;
226 st_translate_fragment_program(st, st->drawpix.z_shader, NULL);
227
228 return st->drawpix.z_shader;
229 }
230
231
232
233 /**
234 * Create a simple vertex shader that just passes through the
235 * vertex position and texcoord (and optionally, color).
236 */
237 static struct st_vertex_program *
238 st_make_passthrough_vertex_shader(struct st_context *st, GLboolean passColor)
239 {
240 GLcontext *ctx = st->ctx;
241 struct st_vertex_program *stvp;
242 struct gl_program *p;
243 GLuint ic = 0;
244
245 if (st->drawpix.vert_shaders[passColor])
246 return st->drawpix.vert_shaders[passColor];
247
248 /*
249 * Create shader now
250 */
251 p = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
252 if (!p)
253 return NULL;
254
255 if (passColor)
256 p->NumInstructions = 4;
257 else
258 p->NumInstructions = 3;
259
260 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
261 if (!p->Instructions) {
262 ctx->Driver.DeleteProgram(ctx, p);
263 return NULL;
264 }
265 _mesa_init_instructions(p->Instructions, p->NumInstructions);
266 /* MOV result.pos, vertex.pos; */
267 p->Instructions[0].Opcode = OPCODE_MOV;
268 p->Instructions[0].DstReg.File = PROGRAM_OUTPUT;
269 p->Instructions[0].DstReg.Index = VERT_RESULT_HPOS;
270 p->Instructions[0].SrcReg[0].File = PROGRAM_INPUT;
271 p->Instructions[0].SrcReg[0].Index = VERT_ATTRIB_POS;
272 /* MOV result.texcoord0, vertex.texcoord0; */
273 p->Instructions[1].Opcode = OPCODE_MOV;
274 p->Instructions[1].DstReg.File = PROGRAM_OUTPUT;
275 p->Instructions[1].DstReg.Index = VERT_RESULT_TEX0;
276 p->Instructions[1].SrcReg[0].File = PROGRAM_INPUT;
277 p->Instructions[1].SrcReg[0].Index = VERT_ATTRIB_TEX0;
278 ic = 2;
279 if (passColor) {
280 /* MOV result.color0, vertex.color0; */
281 p->Instructions[ic].Opcode = OPCODE_MOV;
282 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
283 p->Instructions[ic].DstReg.Index = VERT_RESULT_COL0;
284 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
285 p->Instructions[ic].SrcReg[0].Index = VERT_ATTRIB_COLOR0;
286 ic++;
287 }
288
289 /* END; */
290 p->Instructions[ic].Opcode = OPCODE_END;
291 ic++;
292
293 assert(ic == p->NumInstructions);
294
295 p->InputsRead = VERT_BIT_POS | VERT_BIT_TEX0;
296 p->OutputsWritten = ((1 << VERT_RESULT_TEX0) |
297 (1 << VERT_RESULT_HPOS));
298 if (passColor) {
299 p->InputsRead |= VERT_BIT_COLOR0;
300 p->OutputsWritten |= (1 << VERT_RESULT_COL0);
301 }
302
303 stvp = (struct st_vertex_program *) p;
304 st_translate_vertex_program(st, stvp, NULL, NULL, NULL);
305
306 st->drawpix.vert_shaders[passColor] = stvp;
307
308 return stvp;
309 }
310
311
312 static GLenum
313 _mesa_base_format(GLenum format)
314 {
315 switch (format) {
316 case GL_DEPTH_COMPONENT:
317 return GL_DEPTH_COMPONENT;
318 case GL_DEPTH_STENCIL:
319 return GL_DEPTH_STENCIL;
320 case GL_STENCIL_INDEX:
321 return GL_STENCIL_INDEX;
322 default:
323 return GL_RGBA;
324 }
325 }
326
327
328 /**
329 * Make texture containing an image for glDrawPixels image.
330 * If 'pixels' is NULL, leave the texture image data undefined.
331 */
332 static struct pipe_texture *
333 make_texture(struct st_context *st,
334 GLsizei width, GLsizei height, GLenum format, GLenum type,
335 const struct gl_pixelstore_attrib *unpack,
336 const GLvoid *pixels)
337 {
338 GLcontext *ctx = st->ctx;
339 struct pipe_context *pipe = st->pipe;
340 struct pipe_screen *screen = pipe->screen;
341 const struct gl_texture_format *mformat;
342 struct pipe_texture *pt;
343 enum pipe_format pipeFormat;
344 GLuint cpp;
345 GLenum baseFormat;
346 int ptw, pth;
347
348 baseFormat = _mesa_base_format(format);
349
350 mformat = st_ChooseTextureFormat(ctx, baseFormat, format, type);
351 assert(mformat);
352
353 pipeFormat = st_mesa_format_to_pipe_format(mformat->MesaFormat);
354 assert(pipeFormat);
355 cpp = st_sizeof_format(pipeFormat);
356
357 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
358 if (!pixels)
359 return NULL;
360
361 /* Need to use POT texture? */
362 ptw = width;
363 pth = height;
364 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES)) {
365 int l2pt, maxSize;
366
367 l2pt = util_logbase2(width);
368 if (1<<l2pt != width) {
369 ptw = 1<<(l2pt+1);
370 }
371 l2pt = util_logbase2(height);
372 if (1<<l2pt != height) {
373 pth = 1<<(l2pt+1);
374 }
375
376 /* Check against maximum texture size */
377 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
378 assert(ptw <= maxSize);
379 assert(pth <= maxSize);
380 }
381
382 pt = st_texture_create(st, PIPE_TEXTURE_2D, pipeFormat, 0, ptw, pth, 1,
383 PIPE_TEXTURE_USAGE_SAMPLER);
384 if (!pt) {
385 _mesa_unmap_pbo_source(ctx, unpack);
386 return NULL;
387 }
388
389 {
390 struct pipe_transfer *transfer;
391 static const GLuint dstImageOffsets = 0;
392 GLboolean success;
393 GLubyte *dest;
394 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
395 const StoreTexImageFunc storeImage =
396 _mesa_get_texstore_func(mformat->MesaFormat);
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 = storeImage(ctx, 2, /* dims */
414 baseFormat, /* baseInternalFormat */
415 mformat, /* gl_texture_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 width = (float) ctx->DrawBuffer->Width;
606 const float height = (float) ctx->DrawBuffer->Height;
607 struct pipe_viewport_state vp;
608 vp.scale[0] = 0.5f * width;
609 vp.scale[1] = -0.5f * height;
610 vp.scale[2] = 1.0f;
611 vp.scale[3] = 1.0f;
612 vp.translate[0] = 0.5f * width;
613 vp.translate[1] = 0.5f * height;
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(pipe, GL_DEPTH_COMPONENT, PIPE_TEXTURE_2D,
1022 PIPE_TEXTURE_USAGE_DEPTH_STENCIL);
1023 assert(texFormat != PIPE_FORMAT_NONE); /* XXX no depth texture formats??? */
1024 }
1025 else {
1026 /* default color format */
1027 texFormat = st_choose_format(pipe, GL_RGBA, PIPE_TEXTURE_2D,
1028 PIPE_TEXTURE_USAGE_SAMPLER);
1029 assert(texFormat != PIPE_FORMAT_NONE);
1030 }
1031 }
1032
1033 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1034 srcy = ctx->DrawBuffer->Height - srcy - height;
1035
1036 if (srcy < 0) {
1037 height -= -srcy;
1038 srcy = 0;
1039 }
1040
1041 if (height < 0)
1042 return;
1043 }
1044
1045 /* Need to use POT texture? */
1046 ptw = width;
1047 pth = height;
1048 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES)) {
1049 int l2pt, maxSize;
1050
1051 l2pt = util_logbase2(width);
1052 if (1<<l2pt != width) {
1053 ptw = 1<<(l2pt+1);
1054 }
1055 l2pt = util_logbase2(height);
1056 if (1<<l2pt != height) {
1057 pth = 1<<(l2pt+1);
1058 }
1059
1060 /* Check against maximum texture size */
1061 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
1062 assert(ptw <= maxSize);
1063 assert(pth <= maxSize);
1064 }
1065
1066 pt = st_texture_create(st, PIPE_TEXTURE_2D, texFormat, 0,
1067 ptw, pth, 1,
1068 PIPE_TEXTURE_USAGE_SAMPLER);
1069 if (!pt)
1070 return;
1071
1072
1073 if (srcFormat == texFormat) {
1074 /* copy source framebuffer surface into mipmap/texture */
1075 struct pipe_surface *psRead = screen->get_tex_surface(screen,
1076 rbRead->texture, 0, 0, 0,
1077 PIPE_BUFFER_USAGE_GPU_READ);
1078 struct pipe_surface *psTex = screen->get_tex_surface(screen, pt, 0, 0, 0,
1079 PIPE_BUFFER_USAGE_GPU_WRITE );
1080 pipe->surface_copy(pipe,
1081 psTex, /* dest */
1082 0, 0, /* destx/y */
1083 psRead,
1084 srcx, srcy, width, height);
1085 pipe_surface_reference(&psRead, NULL);
1086 pipe_surface_reference(&psTex, NULL);
1087 }
1088 else {
1089 /* CPU-based fallback/conversion */
1090 struct pipe_transfer *ptRead =
1091 st_cond_flush_get_tex_transfer(st, rbRead->texture, 0, 0, 0,
1092 PIPE_TRANSFER_READ, srcx, srcy, width,
1093 height);
1094 struct pipe_transfer *ptTex;
1095 enum pipe_transfer_usage transfer_usage;
1096
1097 if (type == GL_DEPTH && pf_is_depth_and_stencil(pt->format))
1098 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1099 else
1100 transfer_usage = PIPE_TRANSFER_WRITE;
1101
1102 ptTex = st_cond_flush_get_tex_transfer(st, pt, 0, 0, 0, transfer_usage,
1103 0, 0, width, height);
1104
1105 if (type == GL_COLOR) {
1106 /* alternate path using get/put_tile() */
1107 GLfloat *buf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
1108
1109 pipe_get_tile_rgba(ptRead, 0, 0, width, height, buf);
1110 pipe_put_tile_rgba(ptTex, 0, 0, width, height, buf);
1111
1112 _mesa_free(buf);
1113 }
1114 else {
1115 /* GL_DEPTH */
1116 GLuint *buf = (GLuint *) _mesa_malloc(width * height * sizeof(GLuint));
1117 pipe_get_tile_z(ptRead, 0, 0, width, height, buf);
1118 pipe_put_tile_z(ptTex, 0, 0, width, height, buf);
1119 _mesa_free(buf);
1120 }
1121
1122 screen->tex_transfer_destroy(ptRead);
1123 screen->tex_transfer_destroy(ptTex);
1124 }
1125
1126 /* draw textured quad */
1127 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1128 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1129 pt, stvp, stfp, color, GL_TRUE);
1130
1131 pipe_texture_reference(&pt, NULL);
1132 }
1133
1134
1135
1136 void st_init_drawpixels_functions(struct dd_function_table *functions)
1137 {
1138 functions->DrawPixels = st_DrawPixels;
1139 functions->CopyPixels = st_CopyPixels;
1140 }
1141
1142
1143 void
1144 st_destroy_drawpix(struct st_context *st)
1145 {
1146 st_reference_fragprog(st, &st->drawpix.z_shader, NULL);
1147 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1148 st_reference_vertprog(st, &st->drawpix.vert_shaders[0], NULL);
1149 st_reference_vertprog(st, &st->drawpix.vert_shaders[1], NULL);
1150 }