1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36 #include "main/texformat.h"
37 #include "shader/program.h"
38 #include "shader/prog_parameter.h"
39 #include "shader/prog_print.h"
41 #include "st_context.h"
43 #include "st_atom_constbuf.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"
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 "pipe/util/p_tile.h"
60 #include "shader/prog_instruction.h"
64 * Check if the given program is:
65 * 0: MOVE result.color, fragment.color;
69 is_passthrough_program(const struct gl_fragment_program
*prog
)
71 if (prog
->Base
.NumInstructions
== 2) {
72 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
73 if (inst
[0].Opcode
== OPCODE_MOV
&&
74 inst
[1].Opcode
== OPCODE_END
&&
75 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
76 inst
[0].DstReg
.Index
== FRAG_RESULT_COLR
&&
77 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
78 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
79 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
80 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
89 * Make fragment program for glBitmap:
90 * Sample the texture and kill the fragment if the bit is 0.
91 * This program will be combined with the user's fragment program.
93 static struct st_fragment_program
*
94 make_bitmap_fragment_program(GLcontext
*ctx
)
96 struct st_fragment_program
*stfp
;
100 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
104 p
->NumInstructions
= 5;
106 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
107 if (!p
->Instructions
) {
108 ctx
->Driver
.DeleteProgram(ctx
, p
);
111 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
113 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
114 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
115 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
116 p
->Instructions
[ic
].DstReg
.Index
= 0;
117 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
118 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
119 p
->Instructions
[ic
].TexSrcUnit
= 0;
120 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
123 /* SWZ tmp0.x, tmp0.x, 1111; # tmp0.x = 1.0 */
124 p
->Instructions
[ic
].Opcode
= OPCODE_SWZ
;
125 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
126 p
->Instructions
[ic
].DstReg
.Index
= 0;
127 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_X
;
128 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
129 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
130 p
->Instructions
[ic
].SrcReg
[0].Swizzle
131 = MAKE_SWIZZLE4(SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
);
134 /* SUB tmp0, tmp0.wwww, tmp0.xxxx; # tmp0.w -= 1 */
135 p
->Instructions
[ic
].Opcode
= OPCODE_SUB
;
136 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
137 p
->Instructions
[ic
].DstReg
.Index
= 0;
138 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
139 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
140 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_WWWW
;
141 p
->Instructions
[ic
].SrcReg
[1].File
= PROGRAM_TEMPORARY
;
142 p
->Instructions
[ic
].SrcReg
[1].Index
= 0;
143 p
->Instructions
[ic
].SrcReg
[1].Swizzle
= SWIZZLE_XXXX
; /* 1.0 */
146 /* KIL if tmp0 < 0 */
147 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
148 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
149 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
153 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
155 assert(ic
== p
->NumInstructions
);
157 p
->InputsRead
= FRAG_BIT_TEX0
;
158 p
->OutputsWritten
= 0x0;
160 stfp
= (struct st_fragment_program
*) p
;
161 stfp
->Base
.UsesKill
= GL_TRUE
;
162 st_translate_fragment_program(ctx
->st
, stfp
, NULL
,
163 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
170 * Combine basic bitmap fragment program with the user-defined program.
172 static struct st_fragment_program
*
173 combined_bitmap_fragment_program(GLcontext
*ctx
)
175 struct st_context
*st
= ctx
->st
;
176 struct st_fragment_program
*stfp
;
178 if (!st
->bitmap
.program
) {
179 /* create the basic bitmap fragment program */
180 st
->bitmap
.program
= make_bitmap_fragment_program(ctx
);
183 if (st
->bitmap
.user_prog_sn
== st
->fp
->serialNo
) {
185 stfp
= st
->bitmap
.combined_prog
;
188 /* Concatenate the bitmap program with the current user-defined program.
190 stfp
= (struct st_fragment_program
*)
191 _mesa_combine_programs(ctx
,
192 &st
->bitmap
.program
->Base
.Base
,
197 struct gl_program
*p
= &stfp
->Base
.Base
;
198 printf("Combined bitmap program:\n");
199 _mesa_print_program(p
);
200 printf("InputsRead: 0x%x\n", p
->InputsRead
);
201 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
202 _mesa_print_parameter_list(p
->Parameters
);
206 /* translate to TGSI tokens */
207 st_translate_fragment_program(st
, stfp
, NULL
,
208 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
210 /* save new program, update serial numbers */
211 st
->bitmap
.user_prog_sn
= st
->fp
->serialNo
;
212 st
->bitmap
.combined_prog
= stfp
;
215 /* Ideally we'd have updated the pipe constants during the normal
216 * st/atom mechanism. But we can't since this is specific to glBitmap.
218 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
226 * Make fragment shader for glDraw/CopyPixels. This shader is made
227 * by combining the pixel transfer shader with the user-defined shader.
229 static struct st_fragment_program
*
230 combined_drawpix_fragment_program(GLcontext
*ctx
)
232 struct st_context
*st
= ctx
->st
;
233 struct st_fragment_program
*stfp
;
235 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
236 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
237 /* the pixel tranfer program has not changed and the user-defined
238 * program has not changed, so re-use the combined program.
240 stfp
= st
->pixel_xfer
.combined_prog
;
243 /* Concatenate the pixel transfer program with the current user-
246 if (is_passthrough_program(&st
->fp
->Base
)) {
247 stfp
= (struct st_fragment_program
*)
248 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
251 stfp
= (struct st_fragment_program
*)
252 _mesa_combine_programs(ctx
,
253 &st
->pixel_xfer
.program
->Base
.Base
,
259 struct gl_program
*p
= &stfp
->Base
.Base
;
260 printf("Combined DrawPixels program:\n");
261 _mesa_print_program(p
);
262 printf("InputsRead: 0x%x\n", p
->InputsRead
);
263 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
264 _mesa_print_parameter_list(p
->Parameters
);
268 /* translate to TGSI tokens */
269 st_translate_fragment_program(st
, stfp
, NULL
,
270 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
272 /* save new program, update serial numbers */
273 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
274 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
275 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
276 st
->pixel_xfer
.combined_prog
= stfp
;
279 /* Ideally we'd have updated the pipe constants during the normal
280 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
282 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
289 * Create fragment shader that does a TEX() instruction to get a Z
290 * value, then writes to FRAG_RESULT_DEPR.
291 * Pass fragment color through as-is.
293 static struct st_fragment_program
*
294 make_fragment_shader_z(struct st_context
*st
)
296 GLcontext
*ctx
= st
->ctx
;
297 /* only make programs once and re-use */
298 static struct st_fragment_program
*stfp
= NULL
;
299 struct gl_program
*p
;
305 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
309 p
->NumInstructions
= 3;
311 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
312 if (!p
->Instructions
) {
313 ctx
->Driver
.DeleteProgram(ctx
, p
);
316 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
318 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
319 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
320 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
321 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPR
;
322 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
323 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
324 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
325 p
->Instructions
[ic
].TexSrcUnit
= 0;
326 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
329 /* MOV result.color, fragment.color */
330 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
331 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
332 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
333 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
334 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
338 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
340 assert(ic
== p
->NumInstructions
);
342 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
343 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
) | (1 << FRAG_RESULT_DEPR
);
345 stfp
= (struct st_fragment_program
*) p
;
346 st_translate_fragment_program(st
, stfp
, NULL
,
347 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
355 * Create a simple vertex shader that just passes through the
356 * vertex position and texcoord (and optionally, color).
358 static struct st_vertex_program
*
359 make_vertex_shader(struct st_context
*st
, GLboolean passColor
)
361 /* only make programs once and re-use */
362 static struct st_vertex_program
*progs
[2] = { NULL
, NULL
};
363 GLcontext
*ctx
= st
->ctx
;
364 struct st_vertex_program
*stvp
;
365 struct gl_program
*p
;
368 if (progs
[passColor
])
369 return progs
[passColor
];
371 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
376 p
->NumInstructions
= 4;
378 p
->NumInstructions
= 3;
380 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
381 if (!p
->Instructions
) {
382 ctx
->Driver
.DeleteProgram(ctx
, p
);
385 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
386 /* MOV result.pos, vertex.pos; */
387 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
388 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
389 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
390 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
391 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
392 /* MOV result.texcoord0, vertex.texcoord0; */
393 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
394 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
395 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
396 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
397 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
400 /* MOV result.color0, vertex.color0; */
401 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
402 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
403 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
404 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
405 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
410 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
413 assert(ic
== p
->NumInstructions
);
415 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
416 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
417 (1 << VERT_RESULT_HPOS
));
419 p
->InputsRead
|= VERT_BIT_COLOR0
;
420 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
423 stvp
= (struct st_vertex_program
*) p
;
424 st_translate_vertex_program(st
, stvp
, NULL
,
425 stvp
->tokens
, ST_MAX_SHADER_TOKENS
);
427 progs
[passColor
] = stvp
;
434 _mesa_base_format(GLenum format
)
437 case GL_DEPTH_COMPONENT
:
438 return GL_DEPTH_COMPONENT
;
439 case GL_STENCIL_INDEX
:
440 return GL_STENCIL_INDEX
;
448 * Make texture containing an image for glDrawPixels image.
449 * If 'pixels' is NULL, leave the texture image data undefined.
451 static struct pipe_texture
*
452 make_texture(struct st_context
*st
,
453 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
454 const struct gl_pixelstore_attrib
*unpack
,
455 const GLvoid
*pixels
)
457 GLcontext
*ctx
= st
->ctx
;
458 struct pipe_context
*pipe
= st
->pipe
;
459 const struct gl_texture_format
*mformat
;
460 struct pipe_texture
*pt
;
461 enum pipe_format pipeFormat
;
465 baseFormat
= _mesa_base_format(format
);
467 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
470 pipeFormat
= st_mesa_format_to_pipe_format(mformat
->MesaFormat
);
472 cpp
= st_sizeof_format(pipeFormat
);
474 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, 0, width
, height
,
479 if (unpack
->BufferObj
&& unpack
->BufferObj
->Name
) {
481 pt->region = buffer_object_region(unpack->BufferObj);
483 printf("st_DrawPixels (sourcing from PBO not implemented yet)\n");
487 struct pipe_surface
*surface
;
488 static const GLuint dstImageOffsets
= 0;
491 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
493 /* we'll do pixel transfer in a fragment shader */
494 ctx
->_ImageTransferState
= 0x0;
496 surface
= pipe
->get_tex_surface(pipe
, pt
, 0, 0, 0);
498 /* map texture surface */
499 dest
= pipe_surface_map(surface
);
501 /* Put image into texture surface.
502 * Note that the image is actually going to be upside down in
503 * the texture. We deal with that with texcoords.
505 success
= mformat
->StoreImage(ctx
, 2, /* dims */
506 baseFormat
, /* baseInternalFormat */
507 mformat
, /* gl_texture_format */
509 0, 0, 0, /* dstX/Y/Zoffset */
510 surface
->pitch
* cpp
, /* dstRowStride, bytes */
511 &dstImageOffsets
, /* dstImageOffsets */
512 width
, height
, 1, /* size */
513 format
, type
, /* src format/type */
514 pixels
, /* data source */
518 pipe_surface_unmap(surface
);
519 pipe_surface_reference(&surface
, NULL
);
523 ctx
->_ImageTransferState
= imageTransferStateSave
;
531 * Draw textured quad.
532 * Coords are window coords with y=0=bottom.
535 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
536 GLfloat x1
, GLfloat y1
, GLboolean invertTex
)
538 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
540 GLfloat sLeft
= 0.0, sRight
= 1.0;
541 GLfloat tTop
= invertTex
, tBot
= 1.0 - tTop
;
544 verts
[0][0][0] = x0
; /* attr[0].x */
545 verts
[0][0][1] = y0
; /* attr[0].y */
546 verts
[0][1][0] = sLeft
; /* attr[1].s */
547 verts
[0][1][1] = tTop
; /* attr[1].t */
552 verts
[1][1][0] = sRight
;
553 verts
[1][1][1] = tTop
;
558 verts
[2][1][0] = sRight
;
559 verts
[2][1][1] = tBot
;
564 verts
[3][1][0] = sLeft
;
565 verts
[3][1][1] = tBot
;
567 /* same for all verts: */
568 for (i
= 0; i
< 4; i
++) {
569 verts
[i
][0][2] = z
; /*Z*/
570 verts
[i
][0][3] = 1.0; /*W*/
571 verts
[i
][1][2] = 0.0; /*R*/
572 verts
[i
][1][3] = 1.0; /*Q*/
575 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 2);
580 draw_quad_colored(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
581 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
584 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
586 GLfloat sLeft
= 0.0, sRight
= 1.0;
587 GLfloat tTop
= invertTex
, tBot
= 1.0 - tTop
;
590 verts
[0][0][0] = x0
; /* attr[0].x */
591 verts
[0][0][1] = y0
; /* attr[0].y */
592 verts
[0][2][0] = sLeft
; /* attr[2].s */
593 verts
[0][2][1] = tTop
; /* attr[2].t */
598 verts
[1][2][0] = sRight
;
599 verts
[1][2][1] = tTop
;
604 verts
[2][2][0] = sRight
;
605 verts
[2][2][1] = tBot
;
610 verts
[3][2][0] = sLeft
;
611 verts
[3][2][1] = tBot
;
613 /* same for all verts: */
614 for (i
= 0; i
< 4; i
++) {
615 verts
[i
][0][2] = z
; /*Z*/
616 verts
[i
][0][3] = 1.0; /*W*/
617 verts
[i
][1][0] = color
[0];
618 verts
[i
][1][1] = color
[1];
619 verts
[i
][1][2] = color
[2];
620 verts
[i
][1][3] = color
[3];
621 verts
[i
][2][2] = 0.0; /*R*/
622 verts
[i
][2][3] = 1.0; /*Q*/
625 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 3);
631 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
632 GLsizei width
, GLsizei height
,
633 GLfloat zoomX
, GLfloat zoomY
,
634 struct pipe_texture
*pt
,
635 struct st_vertex_program
*stvp
,
636 struct st_fragment_program
*stfp
,
637 const GLfloat
*color
,
640 const GLuint unit
= 0;
641 struct pipe_context
*pipe
= ctx
->st
->pipe
;
642 GLfloat x0
, y0
, x1
, y1
;
646 /* XXX if DrawPixels image is larger than max texture size, break
649 maxSize
= 1 << (pipe
->get_param(pipe
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
650 assert(width
<= maxSize
);
651 assert(height
<= maxSize
);
653 /* setup state: just scissor */
655 struct pipe_rasterizer_state setup
;
656 const struct cso_rasterizer
*cso
;
657 memset(&setup
, 0, sizeof(setup
));
658 if (ctx
->Scissor
.Enabled
)
660 cso
= st_cached_rasterizer_state(ctx
->st
, &setup
);
661 pipe
->bind_rasterizer_state(pipe
, cso
->data
);
664 /* fragment shader state: TEX lookup program */
665 pipe
->bind_fs_state(pipe
, stfp
->fs
->data
);
667 /* vertex shader state: position + texcoord pass-through */
668 pipe
->bind_vs_state(pipe
, stvp
->cso
->data
);
670 /* texture sampling state: */
672 struct pipe_sampler_state sampler
;
673 const struct cso_sampler
*cso
;
674 memset(&sampler
, 0, sizeof(sampler
));
675 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
676 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
677 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
678 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
679 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
680 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
681 sampler
.normalized_coords
= 1;
682 cso
= st_cached_sampler_state(ctx
->st
, &sampler
);
683 pipe
->bind_sampler_state(pipe
, unit
, cso
->data
);
686 /* viewport state: viewport matching window dims */
688 const float width
= ctx
->DrawBuffer
->Width
;
689 const float height
= ctx
->DrawBuffer
->Height
;
690 struct pipe_viewport_state vp
;
691 vp
.scale
[0] = 0.5 * width
;
692 vp
.scale
[1] = -0.5 * height
;
695 vp
.translate
[0] = 0.5 * width
;
696 vp
.translate
[1] = 0.5 * height
;
697 vp
.translate
[2] = 0.0;
698 vp
.translate
[3] = 0.0;
699 pipe
->set_viewport_state(pipe
, &vp
);
704 pipe
->set_sampler_texture(pipe
, unit
, pt
);
707 /* Compute window coords (y=0=bottom) with pixel zoom.
708 * Recall that these coords are transformed by the current
709 * vertex shader and viewport transformation.
712 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
714 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
716 /* draw textured quad */
718 draw_quad_colored(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
);
720 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, invertTex
);
722 /* restore GL state */
723 pipe
->bind_rasterizer_state(pipe
, ctx
->st
->state
.rasterizer
->data
);
724 pipe
->bind_fs_state(pipe
, ctx
->st
->state
.fs
->data
);
725 pipe
->bind_vs_state(pipe
, ctx
->st
->state
.vs
->cso
->data
);
726 pipe
->set_sampler_texture(pipe
, unit
, ctx
->st
->state
.sampler_texture
[unit
]);
727 pipe
->bind_sampler_state(pipe
, unit
, ctx
->st
->state
.sampler
[unit
]->data
);
728 pipe
->set_viewport_state(pipe
, &ctx
->st
->state
.viewport
);
733 * Check if a GL format/type combination is a match to the given pipe format.
734 * XXX probably move this to a re-usable place.
737 compatible_formats(GLenum format
, GLenum type
, enum pipe_format pipeFormat
)
739 static const GLuint one
= 1;
740 GLubyte littleEndian
= *((GLubyte
*) &one
);
742 if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
744 type
== GL_UNSIGNED_BYTE
&&
748 else if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
749 format
== GL_ABGR_EXT
&&
750 type
== GL_UNSIGNED_BYTE
&&
754 else if (pipeFormat
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
756 type
== GL_UNSIGNED_BYTE
&&
760 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
762 type
== GL_UNSIGNED_SHORT_5_6_5
) {
763 /* endian don't care */
766 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
768 type
== GL_UNSIGNED_SHORT_5_6_5_REV
) {
769 /* endian don't care */
772 else if (pipeFormat
== PIPE_FORMAT_S8_UNORM
&&
773 format
== GL_STENCIL_INDEX
&&
774 type
== GL_UNSIGNED_BYTE
) {
777 else if (pipeFormat
== PIPE_FORMAT_Z32_UNORM
&&
778 format
== GL_DEPTH_COMPONENT
&&
779 type
== GL_UNSIGNED_INT
) {
782 /* XXX add more cases */
790 * Check if any per-fragment ops are enabled.
791 * XXX probably move this to a re-usable place.
794 any_fragment_ops(const struct st_context
*st
)
796 if (st
->state
.depth_stencil
->state
.alpha
.enabled
||
797 st
->state
.blend
->state
.blend_enable
||
798 st
->state
.blend
->state
.logicop_enable
||
799 st
->state
.depth_stencil
->state
.depth
.enabled
)
800 /* XXX more checks */
808 * Check if any pixel transfer ops are enabled.
809 * XXX probably move this to a re-usable place.
812 any_pixel_transfer_ops(const struct st_context
*st
)
814 if (st
->ctx
->Pixel
.RedScale
!= 1.0 ||
815 st
->ctx
->Pixel
.RedBias
!= 0.0 ||
816 st
->ctx
->Pixel
.GreenScale
!= 1.0 ||
817 st
->ctx
->Pixel
.GreenBias
!= 0.0 ||
818 st
->ctx
->Pixel
.BlueScale
!= 1.0 ||
819 st
->ctx
->Pixel
.BlueBias
!= 0.0 ||
820 st
->ctx
->Pixel
.AlphaScale
!= 1.0 ||
821 st
->ctx
->Pixel
.AlphaBias
!= 0.0 ||
822 st
->ctx
->Pixel
.MapColorFlag
)
823 /* XXX more checks */
831 * Draw image with a blit, or other non-textured quad method.
834 draw_blit(struct st_context
*st
,
835 GLsizei width
, GLsizei height
,
836 GLenum format
, GLenum type
, const GLvoid
*pixels
)
844 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
845 GLsizei width
, GLsizei height
, GLenum type
,
846 const struct gl_pixelstore_attrib
*unpack
,
847 const GLvoid
*pixels
)
849 struct st_context
*st
= ctx
->st
;
850 struct pipe_context
*pipe
= st
->pipe
;
851 struct pipe_surface
*ps
= st
->state
.framebuffer
.zsbuf
;
852 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
856 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
858 /* map the stencil buffer */
859 stmap
= pipe_surface_map(ps
);
861 /* if width > MAX_WIDTH, have to process image in chunks */
863 while (skipPixels
< width
) {
864 const GLint spanX
= x
+ skipPixels
;
865 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
867 for (row
= 0; row
< height
; row
++) {
868 GLint spanY
= y
+ row
;
869 GLubyte values
[MAX_WIDTH
];
870 GLenum destType
= GL_UNSIGNED_BYTE
;
871 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
873 GL_COLOR_INDEX
, type
,
875 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
876 type
, source
, unpack
,
877 ctx
->_ImageTransferState
);
880 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
881 spanX, spanY, values);
885 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
886 spanY
= ctx
->DrawBuffer
->Height
- spanY
- 1;
889 switch (ps
->format
) {
890 case PIPE_FORMAT_U_S8
:
892 ubyte
*dest
= stmap
+ spanY
* ps
->pitch
+ spanX
;
893 memcpy(dest
, values
, spanWidth
);
896 case PIPE_FORMAT_S8Z24_UNORM
:
898 uint
*dest
= (uint
*) stmap
+ spanY
* ps
->pitch
+ spanX
;
900 for (k
= 0; k
< spanWidth
; k
++) {
902 p
= (p
& 0xffffff) | (values
[k
] << 24);
912 skipPixels
+= spanWidth
;
915 /* unmap the stencil buffer */
916 pipe_surface_unmap(ps
);
921 * Called via ctx->Driver.DrawPixels()
924 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
925 GLenum format
, GLenum type
,
926 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
928 struct st_fragment_program
*stfp
;
929 struct st_vertex_program
*stvp
;
930 struct st_context
*st
= ctx
->st
;
931 struct pipe_surface
*ps
;
933 const GLfloat
*color
;
935 if (format
== GL_STENCIL_INDEX
) {
936 draw_stencil_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
940 st_validate_state(st
);
942 if (format
== GL_DEPTH_COMPONENT
) {
943 ps
= st
->state
.framebuffer
.zsbuf
;
944 stfp
= make_fragment_shader_z(ctx
->st
);
945 stvp
= make_vertex_shader(ctx
->st
, GL_TRUE
);
946 color
= ctx
->Current
.RasterColor
;
948 else if (format
== GL_STENCIL_INDEX
) {
949 ps
= st
->state
.framebuffer
.zsbuf
;
950 /* XXX special case - can't use texture map */
954 ps
= st
->state
.framebuffer
.cbufs
[0];
955 stfp
= combined_drawpix_fragment_program(ctx
);
956 stvp
= make_vertex_shader(ctx
->st
, GL_FALSE
);
960 bufferFormat
= ps
->format
;
962 if (any_fragment_ops(st
) ||
963 any_pixel_transfer_ops(st
) ||
964 !compatible_formats(format
, type
, ps
->format
)) {
966 struct pipe_texture
*pt
967 = make_texture(ctx
->st
, width
, height
, format
, type
, unpack
, pixels
);
969 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
970 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
971 pt
, stvp
, stfp
, color
, GL_FALSE
);
972 st
->pipe
->texture_release(st
->pipe
, &pt
);
977 draw_blit(st
, width
, height
, format
, type
, pixels
);
984 * Create a texture which represents a bitmap image.
986 static struct pipe_texture
*
987 make_bitmap_texture(GLcontext
*ctx
, GLsizei width
, GLsizei height
,
988 const struct gl_pixelstore_attrib
*unpack
,
989 const GLubyte
*bitmap
)
991 struct pipe_context
*pipe
= ctx
->st
->pipe
;
992 struct pipe_surface
*surface
;
993 uint format
= 0, cpp
, comp
;
995 struct pipe_texture
*pt
;
998 /* find a texture format we know */
999 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
, PIPE_TEXTURE
)) {
1000 format
= PIPE_FORMAT_U_I8
;
1004 else if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A8R8G8B8_UNORM
, PIPE_TEXTURE
)) {
1005 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
1007 comp
= 3; /* alpha channel */ /*XXX little-endian dependency */
1010 /* XXX support more formats */
1017 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, format
, 0, 0, width
, height
,
1022 if (unpack
->BufferObj
&& unpack
->BufferObj
->Name
) {
1024 pt->region = buffer_object_region(unpack->BufferObj);
1026 printf("st_Bitmap (sourcing from PBO not implemented yet)\n");
1029 surface
= pipe
->get_tex_surface(pipe
, pt
, 0, 0, 0);
1031 /* map texture surface */
1032 dest
= pipe_surface_map(surface
);
1034 /* Put image into texture surface.
1035 * Note that the image is actually going to be upside down in
1036 * the texture. We deal with that with texcoords.
1039 for (row
= 0; row
< height
; row
++) {
1040 const GLubyte
*src
= (const GLubyte
*) _mesa_image_address2d(unpack
,
1041 bitmap
, width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
1042 ubyte
*destRow
= dest
+ row
* surface
->pitch
* cpp
;
1044 if (unpack
->LsbFirst
) {
1046 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1047 for (col
= 0; col
< width
; col
++) {
1049 /* set texel to 255 if bit is set */
1050 destRow
[comp
] = (*src
& mask
) ? 255 : 0;
1062 /* get ready for next row */
1068 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1069 for (col
= 0; col
< width
; col
++) {
1071 /* set texel to 255 if bit is set */
1072 destRow
[comp
] =(*src
& mask
) ? 255 : 0;
1084 /* get ready for next row */
1091 /* Release surface */
1092 pipe_surface_unmap(surface
);
1093 pipe_surface_reference(&surface
, NULL
);
1095 pt
->format
= format
;
1103 st_Bitmap(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1104 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
1106 struct st_fragment_program
*stfp
;
1107 struct st_vertex_program
*stvp
;
1108 struct st_context
*st
= ctx
->st
;
1109 struct pipe_texture
*pt
;
1111 stvp
= make_vertex_shader(ctx
->st
, GL_TRUE
);
1112 stfp
= combined_bitmap_fragment_program(ctx
);
1114 st_validate_state(st
);
1116 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
1118 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1119 width
, height
, 1.0, 1.0,
1121 ctx
->Current
.RasterColor
, GL_FALSE
);
1123 st
->pipe
->texture_release(st
->pipe
, &pt
);
1129 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
1130 GLsizei width
, GLsizei height
,
1131 GLint dstx
, GLint dsty
)
1133 struct st_renderbuffer
*rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_StencilBuffer
);
1134 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1135 struct pipe_surface
*psRead
= rbRead
->surface
;
1136 struct pipe_surface
*psDraw
= rbDraw
->surface
;
1137 ubyte
*readMap
, *drawMap
;
1141 buffer
= malloc(width
* height
* sizeof(ubyte
));
1143 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1147 /* map the stencil buffers */
1148 readMap
= pipe_surface_map(psRead
);
1149 drawMap
= pipe_surface_map(psDraw
);
1151 /* this will do stencil pixel transfer ops */
1152 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, GL_UNSIGNED_BYTE
,
1153 &ctx
->DefaultPacking
, buffer
);
1156 /* XXX PixelZoom not handled yet */
1157 for (i
= 0; i
< height
; i
++) {
1164 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1165 y
= ctx
->DrawBuffer
->Height
- y
- 1;
1168 dst
= drawMap
+ (y
* psDraw
->pitch
+ dstx
) * psDraw
->cpp
;
1169 src
= buffer
+ i
* width
;
1171 switch (psDraw
->format
) {
1172 case PIPE_FORMAT_S8Z24_UNORM
:
1174 uint
*dst4
= (uint
*) dst
;
1176 for (j
= 0; j
< width
; j
++) {
1177 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1182 case PIPE_FORMAT_U_S8
:
1183 memcpy(dst
, src
, width
);
1192 /* unmap the stencil buffers */
1193 pipe_surface_unmap(psRead
);
1194 pipe_surface_unmap(psDraw
);
1199 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
1200 GLsizei width
, GLsizei height
,
1201 GLint dstx
, GLint dsty
, GLenum type
)
1203 struct st_context
*st
= ctx
->st
;
1204 struct pipe_context
*pipe
= st
->pipe
;
1205 struct st_renderbuffer
*rbRead
;
1206 struct st_vertex_program
*stvp
;
1207 struct st_fragment_program
*stfp
;
1208 struct pipe_surface
*psRead
;
1209 struct pipe_surface
*psTex
;
1210 struct pipe_texture
*pt
;
1214 /* make sure rendering has completed */
1215 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
1217 st_validate_state(st
);
1219 if (type
== GL_STENCIL
) {
1220 /* can't use texturing to do stencil */
1221 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1225 if (type
== GL_COLOR
) {
1226 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
1228 stfp
= combined_drawpix_fragment_program(ctx
);
1229 stvp
= make_vertex_shader(ctx
->st
, GL_FALSE
);
1232 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1233 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1234 stfp
= make_fragment_shader_z(ctx
->st
);
1235 stvp
= make_vertex_shader(ctx
->st
, GL_TRUE
);
1238 psRead
= rbRead
->surface
;
1239 format
= psRead
->format
;
1241 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, format
, 0, 0, width
, height
,
1246 psTex
= pipe
->get_tex_surface(pipe
, pt
, 0, 0, 0);
1248 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1249 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1252 /* For some drivers (like Xlib) it's not possible to treat the
1253 * front/back color buffers as surfaces (they're XImages and Pixmaps).
1254 * So, this var tells us if we can use surface_copy here...
1256 if (st
->haveFramebufferSurfaces
) {
1257 /* copy source framebuffer surface into mipmap/texture */
1258 pipe
->surface_copy(pipe
,
1262 srcx
, srcy
, width
, height
);
1265 /* alternate path using get/put_tile() */
1266 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1268 pipe_get_tile_rgba(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1269 pipe_put_tile_rgba(pipe
, psTex
, 0, 0, width
, height
, buf
);
1274 /* draw textured quad */
1275 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1276 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1277 pt
, stvp
, stfp
, color
, GL_TRUE
);
1279 pipe_surface_reference(&psTex
, NULL
);
1280 st
->pipe
->texture_release(st
->pipe
, &pt
);
1285 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1287 functions
->DrawPixels
= st_DrawPixels
;
1288 functions
->CopyPixels
= st_CopyPixels
;
1289 functions
->Bitmap
= st_Bitmap
;