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_cb_program.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "st_texture.h"
50 #include "pipe/p_context.h"
51 #include "pipe/p_defines.h"
52 #include "pipe/p_inlines.h"
53 #include "pipe/p_winsys.h"
54 #include "util/p_tile.h"
55 #include "util/u_draw_quad.h"
56 #include "util/u_simple_shaders.h"
57 #include "shader/prog_instruction.h"
58 #include "cso_cache/cso_context.h"
63 * Make fragment program for glBitmap:
64 * Sample the texture and kill the fragment if the bit is 0.
65 * This program will be combined with the user's fragment program.
67 static struct st_fragment_program
*
68 make_bitmap_fragment_program(GLcontext
*ctx
)
70 struct st_fragment_program
*stfp
;
74 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
78 p
->NumInstructions
= 5;
80 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
81 if (!p
->Instructions
) {
82 ctx
->Driver
.DeleteProgram(ctx
, p
);
85 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
87 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
88 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
89 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
90 p
->Instructions
[ic
].DstReg
.Index
= 0;
91 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
92 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
93 p
->Instructions
[ic
].TexSrcUnit
= 0;
94 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
97 /* SWZ tmp0.x, tmp0.x, 1111; # tmp0.x = 1.0 */
98 p
->Instructions
[ic
].Opcode
= OPCODE_SWZ
;
99 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
100 p
->Instructions
[ic
].DstReg
.Index
= 0;
101 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_X
;
102 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
103 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
104 p
->Instructions
[ic
].SrcReg
[0].Swizzle
105 = MAKE_SWIZZLE4(SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
);
108 /* SUB tmp0, tmp0.wwww, tmp0.xxxx; # tmp0.w -= 1 */
109 p
->Instructions
[ic
].Opcode
= OPCODE_SUB
;
110 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
111 p
->Instructions
[ic
].DstReg
.Index
= 0;
112 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
113 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
114 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_WWWW
;
115 p
->Instructions
[ic
].SrcReg
[1].File
= PROGRAM_TEMPORARY
;
116 p
->Instructions
[ic
].SrcReg
[1].Index
= 0;
117 p
->Instructions
[ic
].SrcReg
[1].Swizzle
= SWIZZLE_XXXX
; /* 1.0 */
120 /* KIL if tmp0 < 0 */
121 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
122 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
123 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
127 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
129 assert(ic
== p
->NumInstructions
);
131 p
->InputsRead
= FRAG_BIT_TEX0
;
132 p
->OutputsWritten
= 0x0;
134 stfp
= (struct st_fragment_program
*) p
;
135 stfp
->Base
.UsesKill
= GL_TRUE
;
136 st_translate_fragment_program(ctx
->st
, stfp
, NULL
);
143 * Combine basic bitmap fragment program with the user-defined program.
145 static struct st_fragment_program
*
146 combined_bitmap_fragment_program(GLcontext
*ctx
)
148 struct st_context
*st
= ctx
->st
;
149 struct st_fragment_program
*stfp
;
151 if (!st
->bitmap
.program
) {
152 /* create the basic bitmap fragment program */
153 st
->bitmap
.program
= make_bitmap_fragment_program(ctx
);
156 if (st
->bitmap
.user_prog_sn
== st
->fp
->serialNo
) {
158 stfp
= st
->bitmap
.combined_prog
;
161 /* Concatenate the bitmap program with the current user-defined program.
163 stfp
= (struct st_fragment_program
*)
164 _mesa_combine_programs(ctx
,
165 &st
->bitmap
.program
->Base
.Base
,
170 struct gl_program
*p
= &stfp
->Base
.Base
;
171 printf("Combined bitmap program:\n");
172 _mesa_print_program(p
);
173 printf("InputsRead: 0x%x\n", p
->InputsRead
);
174 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
175 _mesa_print_parameter_list(p
->Parameters
);
179 /* translate to TGSI tokens */
180 st_translate_fragment_program(st
, stfp
, NULL
);
182 /* save new program, update serial numbers */
183 st
->bitmap
.user_prog_sn
= st
->fp
->serialNo
;
184 st
->bitmap
.combined_prog
= stfp
;
187 /* Ideally we'd have updated the pipe constants during the normal
188 * st/atom mechanism. But we can't since this is specific to glBitmap.
190 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
197 * Create a texture which represents a bitmap image.
199 static struct pipe_texture
*
200 make_bitmap_texture(GLcontext
*ctx
, GLsizei width
, GLsizei height
,
201 const struct gl_pixelstore_attrib
*unpack
,
202 const GLubyte
*bitmap
)
204 struct pipe_context
*pipe
= ctx
->st
->pipe
;
205 struct pipe_screen
*screen
= pipe
->screen
;
206 struct pipe_surface
*surface
;
207 uint format
= 0, cpp
, comp
;
209 struct pipe_texture
*pt
;
212 /* find a texture format we know */
213 if (screen
->is_format_supported( screen
, PIPE_FORMAT_U_I8
, PIPE_TEXTURE
)) {
214 format
= PIPE_FORMAT_U_I8
;
218 else if (screen
->is_format_supported( screen
, PIPE_FORMAT_A8R8G8B8_UNORM
, PIPE_TEXTURE
)) {
219 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
221 comp
= 3; /* alpha channel */ /*XXX little-endian dependency */
224 /* XXX support more formats */
229 bitmap
= _mesa_map_bitmap_pbo(ctx
, unpack
, bitmap
);
235 * Create texture to hold bitmap pattern.
237 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, format
, 0, width
, height
,
240 _mesa_unmap_bitmap_pbo(ctx
, unpack
);
244 surface
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0);
246 /* map texture surface */
247 dest
= pipe_surface_map(surface
);
249 /* Put image into texture surface.
250 * Note that the image is actually going to be upside down in
251 * the texture. We deal with that with texcoords.
254 for (row
= 0; row
< height
; row
++) {
255 const GLubyte
*src
= (const GLubyte
*) _mesa_image_address2d(unpack
,
256 bitmap
, width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
257 ubyte
*destRow
= dest
+ row
* surface
->pitch
* cpp
;
259 if (unpack
->LsbFirst
) {
261 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
262 for (col
= 0; col
< width
; col
++) {
264 /* set texel to 255 if bit is set */
265 destRow
[comp
] = (*src
& mask
) ? 255 : 0;
277 /* get ready for next row */
283 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
284 for (col
= 0; col
< width
; col
++) {
286 /* set texel to 255 if bit is set */
287 destRow
[comp
] =(*src
& mask
) ? 255 : 0;
299 /* get ready for next row */
306 _mesa_unmap_bitmap_pbo(ctx
, unpack
);
308 /* Release surface */
309 pipe_surface_unmap(surface
);
310 pipe_surface_reference(&surface
, NULL
);
311 pipe
->texture_update(pipe
, pt
, 0, 0x1);
320 setup_bitmap_vertex_data(struct st_context
*st
,
321 int x
, int y
, int width
, int height
,
322 float z
, const float color
[4])
324 struct pipe_context
*pipe
= st
->pipe
;
325 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
326 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
327 const GLfloat x0
= x
;
328 const GLfloat x1
= x
+ width
;
329 const GLfloat y0
= invert
? (fb
->Height
- y
- height
) : y
;
330 const GLfloat y1
= invert
? (y0
+ height
) : y
+ height
;
331 const GLfloat bias
= st
->bitmap_texcoord_bias
;
332 const GLfloat xBias
= bias
/ (x1
-x0
);
333 const GLfloat yBias
= bias
/ (y1
-y0
);
334 const GLfloat sLeft
= 0.0 + xBias
, sRight
= 1.0 + xBias
;
335 const GLfloat tTop
= 1.0 - yBias
, tBot
= 1.0 - tTop
- yBias
;
339 if (!st
->bitmap
.vbuf
) {
340 st
->bitmap
.vbuf
= pipe
->winsys
->buffer_create(pipe
->winsys
, 32,
341 PIPE_BUFFER_USAGE_VERTEX
,
342 sizeof(st
->bitmap
.vertices
));
345 /* positions, texcoords */
346 st
->bitmap
.vertices
[0][0][0] = x0
;
347 st
->bitmap
.vertices
[0][0][1] = y0
;
348 st
->bitmap
.vertices
[0][2][0] = sLeft
;
349 st
->bitmap
.vertices
[0][2][1] = tTop
;
351 st
->bitmap
.vertices
[1][0][0] = x1
;
352 st
->bitmap
.vertices
[1][0][1] = y0
;
353 st
->bitmap
.vertices
[1][2][0] = sRight
;
354 st
->bitmap
.vertices
[1][2][1] = tTop
;
356 st
->bitmap
.vertices
[2][0][0] = x1
;
357 st
->bitmap
.vertices
[2][0][1] = y1
;
358 st
->bitmap
.vertices
[2][2][0] = sRight
;
359 st
->bitmap
.vertices
[2][2][1] = tBot
;
361 st
->bitmap
.vertices
[3][0][0] = x0
;
362 st
->bitmap
.vertices
[3][0][1] = y1
;
363 st
->bitmap
.vertices
[3][2][0] = sLeft
;
364 st
->bitmap
.vertices
[3][2][1] = tBot
;
366 /* same for all verts: */
367 for (i
= 0; i
< 4; i
++) {
368 st
->bitmap
.vertices
[i
][0][2] = z
;
369 st
->bitmap
.vertices
[i
][0][3] = 1.0;
370 st
->bitmap
.vertices
[i
][1][0] = color
[0];
371 st
->bitmap
.vertices
[i
][1][1] = color
[1];
372 st
->bitmap
.vertices
[i
][1][2] = color
[2];
373 st
->bitmap
.vertices
[i
][1][3] = color
[3];
374 st
->bitmap
.vertices
[i
][2][2] = 0.0; /*R*/
375 st
->bitmap
.vertices
[i
][2][3] = 1.0; /*Q*/
378 /* put vertex data into vbuf */
379 buf
= pipe
->winsys
->buffer_map(pipe
->winsys
, st
->bitmap
.vbuf
,
380 PIPE_BUFFER_USAGE_CPU_WRITE
);
381 memcpy(buf
, st
->bitmap
.vertices
, sizeof(st
->bitmap
.vertices
));
382 pipe
->winsys
->buffer_unmap(pipe
->winsys
, st
->bitmap
.vbuf
);
388 * Render a glBitmap by drawing a textured quad
391 draw_bitmap_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
392 GLsizei width
, GLsizei height
,
393 struct pipe_texture
*pt
,
394 struct st_fragment_program
*stfp
)
396 struct st_context
*st
= ctx
->st
;
397 struct pipe_context
*pipe
= ctx
->st
->pipe
;
398 struct cso_context
*cso
= ctx
->st
->cso_context
;
402 /* XXX if DrawPixels image is larger than max texture size, break
405 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
406 assert(width
<= maxSize
);
407 assert(height
<= maxSize
);
409 cso_save_rasterizer(cso
);
410 cso_save_samplers(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 /* shaders don't go through cso yet */
462 pipe
->bind_fs_state(pipe
, st
->fp
->driver_shader
);
463 pipe
->bind_vs_state(pipe
, st
->vp
->driver_shader
);
464 pipe
->set_sampler_textures(pipe
, ctx
->st
->state
.num_textures
,
465 ctx
->st
->state
.sampler_texture
);
471 st_Bitmap(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
472 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
474 struct st_fragment_program
*stfp
;
475 struct st_context
*st
= ctx
->st
;
476 struct pipe_texture
*pt
;
478 st_validate_state(st
);
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,
491 &st
->bitmap
.vert_shader
);
494 st_validate_state(st
);
496 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
498 assert(pt
->target
== PIPE_TEXTURE_2D
);
499 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
502 pipe_texture_reference(&pt
, NULL
);
508 void st_init_bitmap_functions(struct dd_function_table
*functions
)
510 functions
->Bitmap
= st_Bitmap
;
515 st_destroy_bitmap(struct st_context
*st
)
517 struct pipe_context
*pipe
= st
->pipe
;
519 if (st
->bitmap
.combined_prog
) {
520 st_delete_program(st
->ctx
, &st
->bitmap
.combined_prog
->Base
.Base
);
523 if (st
->bitmap
.program
) {
524 st_delete_program(st
->ctx
, &st
->bitmap
.program
->Base
.Base
);
528 pipe
->delete_vs_state(pipe
, st
->bitmap
.vs
);
529 st
->bitmap
.vs
= NULL
;
531 if (st
->bitmap
.vbuf
) {
532 pipe
->winsys
->buffer_destroy(pipe
->winsys
, st
->bitmap
.vbuf
);
533 st
->bitmap
.vbuf
= NULL
;