[Mesa-dev] [PATCH 4/5] st/mesa: don't keep framebuffer state in st_context

Marek Olšák maraeo at gmail.com
Mon Jun 5 16:50:25 UTC 2017


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

---
 src/mesa/state_tracker/st_atom_framebuffer.c | 65 +++++++++++++++-------------
 src/mesa/state_tracker/st_atom_list.h        |  2 +-
 src/mesa/state_tracker/st_atom_msaa.c        |  5 +--
 src/mesa/state_tracker/st_cb_bitmap.c        | 12 ++---
 src/mesa/state_tracker/st_cb_clear.c         |  4 +-
 src/mesa/state_tracker/st_context.c          |  6 ---
 src/mesa/state_tracker/st_context.h          |  5 ++-
 7 files changed, 48 insertions(+), 51 deletions(-)

diff --git a/src/mesa/state_tracker/st_atom_framebuffer.c b/src/mesa/state_tracker/st_atom_framebuffer.c
index 9b15c88..acbe980 100644
--- a/src/mesa/state_tracker/st_atom_framebuffer.c
+++ b/src/mesa/state_tracker/st_atom_framebuffer.c
@@ -36,20 +36,21 @@
 #include "st_context.h"
 #include "st_atom.h"
 #include "st_cb_bitmap.h"
 #include "st_cb_fbo.h"
 #include "st_texture.h"
 #include "pipe/p_context.h"
 #include "cso_cache/cso_context.h"
 #include "util/u_math.h"
 #include "util/u_inlines.h"
 #include "util/u_format.h"
+#include "util/u_framebuffer.h"
 #include "main/framebuffer.h"
 
 
 /**
  * Update framebuffer size.
  *
  * We need to derive pipe_framebuffer size from the bound pipe_surfaces here
  * instead of copying gl_framebuffer size because for certain target types
  * (like PIPE_TEXTURE_1D_ARRAY) gl_framebuffer::Height has the number of layers
  * instead of 1.
@@ -100,115 +101,119 @@ framebuffer_quantize_num_samples(struct st_context *st, unsigned num_samples)
    }
    return quantized_samples;
 }
 
 /**
  * Update framebuffer state (color, depth, stencil, etc. buffers)
  */
 void
 st_update_framebuffer_state( struct st_context *st )
 {
-   struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
+   struct pipe_framebuffer_state framebuffer;
    struct gl_framebuffer *fb = st->ctx->DrawBuffer;
    struct st_renderbuffer *strb;
    GLuint i;
 
    st_flush_bitmap_cache(st);
    st_invalidate_readpix_cache(st);
 
    st->state.fb_orientation = st_fb_orientation(fb);
 
    /**
     * Quantize the derived default number of samples:
     *
     * A query to the driver of supported MSAA values the
     * hardware supports is done as to legalize the number
     * of application requested samples, NumSamples.
     * See commit eb9cf3c for more information.
     */
    fb->DefaultGeometry._NumSamples =
       framebuffer_quantize_num_samples(st, fb->DefaultGeometry.NumSamples);
 
-   framebuffer->width  = _mesa_geometric_width(fb);
-   framebuffer->height = _mesa_geometric_height(fb);
-   framebuffer->samples = _mesa_geometric_samples(fb);
-   framebuffer->layers = _mesa_geometric_layers(fb);
+   framebuffer.width  = _mesa_geometric_width(fb);
+   framebuffer.height = _mesa_geometric_height(fb);
+   framebuffer.samples = _mesa_geometric_samples(fb);
+   framebuffer.layers = _mesa_geometric_layers(fb);
 
    /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
     * to determine which surfaces to draw to
     */
-   framebuffer->nr_cbufs = fb->_NumColorDrawBuffers;
+   framebuffer.nr_cbufs = fb->_NumColorDrawBuffers;
 
    for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
-      pipe_surface_reference(&framebuffer->cbufs[i], NULL);
-
+      framebuffer.cbufs[i] = NULL;
       strb = st_renderbuffer(fb->_ColorDrawBuffers[i]);
 
       if (strb) {
          if (strb->is_rtt || (strb->texture &&
              _mesa_get_format_color_encoding(strb->Base.Format) == GL_SRGB)) {
             /* rendering to a GL texture, may have to update surface */
             st_update_renderbuffer_surface(st, strb);
          }
 
          if (strb->surface) {
-            pipe_surface_reference(&framebuffer->cbufs[i], strb->surface);
-            update_framebuffer_size(framebuffer, strb->surface);
+            framebuffer.cbufs[i] = strb->surface;
+            update_framebuffer_size(&framebuffer, strb->surface);
          }
          strb->defined = GL_TRUE; /* we'll be drawing something */
       }
    }
 
