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_transfer
*trans
;
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
;
146 if (ctx
->st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
147 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_XXXX
;
149 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
150 p
->Instructions
[ic
].SrcReg
[0].NegateBase
= NEGATE_XYZW
;
154 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
156 assert(ic
== p
->NumInstructions
);
158 p
->InputsRead
= FRAG_BIT_TEX0
;
159 p
->OutputsWritten
= 0x0;
160 p
->SamplersUsed
= (1 << samplerIndex
);
162 stfp
= (struct st_fragment_program
*) p
;
163 stfp
->Base
.UsesKill
= GL_TRUE
;
165 /* No need to send this incomplete program down to hardware:
167 * st_translate_fragment_program(ctx->st, stfp, NULL);
175 find_free_bit(uint bitfield
)
178 for (i
= 0; i
< 32; i
++) {
179 if ((bitfield
& (1 << i
)) == 0) {
188 * Combine basic bitmap fragment program with the user-defined program.
190 static struct st_fragment_program
*
191 combined_bitmap_fragment_program(GLcontext
*ctx
)
193 struct st_context
*st
= ctx
->st
;
194 struct st_fragment_program
*stfp
= st
->fp
;
196 if (!stfp
->bitmap_program
) {
198 * Generate new program which is the user-defined program prefixed
199 * with the bitmap sampler/kill instructions.
201 struct st_fragment_program
*bitmap_prog
;
204 sampler
= find_free_bit(st
->fp
->Base
.Base
.SamplersUsed
);
205 bitmap_prog
= make_bitmap_fragment_program(ctx
, sampler
);
207 stfp
->bitmap_program
= (struct st_fragment_program
*)
208 _mesa_combine_programs(ctx
,
209 &bitmap_prog
->Base
.Base
, &stfp
->Base
.Base
);
210 stfp
->bitmap_program
->bitmap_sampler
= sampler
;
212 /* done with this after combining */
213 st_reference_fragprog(st
, &bitmap_prog
, NULL
);
217 struct gl_program
*p
= &stfp
->bitmap_program
->Base
.Base
;
218 printf("Combined bitmap program:\n");
219 _mesa_print_program(p
);
220 printf("InputsRead: 0x%x\n", p
->InputsRead
);
221 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
222 _mesa_print_parameter_list(p
->Parameters
);
226 /* translate to TGSI tokens */
227 st_translate_fragment_program(st
, stfp
->bitmap_program
, NULL
);
230 return stfp
->bitmap_program
;
235 * Copy user-provide bitmap bits into texture buffer, expanding
237 * "On" bits will set texels to 0xff.
238 * "Off" bits will not modify texels.
239 * Note that the image is actually going to be upside down in
240 * the texture. We deal with that with texcoords.
243 unpack_bitmap(struct st_context
*st
,
244 GLint px
, GLint py
, GLsizei width
, GLsizei height
,
245 const struct gl_pixelstore_attrib
*unpack
,
246 const GLubyte
*bitmap
,
247 ubyte
*destBuffer
, uint destStride
)
251 #define SET_PIXEL(COL, ROW) \
252 destBuffer[(py + (ROW)) * destStride + px + (COL)] = 0x0;
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);
258 if (unpack
->LsbFirst
) {
260 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
261 for (col
= 0; col
< width
; col
++) {
276 /* get ready for next row */
282 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
283 for (col
= 0; col
< width
; col
++) {
298 /* get ready for next row */
310 * Create a texture which represents a bitmap image.
312 static struct pipe_texture
*
313 make_bitmap_texture(GLcontext
*ctx
, GLsizei width
, GLsizei height
,
314 const struct gl_pixelstore_attrib
*unpack
,
315 const GLubyte
*bitmap
)
317 struct pipe_context
*pipe
= ctx
->st
->pipe
;
318 struct pipe_screen
*screen
= pipe
->screen
;
319 struct pipe_transfer
*transfer
;
321 struct pipe_texture
*pt
;
324 bitmap
= _mesa_map_bitmap_pbo(ctx
, unpack
, bitmap
);
330 * Create texture to hold bitmap pattern.
332 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, ctx
->st
->bitmap
.tex_format
,
334 PIPE_TEXTURE_USAGE_SAMPLER
);
336 _mesa_unmap_bitmap_pbo(ctx
, unpack
);
340 transfer
= screen
->get_tex_transfer(screen
, pt
, 0, 0, 0, PIPE_TRANSFER_WRITE
,
341 0, 0, width
, height
);
343 dest
= screen
->transfer_map(screen
, transfer
);
345 /* Put image into texture transfer */
346 memset(dest
, 0xff, height
* transfer
->stride
);
347 unpack_bitmap(ctx
->st
, 0, 0, width
, height
, unpack
, bitmap
,
348 dest
, transfer
->stride
);
350 _mesa_unmap_bitmap_pbo(ctx
, unpack
);
352 /* Release transfer */
353 screen
->transfer_unmap(screen
, transfer
);
354 screen
->tex_transfer_destroy(transfer
);
360 setup_bitmap_vertex_data(struct st_context
*st
,
361 int x
, int y
, int width
, int height
,
362 float z
, const float color
[4])
364 struct pipe_context
*pipe
= st
->pipe
;
365 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
366 const GLfloat fb_width
= (GLfloat
)fb
->Width
;
367 const GLfloat fb_height
= (GLfloat
)fb
->Height
;
368 const GLfloat x0
= (GLfloat
)x
;
369 const GLfloat x1
= (GLfloat
)(x
+ width
);
370 const GLfloat y0
= (GLfloat
)y
;
371 const GLfloat y1
= (GLfloat
)(y
+ height
);
372 const GLfloat sLeft
= (GLfloat
)0.0, sRight
= (GLfloat
)1.0;
373 const GLfloat tTop
= (GLfloat
)0.0, tBot
= (GLfloat
)1.0 - tTop
;
374 const GLfloat clip_x0
= (GLfloat
)(x0
/ fb_width
* 2.0 - 1.0);
375 const GLfloat clip_y0
= (GLfloat
)(y0
/ fb_height
* 2.0 - 1.0);
376 const GLfloat clip_x1
= (GLfloat
)(x1
/ fb_width
* 2.0 - 1.0);
377 const GLfloat clip_y1
= (GLfloat
)(y1
/ fb_height
* 2.0 - 1.0);
378 const GLuint max_slots
= 4096 / sizeof(st
->bitmap
.vertices
);
381 if (st
->bitmap
.vbuf_slot
>= max_slots
) {
382 pipe_buffer_reference(&st
->bitmap
.vbuf
, NULL
);
383 st
->bitmap
.vbuf_slot
= 0;
386 if (!st
->bitmap
.vbuf
) {
387 st
->bitmap
.vbuf
= pipe_buffer_create(pipe
->screen
, 32,
388 PIPE_BUFFER_USAGE_VERTEX
,
389 max_slots
* sizeof(st
->bitmap
.vertices
));
392 /* Positions are in clip coords since we need to do clipping in case
393 * the bitmap quad goes beyond the window bounds.
395 st
->bitmap
.vertices
[0][0][0] = clip_x0
;
396 st
->bitmap
.vertices
[0][0][1] = clip_y0
;
397 st
->bitmap
.vertices
[0][2][0] = sLeft
;
398 st
->bitmap
.vertices
[0][2][1] = tTop
;
400 st
->bitmap
.vertices
[1][0][0] = clip_x1
;
401 st
->bitmap
.vertices
[1][0][1] = clip_y0
;
402 st
->bitmap
.vertices
[1][2][0] = sRight
;
403 st
->bitmap
.vertices
[1][2][1] = tTop
;
405 st
->bitmap
.vertices
[2][0][0] = clip_x1
;
406 st
->bitmap
.vertices
[2][0][1] = clip_y1
;
407 st
->bitmap
.vertices
[2][2][0] = sRight
;
408 st
->bitmap
.vertices
[2][2][1] = tBot
;
410 st
->bitmap
.vertices
[3][0][0] = clip_x0
;
411 st
->bitmap
.vertices
[3][0][1] = clip_y1
;
412 st
->bitmap
.vertices
[3][2][0] = sLeft
;
413 st
->bitmap
.vertices
[3][2][1] = tBot
;
415 /* same for all verts: */
416 for (i
= 0; i
< 4; i
++) {
417 st
->bitmap
.vertices
[i
][0][2] = z
;
418 st
->bitmap
.vertices
[i
][0][3] = 1.0;
419 st
->bitmap
.vertices
[i
][1][0] = color
[0];
420 st
->bitmap
.vertices
[i
][1][1] = color
[1];
421 st
->bitmap
.vertices
[i
][1][2] = color
[2];
422 st
->bitmap
.vertices
[i
][1][3] = color
[3];
423 st
->bitmap
.vertices
[i
][2][2] = 0.0; /*R*/
424 st
->bitmap
.vertices
[i
][2][3] = 1.0; /*Q*/
427 /* put vertex data into vbuf */
428 pipe_buffer_write(pipe
->screen
,
430 st
->bitmap
.vbuf_slot
* sizeof st
->bitmap
.vertices
,
431 sizeof st
->bitmap
.vertices
,
432 st
->bitmap
.vertices
);
434 return st
->bitmap
.vbuf_slot
++ * sizeof st
->bitmap
.vertices
;
440 * Render a glBitmap by drawing a textured quad
443 draw_bitmap_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
444 GLsizei width
, GLsizei height
,
445 struct pipe_texture
*pt
,
446 const GLfloat
*color
)
448 struct st_context
*st
= ctx
->st
;
449 struct pipe_context
*pipe
= ctx
->st
->pipe
;
450 struct cso_context
*cso
= ctx
->st
->cso_context
;
451 struct st_fragment_program
*stfp
;
455 stfp
= combined_bitmap_fragment_program(ctx
);
457 /* As an optimization, Mesa's fragment programs will sometimes get the
458 * primary color from a statevar/constant rather than a varying variable.
459 * when that's the case, we need to ensure that we use the 'color'
460 * parameter and not the current attribute color (which may have changed
461 * through glRasterPos and state validation.
462 * So, we force the proper color here. Not elegant, but it works.
465 GLfloat colorSave
[4];
466 COPY_4V(colorSave
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
467 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], color
);
468 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
469 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], colorSave
);
474 /* XXX if the bitmap is larger than the max texture size, break
477 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
478 assert(width
<= (GLsizei
)maxSize
);
479 assert(height
<= (GLsizei
)maxSize
);
481 cso_save_rasterizer(cso
);
482 cso_save_samplers(cso
);
483 cso_save_sampler_textures(cso
);
484 cso_save_viewport(cso
);
485 cso_save_fragment_shader(cso
);
486 cso_save_vertex_shader(cso
);
488 /* rasterizer state: just scissor */
489 st
->bitmap
.rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
490 cso_set_rasterizer(cso
, &st
->bitmap
.rasterizer
);
492 /* fragment shader state: TEX lookup program */
493 cso_set_fragment_shader_handle(cso
, stfp
->driver_shader
);
495 /* vertex shader state: position + texcoord pass-through */
496 cso_set_vertex_shader_handle(cso
, st
->bitmap
.vs
);
498 /* user samplers, plus our bitmap sampler */
500 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
501 uint num
= MAX2(stfp
->bitmap_sampler
+ 1, st
->state
.num_samplers
);
503 for (i
= 0; i
< st
->state
.num_samplers
; i
++) {
504 samplers
[i
] = &st
->state
.samplers
[i
];
506 samplers
[stfp
->bitmap_sampler
] = &st
->bitmap
.sampler
;
507 cso_set_samplers(cso
, num
, (const struct pipe_sampler_state
**) samplers
);
510 /* user textures, plus the bitmap texture */
512 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
513 uint num
= MAX2(stfp
->bitmap_sampler
+ 1, st
->state
.num_textures
);
514 memcpy(textures
, st
->state
.sampler_texture
, sizeof(textures
));
515 textures
[stfp
->bitmap_sampler
] = pt
;
516 cso_set_sampler_textures(cso
, num
, textures
);
519 /* viewport state: viewport matching window dims */
521 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
522 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
523 const GLfloat width
= (GLfloat
)fb
->Width
;
524 const GLfloat height
= (GLfloat
)fb
->Height
;
525 struct pipe_viewport_state vp
;
526 vp
.scale
[0] = 0.5f
* width
;
527 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
530 vp
.translate
[0] = 0.5f
* width
;
531 vp
.translate
[1] = 0.5f
* height
;
532 vp
.translate
[2] = 0.0f
;
533 vp
.translate
[3] = 0.0f
;
534 cso_set_viewport(cso
, &vp
);
537 /* draw textured quad */
538 offset
= setup_bitmap_vertex_data(st
, x
, y
, width
, height
,
539 ctx
->Current
.RasterPos
[2],
542 util_draw_vertex_buffer(pipe
, st
->bitmap
.vbuf
, offset
,
543 PIPE_PRIM_TRIANGLE_FAN
,
545 3); /* attribs/vert */
549 cso_restore_rasterizer(cso
);
550 cso_restore_samplers(cso
);
551 cso_restore_sampler_textures(cso
);
552 cso_restore_viewport(cso
);
553 cso_restore_fragment_shader(cso
);
554 cso_restore_vertex_shader(cso
);
559 reset_cache(struct st_context
*st
)
561 struct pipe_context
*pipe
= st
->pipe
;
562 struct pipe_screen
*screen
= pipe
->screen
;
563 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
565 //memset(cache->buffer, 0xff, sizeof(cache->buffer));
566 cache
->empty
= GL_TRUE
;
568 cache
->xmin
= 1000000;
569 cache
->xmax
= -1000000;
570 cache
->ymin
= 1000000;
571 cache
->ymax
= -1000000;
574 screen
->tex_transfer_destroy(cache
->trans
);
576 assert(!cache
->texture
);
578 /* allocate a new texture */
579 cache
->texture
= st_texture_create(st
, PIPE_TEXTURE_2D
,
580 st
->bitmap
.tex_format
, 0,
581 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
582 1, PIPE_TEXTURE_USAGE_SAMPLER
);
584 /* Map the texture transfer.
585 * Subsequent glBitmap calls will write into the texture image.
587 cache
->trans
= screen
->get_tex_transfer(screen
, cache
->texture
, 0, 0, 0,
588 PIPE_TRANSFER_WRITE
, 0, 0,
590 BITMAP_CACHE_HEIGHT
);
591 cache
->buffer
= screen
->transfer_map(screen
, cache
->trans
);
593 /* init image to all 0xff */
594 memset(cache
->buffer
, 0xff, cache
->trans
->stride
* BITMAP_CACHE_HEIGHT
);
599 * If there's anything in the bitmap cache, draw/flush it now.
602 st_flush_bitmap_cache(struct st_context
*st
)
604 if (!st
->bitmap
.cache
->empty
) {
605 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
607 if (st
->ctx
->DrawBuffer
) {
608 struct pipe_context
*pipe
= st
->pipe
;
609 struct pipe_screen
*screen
= pipe
->screen
;
611 assert(cache
->xmin
<= cache
->xmax
);
613 /* printf("flush size %d x %d at %d, %d\n",
614 cache->xmax - cache->xmin,
615 cache->ymax - cache->ymin,
616 cache->xpos, cache->ypos);
619 /* The texture transfer has been mapped until now.
620 * So unmap and release the texture transfer before drawing.
622 screen
->transfer_unmap(screen
, cache
->trans
);
623 cache
->buffer
= NULL
;
625 screen
->tex_transfer_destroy(cache
->trans
);
628 draw_bitmap_quad(st
->ctx
,
631 st
->ctx
->Current
.RasterPos
[2],
632 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
637 /* release/free the texture */
638 pipe_texture_reference(&cache
->texture
, NULL
);
644 /* Flush bitmap cache and release vertex buffer.
647 st_flush_bitmap( struct st_context
*st
)
649 st_flush_bitmap_cache(st
);
651 /* Release vertex buffer to avoid synchronous rendering if we were
652 * to map it in the next frame.
654 pipe_buffer_reference(&st
->bitmap
.vbuf
, NULL
);
655 st
->bitmap
.vbuf_slot
= 0;
660 * Try to accumulate this glBitmap call in the bitmap cache.
661 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
664 accum_bitmap(struct st_context
*st
,
665 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
666 const struct gl_pixelstore_attrib
*unpack
,
667 const GLubyte
*bitmap
)
669 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
672 if (width
> BITMAP_CACHE_WIDTH
||
673 height
> BITMAP_CACHE_HEIGHT
)
674 return GL_FALSE
; /* too big to cache */
677 px
= x
- cache
->xpos
; /* pos in buffer */
678 py
= y
- cache
->ypos
;
679 if (px
< 0 || px
+ width
> BITMAP_CACHE_WIDTH
||
680 py
< 0 || py
+ height
> BITMAP_CACHE_HEIGHT
||
681 !TEST_EQ_4V(st
->ctx
->Current
.RasterColor
, cache
->color
)) {
682 /* This bitmap would extend beyond cache bounds, or the bitmap
684 * so flush and continue.
686 st_flush_bitmap_cache(st
);
691 /* Initialize. Center bitmap vertically in the buffer. */
693 py
= (BITMAP_CACHE_HEIGHT
- height
) / 2;
695 cache
->ypos
= y
- py
;
696 cache
->empty
= GL_FALSE
;
697 COPY_4FV(cache
->color
, st
->ctx
->Current
.RasterColor
);
706 if (x
+ width
> cache
->xmax
)
707 cache
->xmax
= x
+ width
;
708 if (y
+ height
> cache
->ymax
)
709 cache
->ymax
= y
+ height
;
711 unpack_bitmap(st
, px
, py
, width
, height
, unpack
, bitmap
,
712 cache
->buffer
, BITMAP_CACHE_WIDTH
);
714 return GL_TRUE
; /* accumulated */
720 * Called via ctx->Driver.Bitmap()
723 st_Bitmap(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
724 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
726 struct st_context
*st
= ctx
->st
;
727 struct pipe_texture
*pt
;
729 if (width
== 0 || height
== 0)
732 st_validate_state(st
);
734 if (!st
->bitmap
.vs
) {
735 /* create pass-through vertex shader now */
736 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
738 TGSI_SEMANTIC_GENERIC
};
739 const uint semantic_indexes
[] = { 0, 0, 0 };
740 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
745 if (UseBitmapCache
&& accum_bitmap(st
, x
, y
, width
, height
, unpack
, bitmap
))
748 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
750 assert(pt
->target
== PIPE_TEXTURE_2D
);
751 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
753 st
->ctx
->Current
.RasterColor
);
754 /* release/free the texture */
755 pipe_texture_reference(&pt
, NULL
);
760 /** Per-context init */
762 st_init_bitmap_functions(struct dd_function_table
*functions
)
764 functions
->Bitmap
= st_Bitmap
;
768 /** Per-context init */
770 st_init_bitmap(struct st_context
*st
)
772 struct pipe_sampler_state
*sampler
= &st
->bitmap
.sampler
;
773 struct pipe_context
*pipe
= st
->pipe
;
774 struct pipe_screen
*screen
= pipe
->screen
;
776 /* init sampler state once */
777 memset(sampler
, 0, sizeof(*sampler
));
778 sampler
->wrap_s
= PIPE_TEX_WRAP_CLAMP
;
779 sampler
->wrap_t
= PIPE_TEX_WRAP_CLAMP
;
780 sampler
->wrap_r
= PIPE_TEX_WRAP_CLAMP
;
781 sampler
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
782 sampler
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
783 sampler
->mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
784 sampler
->normalized_coords
= 1;
786 /* init baseline rasterizer state once */
787 memset(&st
->bitmap
.rasterizer
, 0, sizeof(st
->bitmap
.rasterizer
));
788 st
->bitmap
.rasterizer
.gl_rasterization_rules
= 1;
790 /* find a usable texture format */
791 if (screen
->is_format_supported(screen
, PIPE_FORMAT_I8_UNORM
, PIPE_TEXTURE_2D
,
792 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
793 st
->bitmap
.tex_format
= PIPE_FORMAT_I8_UNORM
;
795 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_A8_UNORM
, PIPE_TEXTURE_2D
,
796 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
797 st
->bitmap
.tex_format
= PIPE_FORMAT_A8_UNORM
;
799 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_L8_UNORM
, PIPE_TEXTURE_2D
,
800 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
801 st
->bitmap
.tex_format
= PIPE_FORMAT_L8_UNORM
;
804 /* XXX support more formats */
808 /* alloc bitmap cache object */
809 st
->bitmap
.cache
= ST_CALLOC_STRUCT(bitmap_cache
);
815 /** Per-context tear-down */
817 st_destroy_bitmap(struct st_context
*st
)
819 struct pipe_context
*pipe
= st
->pipe
;
820 struct pipe_screen
*screen
= pipe
->screen
;
821 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
823 screen
->transfer_unmap(screen
, cache
->trans
);
824 screen
->tex_transfer_destroy(cache
->trans
);
827 cso_delete_vertex_shader(st
->cso_context
, st
->bitmap
.vs
);
828 st
->bitmap
.vs
= NULL
;
831 if (st
->bitmap
.vbuf
) {
832 pipe_buffer_reference(&st
->bitmap
.vbuf
, NULL
);
833 st
->bitmap
.vbuf
= NULL
;
836 if (st
->bitmap
.cache
) {
837 pipe_texture_reference(&st
->bitmap
.cache
->texture
, NULL
);
838 _mesa_free(st
->bitmap
.cache
);
839 st
->bitmap
.cache
= NULL
;