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 "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 struct st_vertex_program
*
359 st_make_passthrough_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, 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, GL_FALSE
);
580 draw_quad_colored(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
581 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
584 GLfloat bias
= ctx
->st
->bitmap_texcoord_bias
;
585 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
587 GLfloat xBias
= bias
/ (x1
-x0
);
588 GLfloat yBias
= bias
/ (y1
-y0
);
589 GLfloat sLeft
= 0.0 + xBias
, sRight
= 1.0 + xBias
;
590 GLfloat tTop
= invertTex
- yBias
, tBot
= 1.0 - tTop
- yBias
;
593 verts
[0][0][0] = x0
; /* attr[0].x */
594 verts
[0][0][1] = y0
; /* attr[0].y */
595 verts
[0][2][0] = sLeft
; /* attr[2].s */
596 verts
[0][2][1] = tTop
; /* attr[2].t */
601 verts
[1][2][0] = sRight
;
602 verts
[1][2][1] = tTop
;
607 verts
[2][2][0] = sRight
;
608 verts
[2][2][1] = tBot
;
613 verts
[3][2][0] = sLeft
;
614 verts
[3][2][1] = tBot
;
616 /* same for all verts: */
617 for (i
= 0; i
< 4; i
++) {
618 verts
[i
][0][2] = z
; /*Z*/
619 verts
[i
][0][3] = 1.0; /*W*/
620 verts
[i
][1][0] = color
[0];
621 verts
[i
][1][1] = color
[1];
622 verts
[i
][1][2] = color
[2];
623 verts
[i
][1][3] = color
[3];
624 verts
[i
][2][2] = 0.0; /*R*/
625 verts
[i
][2][3] = 1.0; /*Q*/
628 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 3, GL_FALSE
);
634 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
635 GLsizei width
, GLsizei height
,
636 GLfloat zoomX
, GLfloat zoomY
,
637 struct pipe_texture
*pt
,
638 struct st_vertex_program
*stvp
,
639 struct st_fragment_program
*stfp
,
640 const GLfloat
*color
,
643 const GLuint unit
= 0;
644 struct pipe_context
*pipe
= ctx
->st
->pipe
;
645 GLfloat x0
, y0
, x1
, y1
;
649 /* XXX if DrawPixels image is larger than max texture size, break
652 maxSize
= 1 << (pipe
->get_param(pipe
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
653 assert(width
<= maxSize
);
654 assert(height
<= maxSize
);
656 /* setup state: just scissor */
658 struct pipe_rasterizer_state setup
;
659 const struct cso_rasterizer
*cso
;
660 memset(&setup
, 0, sizeof(setup
));
661 if (ctx
->Scissor
.Enabled
)
663 cso
= st_cached_rasterizer_state(ctx
->st
, &setup
);
664 pipe
->bind_rasterizer_state(pipe
, cso
->data
);
667 /* fragment shader state: TEX lookup program */
668 pipe
->bind_fs_state(pipe
, stfp
->cso
->data
);
670 /* vertex shader state: position + texcoord pass-through */
671 pipe
->bind_vs_state(pipe
, stvp
->cso
->data
);
673 /* texture sampling state: */
675 struct pipe_sampler_state sampler
;
676 const struct cso_sampler
*cso
;
677 memset(&sampler
, 0, sizeof(sampler
));
678 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
679 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
680 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
681 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
682 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
683 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
684 sampler
.normalized_coords
= 1;
685 cso
= st_cached_sampler_state(ctx
->st
, &sampler
);
686 pipe
->bind_sampler_state(pipe
, unit
, cso
->data
);
689 /* viewport state: viewport matching window dims */
691 const float width
= ctx
->DrawBuffer
->Width
;
692 const float height
= ctx
->DrawBuffer
->Height
;
693 struct pipe_viewport_state vp
;
694 vp
.scale
[0] = 0.5 * width
;
695 vp
.scale
[1] = -0.5 * height
;
698 vp
.translate
[0] = 0.5 * width
;
699 vp
.translate
[1] = 0.5 * height
;
700 vp
.translate
[2] = 0.0;
701 vp
.translate
[3] = 0.0;
702 pipe
->set_viewport_state(pipe
, &vp
);
707 pipe
->set_sampler_texture(pipe
, unit
, pt
);
710 /* Compute window coords (y=0=bottom) with pixel zoom.
711 * Recall that these coords are transformed by the current
712 * vertex shader and viewport transformation.
715 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
717 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
719 /* draw textured quad */
721 draw_quad_colored(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
);
723 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, invertTex
);
725 /* restore GL state */
726 pipe
->bind_rasterizer_state(pipe
, ctx
->st
->state
.rasterizer
->data
);
727 pipe
->bind_fs_state(pipe
, ctx
->st
->state
.fs
->data
);
728 pipe
->bind_vs_state(pipe
, ctx
->st
->state
.vs
->cso
->data
);
729 pipe
->set_sampler_texture(pipe
, unit
, ctx
->st
->state
.sampler_texture
[unit
]);
730 pipe
->bind_sampler_state(pipe
, unit
, ctx
->st
->state
.sampler
[unit
]->data
);
731 pipe
->set_viewport_state(pipe
, &ctx
->st
->state
.viewport
);
736 * Check if a GL format/type combination is a match to the given pipe format.
737 * XXX probably move this to a re-usable place.
740 compatible_formats(GLenum format
, GLenum type
, enum pipe_format pipeFormat
)
742 static const GLuint one
= 1;
743 GLubyte littleEndian
= *((GLubyte
*) &one
);
745 if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
747 type
== GL_UNSIGNED_BYTE
&&
751 else if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
752 format
== GL_ABGR_EXT
&&
753 type
== GL_UNSIGNED_BYTE
&&
757 else if (pipeFormat
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
759 type
== GL_UNSIGNED_BYTE
&&
763 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
765 type
== GL_UNSIGNED_SHORT_5_6_5
) {
766 /* endian don't care */
769 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
771 type
== GL_UNSIGNED_SHORT_5_6_5_REV
) {
772 /* endian don't care */
775 else if (pipeFormat
== PIPE_FORMAT_S8_UNORM
&&
776 format
== GL_STENCIL_INDEX
&&
777 type
== GL_UNSIGNED_BYTE
) {
780 else if (pipeFormat
== PIPE_FORMAT_Z32_UNORM
&&
781 format
== GL_DEPTH_COMPONENT
&&
782 type
== GL_UNSIGNED_INT
) {
785 /* XXX add more cases */
793 * Check if any per-fragment ops are enabled.
794 * XXX probably move this to a re-usable place.
797 any_fragment_ops(const struct st_context
*st
)
799 if (st
->state
.depth_stencil
->state
.alpha
.enabled
||
800 st
->state
.blend
->state
.blend_enable
||
801 st
->state
.blend
->state
.logicop_enable
||
802 st
->state
.depth_stencil
->state
.depth
.enabled
)
803 /* XXX more checks */
811 * Check if any pixel transfer ops are enabled.
812 * XXX probably move this to a re-usable place.
815 any_pixel_transfer_ops(const struct st_context
*st
)
817 if (st
->ctx
->Pixel
.RedScale
!= 1.0 ||
818 st
->ctx
->Pixel
.RedBias
!= 0.0 ||
819 st
->ctx
->Pixel
.GreenScale
!= 1.0 ||
820 st
->ctx
->Pixel
.GreenBias
!= 0.0 ||
821 st
->ctx
->Pixel
.BlueScale
!= 1.0 ||
822 st
->ctx
->Pixel
.BlueBias
!= 0.0 ||
823 st
->ctx
->Pixel
.AlphaScale
!= 1.0 ||
824 st
->ctx
->Pixel
.AlphaBias
!= 0.0 ||
825 st
->ctx
->Pixel
.MapColorFlag
)
826 /* XXX more checks */
834 * Draw image with a blit, or other non-textured quad method.
837 draw_blit(struct st_context
*st
,
838 GLsizei width
, GLsizei height
,
839 GLenum format
, GLenum type
, const GLvoid
*pixels
)
847 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
848 GLsizei width
, GLsizei height
, GLenum type
,
849 const struct gl_pixelstore_attrib
*unpack
,
850 const GLvoid
*pixels
)
852 struct st_context
*st
= ctx
->st
;
853 struct pipe_context
*pipe
= st
->pipe
;
854 struct pipe_surface
*ps
= st
->state
.framebuffer
.zsbuf
;
855 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
859 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
861 /* map the stencil buffer */
862 stmap
= pipe_surface_map(ps
);
864 /* if width > MAX_WIDTH, have to process image in chunks */
866 while (skipPixels
< width
) {
867 const GLint spanX
= x
+ skipPixels
;
868 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
870 for (row
= 0; row
< height
; row
++) {
871 GLint spanY
= y
+ row
;
872 GLubyte values
[MAX_WIDTH
];
873 GLenum destType
= GL_UNSIGNED_BYTE
;
874 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
876 GL_COLOR_INDEX
, type
,
878 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
879 type
, source
, unpack
,
880 ctx
->_ImageTransferState
);
883 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
884 spanX, spanY, values);
888 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
889 spanY
= ctx
->DrawBuffer
->Height
- spanY
- 1;
892 switch (ps
->format
) {
893 case PIPE_FORMAT_U_S8
:
895 ubyte
*dest
= stmap
+ spanY
* ps
->pitch
+ spanX
;
896 memcpy(dest
, values
, spanWidth
);
899 case PIPE_FORMAT_S8Z24_UNORM
:
901 uint
*dest
= (uint
*) stmap
+ spanY
* ps
->pitch
+ spanX
;
903 for (k
= 0; k
< spanWidth
; k
++) {
905 p
= (p
& 0xffffff) | (values
[k
] << 24);
915 skipPixels
+= spanWidth
;
918 /* unmap the stencil buffer */
919 pipe_surface_unmap(ps
);
924 * Called via ctx->Driver.DrawPixels()
927 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
928 GLenum format
, GLenum type
,
929 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
931 struct st_fragment_program
*stfp
;
932 struct st_vertex_program
*stvp
;
933 struct st_context
*st
= ctx
->st
;
934 struct pipe_surface
*ps
;
936 const GLfloat
*color
;
938 if (format
== GL_STENCIL_INDEX
) {
939 draw_stencil_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
943 st_validate_state(st
);
945 if (format
== GL_DEPTH_COMPONENT
) {
946 ps
= st
->state
.framebuffer
.zsbuf
;
947 stfp
= make_fragment_shader_z(ctx
->st
);
948 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
949 color
= ctx
->Current
.RasterColor
;
951 else if (format
== GL_STENCIL_INDEX
) {
952 ps
= st
->state
.framebuffer
.zsbuf
;
953 /* XXX special case - can't use texture map */
957 ps
= st
->state
.framebuffer
.cbufs
[0];
958 stfp
= combined_drawpix_fragment_program(ctx
);
959 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
963 bufferFormat
= ps
->format
;
965 if (any_fragment_ops(st
) ||
966 any_pixel_transfer_ops(st
) ||
967 !compatible_formats(format
, type
, ps
->format
)) {
969 struct pipe_texture
*pt
970 = make_texture(ctx
->st
, width
, height
, format
, type
, unpack
, pixels
);
972 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
973 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
974 pt
, stvp
, stfp
, color
, GL_FALSE
);
975 st
->pipe
->texture_release(st
->pipe
, &pt
);
980 draw_blit(st
, width
, height
, format
, type
, pixels
);
987 * Create a texture which represents a bitmap image.
989 static struct pipe_texture
*
990 make_bitmap_texture(GLcontext
*ctx
, GLsizei width
, GLsizei height
,
991 const struct gl_pixelstore_attrib
*unpack
,
992 const GLubyte
*bitmap
)
994 struct pipe_context
*pipe
= ctx
->st
->pipe
;
995 struct pipe_surface
*surface
;
996 uint format
= 0, cpp
, comp
;
998 struct pipe_texture
*pt
;
1001 /* find a texture format we know */
1002 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
, PIPE_TEXTURE
)) {
1003 format
= PIPE_FORMAT_U_I8
;
1007 else if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A8R8G8B8_UNORM
, PIPE_TEXTURE
)) {
1008 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
1010 comp
= 3; /* alpha channel */ /*XXX little-endian dependency */
1013 /* XXX support more formats */
1020 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, format
, 0, width
, height
,
1025 if (unpack
->BufferObj
&& unpack
->BufferObj
->Name
) {
1027 pt->region = buffer_object_region(unpack->BufferObj);
1029 printf("st_Bitmap (sourcing from PBO not implemented yet)\n");
1032 surface
= pipe
->get_tex_surface(pipe
, pt
, 0, 0, 0);
1034 /* map texture surface */
1035 dest
= pipe_surface_map(surface
);
1037 /* Put image into texture surface.
1038 * Note that the image is actually going to be upside down in
1039 * the texture. We deal with that with texcoords.
1042 for (row
= 0; row
< height
; row
++) {
1043 const GLubyte
*src
= (const GLubyte
*) _mesa_image_address2d(unpack
,
1044 bitmap
, width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
1045 ubyte
*destRow
= dest
+ row
* surface
->pitch
* cpp
;
1047 if (unpack
->LsbFirst
) {
1049 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1050 for (col
= 0; col
< width
; col
++) {
1052 /* set texel to 255 if bit is set */
1053 destRow
[comp
] = (*src
& mask
) ? 255 : 0;
1065 /* get ready for next row */
1071 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1072 for (col
= 0; col
< width
; col
++) {
1074 /* set texel to 255 if bit is set */
1075 destRow
[comp
] =(*src
& mask
) ? 255 : 0;
1087 /* get ready for next row */
1094 /* Release surface */
1095 pipe_surface_unmap(surface
);
1096 pipe_surface_reference(&surface
, NULL
);
1098 pt
->format
= format
;
1106 st_Bitmap(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1107 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
1109 struct st_fragment_program
*stfp
;
1110 struct st_vertex_program
*stvp
;
1111 struct st_context
*st
= ctx
->st
;
1112 struct pipe_texture
*pt
;
1114 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
1115 stfp
= combined_bitmap_fragment_program(ctx
);
1117 st_validate_state(st
);
1119 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
1121 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1122 width
, height
, 1.0, 1.0,
1124 ctx
->Current
.RasterColor
, GL_FALSE
);
1126 st
->pipe
->texture_release(st
->pipe
, &pt
);
1132 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
1133 GLsizei width
, GLsizei height
,
1134 GLint dstx
, GLint dsty
)
1136 struct st_renderbuffer
*rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_StencilBuffer
);
1137 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1138 struct pipe_surface
*psRead
= rbRead
->surface
;
1139 struct pipe_surface
*psDraw
= rbDraw
->surface
;
1140 ubyte
*readMap
, *drawMap
;
1144 buffer
= malloc(width
* height
* sizeof(ubyte
));
1146 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1150 /* map the stencil buffers */
1151 readMap
= pipe_surface_map(psRead
);
1152 drawMap
= pipe_surface_map(psDraw
);
1154 /* this will do stencil pixel transfer ops */
1155 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, GL_UNSIGNED_BYTE
,
1156 &ctx
->DefaultPacking
, buffer
);
1159 /* XXX PixelZoom not handled yet */
1160 for (i
= 0; i
< height
; i
++) {
1167 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1168 y
= ctx
->DrawBuffer
->Height
- y
- 1;
1171 dst
= drawMap
+ (y
* psDraw
->pitch
+ dstx
) * psDraw
->cpp
;
1172 src
= buffer
+ i
* width
;
1174 switch (psDraw
->format
) {
1175 case PIPE_FORMAT_S8Z24_UNORM
:
1177 uint
*dst4
= (uint
*) dst
;
1179 for (j
= 0; j
< width
; j
++) {
1180 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1185 case PIPE_FORMAT_U_S8
:
1186 memcpy(dst
, src
, width
);
1195 /* unmap the stencil buffers */
1196 pipe_surface_unmap(psRead
);
1197 pipe_surface_unmap(psDraw
);
1202 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
1203 GLsizei width
, GLsizei height
,
1204 GLint dstx
, GLint dsty
, GLenum type
)
1206 struct st_context
*st
= ctx
->st
;
1207 struct pipe_context
*pipe
= st
->pipe
;
1208 struct st_renderbuffer
*rbRead
;
1209 struct st_vertex_program
*stvp
;
1210 struct st_fragment_program
*stfp
;
1211 struct pipe_surface
*psRead
;
1212 struct pipe_surface
*psTex
;
1213 struct pipe_texture
*pt
;
1217 /* make sure rendering has completed */
1218 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
1220 st_validate_state(st
);
1222 if (type
== GL_STENCIL
) {
1223 /* can't use texturing to do stencil */
1224 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1228 if (type
== GL_COLOR
) {
1229 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
1231 stfp
= combined_drawpix_fragment_program(ctx
);
1232 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
1235 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1236 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1237 stfp
= make_fragment_shader_z(ctx
->st
);
1238 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
1241 psRead
= rbRead
->surface
;
1242 format
= psRead
->format
;
1244 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, format
, 0, width
, height
,
1249 psTex
= pipe
->get_tex_surface(pipe
, pt
, 0, 0, 0);
1251 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1252 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1255 /* For some drivers (like Xlib) it's not possible to treat the
1256 * front/back color buffers as surfaces (they're XImages and Pixmaps).
1257 * So, this var tells us if we can use surface_copy here...
1259 if (st
->haveFramebufferSurfaces
) {
1260 /* copy source framebuffer surface into mipmap/texture */
1261 pipe
->surface_copy(pipe
,
1266 srcx
, srcy
, width
, height
);
1269 /* alternate path using get/put_tile() */
1270 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1272 pipe_get_tile_rgba(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1273 pipe_put_tile_rgba(pipe
, psTex
, 0, 0, width
, height
, buf
);
1278 /* draw textured quad */
1279 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1280 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1281 pt
, stvp
, stfp
, color
, GL_TRUE
);
1283 pipe_surface_reference(&psTex
, NULL
);
1284 st
->pipe
->texture_release(st
->pipe
, &pt
);
1289 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1291 functions
->DrawPixels
= st_DrawPixels
;
1292 functions
->CopyPixels
= st_CopyPixels
;
1293 functions
->Bitmap
= st_Bitmap
;