-   for (i = framebuffer->nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) {
-      pipe_surface_reference(&framebuffer->cbufs[i], NULL);
+   for (i = framebuffer.nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) {
+      framebuffer.cbufs[i] = NULL;
    }
 
    /* Remove trailing GL_NONE draw buffers. */
-   while (framebuffer->nr_cbufs &&
-          !framebuffer->cbufs[framebuffer->nr_cbufs-1]) {
-      framebuffer->nr_cbufs--;
+   while (framebuffer.nr_cbufs &&
+          !framebuffer.cbufs[framebuffer.nr_cbufs-1]) {
+      framebuffer.nr_cbufs--;
    }
 
    /*
     * Depth/Stencil renderbuffer/surface.
     */
    strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
    if (strb) {
       if (strb->is_rtt) {
          /* rendering to a GL texture, may have to update surface */
          st_update_renderbuffer_surface(st, strb);
       }
-      pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
-      update_framebuffer_size(framebuffer, strb->surface);
+      framebuffer.zsbuf = strb->surface;
+      update_framebuffer_size(&framebuffer, strb->surface);
    }
    else {
       strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
       if (strb) {
          if (strb->is_rtt) {
             /* rendering to a GL texture, may have to update surface */
             st_update_renderbuffer_surface(st, strb);
          }
-         pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
-         update_framebuffer_size(framebuffer, strb->surface);
+         framebuffer.zsbuf = strb->surface;
+         update_framebuffer_size(&framebuffer, strb->surface);
       }
       else
-         pipe_surface_reference(&framebuffer->zsbuf, NULL);
+         framebuffer.zsbuf = NULL;
    }
 
 #ifdef DEBUG
    /* Make sure the resource binding flags were set properly */
-   for (i = 0; i < framebuffer->nr_cbufs; i++) {
-      assert(!framebuffer->cbufs[i] ||
-             framebuffer->cbufs[i]->texture->bind & PIPE_BIND_RENDER_TARGET);
+   for (i = 0; i < framebuffer.nr_cbufs; i++) {
+      assert(!framebuffer.cbufs[i] ||
+             framebuffer.cbufs[i]->texture->bind & PIPE_BIND_RENDER_TARGET);
    }
-   if (framebuffer->zsbuf) {
-      assert(framebuffer->zsbuf->texture->bind & PIPE_BIND_DEPTH_STENCIL);
+   if (framebuffer.zsbuf) {
+      assert(framebuffer.zsbuf->texture->bind & PIPE_BIND_DEPTH_STENCIL);
    }
 #endif
 
-   if (framebuffer->width == USHRT_MAX)
-      framebuffer->width = 0;
-   if (framebuffer->height == USHRT_MAX)
-      framebuffer->height = 0;
+   if (framebuffer.width == USHRT_MAX)
+      framebuffer.width = 0;
+   if (framebuffer.height == USHRT_MAX)
+      framebuffer.height = 0;
+
+   cso_set_framebuffer(st->cso_context, &framebuffer);
 
-   cso_set_framebuffer(st->cso_context, framebuffer);
+   st->state.fb_width = framebuffer.width;
+   st->state.fb_height = framebuffer.height;
+   st->state.fb_num_samples = util_framebuffer_get_num_samples(&framebuffer);
+   st->state.fb_num_layers = util_framebuffer_get_num_layers(&framebuffer);
 }
diff --git a/src/mesa/state_tracker/st_atom_list.h b/src/mesa/state_tracker/st_atom_list.h
index ca997af1..614ee90 100644
--- a/src/mesa/state_tracker/st_atom_list.h
+++ b/src/mesa/state_tracker/st_atom_list.h
@@ -28,21 +28,21 @@ ST_STATE(ST_NEW_TES_SAMPLERS, st_update_tesseval_samplers) /* depends on update_
 ST_STATE(ST_NEW_GS_SAMPLERS, st_update_geometry_samplers) /* depends on update_*_texture for swizzle */
 ST_STATE(ST_NEW_FS_SAMPLERS, st_update_fragment_samplers) /* depends on update_*_texture for swizzle */
 
 ST_STATE(ST_NEW_VS_IMAGES, st_bind_vs_images)
 ST_STATE(ST_NEW_TCS_IMAGES, st_bind_tcs_images)
 ST_STATE(ST_NEW_TES_IMAGES, st_bind_tes_images)
 ST_STATE(ST_NEW_GS_IMAGES, st_bind_gs_images)
 ST_STATE(ST_NEW_FS_IMAGES, st_bind_fs_images)
 
 ST_STATE(ST_NEW_FB_STATE, st_update_framebuffer_state) /* depends on update_*_texture and bind_*_images */
-ST_STATE(ST_NEW_SAMPLE_MASK, st_update_sample_mask)
+ST_STATE(ST_NEW_SAMPLE_MASK, st_update_sample_mask) /* depends on update_framebuffer_state */
 ST_STATE(ST_NEW_SAMPLE_SHADING, st_update_sample_shading)
 
 ST_STATE(ST_NEW_VS_CONSTANTS, st_update_vs_constants)
 ST_STATE(ST_NEW_TCS_CONSTANTS, st_update_tcs_constants)
 ST_STATE(ST_NEW_TES_CONSTANTS, st_update_tes_constants)
 ST_STATE(ST_NEW_GS_CONSTANTS, st_update_gs_constants)
 ST_STATE(ST_NEW_FS_CONSTANTS, st_update_fs_constants)
 
 ST_STATE(ST_NEW_VS_UBOS, st_bind_vs_ubos)
 ST_STATE(ST_NEW_TCS_UBOS, st_bind_tcs_ubos)
diff --git a/src/mesa/state_tracker/st_atom_msaa.c b/src/mesa/state_tracker/st_atom_msaa.c
index c591fad..dd06517 100644
--- a/src/mesa/state_tracker/st_atom_msaa.c
+++ b/src/mesa/state_tracker/st_atom_msaa.c
@@ -26,31 +26,28 @@
  **************************************************************************/
 
 
 #include "st_context.h"
 #include "pipe/p_screen.h"
 #include "pipe/p_context.h"
 #include "st_atom.h"
 #include "st_program.h"
 
 #include "cso_cache/cso_context.h"
-#include "util/u_framebuffer.h"
 
 
 /* Update the sample mask for MSAA.
  */
 void st_update_sample_mask( struct st_context *st )
 {
    unsigned sample_mask = 0xffffffff;
-   struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
-   /* dependency here on bound surface (or rather, sample count) is worrying */
-   unsigned sample_count = util_framebuffer_get_num_samples(framebuffer);
+   unsigned sample_count = st->state.fb_num_samples;
 
    if (st->ctx->Multisample.Enabled && sample_count > 1) {
       /* unlike in gallium/d3d10 the mask is only active if msaa is enabled */
       if (st->ctx->Multisample.SampleCoverage) {
          unsigned nr_bits;
          nr_bits = (unsigned)
             (st->ctx->Multisample.SampleCoverageValue * (float)sample_count);
          /* there's lot of ways how to do this. We just use first few bits,
             since we have no knowledge of sample positions here. When
             app-supplied mask though is used too might need to be smarter.
diff --git a/src/mesa/state_tracker/st_cb_bitmap.c b/src/mesa/state_tracker/st_cb_bitmap.c
index ef3f64b..7ba6d82 100644
--- a/src/mesa/state_tracker/st_cb_bitmap.c
+++ b/src/mesa/state_tracker/st_cb_bitmap.c
@@ -242,22 +242,22 @@ setup_render_state(struct gl_context *ctx,
       struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
       uint num = MAX2(fpv->bitmap_sampler + 1,
                       st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
       memcpy(sampler_views, st->state.sampler_views[PIPE_SHADER_FRAGMENT],
              sizeof(sampler_views));
       sampler_views[fpv->bitmap_sampler] = sv;
       cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num, sampler_views);
    }
 
    /* viewport state: viewport matching window dims */
-   cso_set_viewport_dims(cso, st->state.framebuffer.width,
-                         st->state.framebuffer.height,
+   cso_set_viewport_dims(cso, st->state.fb_width,
+                         st->state.fb_height,
                          st->state.fb_orientation == Y_0_TOP);
 
    cso_set_vertex_elements(cso, 3, st->util_velems);
 
    cso_set_stream_outputs(st->cso_context, 0, NULL, NULL);
 }
 
 
 /**
  * Restore pipeline state after rendering the bitmap textured quad.
@@ -276,22 +276,22 @@ restore_render_state(struct gl_context *ctx)
  * Render a glBitmap by drawing a textured quad
  */
 static void
 draw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
                  GLsizei width, GLsizei height,
                  struct pipe_sampler_view *sv,
                  const GLfloat *color)
 {
    struct st_context *st = st_context(ctx);
    struct pipe_context *pipe = st->pipe;
-   const float fb_width = (float) st->state.framebuffer.width;
-   const float fb_height = (float) st->state.framebuffer.height;
+   const float fb_width = (float) st->state.fb_width;
+   const float fb_height = (float) st->state.fb_height;
    const float x0 = (float) x;
    const float x1 = (float) (x + width);
    const float y0 = (float) y;
    const float y1 = (float) (y + height);
    float sLeft = 0.0f, sRight = 1.0f;
    float tTop = 0.0f, tBot = 1.0f - tTop;
    const float clip_x0 = x0 / fb_width * 2.0f - 1.0f;
    const float clip_y0 = y0 / fb_height * 2.0f - 1.0f;
    const float clip_x1 = x1 / fb_width * 2.0f - 1.0f;
    const float clip_y1 = y1 / fb_height * 2.0f - 1.0f;
@@ -659,22 +659,22 @@ st_DrawAtlasBitmaps(struct gl_context *ctx,
                     const struct gl_bitmap_atlas *atlas,
                     GLuint count, const GLubyte *ids)
 {
    struct st_context *st = st_context(ctx);
    struct pipe_context *pipe = st->pipe;
    struct st_texture_object *stObj = st_texture_object(atlas->texObj);
    struct pipe_sampler_view *sv;
    /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
    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 float clip_x_scale = 2.0f / st->state.fb_width;
+   const float clip_y_scale = 2.0f / st->state.fb_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.vs) {
       init_bitmap_state(st);
    }
 
diff --git a/src/mesa/state_tracker/st_cb_clear.c b/src/mesa/state_tracker/st_cb_clear.c
index f507775..175e2e8 100644
--- a/src/mesa/state_tracker/st_cb_clear.c
+++ b/src/mesa/state_tracker/st_cb_clear.c
@@ -46,21 +46,20 @@
 #include "st_cb_fbo.h"
 #include "st_draw.h"
 #include "st_format.h"
 #include "st_program.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_shader_tokens.h"
 #include "pipe/p_state.h"
 #include "pipe/p_defines.h"
 #include "util/u_format.h"
-#include "util/u_framebuffer.h"
 #include "util/u_inlines.h"
 #include "util/u_simple_shaders.h"
 
 #include "cso_cache/cso_context.h"
 
 
 /**
  * Do per-context initialization for glClear.
  */
 void
@@ -177,22 +176,21 @@ clear_with_quad(struct gl_context *ctx, unsigned clear_buffers)
 {
    struct st_context *st = st_context(ctx);
    struct cso_context *cso = st->cso_context;
    const struct gl_framebuffer *fb = ctx->DrawBuffer;
    const GLfloat fb_width = (GLfloat) fb->Width;
    const GLfloat fb_height = (GLfloat) fb->Height;
    const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
    const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
    const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
    const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
-   unsigned num_layers =
-      util_framebuffer_get_num_layers(&st->state.framebuffer);
+   unsigned num_layers = st->state.fb_num_layers;
 
    /*
    printf("%s %s%s%s %f,%f %f,%f\n", __func__,
 	  color ? "color, " : "",
 	  depth ? "depth, " : "",
 	  stencil ? "stencil" : "",
 	  x0, y0,
 	  x1, y1);
    */
 
diff --git a/src/mesa/state_tracker/st_context.c b/src/mesa/state_tracker/st_context.c
index 9b803c6..a1328fb 100644
--- a/src/mesa/state_tracker/st_context.c
+++ b/src/mesa/state_tracker/st_context.c
@@ -573,39 +573,33 @@ destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
 {
    struct gl_texture_object *texObj = (struct gl_texture_object *) data;
    struct st_context *st = (struct st_context *) userData;
 
    st_texture_release_sampler_view(st, st_texture_object(texObj));
 }
  
 void st_destroy_context( struct st_context *st )
 {
    struct gl_context *ctx = st->ctx;
-   GLuint i;
 
    /* This must be called first so that glthread has a chance to finish */
    _mesa_glthread_destroy(ctx);
 
    _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
 
    st_reference_fragprog(st, &st->fp, NULL);
    st_reference_prog(st, &st->gp, NULL);
    st_reference_vertprog(st, &st->vp, NULL);
    st_reference_prog(st, &st->tcp, NULL);
    st_reference_prog(st, &st->tep, NULL);
    st_reference_compprog(st, &st->cp, NULL);
 
-   /* release framebuffer surfaces */
-   for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
-      pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
-   }
-   pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
    pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
    pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
 
    _vbo_DestroyContext(ctx);
 
    st_destroy_program_variants(st);
 
    _mesa_free_context_data(ctx);
 
    /* This will free the st_context too, so 'st' must not be accessed
diff --git a/src/mesa/state_tracker/st_context.h b/src/mesa/state_tracker/st_context.h
index 520cd8d..631c3ae 100644
--- a/src/mesa/state_tracker/st_context.h
+++ b/src/mesa/state_tracker/st_context.h
@@ -132,21 +132,24 @@ struct st_context
       struct pipe_rasterizer_state          rasterizer;
       struct pipe_sampler_state samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
       GLuint num_samplers[PIPE_SHADER_TYPES];
       struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
       GLuint num_sampler_views[PIPE_SHADER_TYPES];
       struct pipe_clip_state clip;
       struct {
          void *ptr;
          unsigned size;
       } constants[PIPE_SHADER_TYPES];
-      struct pipe_framebuffer_state framebuffer;
+      unsigned fb_width;
+      unsigned fb_height;
+      unsigned fb_num_samples;
+      unsigned fb_num_layers;
       struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
       struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
       struct {
          unsigned num;
          boolean include;
          struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
       } window_rects;
       unsigned sample_mask;
 
       GLuint poly_stipple[32];  /**< In OpenGL's bottom-to-top order */
-- 
2.7.4



More information about the mesa-dev mailing list