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 "program/program.h"
38 #include "program/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"
47 #include "pipe/p_context.h"
48 #include "pipe/p_defines.h"
49 #include "pipe/p_shader_tokens.h"
50 #include "util/u_inlines.h"
51 #include "util/u_draw_quad.h"
52 #include "util/u_simple_shaders.h"
53 #include "program/prog_instruction.h"
54 #include "cso_cache/cso_context.h"
60 * glBitmaps are drawn as textured quads. The user's bitmap pattern
61 * is stored in a texture image. An alpha8 texture format is used.
62 * The fragment shader samples a bit (texel) from the texture, then
63 * discards the fragment if the bit is off.
65 * Note that we actually store the inverse image of the bitmap to
66 * simplify the fragment program. An "on" bit gets stored as texel=0x0
67 * and an "off" bit is stored as texel=0xff. Then we kill the
68 * fragment if the negated texel value is less than zero.
73 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
74 * buffer which is then rendered en mass upon a flush, state change, etc.
75 * A wide, short buffer is used to target the common case of a series
76 * of glBitmap calls being used to draw text.
78 static GLboolean UseBitmapCache
= GL_TRUE
;
81 #define BITMAP_CACHE_WIDTH 512
82 #define BITMAP_CACHE_HEIGHT 32
86 /** Window pos to render the cached image */
88 /** Bounds of region used in window coords */
89 GLint xmin
, ymin
, xmax
, ymax
;
93 /** Bitmap's Z position */
96 struct pipe_resource
*texture
;
97 struct pipe_transfer
*trans
;
101 /** An I8 texture image: */
106 /** Epsilon for Z comparisons */
107 #define Z_EPSILON 1e-06
111 * Make fragment program for glBitmap:
112 * Sample the texture and kill the fragment if the bit is 0.
113 * This program will be combined with the user's fragment program.
115 static struct st_fragment_program
*
116 make_bitmap_fragment_program(struct gl_context
*ctx
, GLuint samplerIndex
)
118 struct st_context
*st
= st_context(ctx
);
119 struct st_fragment_program
*stfp
;
120 struct gl_program
*p
;
123 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
127 p
->NumInstructions
= 3;
129 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
130 if (!p
->Instructions
) {
131 ctx
->Driver
.DeleteProgram(ctx
, p
);
134 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
136 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
137 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
138 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
139 p
->Instructions
[ic
].DstReg
.Index
= 0;
140 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
141 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
142 p
->Instructions
[ic
].TexSrcUnit
= samplerIndex
;
143 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
146 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
147 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
148 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
150 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
151 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_XXXX
;
153 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
154 p
->Instructions
[ic
].SrcReg
[0].Negate
= NEGATE_XYZW
;
158 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
160 assert(ic
== p
->NumInstructions
);
162 p
->InputsRead
= FRAG_BIT_TEX0
;
163 p
->OutputsWritten
= 0x0;
164 p
->SamplersUsed
= (1 << samplerIndex
);
166 stfp
= (struct st_fragment_program
*) p
;
167 stfp
->Base
.UsesKill
= GL_TRUE
;
174 find_free_bit(uint bitfield
)
177 for (i
= 0; i
< 32; i
++) {
178 if ((bitfield
& (1 << i
)) == 0) {
187 * Combine basic bitmap fragment program with the user-defined program.
188 * \param st current context
189 * \param fpIn the incoming fragment program
190 * \param fpOut the new fragment program which does fragment culling
191 * \param bitmap_sampler sampler number for the bitmap texture
194 st_make_bitmap_fragment_program(struct st_context
*st
,
195 struct gl_fragment_program
*fpIn
,
196 struct gl_fragment_program
**fpOut
,
197 GLuint
*bitmap_sampler
)
199 struct st_fragment_program
*bitmap_prog
;
200 struct gl_program
*newProg
;
204 * Generate new program which is the user-defined program prefixed
205 * with the bitmap sampler/kill instructions.
207 sampler
= find_free_bit(fpIn
->Base
.SamplersUsed
);
208 bitmap_prog
= make_bitmap_fragment_program(st
->ctx
, sampler
);
210 newProg
= _mesa_combine_programs(st
->ctx
,
211 &bitmap_prog
->Base
.Base
,
213 /* done with this after combining */
214 st_reference_fragprog(st
, &bitmap_prog
, NULL
);
218 printf("Combined bitmap program:\n");
219 _mesa_print_program(newProg
);
220 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
221 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
222 _mesa_print_parameter_list(newProg
->Parameters
);
227 *fpOut
= (struct gl_fragment_program
*) newProg
;
228 *bitmap_sampler
= sampler
;
233 * Copy user-provide bitmap bits into texture buffer, expanding
235 * "On" bits will set texels to 0x0.
236 * "Off" bits will not modify texels.
237 * Note that the image is actually going to be upside down in
238 * the texture. We deal with that with texcoords.
241 unpack_bitmap(struct st_context
*st
,
242 GLint px
, GLint py
, GLsizei width
, GLsizei height
,
243 const struct gl_pixelstore_attrib
*unpack
,
244 const GLubyte
*bitmap
,
245 ubyte
*destBuffer
, uint destStride
)
247 destBuffer
+= py
* destStride
+ px
;
249 _mesa_expand_bitmap(width
, height
, unpack
, bitmap
,
250 destBuffer
, destStride
, 0x0);
255 * Create a texture which represents a bitmap image.
257 static struct pipe_resource
*
258 make_bitmap_texture(struct gl_context
*ctx
, GLsizei width
, GLsizei height
,
259 const struct gl_pixelstore_attrib
*unpack
,
260 const GLubyte
*bitmap
)
262 struct st_context
*st
= st_context(ctx
);
263 struct pipe_context
*pipe
= st
->pipe
;
264 struct pipe_transfer
*transfer
;
266 struct pipe_resource
*pt
;
269 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
275 * Create texture to hold bitmap pattern.
277 pt
= st_texture_create(st
, st
->internal_target
, st
->bitmap
.tex_format
,
279 PIPE_BIND_SAMPLER_VIEW
);
281 _mesa_unmap_pbo_source(ctx
, unpack
);
285 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
287 0, 0, width
, height
);
289 dest
= pipe_transfer_map(pipe
, transfer
);
291 /* Put image into texture transfer */
292 memset(dest
, 0xff, height
* transfer
->stride
);
293 unpack_bitmap(st
, 0, 0, width
, height
, unpack
, bitmap
,
294 dest
, transfer
->stride
);
296 _mesa_unmap_pbo_source(ctx
, unpack
);
298 /* Release transfer */
299 pipe_transfer_unmap(pipe
, transfer
);
300 pipe
->transfer_destroy(pipe
, transfer
);
306 setup_bitmap_vertex_data(struct st_context
*st
, bool normalized
,
307 int x
, int y
, int width
, int height
,
308 float z
, const float color
[4])
310 struct pipe_context
*pipe
= st
->pipe
;
311 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
312 const GLfloat fb_width
= (GLfloat
)fb
->Width
;
313 const GLfloat fb_height
= (GLfloat
)fb
->Height
;
314 const GLfloat x0
= (GLfloat
)x
;
315 const GLfloat x1
= (GLfloat
)(x
+ width
);
316 const GLfloat y0
= (GLfloat
)y
;
317 const GLfloat y1
= (GLfloat
)(y
+ height
);
318 GLfloat sLeft
= (GLfloat
)0.0, sRight
= (GLfloat
)1.0;
319 GLfloat tTop
= (GLfloat
)0.0, tBot
= (GLfloat
)1.0 - tTop
;
320 const GLfloat clip_x0
= (GLfloat
)(x0
/ fb_width
* 2.0 - 1.0);
321 const GLfloat clip_y0
= (GLfloat
)(y0
/ fb_height
* 2.0 - 1.0);
322 const GLfloat clip_x1
= (GLfloat
)(x1
/ fb_width
* 2.0 - 1.0);
323 const GLfloat clip_y1
= (GLfloat
)(y1
/ fb_height
* 2.0 - 1.0);
324 const GLuint max_slots
= 1; /* 4096 / sizeof(st->bitmap.vertices); */
333 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
334 * no_flush) updates to buffers where we know there is no conflict
335 * with previous data. Currently using max_slots > 1 will cause
336 * synchronous rendering if the driver flushes its command buffers
337 * between one bitmap and the next. Our flush hook below isn't
338 * sufficient to catch this as the driver doesn't tell us when it
339 * flushes its own command buffers. Until this gets fixed, pay the
340 * price of allocating a new buffer for each bitmap cache-flush to
341 * avoid synchronous rendering.
343 if (st
->bitmap
.vbuf_slot
>= max_slots
) {
344 pipe_resource_reference(&st
->bitmap
.vbuf
, NULL
);
345 st
->bitmap
.vbuf_slot
= 0;
348 if (!st
->bitmap
.vbuf
) {
349 st
->bitmap
.vbuf
= pipe_buffer_create(pipe
->screen
,
350 PIPE_BIND_VERTEX_BUFFER
,
352 sizeof(st
->bitmap
.vertices
));
355 /* Positions are in clip coords since we need to do clipping in case
356 * the bitmap quad goes beyond the window bounds.
358 st
->bitmap
.vertices
[0][0][0] = clip_x0
;
359 st
->bitmap
.vertices
[0][0][1] = clip_y0
;
360 st
->bitmap
.vertices
[0][2][0] = sLeft
;
361 st
->bitmap
.vertices
[0][2][1] = tTop
;
363 st
->bitmap
.vertices
[1][0][0] = clip_x1
;
364 st
->bitmap
.vertices
[1][0][1] = clip_y0
;
365 st
->bitmap
.vertices
[1][2][0] = sRight
;
366 st
->bitmap
.vertices
[1][2][1] = tTop
;
368 st
->bitmap
.vertices
[2][0][0] = clip_x1
;
369 st
->bitmap
.vertices
[2][0][1] = clip_y1
;
370 st
->bitmap
.vertices
[2][2][0] = sRight
;
371 st
->bitmap
.vertices
[2][2][1] = tBot
;
373 st
->bitmap
.vertices
[3][0][0] = clip_x0
;
374 st
->bitmap
.vertices
[3][0][1] = clip_y1
;
375 st
->bitmap
.vertices
[3][2][0] = sLeft
;
376 st
->bitmap
.vertices
[3][2][1] = tBot
;
378 /* same for all verts: */
379 for (i
= 0; i
< 4; i
++) {
380 st
->bitmap
.vertices
[i
][0][2] = z
;
381 st
->bitmap
.vertices
[i
][0][3] = 1.0;
382 st
->bitmap
.vertices
[i
][1][0] = color
[0];
383 st
->bitmap
.vertices
[i
][1][1] = color
[1];
384 st
->bitmap
.vertices
[i
][1][2] = color
[2];
385 st
->bitmap
.vertices
[i
][1][3] = color
[3];
386 st
->bitmap
.vertices
[i
][2][2] = 0.0; /*R*/
387 st
->bitmap
.vertices
[i
][2][3] = 1.0; /*Q*/
390 /* put vertex data into vbuf */
391 pipe_buffer_write_nooverlap(st
->pipe
,
394 * sizeof(st
->bitmap
.vertices
),
395 sizeof st
->bitmap
.vertices
,
396 st
->bitmap
.vertices
);
398 return st
->bitmap
.vbuf_slot
++ * sizeof st
->bitmap
.vertices
;
404 * Render a glBitmap by drawing a textured quad
407 draw_bitmap_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
408 GLsizei width
, GLsizei height
,
409 struct pipe_sampler_view
*sv
,
410 const GLfloat
*color
)
412 struct st_context
*st
= st_context(ctx
);
413 struct pipe_context
*pipe
= st
->pipe
;
414 struct cso_context
*cso
= st
->cso_context
;
415 struct st_fp_variant
*fpv
;
416 struct st_fp_variant_key key
;
420 memset(&key
, 0, sizeof(key
));
422 key
.bitmap
= GL_TRUE
;
424 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
426 /* As an optimization, Mesa's fragment programs will sometimes get the
427 * primary color from a statevar/constant rather than a varying variable.
428 * when that's the case, we need to ensure that we use the 'color'
429 * parameter and not the current attribute color (which may have changed
430 * through glRasterPos and state validation.
431 * So, we force the proper color here. Not elegant, but it works.
434 GLfloat colorSave
[4];
435 COPY_4V(colorSave
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
436 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], color
);
437 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
438 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], colorSave
);
443 /* XXX if the bitmap is larger than the max texture size, break
446 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
447 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
448 assert(width
<= (GLsizei
)maxSize
);
449 assert(height
<= (GLsizei
)maxSize
);
451 cso_save_rasterizer(cso
);
452 cso_save_samplers(cso
);
453 cso_save_fragment_sampler_views(cso
);
454 cso_save_viewport(cso
);
455 cso_save_fragment_shader(cso
);
456 cso_save_vertex_shader(cso
);
457 cso_save_vertex_elements(cso
);
459 /* rasterizer state: just scissor */
460 st
->bitmap
.rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
461 cso_set_rasterizer(cso
, &st
->bitmap
.rasterizer
);
463 /* fragment shader state: TEX lookup program */
464 cso_set_fragment_shader_handle(cso
, fpv
->driver_shader
);
466 /* vertex shader state: position + texcoord pass-through */
467 cso_set_vertex_shader_handle(cso
, st
->bitmap
.vs
);
469 /* user samplers, plus our bitmap sampler */
471 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
472 uint num
= MAX2(fpv
->bitmap_sampler
+ 1, st
->state
.num_samplers
);
474 for (i
= 0; i
< st
->state
.num_samplers
; i
++) {
475 samplers
[i
] = &st
->state
.samplers
[i
];
477 samplers
[fpv
->bitmap_sampler
] =
478 &st
->bitmap
.samplers
[sv
->texture
->target
!= PIPE_TEXTURE_RECT
];
479 cso_set_samplers(cso
, num
, (const struct pipe_sampler_state
**) samplers
);
482 /* user textures, plus the bitmap texture */
484 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
485 uint num
= MAX2(fpv
->bitmap_sampler
+ 1, st
->state
.num_textures
);
486 memcpy(sampler_views
, st
->state
.sampler_views
, sizeof(sampler_views
));
487 sampler_views
[fpv
->bitmap_sampler
] = sv
;
488 cso_set_fragment_sampler_views(cso
, num
, sampler_views
);
491 /* viewport state: viewport matching window dims */
493 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
494 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
495 const GLfloat width
= (GLfloat
)fb
->Width
;
496 const GLfloat height
= (GLfloat
)fb
->Height
;
497 struct pipe_viewport_state vp
;
498 vp
.scale
[0] = 0.5f
* width
;
499 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
502 vp
.translate
[0] = 0.5f
* width
;
503 vp
.translate
[1] = 0.5f
* height
;
504 vp
.translate
[2] = 0.5f
;
505 vp
.translate
[3] = 0.0f
;
506 cso_set_viewport(cso
, &vp
);
509 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
511 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
514 /* draw textured quad */
515 offset
= setup_bitmap_vertex_data(st
,
516 sv
->texture
->target
!= PIPE_TEXTURE_RECT
,
517 x
, y
, width
, height
, z
, color
);
519 util_draw_vertex_buffer(pipe
, st
->bitmap
.vbuf
, offset
,
520 PIPE_PRIM_TRIANGLE_FAN
,
522 3); /* attribs/vert */
526 cso_restore_rasterizer(cso
);
527 cso_restore_samplers(cso
);
528 cso_restore_fragment_sampler_views(cso
);
529 cso_restore_viewport(cso
);
530 cso_restore_fragment_shader(cso
);
531 cso_restore_vertex_shader(cso
);
532 cso_restore_vertex_elements(cso
);
537 reset_cache(struct st_context
*st
)
539 struct pipe_context
*pipe
= st
->pipe
;
540 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
542 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
543 cache
->empty
= GL_TRUE
;
545 cache
->xmin
= 1000000;
546 cache
->xmax
= -1000000;
547 cache
->ymin
= 1000000;
548 cache
->ymax
= -1000000;
551 pipe
->transfer_destroy(pipe
, cache
->trans
);
555 assert(!cache
->texture
);
557 /* allocate a new texture */
558 cache
->texture
= st_texture_create(st
, PIPE_TEXTURE_2D
,
559 st
->bitmap
.tex_format
, 0,
560 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
562 PIPE_BIND_SAMPLER_VIEW
);
566 /** Print bitmap image to stdout (debug) */
568 print_cache(const struct bitmap_cache
*cache
)
572 for (i
= 0; i
< BITMAP_CACHE_HEIGHT
; i
++) {
573 k
= BITMAP_CACHE_WIDTH
* (BITMAP_CACHE_HEIGHT
- i
- 1);
574 for (j
= 0; j
< BITMAP_CACHE_WIDTH
; j
++) {
575 if (cache
->buffer
[k
])
587 * Create gallium pipe_transfer object for the bitmap cache.
590 create_cache_trans(struct st_context
*st
)
592 struct pipe_context
*pipe
= st
->pipe
;
593 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
598 /* Map the texture transfer.
599 * Subsequent glBitmap calls will write into the texture image.
601 cache
->trans
= pipe_get_transfer(st
->pipe
, cache
->texture
, 0, 0,
602 PIPE_TRANSFER_WRITE
, 0, 0,
604 BITMAP_CACHE_HEIGHT
);
605 cache
->buffer
= pipe_transfer_map(pipe
, cache
->trans
);
607 /* init image to all 0xff */
608 memset(cache
->buffer
, 0xff, cache
->trans
->stride
* BITMAP_CACHE_HEIGHT
);
613 * If there's anything in the bitmap cache, draw/flush it now.
616 st_flush_bitmap_cache(struct st_context
*st
)
618 if (!st
->bitmap
.cache
->empty
) {
619 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
621 if (st
->ctx
->DrawBuffer
) {
622 struct pipe_context
*pipe
= st
->pipe
;
623 struct pipe_sampler_view
*sv
;
625 assert(cache
->xmin
<= cache
->xmax
);
627 /* printf("flush size %d x %d at %d, %d\n",
628 cache->xmax - cache->xmin,
629 cache->ymax - cache->ymin,
630 cache->xpos, cache->ypos);
633 /* The texture transfer has been mapped until now.
634 * So unmap and release the texture transfer before drawing.
639 pipe_transfer_unmap(pipe
, cache
->trans
);
640 cache
->buffer
= NULL
;
642 pipe
->transfer_destroy(pipe
, cache
->trans
);
646 sv
= st_create_texture_sampler_view(st
->pipe
, cache
->texture
);
648 draw_bitmap_quad(st
->ctx
,
652 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
656 pipe_sampler_view_reference(&sv
, NULL
);
660 /* release/free the texture */
661 pipe_resource_reference(&cache
->texture
, NULL
);
669 * Flush bitmap cache and release vertex buffer.
672 st_flush_bitmap( struct st_context
*st
)
674 st_flush_bitmap_cache(st
);
676 /* Release vertex buffer to avoid synchronous rendering if we were
677 * to map it in the next frame.
679 pipe_resource_reference(&st
->bitmap
.vbuf
, NULL
);
680 st
->bitmap
.vbuf_slot
= 0;
685 * Try to accumulate this glBitmap call in the bitmap cache.
686 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
689 accum_bitmap(struct st_context
*st
,
690 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
691 const struct gl_pixelstore_attrib
*unpack
,
692 const GLubyte
*bitmap
)
694 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
695 int px
= -999, py
= -999;
696 const GLfloat z
= st
->ctx
->Current
.RasterPos
[2];
698 if (width
> BITMAP_CACHE_WIDTH
||
699 height
> BITMAP_CACHE_HEIGHT
)
700 return GL_FALSE
; /* too big to cache */
703 px
= x
- cache
->xpos
; /* pos in buffer */
704 py
= y
- cache
->ypos
;
705 if (px
< 0 || px
+ width
> BITMAP_CACHE_WIDTH
||
706 py
< 0 || py
+ height
> BITMAP_CACHE_HEIGHT
||
707 !TEST_EQ_4V(st
->ctx
->Current
.RasterColor
, cache
->color
) ||
708 ((fabs(z
- cache
->zpos
) > Z_EPSILON
))) {
709 /* This bitmap would extend beyond cache bounds, or the bitmap
711 * so flush and continue.
713 st_flush_bitmap_cache(st
);
718 /* Initialize. Center bitmap vertically in the buffer. */
720 py
= (BITMAP_CACHE_HEIGHT
- height
) / 2;
722 cache
->ypos
= y
- py
;
724 cache
->empty
= GL_FALSE
;
725 COPY_4FV(cache
->color
, st
->ctx
->Current
.RasterColor
);
735 if (x
+ width
> cache
->xmax
)
736 cache
->xmax
= x
+ width
;
737 if (y
+ height
> cache
->ymax
)
738 cache
->ymax
= y
+ height
;
740 /* create the transfer if needed */
741 create_cache_trans(st
);
743 unpack_bitmap(st
, px
, py
, width
, height
, unpack
, bitmap
,
744 cache
->buffer
, BITMAP_CACHE_WIDTH
);
746 return GL_TRUE
; /* accumulated */
752 * Called via ctx->Driver.Bitmap()
755 st_Bitmap(struct gl_context
*ctx
, GLint x
, GLint y
,
756 GLsizei width
, GLsizei height
,
757 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
759 struct st_context
*st
= st_context(ctx
);
760 struct pipe_resource
*pt
;
762 if (width
== 0 || height
== 0)
765 st_validate_state(st
);
767 if (!st
->bitmap
.vs
) {
768 /* create pass-through vertex shader now */
769 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
771 TGSI_SEMANTIC_GENERIC
};
772 const uint semantic_indexes
[] = { 0, 0, 0 };
773 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
778 if (UseBitmapCache
&& accum_bitmap(st
, x
, y
, width
, height
, unpack
, bitmap
))
781 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
783 struct pipe_sampler_view
*sv
=
784 st_create_texture_sampler_view(st
->pipe
, pt
);
786 assert(pt
->target
== PIPE_TEXTURE_2D
|| pt
->target
== PIPE_TEXTURE_RECT
);
789 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
791 st
->ctx
->Current
.RasterColor
);
793 pipe_sampler_view_reference(&sv
, NULL
);
796 /* release/free the texture */
797 pipe_resource_reference(&pt
, NULL
);
802 /** Per-context init */
804 st_init_bitmap_functions(struct dd_function_table
*functions
)
806 functions
->Bitmap
= st_Bitmap
;
810 /** Per-context init */
812 st_init_bitmap(struct st_context
*st
)
814 struct pipe_sampler_state
*sampler
= &st
->bitmap
.samplers
[0];
815 struct pipe_context
*pipe
= st
->pipe
;
816 struct pipe_screen
*screen
= pipe
->screen
;
818 /* init sampler state once */
819 memset(sampler
, 0, sizeof(*sampler
));
820 sampler
->wrap_s
= PIPE_TEX_WRAP_CLAMP
;
821 sampler
->wrap_t
= PIPE_TEX_WRAP_CLAMP
;
822 sampler
->wrap_r
= PIPE_TEX_WRAP_CLAMP
;
823 sampler
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
824 sampler
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
825 sampler
->mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
826 st
->bitmap
.samplers
[1] = *sampler
;
827 st
->bitmap
.samplers
[1].normalized_coords
= 1;
829 /* init baseline rasterizer state once */
830 memset(&st
->bitmap
.rasterizer
, 0, sizeof(st
->bitmap
.rasterizer
));
831 st
->bitmap
.rasterizer
.gl_rasterization_rules
= 1;
833 /* find a usable texture format */
834 if (screen
->is_format_supported(screen
, PIPE_FORMAT_I8_UNORM
,
836 PIPE_BIND_SAMPLER_VIEW
, 0)) {
837 st
->bitmap
.tex_format
= PIPE_FORMAT_I8_UNORM
;
839 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_A8_UNORM
,
841 PIPE_BIND_SAMPLER_VIEW
, 0)) {
842 st
->bitmap
.tex_format
= PIPE_FORMAT_A8_UNORM
;
844 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_L8_UNORM
,
846 PIPE_BIND_SAMPLER_VIEW
, 0)) {
847 st
->bitmap
.tex_format
= PIPE_FORMAT_L8_UNORM
;
850 /* XXX support more formats */
854 /* alloc bitmap cache object */
855 st
->bitmap
.cache
= ST_CALLOC_STRUCT(bitmap_cache
);
861 /** Per-context tear-down */
863 st_destroy_bitmap(struct st_context
*st
)
865 struct pipe_context
*pipe
= st
->pipe
;
866 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
869 cso_delete_vertex_shader(st
->cso_context
, st
->bitmap
.vs
);
870 st
->bitmap
.vs
= NULL
;
873 if (st
->bitmap
.vbuf
) {
874 pipe_resource_reference(&st
->bitmap
.vbuf
, NULL
);
875 st
->bitmap
.vbuf
= NULL
;
880 pipe_transfer_unmap(pipe
, cache
->trans
);
881 pipe
->transfer_destroy(pipe
, cache
->trans
);
883 pipe_resource_reference(&st
->bitmap
.cache
->texture
, NULL
);
884 free(st
->bitmap
.cache
);
885 st
->bitmap
.cache
= NULL
;
889 #endif /* FEATURE_drawpix */