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