[Mesa-dev] [PATCH 5/7] st/mesa: put the bitmap_cache structure inside st_context

Marek Olšák maraeo at gmail.com
Fri Apr 14 15:07:12 UTC 2017


From: Marek Olšák <marek.olsak at amd.com>

This is nicer on caches, and the next commit will need to access
the structure from a different place.
---
 src/mesa/state_tracker/st_cb_bitmap.c | 54 +++++++++--------------------------
 src/mesa/state_tracker/st_context.c   |  2 ++
 src/mesa/state_tracker/st_context.h   | 23 +++++++++++++--
 3 files changed, 36 insertions(+), 43 deletions(-)

diff --git a/src/mesa/state_tracker/st_cb_bitmap.c b/src/mesa/state_tracker/st_cb_bitmap.c
index cf820e4..e0745f4 100644
--- a/src/mesa/state_tracker/st_cb_bitmap.c
+++ b/src/mesa/state_tracker/st_cb_bitmap.c
@@ -76,41 +76,20 @@
  * buffer which is then rendered en mass upon a flush, state change, etc.
  * A wide, short buffer is used to target the common case of a series
  * of glBitmap calls being used to draw text.
  */
 static GLboolean UseBitmapCache = GL_TRUE;
 
 
 #define BITMAP_CACHE_WIDTH  512
 #define BITMAP_CACHE_HEIGHT 32
 
-struct bitmap_cache
-{
-   /** Window pos to render the cached image */
-   GLint xpos, ypos;
-   /** Bounds of region used in window coords */
-   GLint xmin, ymin, xmax, ymax;
-
-   GLfloat color[4];
-
-   /** Bitmap's Z position */
-   GLfloat zpos;
-
-   struct pipe_resource *texture;
-   struct pipe_transfer *trans;
-
-   GLboolean empty;
-
-   /** An I8 texture image: */
-   ubyte *buffer;
-};
-
 
 /** Epsilon for Z comparisons */
 #define Z_EPSILON 1e-06
 
 
 /**
  * Copy user-provide bitmap bits into texture buffer, expanding
  * bits into texels.
  * "On" bits will set texels to 0x0.
  * "Off" bits will not modify texels.
@@ -347,21 +326,21 @@ draw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
    restore_render_state(ctx);
 
    /* We uploaded modified constants, need to invalidate them. */
    st->dirty |= ST_NEW_FS_CONSTANTS;
 }
 
 
 static void
 reset_cache(struct st_context *st)
 {
-   struct bitmap_cache *cache = st->bitmap.cache;
+   struct st_bitmap_cache *cache = &st->bitmap.cache;
 
    /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
    cache->empty = GL_TRUE;
 
    cache->xmin = 1000000;
    cache->xmax = -1000000;
    cache->ymin = 1000000;
    cache->ymax = -1000000;
 
    assert(!cache->texture);
@@ -370,21 +349,21 @@ reset_cache(struct st_context *st)
    cache->texture = st_texture_create(st, st->internal_target,
                                       st->bitmap.tex_format, 0,
                                       BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
                                       1, 1, 0,
 				      PIPE_BIND_SAMPLER_VIEW);
 }
 
 
 /** Print bitmap image to stdout (debug) */
 static void
