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 "shader/program.h"
38 #include "shader/prog_print.h"
40 #include "st_context.h"
42 #include "st_atom_constbuf.h"
43 #include "st_program.h"
44 #include "st_cb_bitmap.h"
45 #include "st_texture.h"
46 #include "st_inlines.h"
48 #include "pipe/p_context.h"
49 #include "pipe/p_defines.h"
50 #include "util/u_inlines.h"
51 #include "util/u_draw_quad.h"
52 #include "util/u_simple_shaders.h"
53 #include "shader/prog_instruction.h"
54 #include "cso_cache/cso_context.h"
59 * glBitmaps are drawn as textured quads. The user's bitmap pattern
60 * is stored in a texture image. An alpha8 texture format is used.
61 * The fragment shader samples a bit (texel) from the texture, then
62 * discards the fragment if the bit is off.
64 * Note that we actually store the inverse image of the bitmap to
65 * simplify the fragment program. An "on" bit gets stored as texel=0x0
66 * and an "off" bit is stored as texel=0xff. Then we kill the
67 * fragment if the negated texel value is less than zero.
72 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
73 * buffer which is then rendered en mass upon a flush, state change, etc.
74 * A wide, short buffer is used to target the common case of a series
75 * of glBitmap calls being used to draw text.
77 static GLboolean UseBitmapCache = GL_TRUE;
80 #define BITMAP_CACHE_WIDTH 512
81 #define BITMAP_CACHE_HEIGHT 32
85 /** Window pos to render the cached image */
87 /** Bounds of region used in window coords */
88 GLint xmin, ymin, xmax, ymax;
92 /** Bitmap's Z position */
95 struct pipe_texture *texture;
96 struct pipe_transfer *trans;
100 /** An I8 texture image: */
105 /** Epsilon for Z comparisons */
106 #define Z_EPSILON 1e-06
110 * Make fragment program for glBitmap:
111 * Sample the texture and kill the fragment if the bit is 0.
112 * This program will be combined with the user's fragment program.
114 static struct st_fragment_program *
115 make_bitmap_fragment_program(GLcontext *ctx, GLuint samplerIndex)
117 struct st_fragment_program *stfp;
118 struct gl_program *p;
121 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
125 p->NumInstructions = 3;
127 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
128 if (!p->Instructions) {
129 ctx->Driver.DeleteProgram(ctx, p);
132 _mesa_init_instructions(p->Instructions, p->NumInstructions);
134 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
135 p->Instructions[ic].Opcode = OPCODE_TEX;
136 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
137 p->Instructions[ic].DstReg.Index = 0;
138 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
139 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
140 p->Instructions[ic].TexSrcUnit = samplerIndex;
141 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
144 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
145 p->Instructions[ic].Opcode = OPCODE_KIL;
146 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
148 if (ctx->st->bitmap.tex_format == PIPE_FORMAT_L8_UNORM)
149 p->Instructions[ic].SrcReg[0].Swizzle = SWIZZLE_XXXX;
151 p->Instructions[ic].SrcReg[0].Index = 0;
152 p->Instructions[ic].SrcReg[0].Negate = NEGATE_XYZW;
156 p->Instructions[ic++].Opcode = OPCODE_END;
158 assert(ic == p->NumInstructions);
160 p->InputsRead = FRAG_BIT_TEX0;
161 p->OutputsWritten = 0x0;
162 p->SamplersUsed = (1 << samplerIndex);
164 stfp = (struct st_fragment_program *) p;
165 stfp->Base.UsesKill = GL_TRUE;
172 find_free_bit(uint bitfield)
175 for (i = 0; i < 32; i++) {
176 if ((bitfield & (1 << i)) == 0) {
185 * Combine basic bitmap fragment program with the user-defined program.
187 static struct st_fragment_program *
188 combined_bitmap_fragment_program(GLcontext *ctx)
190 struct st_context *st = ctx->st;
191 struct st_fragment_program *stfp = st->fp;
193 if (!stfp->bitmap_program) {
195 * Generate new program which is the user-defined program prefixed
196 * with the bitmap sampler/kill instructions.
198 struct st_fragment_program *bitmap_prog;
201 sampler = find_free_bit(st->fp->Base.Base.SamplersUsed);
202 bitmap_prog = make_bitmap_fragment_program(ctx, sampler);
204 stfp->bitmap_program = (struct st_fragment_program *)
205 _mesa_combine_programs(ctx,
206 &bitmap_prog->Base.Base, &stfp->Base.Base);
207 stfp->bitmap_program->bitmap_sampler = sampler;
209 /* done with this after combining */
210 st_reference_fragprog(st, &bitmap_prog, NULL);
214 struct gl_program *p = &stfp->bitmap_program->Base.Base;
215 printf("Combined bitmap program:\n");
216 _mesa_print_program(p);
217 printf("InputsRead: 0x%x\n", p->InputsRead);
218 printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
219 _mesa_print_parameter_list(p->Parameters);
223 /* translate to TGSI tokens */
224 st_translate_fragment_program(st, stfp->bitmap_program);
227 return stfp->bitmap_program;
232 * Copy user-provide bitmap bits into texture buffer, expanding
234 * "On" bits will set texels to 0x0.
235 * "Off" bits will not modify texels.
236 * Note that the image is actually going to be upside down in
237 * the texture. We deal with that with texcoords.
240 unpack_bitmap(struct st_context *st,
241 GLint px, GLint py, GLsizei width, GLsizei height,
242 const struct gl_pixelstore_attrib *unpack,
243 const GLubyte *bitmap,
244 ubyte *destBuffer, uint destStride)
246 destBuffer += py * destStride + px;
248 _mesa_expand_bitmap(width, height, unpack, bitmap,
249 destBuffer, destStride, 0x0);
254 * Create a texture which represents a bitmap image.
256 static struct pipe_texture *
257 make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
258 const struct gl_pixelstore_attrib *unpack,
259 const GLubyte *bitmap)
261 struct pipe_context *pipe = ctx->st->pipe;
262 struct pipe_transfer *transfer;
264 struct pipe_texture *pt;
267 bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
273 * Create texture to hold bitmap pattern.
275 pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, ctx->st->bitmap.tex_format,
277 PIPE_TEXTURE_USAGE_SAMPLER);
279 _mesa_unmap_pbo_source(ctx, unpack);
283 transfer = st_no_flush_get_tex_transfer(st_context(ctx), pt, 0, 0, 0,
285 0, 0, width, height);
287 dest = pipe->transfer_map(pipe, transfer);
289 /* Put image into texture transfer */
290 memset(dest, 0xff, height * transfer->stride);
291 unpack_bitmap(ctx->st, 0, 0, width, height, unpack, bitmap,
292 dest, transfer->stride);
294 _mesa_unmap_pbo_source(ctx, unpack);
296 /* Release transfer */
297 pipe->transfer_unmap(pipe, transfer);
298 pipe->tex_transfer_destroy(pipe, transfer);
304 setup_bitmap_vertex_data(struct st_context *st,
305 int x, int y, int width, int height,
306 float z, const float color[4])
308 struct pipe_context *pipe = st->pipe;
309 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
310 const GLfloat fb_width = (GLfloat)fb->Width;
311 const GLfloat fb_height = (GLfloat)fb->Height;
312 const GLfloat x0 = (GLfloat)x;
313 const GLfloat x1 = (GLfloat)(x + width);
314 const GLfloat y0 = (GLfloat)y;
315 const GLfloat y1 = (GLfloat)(y + height);
316 const GLfloat sLeft = (GLfloat)0.0, sRight = (GLfloat)1.0;
317 const GLfloat tTop = (GLfloat)0.0, tBot = (GLfloat)1.0 - tTop;
318 const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
319 const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
320 const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
321 const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
323 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
324 * no_flush) updates to buffers where we know there is no conflict
325 * with previous data. Currently using max_slots > 1 will cause
326 * synchronous rendering if the driver flushes its command buffers
327 * between one bitmap and the next. Our flush hook below isn't
328 * sufficient to catch this as the driver doesn't tell us when it
329 * flushes its own command buffers. Until this gets fixed, pay the
330 * price of allocating a new buffer for each bitmap cache-flush to
331 * avoid synchronous rendering.
333 const GLuint max_slots = 1; /* 4096 / sizeof(st->bitmap.vertices); */
336 if (st->bitmap.vbuf_slot >= max_slots) {
337 pipe_buffer_reference(&st->bitmap.vbuf, NULL);
338 st->bitmap.vbuf_slot = 0;
341 if (!st->bitmap.vbuf) {
342 st->bitmap.vbuf = pipe_buffer_create(pipe->screen, 32,
343 PIPE_BUFFER_USAGE_VERTEX,
344 max_slots * sizeof(st->bitmap.vertices));
347 /* Positions are in clip coords since we need to do clipping in case
348 * the bitmap quad goes beyond the window bounds.
350 st->bitmap.vertices[0][0][0] = clip_x0;
351 st->bitmap.vertices[0][0][1] = clip_y0;
352 st->bitmap.vertices[0][2][0] = sLeft;
353 st->bitmap.vertices[0][2][1] = tTop;
355 st->bitmap.vertices[1][0][0] = clip_x1;
356 st->bitmap.vertices[1][0][1] = clip_y0;
357 st->bitmap.vertices[1][2][0] = sRight;
358 st->bitmap.vertices[1][2][1] = tTop;
360 st->bitmap.vertices[2][0][0] = clip_x1;
361 st->bitmap.vertices[2][0][1] = clip_y1;
362 st->bitmap.vertices[2][2][0] = sRight;
363 st->bitmap.vertices[2][2][1] = tBot;
365 st->bitmap.vertices[3][0][0] = clip_x0;
366 st->bitmap.vertices[3][0][1] = clip_y1;
367 st->bitmap.vertices[3][2][0] = sLeft;
368 st->bitmap.vertices[3][2][1] = tBot;
370 /* same for all verts: */
371 for (i = 0; i < 4; i++) {
372 st->bitmap.vertices[i][0][2] = z;
373 st->bitmap.vertices[i][0][3] = 1.0;
374 st->bitmap.vertices[i][1][0] = color[0];
375 st->bitmap.vertices[i][1][1] = color[1];
376 st->bitmap.vertices[i][1][2] = color[2];
377 st->bitmap.vertices[i][1][3] = color[3];
378 st->bitmap.vertices[i][2][2] = 0.0; /*R*/
379 st->bitmap.vertices[i][2][3] = 1.0; /*Q*/
382 /* put vertex data into vbuf */
383 st_no_flush_pipe_buffer_write_nooverlap(st,
385 st->bitmap.vbuf_slot * sizeof st->bitmap.vertices,
386 sizeof st->bitmap.vertices,
387 st->bitmap.vertices);
389 return st->bitmap.vbuf_slot++ * sizeof st->bitmap.vertices;
395 * Render a glBitmap by drawing a textured quad
398 draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
399 GLsizei width, GLsizei height,
400 struct pipe_sampler_view *sv,
401 const GLfloat *color)
403 struct st_context *st = ctx->st;
404 struct pipe_context *pipe = ctx->st->pipe;
405 struct cso_context *cso = ctx->st->cso_context;
406 struct st_fragment_program *stfp;
410 stfp = combined_bitmap_fragment_program(ctx);
412 /* As an optimization, Mesa's fragment programs will sometimes get the
413 * primary color from a statevar/constant rather than a varying variable.
414 * when that's the case, we need to ensure that we use the 'color'
415 * parameter and not the current attribute color (which may have changed
416 * through glRasterPos and state validation.
417 * So, we force the proper color here. Not elegant, but it works.
420 GLfloat colorSave[4];
421 COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
422 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
423 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
424 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
429 /* XXX if the bitmap is larger than the max texture size, break
432 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
433 assert(width <= (GLsizei)maxSize);
434 assert(height <= (GLsizei)maxSize);
436 cso_save_rasterizer(cso);
437 cso_save_samplers(cso);
438 cso_save_fragment_sampler_views(cso);
439 cso_save_viewport(cso);
440 cso_save_fragment_shader(cso);
441 cso_save_vertex_shader(cso);
442 cso_save_vertex_elements(cso);
444 /* rasterizer state: just scissor */
445 st->bitmap.rasterizer.scissor = ctx->Scissor.Enabled;
446 cso_set_rasterizer(cso, &st->bitmap.rasterizer);
448 /* fragment shader state: TEX lookup program */
449 cso_set_fragment_shader_handle(cso, stfp->driver_shader);
451 /* vertex shader state: position + texcoord pass-through */
452 cso_set_vertex_shader_handle(cso, st->bitmap.vs);
454 /* user samplers, plus our bitmap sampler */
456 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
457 uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_samplers);
459 for (i = 0; i < st->state.num_samplers; i++) {
460 samplers[i] = &st->state.samplers[i];
462 samplers[stfp->bitmap_sampler] = &st->bitmap.sampler;
463 cso_set_samplers(cso, num, (const struct pipe_sampler_state **) samplers);
466 /* user textures, plus the bitmap texture */
468 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
469 uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_textures);
470 memcpy(sampler_views, st->state.sampler_views, sizeof(sampler_views));
471 sampler_views[stfp->bitmap_sampler] = sv;
472 cso_set_fragment_sampler_views(cso, num, sampler_views);
475 /* viewport state: viewport matching window dims */
477 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
478 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
479 const GLfloat width = (GLfloat)fb->Width;
480 const GLfloat height = (GLfloat)fb->Height;
481 struct pipe_viewport_state vp;
482 vp.scale[0] = 0.5f * width;
483 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
486 vp.translate[0] = 0.5f * width;
487 vp.translate[1] = 0.5f * height;
488 vp.translate[2] = 0.5f;
489 vp.translate[3] = 0.0f;
490 cso_set_viewport(cso, &vp);
493 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
495 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
498 /* draw textured quad */
499 offset = setup_bitmap_vertex_data(st, x, y, width, height, z, color);
501 util_draw_vertex_buffer(pipe, st->bitmap.vbuf, offset,
502 PIPE_PRIM_TRIANGLE_FAN,
504 3); /* attribs/vert */
508 cso_restore_rasterizer(cso);
509 cso_restore_samplers(cso);
510 cso_restore_fragment_sampler_views(cso);
511 cso_restore_viewport(cso);
512 cso_restore_fragment_shader(cso);
513 cso_restore_vertex_shader(cso);
514 cso_restore_vertex_elements(cso);
519 reset_cache(struct st_context *st)
521 struct pipe_context *pipe = st->pipe;
522 struct bitmap_cache *cache = st->bitmap.cache;
524 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
525 cache->empty = GL_TRUE;
527 cache->xmin = 1000000;
528 cache->xmax = -1000000;
529 cache->ymin = 1000000;
530 cache->ymax = -1000000;
533 pipe->tex_transfer_destroy(pipe, cache->trans);
537 assert(!cache->texture);
539 /* allocate a new texture */
540 cache->texture = st_texture_create(st, PIPE_TEXTURE_2D,
541 st->bitmap.tex_format, 0,
542 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
543 1, PIPE_TEXTURE_USAGE_SAMPLER);
547 /** Print bitmap image to stdout (debug) */
549 print_cache(const struct bitmap_cache *cache)
553 for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
554 k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
555 for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
556 if (cache->buffer[k])
568 create_cache_trans(struct st_context *st)
570 struct pipe_context *pipe = st->pipe;
571 struct bitmap_cache *cache = st->bitmap.cache;
576 /* Map the texture transfer.
577 * Subsequent glBitmap calls will write into the texture image.
579 cache->trans = st_no_flush_get_tex_transfer(st, cache->texture, 0, 0, 0,
580 PIPE_TRANSFER_WRITE, 0, 0,
582 BITMAP_CACHE_HEIGHT);
583 cache->buffer = pipe->transfer_map(pipe, cache->trans);
585 /* init image to all 0xff */
586 memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
591 * If there's anything in the bitmap cache, draw/flush it now.
594 st_flush_bitmap_cache(struct st_context *st)
596 if (!st->bitmap.cache->empty) {
597 struct bitmap_cache *cache = st->bitmap.cache;
599 if (st->ctx->DrawBuffer) {
600 struct pipe_context *pipe = st->pipe;
601 struct pipe_sampler_view *sv;
603 assert(cache->xmin <= cache->xmax);
605 /* printf("flush size %d x %d at %d, %d\n",
606 cache->xmax - cache->xmin,
607 cache->ymax - cache->ymin,
608 cache->xpos, cache->ypos);
611 /* The texture transfer has been mapped until now.
612 * So unmap and release the texture transfer before drawing.
617 pipe->transfer_unmap(pipe, cache->trans);
618 cache->buffer = NULL;
620 pipe->tex_transfer_destroy(pipe, cache->trans);
624 sv = st_sampler_view_from_texture(st->pipe, cache->texture);
626 draw_bitmap_quad(st->ctx,
630 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
634 pipe_sampler_view_reference(&sv, NULL);
638 /* release/free the texture */
639 pipe_texture_reference(&cache->texture, NULL);
645 /* Flush bitmap cache and release vertex buffer.
648 st_flush_bitmap( struct st_context *st )
650 st_flush_bitmap_cache(st);
652 /* Release vertex buffer to avoid synchronous rendering if we were
653 * to map it in the next frame.
655 pipe_buffer_reference(&st->bitmap.vbuf, NULL);
656 st->bitmap.vbuf_slot = 0;
661 * Try to accumulate this glBitmap call in the bitmap cache.
662 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
665 accum_bitmap(struct st_context *st,
666 GLint x, GLint y, GLsizei width, GLsizei height,
667 const struct gl_pixelstore_attrib *unpack,
668 const GLubyte *bitmap )
670 struct bitmap_cache *cache = st->bitmap.cache;
671 int px = -999, py = -999;
672 const GLfloat z = st->ctx->Current.RasterPos[2];
674 if (width > BITMAP_CACHE_WIDTH ||
675 height > BITMAP_CACHE_HEIGHT)
676 return GL_FALSE; /* too big to cache */
679 px = x - cache->xpos; /* pos in buffer */
680 py = y - cache->ypos;
681 if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
682 py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
683 !TEST_EQ_4V(st->ctx->Current.RasterColor, cache->color) ||
684 ((fabs(z - cache->zpos) > Z_EPSILON))) {
685 /* This bitmap would extend beyond cache bounds, or the bitmap
687 * so flush and continue.
689 st_flush_bitmap_cache(st);
694 /* Initialize. Center bitmap vertically in the buffer. */
696 py = (BITMAP_CACHE_HEIGHT - height) / 2;
698 cache->ypos = y - py;
700 cache->empty = GL_FALSE;
701 COPY_4FV(cache->color, st->ctx->Current.RasterColor);
711 if (x + width > cache->xmax)
712 cache->xmax = x + width;
713 if (y + height > cache->ymax)
714 cache->ymax = y + height;
716 /* create the transfer if needed */
717 create_cache_trans(st);
719 unpack_bitmap(st, px, py, width, height, unpack, bitmap,
720 cache->buffer, BITMAP_CACHE_WIDTH);
722 return GL_TRUE; /* accumulated */
728 * Called via ctx->Driver.Bitmap()
731 st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
732 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
734 struct st_context *st = ctx->st;
735 struct pipe_texture *pt;
737 if (width == 0 || height == 0)
740 st_validate_state(st);
742 if (!st->bitmap.vs) {
743 /* create pass-through vertex shader now */
744 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
746 TGSI_SEMANTIC_GENERIC };
747 const uint semantic_indexes[] = { 0, 0, 0 };
748 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
753 if (UseBitmapCache && accum_bitmap(st, x, y, width, height, unpack, bitmap))
756 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
758 struct pipe_sampler_view *sv = st_sampler_view_from_texture(st->pipe, pt);
760 assert(pt->target == PIPE_TEXTURE_2D);
763 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
765 st->ctx->Current.RasterColor);
767 pipe_sampler_view_reference(&sv, NULL);
770 /* release/free the texture */
771 pipe_texture_reference(&pt, NULL);
776 /** Per-context init */
778 st_init_bitmap_functions(struct dd_function_table *functions)
780 functions->Bitmap = st_Bitmap;
784 /** Per-context init */
786 st_init_bitmap(struct st_context *st)
788 struct pipe_sampler_state *sampler = &st->bitmap.sampler;
789 struct pipe_context *pipe = st->pipe;
790 struct pipe_screen *screen = pipe->screen;
792 /* init sampler state once */
793 memset(sampler, 0, sizeof(*sampler));
794 sampler->wrap_s = PIPE_TEX_WRAP_CLAMP;
795 sampler->wrap_t = PIPE_TEX_WRAP_CLAMP;
796 sampler->wrap_r = PIPE_TEX_WRAP_CLAMP;
797 sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST;
798 sampler->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
799 sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
800 sampler->normalized_coords = 1;
802 /* init baseline rasterizer state once */
803 memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
804 st->bitmap.rasterizer.gl_rasterization_rules = 1;
806 /* find a usable texture format */
807 if (screen->is_format_supported(screen, PIPE_FORMAT_I8_UNORM, PIPE_TEXTURE_2D,
808 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
809 st->bitmap.tex_format = PIPE_FORMAT_I8_UNORM;
811 else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM, PIPE_TEXTURE_2D,
812 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
813 st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
815 else if (screen->is_format_supported(screen, PIPE_FORMAT_L8_UNORM, PIPE_TEXTURE_2D,
816 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
817 st->bitmap.tex_format = PIPE_FORMAT_L8_UNORM;
820 /* XXX support more formats */
824 /* alloc bitmap cache object */
825 st->bitmap.cache = ST_CALLOC_STRUCT(bitmap_cache);
831 /** Per-context tear-down */
833 st_destroy_bitmap(struct st_context *st)
835 struct pipe_context *pipe = st->pipe;
836 struct bitmap_cache *cache = st->bitmap.cache;
841 cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
842 st->bitmap.vs = NULL;
845 if (st->bitmap.vbuf) {
846 pipe_buffer_reference(&st->bitmap.vbuf, NULL);
847 st->bitmap.vbuf = NULL;
852 pipe->transfer_unmap(pipe, cache->trans);
853 pipe->tex_transfer_destroy(pipe, cache->trans);
855 pipe_texture_reference(&st->bitmap.cache->texture, NULL);
856 free(st->bitmap.cache);
857 st->bitmap.cache = NULL;