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