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