-print_cache(const struct bitmap_cache *cache)
+print_cache(const struct st_bitmap_cache *cache)
 {
    int i, j, k;
 
    for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
       k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
       for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
          if (cache->buffer[k])
             printf("X");
          else
             printf(" ");
@@ -395,21 +374,21 @@ print_cache(const struct bitmap_cache *cache)
 }
 
 
 /**
  * Create gallium pipe_transfer object for the bitmap cache.
  */
 static void
 create_cache_trans(struct st_context *st)
 {
    struct pipe_context *pipe = st->pipe;
-   struct bitmap_cache *cache = st->bitmap.cache;
+   struct st_bitmap_cache *cache = &st->bitmap.cache;
 
    if (cache->trans)
       return;
 
    /* Map the texture transfer.
     * Subsequent glBitmap calls will write into the texture image.
     */
    cache->buffer = pipe_transfer_map(pipe, cache->texture, 0, 0,
                                      PIPE_TRANSFER_WRITE, 0, 0,
                                      BITMAP_CACHE_WIDTH,
@@ -419,23 +398,23 @@ create_cache_trans(struct st_context *st)
    memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
 }
 
 
 /**
  * If there's anything in the bitmap cache, draw/flush it now.
  */
 void
 st_flush_bitmap_cache(struct st_context *st)
 {
-   struct bitmap_cache *cache = st->bitmap.cache;
+   struct st_bitmap_cache *cache = &st->bitmap.cache;
 
-   if (cache && !cache->empty) {
+   if (!cache->empty) {
       struct pipe_context *pipe = st->pipe;
       struct pipe_sampler_view *sv;
 
       assert(cache->xmin <= cache->xmax);
 
       if (0)
          printf("flush bitmap, size %d x %d  at %d, %d\n",
                 cache->xmax - cache->xmin,
                 cache->ymax - cache->ymin,
                 cache->xpos, cache->ypos);
@@ -476,21 +455,21 @@ st_flush_bitmap_cache(struct st_context *st)
  * Try to accumulate this glBitmap call in the bitmap cache.
  * \return  GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
  */
 static GLboolean
 accum_bitmap(struct gl_context *ctx,
              GLint x, GLint y, GLsizei width, GLsizei height,
              const struct gl_pixelstore_attrib *unpack,
              const GLubyte *bitmap )
 {
    struct st_context *st = ctx->st;
-   struct bitmap_cache *cache = st->bitmap.cache;
+   struct st_bitmap_cache *cache = &st->bitmap.cache;
    int px = -999, py = -999;
    const GLfloat z = ctx->Current.RasterPos[2];
 
    if (width > BITMAP_CACHE_WIDTH ||
        height > BITMAP_CACHE_HEIGHT)
       return GL_FALSE; /* too big to cache */
 
    if (!cache->empty) {
       px = x - cache->xpos;  /* pos in buffer */
       py = y - cache->ypos;
@@ -550,28 +529,25 @@ accum_bitmap(struct gl_context *ctx,
 /**
  * One-time init for drawing bitmaps.
  */
 static void
 init_bitmap_state(struct st_context *st)
 {
    struct pipe_context *pipe = st->pipe;
    struct pipe_screen *screen = pipe->screen;
 
    /* This function should only be called once */
-   assert(st->bitmap.cache == NULL);
+   assert(st->bitmap.vs == NULL);
 
    assert(st->internal_target == PIPE_TEXTURE_2D ||
           st->internal_target == PIPE_TEXTURE_RECT);
 
-   /* alloc bitmap cache object */
-   st->bitmap.cache = ST_CALLOC_STRUCT(bitmap_cache);
-
    /* init sampler state once */
    memset(&st->bitmap.sampler, 0, sizeof(st->bitmap.sampler));
    st->bitmap.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
    st->bitmap.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
    st->bitmap.sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
    st->bitmap.sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
    st->bitmap.sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
    st->bitmap.sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
    st->bitmap.sampler.normalized_coords = st->internal_target == PIPE_TEXTURE_2D;
 
@@ -631,21 +607,21 @@ st_Bitmap(struct gl_context *ctx, GLint x, GLint y,
           const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
 {
    struct st_context *st = st_context(ctx);
    struct pipe_resource *pt;
 
    assert(width > 0);
    assert(height > 0);
 
    st_invalidate_readpix_cache(st);
 
-   if (!st->bitmap.cache) {
+   if (!st->bitmap.vs) {
       init_bitmap_state(st);
    }
 
    /* We only need to validate any non-ST_NEW_CONSTANTS state. The VS we use
     * for bitmap drawing uses no constants and the FS constants are
     * explicitly uploaded in the draw_bitmap_quad() function.
     */
    if ((st->dirty | ctx->NewDriverState) & ~ST_NEW_CONSTANTS &
        ST_PIPELINE_RENDER_STATE_MASK ||
        st->gfx_shaders_may_be_dirty) {
@@ -691,21 +667,21 @@ st_DrawAtlasBitmaps(struct gl_context *ctx,
    const float z = ctx->Current.RasterPos[2] * 2.0f - 1.0f;
    const float *color = ctx->Current.RasterColor;
    const float clip_x_scale = 2.0f / st->state.framebuffer.width;
    const float clip_y_scale = 2.0f / st->state.framebuffer.height;
    const unsigned num_verts = count * 4;
    const unsigned num_vert_bytes = num_verts * sizeof(struct st_util_vertex);
    struct st_util_vertex *verts;
    struct pipe_vertex_buffer vb = {0};
    unsigned i;
 
-   if (!st->bitmap.cache) {
+   if (!st->bitmap.vs) {
       init_bitmap_state(st);
    }
 
    st_flush_bitmap_cache(st);
 
    st_validate_state(st, ST_PIPELINE_RENDER);
    st_invalidate_readpix_cache(st);
 
    sv = st_create_texture_sampler_view(pipe, stObj->pt);
    if (!sv) {
@@ -822,26 +798,22 @@ st_init_bitmap_functions(struct dd_function_table *functions)
    functions->Bitmap = st_Bitmap;
    functions->DrawAtlasBitmaps = st_DrawAtlasBitmaps;
 }
 
 
 /** Per-context tear-down */
 void
 st_destroy_bitmap(struct st_context *st)
 {
    struct pipe_context *pipe = st->pipe;
-   struct bitmap_cache *cache = st->bitmap.cache;
+   struct st_bitmap_cache *cache = &st->bitmap.cache;
 
    if (st->bitmap.vs) {
       cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
       st->bitmap.vs = NULL;
    }
 
-   if (cache) {
-      if (cache->trans && cache->buffer) {
-         pipe_transfer_unmap(pipe, cache->trans);
-      }
-      pipe_resource_reference(&st->bitmap.cache->texture, NULL);
-      free(st->bitmap.cache);
-      st->bitmap.cache = NULL;
+   if (cache->trans && cache->buffer) {
+      pipe_transfer_unmap(pipe, cache->trans);
    }
+   pipe_resource_reference(&st->bitmap.cache.texture, NULL);
 }
diff --git a/src/mesa/state_tracker/st_context.c b/src/mesa/state_tracker/st_context.c
index fb98a34..8013226 100644
--- a/src/mesa/state_tracker/st_context.c
+++ b/src/mesa/state_tracker/st_context.c
@@ -477,20 +477,22 @@ st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe,
    st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
          st->has_shareable_shaders &&
          !st->clamp_frag_color_in_shader &&
          !st->force_persample_in_shader;
 
    st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
    st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] = st->has_shareable_shaders;
    st->shader_has_one_variant[MESA_SHADER_GEOMETRY] = st->has_shareable_shaders;
    st->shader_has_one_variant[MESA_SHADER_COMPUTE] = st->has_shareable_shaders;
 
+   st->bitmap.cache.empty = true;
+
    _mesa_compute_version(ctx);
 
    if (ctx->Version == 0) {
       /* This can happen when a core profile was requested, but the driver
        * does not support some features of GL 3.1 or later.
        */
       st_destroy_context_priv(st, false);
       return NULL;
    }
 
diff --git a/src/mesa/state_tracker/st_context.h b/src/mesa/state_tracker/st_context.h
index dfee927..74ab43c 100644
--- a/src/mesa/state_tracker/st_context.h
+++ b/src/mesa/state_tracker/st_context.h
@@ -33,39 +33,58 @@
 #include "main/fbobject.h"
 #include "state_tracker/st_atom.h"
 #include "util/u_inlines.h"
 
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 
-struct bitmap_cache;
 struct dd_function_table;
 struct draw_context;
 struct draw_stage;
 struct gen_mipmap_state;
 struct st_context;
 struct st_fragment_program;
 struct st_perf_monitor_group;
 struct u_upload_mgr;
 
 
 /** For drawing quads for glClear, glDraw/CopyPixels, glBitmap, etc. */
 struct st_util_vertex
 {
    float x, y, z;
    float r, g, b, a;
    float s, t;
 };
 
+struct st_bitmap_cache
+{
+   /** Window pos to render the cached image */
+   GLint xpos, ypos;
+   /** Bounds of region used in window coords */
+   GLint xmin, ymin, xmax, ymax;
+
+   GLfloat color[4];
+
+   /** Bitmap's Z position */
+   GLfloat zpos;
+
+   struct pipe_resource *texture;
+   struct pipe_transfer *trans;
+
+   GLboolean empty;
+
+   /** An I8 texture image: */
+   ubyte *buffer;
+};
 
 struct st_context
 {
    struct st_context_iface iface;
 
    struct gl_context *ctx;
 
    struct pipe_context *pipe;
 
    struct draw_context *draw;  /**< For selection/feedback/rastpos only */
@@ -173,21 +192,21 @@ struct st_context
       struct pipe_sampler_view *pixelmap_sampler_view;
    } pixel_xfer;
 
    /** for glBitmap */
    struct {
       struct pipe_rasterizer_state rasterizer;
       struct pipe_sampler_state sampler;
       struct pipe_sampler_state atlas_sampler;
       enum pipe_format tex_format;
       void *vs;
-      struct bitmap_cache *cache;
+      struct st_bitmap_cache cache;
    } bitmap;
 
    /** for glDraw/CopyPixels */
    struct {
       void *zs_shaders[4];
       void *vert_shaders[2];   /**< ureg shaders */
    } drawpix;
 
    struct {
       GLsizei width, height;
-- 
2.7.4



More information about the mesa-dev mailing list