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/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"
42 #include "st_context.h"
44 #include "st_atom_constbuf.h"
45 #include "st_program.h"
46 #include "st_cb_bitmap.h"
47 #include "st_mesa_to_tgsi.h"
48 #include "st_texture.h"
49 #include "pipe/p_context.h"
50 #include "pipe/p_defines.h"
51 #include "pipe/p_inlines.h"
52 #include "pipe/p_winsys.h"
53 #include "util/p_tile.h"
54 #include "util/u_draw_quad.h"
55 #include "util/u_simple_shaders.h"
56 #include "shader/prog_instruction.h"
57 #include "cso_cache/cso_context.h"
62 * Make fragment program for glBitmap:
63 * Sample the texture and kill the fragment if the bit is 0.
64 * This program will be combined with the user's fragment program.
66 static struct st_fragment_program
*
67 make_bitmap_fragment_program(GLcontext
*ctx
)
69 struct st_fragment_program
*stfp
;
73 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
77 p
->NumInstructions
= 5;
79 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
80 if (!p
->Instructions
) {
81 ctx
->Driver
.DeleteProgram(ctx
, p
);
84 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
86 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
87 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
88 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
89 p
->Instructions
[ic
].DstReg
.Index
= 0;
90 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
91 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
92 p
->Instructions
[ic
].TexSrcUnit
= 0;
93 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
96 /* SWZ tmp0.x, tmp0.x, 1111; # tmp0.x = 1.0 */
97 p
->Instructions
[ic
].Opcode
= OPCODE_SWZ
;
98 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
99 p
->Instructions
[ic
].DstReg
.Index
= 0;
100 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_X
;
101 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
102 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
103 p
->Instructions
[ic
].SrcReg
[0].Swizzle
104 = MAKE_SWIZZLE4(SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
);
107 /* SUB tmp0, tmp0.wwww, tmp0.xxxx; # tmp0.w -= 1 */
108 p
->Instructions
[ic
].Opcode
= OPCODE_SUB
;
109 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
110 p
->Instructions
[ic
].DstReg
.Index
= 0;
111 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
112 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
113 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_WWWW
;
114 p
->Instructions
[ic
].SrcReg
[1].File
= PROGRAM_TEMPORARY
;
115 p
->Instructions
[ic
].SrcReg
[1].Index
= 0;
116 p
->Instructions
[ic
].SrcReg
[1].Swizzle
= SWIZZLE_XXXX
; /* 1.0 */
119 /* KIL if tmp0 < 0 */
120 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
121 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
122 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
126 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
128 assert(ic
== p
->NumInstructions
);
130 p
->InputsRead
= FRAG_BIT_TEX0
;
131 p
->OutputsWritten
= 0x0;
133 stfp
= (struct st_fragment_program
*) p
;
134 stfp
->Base
.UsesKill
= GL_TRUE
;
135 st_translate_fragment_program(ctx
->st
, stfp
, NULL
);
142 * Combine basic bitmap fragment program with the user-defined program.
144 static struct st_fragment_program
*
145 combined_bitmap_fragment_program(GLcontext
*ctx
)
147 struct st_context
*st
= ctx
->st
;
148 struct st_fragment_program
*stfp
;
150 if (!st
->bitmap
.program
) {
151 /* create the basic bitmap fragment program */
152 st
->bitmap
.program
= make_bitmap_fragment_program(ctx
);
155 if (st
->bitmap
.user_prog_sn
== st
->fp
->serialNo
) {
157 stfp
= st
->bitmap
.combined_prog
;
160 /* Concatenate the bitmap program with the current user-defined program.
162 stfp
= (struct st_fragment_program
*)
163 _mesa_combine_programs(ctx
,
164 &st
->bitmap
.program
->Base
.Base
,
169 struct gl_program
*p
= &stfp
->Base
.Base
;
170 printf("Combined bitmap program:\n");
171 _mesa_print_program(p
);
172 printf("InputsRead: 0x%x\n", p
->InputsRead
);
173 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
174 _mesa_print_parameter_list(p
->Parameters
);
178 /* translate to TGSI tokens */
179 st_translate_fragment_program(st
, stfp
, NULL
);
181 /* save new program, update serial numbers */
182 st
->bitmap
.user_prog_sn
= st
->fp
->serialNo
;
183 st
->bitmap
.combined_prog
= stfp
;
186 /* Ideally we'd have updated the pipe constants during the normal
187 * st/atom mechanism. But we can't since this is specific to glBitmap.
189 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
196 * Create a texture which represents a bitmap image.
198 static struct pipe_texture
*
199 make_bitmap_texture(GLcontext
*ctx
, GLsizei width
, GLsizei height
,
200 const struct gl_pixelstore_attrib
*unpack
,
201 const GLubyte
*bitmap
)
203 struct pipe_context
*pipe
= ctx
->st
->pipe
;
204 struct pipe_screen
*screen
= pipe
->screen
;
205 struct pipe_surface
*surface
;
206 uint format
= 0, cpp
, comp
;
208 struct pipe_texture
*pt
;
211 /* find a texture format we know */
212 if (screen
->is_format_supported( screen
, PIPE_FORMAT_U_I8
, PIPE_TEXTURE
)) {
213 format
= PIPE_FORMAT_U_I8
;
217 else if (screen
->is_format_supported( screen
, PIPE_FORMAT_A8R8G8B8_UNORM
, PIPE_TEXTURE
)) {
218 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
220 comp
= 3; /* alpha channel */ /*XXX little-endian dependency */
223 /* XXX support more formats */
228 bitmap
= _mesa_map_bitmap_pbo(ctx
, unpack
, bitmap
);
234 * Create texture to hold bitmap pattern.
236 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, format
, 0, width
, height
,
239 _mesa_unmap_bitmap_pbo(ctx
, unpack
);
243 surface
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0);
245 /* map texture surface */
246 dest
= pipe_surface_map(surface
);
248 /* Put image into texture surface.
249 * Note that the image is actually going to be upside down in
250 * the texture. We deal with that with texcoords.
253 for (row
= 0; row
< height
; row
++) {
254 const GLubyte
*src
= (const GLubyte
*) _mesa_image_address2d(unpack
,
255 bitmap
, width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
256 ubyte
*destRow
= dest
+ row
* surface
->pitch
* cpp
;
258 if (unpack
->LsbFirst
) {
260 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
261 for (col
= 0; col
< width
; col
++) {
263 /* set texel to 255 if bit is set */
264 destRow
[comp
] = (*src
& mask
) ? 255 : 0;
276 /* get ready for next row */
282 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
283 for (col
= 0; col
< width
; col
++) {
285 /* set texel to 255 if bit is set */
286 destRow
[comp
] =(*src
& mask
) ? 255 : 0;
298 /* get ready for next row */
305 _mesa_unmap_bitmap_pbo(ctx
, unpack
);
307 /* Release surface */
308 pipe_surface_unmap(surface
);
309 pipe_surface_reference(&surface
, NULL
);
310 pipe
->texture_update(pipe
, pt
, 0, 0x1);
319 setup_bitmap_vertex_data(struct st_context
*st
,
320 int x
, int y
, int width
, int height
,
321 float z
, const float color
[4])
323 struct pipe_context
*pipe
= st
->pipe
;
324 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
325 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
326 const GLfloat x0
= x
;
327 const GLfloat x1
= x
+ width
;
328 const GLfloat y0
= invert
? (fb
->Height
- y
- height
) : y
;
329 const GLfloat y1
= invert
? (y0
+ height
) : y
+ height
;
330 const GLfloat bias
= st
->bitmap_texcoord_bias
;
331 const GLfloat xBias
= bias
/ (x1
-x0
);
332 const GLfloat yBias
= bias
/ (y1
-y0
);
333 const GLfloat sLeft
= 0.0 + xBias
, sRight
= 1.0 + xBias
;
334 const GLfloat tTop
= 1.0 - yBias
, tBot
= 1.0 - tTop
- yBias
;
338 if (!st
->bitmap
.vbuf
) {
339 st
->bitmap
.vbuf
= pipe
->winsys
->buffer_create(pipe
->winsys
, 32,
340 PIPE_BUFFER_USAGE_VERTEX
,
341 sizeof(st
->bitmap
.vertices
));
344 /* positions, texcoords */
345 st
->bitmap
.vertices
[0][0][0] = x0
;
346 st
->bitmap
.vertices
[0][0][1] = y0
;
347 st
->bitmap
.vertices
[0][2][0] = sLeft
;
348 st
->bitmap
.vertices
[0][2][1] = tTop
;
350 st
->bitmap
.vertices
[1][0][0] = x1
;
351 st
->bitmap
.vertices
[1][0][1] = y0
;
352 st
->bitmap
.vertices
[1][2][0] = sRight
;
353 st
->bitmap
.vertices
[1][2][1] = tTop
;
355 st
->bitmap
.vertices
[2][0][0] = x1
;
356 st
->bitmap
.vertices
[2][0][1] = y1
;
357 st
->bitmap
.vertices
[2][2][0] = sRight
;
358 st
->bitmap
.vertices
[2][2][1] = tBot
;
360 st
->bitmap
.vertices
[3][0][0] = x0
;
361 st
->bitmap
.vertices
[3][0][1] = y1
;
362 st
->bitmap
.vertices
[3][2][0] = sLeft
;
363 st
->bitmap
.vertices
[3][2][1] = tBot
;
365 /* same for all verts: */
366 for (i
= 0; i
< 4; i
++) {
367 st
->bitmap
.vertices
[i
][0][2] = z
;
368 st
->bitmap
.vertices
[i
][0][3] = 1.0;
369 st
->bitmap
.vertices
[i
][1][0] = color
[0];
370 st
->bitmap
.vertices
[i
][1][1] = color
[1];
371 st
->bitmap
.vertices
[i
][1][2] = color
[2];
372 st
->bitmap
.vertices
[i
][1][3] = color
[3];
373 st
->bitmap
.vertices
[i
][2][2] = 0.0; /*R*/
374 st
->bitmap
.vertices
[i
][2][3] = 1.0; /*Q*/
377 /* put vertex data into vbuf */
378 buf
= pipe
->winsys
->buffer_map(pipe
->winsys
, st
->bitmap
.vbuf
,
379 PIPE_BUFFER_USAGE_CPU_WRITE
);
380 memcpy(buf
, st
->bitmap
.vertices
, sizeof(st
->bitmap
.vertices
));
381 pipe
->winsys
->buffer_unmap(pipe
->winsys
, st
->bitmap
.vbuf
);
387 * Render a glBitmap by drawing a textured quad
390 draw_bitmap_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
391 GLsizei width
, GLsizei height
,
392 struct pipe_texture
*pt
,
393 struct st_fragment_program
*stfp
)
395 struct st_context
*st
= ctx
->st
;
396 struct pipe_context
*pipe
= ctx
->st
->pipe
;
397 struct cso_context
*cso
= ctx
->st
->cso_context
;
401 /* XXX if DrawPixels image is larger than max texture size, break
404 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
405 assert(width
<= maxSize
);
406 assert(height
<= maxSize
);
408 cso_save_rasterizer(cso
);
409 cso_save_samplers(cso
);
410 //cso_save_viewport(cso);
412 /* rasterizer state: just scissor */
414 struct pipe_rasterizer_state rasterizer
;
415 memset(&rasterizer
, 0, sizeof(rasterizer
));
416 if (ctx
->Scissor
.Enabled
)
417 rasterizer
.scissor
= 1;
418 rasterizer
.bypass_clipping
= 1;
420 cso_set_rasterizer(cso
, &rasterizer
);
423 /* fragment shader state: TEX lookup program */
424 pipe
->bind_fs_state(pipe
, stfp
->driver_shader
);
426 /* vertex shader state: position + texcoord pass-through */
427 pipe
->bind_vs_state(pipe
, st
->bitmap
.vs
);
429 /* sampler / texture state */
431 struct pipe_sampler_state sampler
;
432 memset(&sampler
, 0, sizeof(sampler
));
433 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
434 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
435 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
436 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
437 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
438 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
439 sampler
.normalized_coords
= 1;
441 cso_single_sampler(cso
, 0, &sampler
);
442 cso_single_sampler_done(cso
);
444 pipe
->set_sampler_textures(pipe
, 1, &pt
);
447 /* draw textured quad */
448 setup_bitmap_vertex_data(st
, x
, y
, width
, height
,
449 ctx
->Current
.RasterPos
[2],
450 ctx
->Current
.RasterColor
);
452 util_draw_vertex_buffer(pipe
, st
->bitmap
.vbuf
,
453 PIPE_PRIM_TRIANGLE_FAN
,
455 3); /* attribs/vert */
459 cso_restore_rasterizer(cso
);
460 cso_restore_samplers(cso
);
461 //cso_restore_viewport(cso);
462 /* shaders don't go through cso yet */
463 pipe
->bind_fs_state(pipe
, st
->fp
->driver_shader
);
464 pipe
->bind_vs_state(pipe
, st
->vp
->driver_shader
);
466 pipe
->set_sampler_textures(pipe
, ctx
->st
->state
.num_textures
,
467 ctx
->st
->state
.sampler_texture
);
473 st_Bitmap(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
474 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
476 struct st_fragment_program
*stfp
;
477 struct st_context
*st
= ctx
->st
;
478 struct pipe_texture
*pt
;
480 stfp
= combined_bitmap_fragment_program(ctx
);
482 if (!st
->bitmap
.vs
) {
483 /* create pass-through vertex shader now */
484 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
486 TGSI_SEMANTIC_GENERIC
};
487 const uint semantic_indexes
[] = { 0, 0, 0 };
488 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
493 st_validate_state(st
);
495 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
497 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
500 pipe_texture_reference(&pt
, NULL
);
506 void st_init_bitmap_functions(struct dd_function_table
*functions
)
508 functions
->Bitmap
= st_Bitmap
;
513 st_destroy_bitmap(struct st_context
*st
)
515 struct pipe_context
*pipe
= st
->pipe
;
517 /* XXX free frag shader state */
520 pipe
->delete_vs_state(pipe
, st
->bitmap
.vs
);
521 st
->bitmap
.vs
= NULL
;
523 if (st
->bitmap
.vbuf
) {
524 pipe
->winsys
->buffer_destroy(pipe
->winsys
, st
->bitmap
.vbuf
);
525 st
->bitmap
.vbuf
= NULL
;