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 "util/u_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 * glBitmaps are drawn as textured quads. The user's bitmap pattern
63 * is stored in a texture image. An alpha8 texture format is used.
64 * The fragment shader samples a bit (texel) from the texture, then
65 * discards the fragment if the bit is off.
67 * Note that we actually store the inverse image of the bitmap to
68 * simplify the fragment program. An "on" bit gets stored as texel=0x0
69 * and an "off" bit is stored as texel=0xff. Then we kill the
70 * fragment if the negated texel value is less than zero.
75 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
76 * buffer which is then rendered en mass upon a flush, state change, etc.
77 * A wide, short buffer is used to target the common case of a series
78 * of glBitmap calls being used to draw text.
80 static GLboolean UseBitmapCache
= GL_TRUE
;
83 #define BITMAP_CACHE_WIDTH 512
84 #define BITMAP_CACHE_HEIGHT 32
88 /** Window pos to render the cached image */
90 /** Bounds of region used in window coords */
91 GLint xmin
, ymin
, xmax
, ymax
;
95 struct pipe_texture
*texture
;
96 struct pipe_surface
*surf
;
100 /** An I8 texture image: */
108 * Make fragment program for glBitmap:
109 * Sample the texture and kill the fragment if the bit is 0.
110 * This program will be combined with the user's fragment program.
112 static struct st_fragment_program
*
113 make_bitmap_fragment_program(GLcontext
*ctx
, GLuint samplerIndex
)
115 struct st_fragment_program
*stfp
;
116 struct gl_program
*p
;
119 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
123 p
->NumInstructions
= 3;
125 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
126 if (!p
->Instructions
) {
127 ctx
->Driver
.DeleteProgram(ctx
, p
);
130 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
132 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
133 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
134 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
135 p
->Instructions
[ic
].DstReg
.Index
= 0;
136 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
137 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
138 p
->Instructions
[ic
].TexSrcUnit
= samplerIndex
;
139 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
142 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
143 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
144 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
145 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
146 p
->Instructions
[ic
].SrcReg
[0].NegateBase
= NEGATE_XYZW
;
150 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
152 assert(ic
== p
->NumInstructions
);
154 p
->InputsRead
= FRAG_BIT_TEX0
;
155 p
->OutputsWritten
= 0x0;
156 p
->SamplersUsed
= (1 << samplerIndex
);
158 stfp
= (struct st_fragment_program
*) p
;
159 stfp
->Base
.UsesKill
= GL_TRUE
;
160 st_translate_fragment_program(ctx
->st
, stfp
, NULL
);
167 find_free_bit(uint bitfield
)
170 for (i
= 0; i
< 32; i
++) {
171 if ((bitfield
& (1 << i
)) == 0) {
180 * Combine basic bitmap fragment program with the user-defined program.
182 static struct st_fragment_program
*
183 combined_bitmap_fragment_program(GLcontext
*ctx
)
185 struct st_context
*st
= ctx
->st
;
186 struct st_fragment_program
*stfp
= st
->fp
;
188 if (!stfp
->bitmap_program
) {
190 * Generate new program which is the user-defined program prefixed
191 * with the bitmap sampler/kill instructions.
193 struct st_fragment_program
*bitmap_prog
;
196 sampler
= find_free_bit(st
->fp
->Base
.Base
.SamplersUsed
);
197 bitmap_prog
= make_bitmap_fragment_program(ctx
, sampler
);
199 stfp
->bitmap_program
= (struct st_fragment_program
*)
200 _mesa_combine_programs(ctx
,
201 &bitmap_prog
->Base
.Base
, &stfp
->Base
.Base
);
202 stfp
->bitmap_program
->bitmap_sampler
= sampler
;
204 /* done with this after combining */
205 st_reference_fragprog(st
, &bitmap_prog
, NULL
);
209 struct gl_program
*p
= &stfp
->bitmap_program
->Base
.Base
;
210 printf("Combined bitmap program:\n");
211 _mesa_print_program(p
);
212 printf("InputsRead: 0x%x\n", p
->InputsRead
);
213 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
214 _mesa_print_parameter_list(p
->Parameters
);
218 /* translate to TGSI tokens */
219 st_translate_fragment_program(st
, stfp
->bitmap_program
, NULL
);
222 return stfp
->bitmap_program
;
227 * Copy user-provide bitmap bits into texture buffer, expanding
229 * "On" bits will set texels to 0xff.
230 * "Off" bits will not modify texels.
231 * Note that the image is actually going to be upside down in
232 * the texture. We deal with that with texcoords.
235 unpack_bitmap(struct st_context
*st
,
236 GLint px
, GLint py
, GLsizei width
, GLsizei height
,
237 const struct gl_pixelstore_attrib
*unpack
,
238 const GLubyte
*bitmap
,
239 ubyte
*destBuffer
, uint destStride
)
243 #define SET_PIXEL(COL, ROW) \
244 destBuffer[(py + (ROW)) * destStride + px + (COL)] = 0x0;
246 for (row
= 0; row
< height
; row
++) {
247 const GLubyte
*src
= (const GLubyte
*) _mesa_image_address2d(unpack
,
248 bitmap
, width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
250 if (unpack
->LsbFirst
) {
252 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
253 for (col
= 0; col
< width
; col
++) {
268 /* get ready for next row */
274 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
275 for (col
= 0; col
< width
; col
++) {
290 /* get ready for next row */
302 * Create a texture which represents a bitmap image.
304 static struct pipe_texture
*
305 make_bitmap_texture(GLcontext
*ctx
, GLsizei width
, GLsizei height
,
306 const struct gl_pixelstore_attrib
*unpack
,
307 const GLubyte
*bitmap
)
309 struct pipe_context
*pipe
= ctx
->st
->pipe
;
310 struct pipe_screen
*screen
= pipe
->screen
;
311 struct pipe_surface
*surface
;
313 struct pipe_texture
*pt
;
316 bitmap
= _mesa_map_bitmap_pbo(ctx
, unpack
, bitmap
);
322 * Create texture to hold bitmap pattern.
324 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, ctx
->st
->bitmap
.tex_format
,
325 0, width
, height
, 1, 0,
326 PIPE_TEXTURE_USAGE_SAMPLER
);
328 _mesa_unmap_bitmap_pbo(ctx
, unpack
);
332 surface
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
333 PIPE_BUFFER_USAGE_CPU_WRITE
);
335 /* map texture surface */
336 dest
= screen
->surface_map(screen
, surface
, PIPE_BUFFER_USAGE_CPU_WRITE
);
338 /* Put image into texture surface */
339 memset(dest
, 0xff, height
* surface
->stride
);
340 unpack_bitmap(ctx
->st
, 0, 0, width
, height
, unpack
, bitmap
,
341 dest
, surface
->stride
);
343 _mesa_unmap_bitmap_pbo(ctx
, unpack
);
345 /* Release surface */
346 screen
->surface_unmap(screen
, surface
);
347 pipe_surface_reference(&surface
, NULL
);
354 setup_bitmap_vertex_data(struct st_context
*st
,
355 int x
, int y
, int width
, int height
,
356 float z
, const float color
[4])
358 struct pipe_context
*pipe
= st
->pipe
;
359 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
360 const GLfloat fb_width
= (GLfloat
)fb
->Width
;
361 const GLfloat fb_height
= (GLfloat
)fb
->Height
;
362 const GLfloat x0
= (GLfloat
)x
;
363 const GLfloat x1
= (GLfloat
)(x
+ width
);
364 const GLfloat y0
= (GLfloat
)y
;
365 const GLfloat y1
= (GLfloat
)(y
+ height
);
366 const GLfloat sLeft
= (GLfloat
)0.0, sRight
= (GLfloat
)1.0;
367 const GLfloat tTop
= (GLfloat
)0.0, tBot
= (GLfloat
)1.0 - tTop
;
368 const GLfloat clip_x0
= (GLfloat
)(x0
/ fb_width
* 2.0 - 1.0);
369 const GLfloat clip_y0
= (GLfloat
)(y0
/ fb_height
* 2.0 - 1.0);
370 const GLfloat clip_x1
= (GLfloat
)(x1
/ fb_width
* 2.0 - 1.0);
371 const GLfloat clip_y1
= (GLfloat
)(y1
/ fb_height
* 2.0 - 1.0);
375 if (!st
->bitmap
.vbuf
) {
376 st
->bitmap
.vbuf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
377 sizeof(st
->bitmap
.vertices
));
380 /* Positions are in clip coords since we need to do clipping in case
381 * the bitmap quad goes beyond the window bounds.
383 st
->bitmap
.vertices
[0][0][0] = clip_x0
;
384 st
->bitmap
.vertices
[0][0][1] = clip_y0
;
385 st
->bitmap
.vertices
[0][2][0] = sLeft
;
386 st
->bitmap
.vertices
[0][2][1] = tTop
;
388 st
->bitmap
.vertices
[1][0][0] = clip_x1
;
389 st
->bitmap
.vertices
[1][0][1] = clip_y0
;
390 st
->bitmap
.vertices
[1][2][0] = sRight
;
391 st
->bitmap
.vertices
[1][2][1] = tTop
;
393 st
->bitmap
.vertices
[2][0][0] = clip_x1
;
394 st
->bitmap
.vertices
[2][0][1] = clip_y1
;
395 st
->bitmap
.vertices
[2][2][0] = sRight
;
396 st
->bitmap
.vertices
[2][2][1] = tBot
;
398 st
->bitmap
.vertices
[3][0][0] = clip_x0
;
399 st
->bitmap
.vertices
[3][0][1] = clip_y1
;
400 st
->bitmap
.vertices
[3][2][0] = sLeft
;
401 st
->bitmap
.vertices
[3][2][1] = tBot
;
403 /* same for all verts: */
404 for (i
= 0; i
< 4; i
++) {
405 st
->bitmap
.vertices
[i
][0][2] = z
;
406 st
->bitmap
.vertices
[i
][0][3] = 1.0;
407 st
->bitmap
.vertices
[i
][1][0] = color
[0];
408 st
->bitmap
.vertices
[i
][1][1] = color
[1];
409 st
->bitmap
.vertices
[i
][1][2] = color
[2];
410 st
->bitmap
.vertices
[i
][1][3] = color
[3];
411 st
->bitmap
.vertices
[i
][2][2] = 0.0; /*R*/
412 st
->bitmap
.vertices
[i
][2][3] = 1.0; /*Q*/
415 /* put vertex data into vbuf */
416 buf
= pipe_buffer_map(pipe
->screen
, st
->bitmap
.vbuf
, PIPE_BUFFER_USAGE_CPU_WRITE
);
417 memcpy(buf
, st
->bitmap
.vertices
, sizeof(st
->bitmap
.vertices
));
418 pipe_buffer_unmap(pipe
->screen
, st
->bitmap
.vbuf
);
424 * Render a glBitmap by drawing a textured quad
427 draw_bitmap_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
428 GLsizei width
, GLsizei height
,
429 struct pipe_texture
*pt
,
430 const GLfloat
*color
)
432 struct st_context
*st
= ctx
->st
;
433 struct pipe_context
*pipe
= ctx
->st
->pipe
;
434 struct cso_context
*cso
= ctx
->st
->cso_context
;
435 struct st_fragment_program
*stfp
;
438 stfp
= combined_bitmap_fragment_program(ctx
);
440 /* As an optimization, Mesa's fragment programs will sometimes get the
441 * primary color from a statevar/constant rather than a varying variable.
442 * when that's the case, we need to ensure that we use the 'color'
443 * parameter and not the current attribute color (which may have changed
444 * through glRasterPos and state validation.
445 * So, we force the proper color here. Not elegant, but it works.
448 GLfloat colorSave
[4];
449 COPY_4V(colorSave
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
450 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], color
);
451 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
452 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], colorSave
);
457 /* XXX if the bitmap is larger than the max texture size, break
460 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
461 assert(width
<= (GLsizei
)maxSize
);
462 assert(height
<= (GLsizei
)maxSize
);
464 cso_save_rasterizer(cso
);
465 cso_save_samplers(cso
);
466 cso_save_sampler_textures(cso
);
467 cso_save_viewport(cso
);
468 cso_save_fragment_shader(cso
);
469 cso_save_vertex_shader(cso
);
471 /* rasterizer state: just scissor */
472 st
->bitmap
.rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
473 cso_set_rasterizer(cso
, &st
->bitmap
.rasterizer
);
475 /* fragment shader state: TEX lookup program */
476 cso_set_fragment_shader_handle(cso
, stfp
->driver_shader
);
478 /* vertex shader state: position + texcoord pass-through */
479 cso_set_vertex_shader_handle(cso
, st
->bitmap
.vs
);
481 /* user samplers, plus our bitmap sampler */
483 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
484 uint num
= MAX2(stfp
->bitmap_sampler
+ 1, st
->state
.num_samplers
);
486 for (i
= 0; i
< st
->state
.num_samplers
; i
++) {
487 samplers
[i
] = &st
->state
.samplers
[i
];
489 samplers
[stfp
->bitmap_sampler
] = &st
->bitmap
.sampler
;
490 cso_set_samplers(cso
, num
, (const struct pipe_sampler_state
**) samplers
);
493 /* user textures, plus the bitmap texture */
495 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
496 uint num
= MAX2(stfp
->bitmap_sampler
+ 1, st
->state
.num_textures
);
497 memcpy(textures
, st
->state
.sampler_texture
, sizeof(textures
));
498 textures
[stfp
->bitmap_sampler
] = pt
;
499 cso_set_sampler_textures(cso
, num
, textures
);
502 /* viewport state: viewport matching window dims */
504 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
505 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
506 const GLfloat width
= (GLfloat
)fb
->Width
;
507 const GLfloat height
= (GLfloat
)fb
->Height
;
508 struct pipe_viewport_state vp
;
509 vp
.scale
[0] = 0.5f
* width
;
510 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
513 vp
.translate
[0] = 0.5f
* width
;
514 vp
.translate
[1] = 0.5f
* height
;
515 vp
.translate
[2] = 0.0f
;
516 vp
.translate
[3] = 0.0f
;
517 cso_set_viewport(cso
, &vp
);
520 /* draw textured quad */
521 setup_bitmap_vertex_data(st
, x
, y
, width
, height
,
522 ctx
->Current
.RasterPos
[2],
525 util_draw_vertex_buffer(pipe
, st
->bitmap
.vbuf
,
526 PIPE_PRIM_TRIANGLE_FAN
,
528 3); /* attribs/vert */
532 cso_restore_rasterizer(cso
);
533 cso_restore_samplers(cso
);
534 cso_restore_sampler_textures(cso
);
535 cso_restore_viewport(cso
);
536 cso_restore_fragment_shader(cso
);
537 cso_restore_vertex_shader(cso
);
542 reset_cache(struct st_context
*st
)
544 struct pipe_context
*pipe
= st
->pipe
;
545 struct pipe_screen
*screen
= pipe
->screen
;
546 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
548 //memset(cache->buffer, 0xff, sizeof(cache->buffer));
549 cache
->empty
= GL_TRUE
;
551 cache
->xmin
= 1000000;
552 cache
->xmax
= -1000000;
553 cache
->ymin
= 1000000;
554 cache
->ymax
= -1000000;
557 screen
->tex_surface_release(screen
, &cache
->surf
);
559 assert(!cache
->texture
);
561 /* allocate a new texture */
562 cache
->texture
= st_texture_create(st
, PIPE_TEXTURE_2D
,
563 st
->bitmap
.tex_format
, 0,
564 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
566 PIPE_TEXTURE_USAGE_SAMPLER
);
568 /* Map the texture surface.
569 * Subsequent glBitmap calls will write into the texture image.
571 cache
->surf
= screen
->get_tex_surface(screen
, cache
->texture
, 0, 0, 0,
572 PIPE_BUFFER_USAGE_CPU_WRITE
);
573 cache
->buffer
= screen
->surface_map(screen
, cache
->surf
,
574 PIPE_BUFFER_USAGE_CPU_WRITE
);
576 /* init image to all 0xff */
577 memset(cache
->buffer
, 0xff, BITMAP_CACHE_WIDTH
* BITMAP_CACHE_HEIGHT
);
582 * If there's anything in the bitmap cache, draw/flush it now.
585 st_flush_bitmap_cache(struct st_context
*st
)
587 if (!st
->bitmap
.cache
->empty
) {
588 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
590 if (st
->ctx
->DrawBuffer
) {
591 struct pipe_context
*pipe
= st
->pipe
;
592 struct pipe_screen
*screen
= pipe
->screen
;
594 assert(cache
->xmin
<= cache
->xmax
);
596 printf("flush size %d x %d at %d, %d\n",
597 cache->xmax - cache->xmin,
598 cache->ymax - cache->ymin,
599 cache->xpos, cache->ypos);
602 /* The texture surface has been mapped until now.
603 * So unmap and release the texture surface before drawing.
605 screen
->surface_unmap(screen
, cache
->surf
);
606 cache
->buffer
= NULL
;
608 screen
->tex_surface_release(screen
, &cache
->surf
);
610 draw_bitmap_quad(st
->ctx
,
613 st
->ctx
->Current
.RasterPos
[2],
614 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
619 /* release/free the texture */
620 pipe_texture_reference(&cache
->texture
, NULL
);
628 * Try to accumulate this glBitmap call in the bitmap cache.
629 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
632 accum_bitmap(struct st_context
*st
,
633 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
634 const struct gl_pixelstore_attrib
*unpack
,
635 const GLubyte
*bitmap
)
637 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
640 if (width
> BITMAP_CACHE_WIDTH
||
641 height
> BITMAP_CACHE_HEIGHT
)
642 return GL_FALSE
; /* too big to cache */
645 px
= x
- cache
->xpos
; /* pos in buffer */
646 py
= y
- cache
->ypos
;
647 if (px
< 0 || px
+ width
> BITMAP_CACHE_WIDTH
||
648 py
< 0 || py
+ height
> BITMAP_CACHE_HEIGHT
||
649 !TEST_EQ_4V(st
->ctx
->Current
.RasterColor
, cache
->color
)) {
650 /* This bitmap would extend beyond cache bounds, or the bitmap
652 * so flush and continue.
654 st_flush_bitmap_cache(st
);
659 /* Initialize. Center bitmap vertically in the buffer. */
661 py
= (BITMAP_CACHE_HEIGHT
- height
) / 2;
663 cache
->ypos
= y
- py
;
664 cache
->empty
= GL_FALSE
;
665 COPY_4FV(cache
->color
, st
->ctx
->Current
.RasterColor
);
674 if (x
+ width
> cache
->xmax
)
675 cache
->xmax
= x
+ width
;
676 if (y
+ height
> cache
->ymax
)
677 cache
->ymax
= y
+ height
;
679 unpack_bitmap(st
, px
, py
, width
, height
, unpack
, bitmap
,
680 cache
->buffer
, BITMAP_CACHE_WIDTH
);
682 return GL_TRUE
; /* accumulated */
688 * Called via ctx->Driver.Bitmap()
691 st_Bitmap(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
692 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
694 struct st_context
*st
= ctx
->st
;
695 struct pipe_texture
*pt
;
697 if (width
== 0 || height
== 0)
700 st_validate_state(st
);
702 if (!st
->bitmap
.vs
) {
703 /* create pass-through vertex shader now */
704 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
706 TGSI_SEMANTIC_GENERIC
};
707 const uint semantic_indexes
[] = { 0, 0, 0 };
708 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
711 &st
->bitmap
.vert_shader
);
714 if (UseBitmapCache
&& accum_bitmap(st
, x
, y
, width
, height
, unpack
, bitmap
))
717 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
719 assert(pt
->target
== PIPE_TEXTURE_2D
);
720 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
722 st
->ctx
->Current
.RasterColor
);
723 /* release/free the texture */
724 pipe_texture_reference(&pt
, NULL
);
729 /** Per-context init */
731 st_init_bitmap_functions(struct dd_function_table
*functions
)
733 functions
->Bitmap
= st_Bitmap
;
737 /** Per-context init */
739 st_init_bitmap(struct st_context
*st
)
741 struct pipe_sampler_state
*sampler
= &st
->bitmap
.sampler
;
742 struct pipe_context
*pipe
= st
->pipe
;
743 struct pipe_screen
*screen
= pipe
->screen
;
745 /* init sampler state once */
746 memset(sampler
, 0, sizeof(*sampler
));
747 sampler
->wrap_s
= PIPE_TEX_WRAP_CLAMP
;
748 sampler
->wrap_t
= PIPE_TEX_WRAP_CLAMP
;
749 sampler
->wrap_r
= PIPE_TEX_WRAP_CLAMP
;
750 sampler
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
751 sampler
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
752 sampler
->mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
753 sampler
->normalized_coords
= 1;
755 /* init baseline rasterizer state once */
756 memset(&st
->bitmap
.rasterizer
, 0, sizeof(st
->bitmap
.rasterizer
));
757 st
->bitmap
.rasterizer
.gl_rasterization_rules
= 1;
758 st
->bitmap
.rasterizer
.bypass_vs
= 1;
760 /* find a usable texture format */
761 if (screen
->is_format_supported(screen
, PIPE_FORMAT_I8_UNORM
, PIPE_TEXTURE_2D
,
762 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
763 st
->bitmap
.tex_format
= PIPE_FORMAT_I8_UNORM
;
766 /* XXX support more formats */
770 /* alloc bitmap cache object */
771 st
->bitmap
.cache
= CALLOC_STRUCT(bitmap_cache
);
777 /** Per-context tear-down */
779 st_destroy_bitmap(struct st_context
*st
)
781 struct pipe_context
*pipe
= st
->pipe
;
782 struct pipe_screen
*screen
= pipe
->screen
;
783 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
785 screen
->surface_unmap(screen
, cache
->surf
);
786 screen
->tex_surface_release(screen
, &cache
->surf
);
789 cso_delete_vertex_shader(st
->cso_context
, st
->bitmap
.vs
);
790 st
->bitmap
.vs
= NULL
;
793 if (st
->bitmap
.vbuf
) {
794 pipe_buffer_destroy(pipe
->screen
, st
->bitmap
.vbuf
);
795 st
->bitmap
.vbuf
= NULL
;
798 if (st
->bitmap
.cache
) {
799 pipe_texture_release(&st
->bitmap
.cache
->texture
);
800 FREE(st
->bitmap
.cache
);
801 st
->bitmap
.cache
= NULL
;