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