st/nine: Cache constant buffer size
authorAxel Davy <davyaxel0@gmail.com>
Wed, 23 Jan 2019 21:48:17 +0000 (22:48 +0100)
committerAxel Davy <davyaxel0@gmail.com>
Tue, 30 Apr 2019 17:18:52 +0000 (19:18 +0200)
The shader constant buffer size with the
constant compaction code can vary depending
on the shader variant compiled (for example if
fog constants are required, etc).
Thus instead of using fixed size for the shader,
add in the variant cache the size required, pass it
to the context, and use this value.

Signed-off-by: Axel Davy <davyaxel0@gmail.com>
src/gallium/state_trackers/nine/nine_shader.h
src/gallium/state_trackers/nine/nine_state.c
src/gallium/state_trackers/nine/nine_state.h
src/gallium/state_trackers/nine/pixelshader9.c
src/gallium/state_trackers/nine/pixelshader9.h
src/gallium/state_trackers/nine/vertexshader9.c
src/gallium/state_trackers/nine/vertexshader9.h

index aa0c0ab1f7a0d7531ddb6f9966e75261a27eb2fd..65dc2efd62acf7c6d2da10cad9646e0d902ac194 100644 (file)
@@ -119,16 +119,21 @@ struct nine_shader_variant
     struct nine_shader_variant *next;
     void *cso;
     unsigned *const_ranges;
+    unsigned const_used_size;
     uint64_t key;
 };
 
 static inline void *
-nine_shader_variant_get(struct nine_shader_variant *list, unsigned **const_ranges, uint64_t key)
+nine_shader_variant_get(struct nine_shader_variant *list,
+                        unsigned **const_ranges,
+                        unsigned *const_used_size,
+                        uint64_t key)
 {
     while (list->key != key && list->next)
         list = list->next;
     if (list->key == key) {
         *const_ranges = list->const_ranges;
+        *const_used_size = list->const_used_size;
         return list->cso;
     }
     return NULL;
@@ -136,7 +141,9 @@ nine_shader_variant_get(struct nine_shader_variant *list, unsigned **const_range
 
 static inline boolean
 nine_shader_variant_add(struct nine_shader_variant *list,
-                          uint64_t key, void *cso, unsigned *const_ranges)
+                        uint64_t key, void *cso,
+                        unsigned *const_ranges,
+                        unsigned const_used_size)
 {
     while (list->next) {
         assert(list->key != key);
@@ -149,6 +156,7 @@ nine_shader_variant_add(struct nine_shader_variant *list,
     list->next->key = key;
     list->next->cso = cso;
     list->next->const_ranges = const_ranges;
+    list->next->const_used_size = const_used_size;
     return TRUE;
 }
 
index 6440cf46c93fc7d617731867c4058b9e126b6f5d..7de5fc9a4c6e0ecc2c34e5e8ef1054b6eb89f6e9 100644 (file)
@@ -437,7 +437,7 @@ prepare_vs_constants_userbuf(struct NineDevice9 *device)
     struct pipe_constant_buffer cb;
     cb.buffer = NULL;
     cb.buffer_offset = 0;
-    cb.buffer_size = context->vs->const_used_size;
+    cb.buffer_size = context->cso_shader.vs_const_used_size;
     cb.user_buffer = context->vs_const_f;
 
     if (context->swvp) {
@@ -518,7 +518,7 @@ prepare_ps_constants_userbuf(struct NineDevice9 *device)
     struct pipe_constant_buffer cb;
     cb.buffer = NULL;
     cb.buffer_offset = 0;
-    cb.buffer_size = context->ps->const_used_size;
+    cb.buffer_size = context->cso_shader.ps_const_used_size;
     cb.user_buffer = context->ps_const_f;
 
     if (context->changed.ps_const_i) {
@@ -535,7 +535,7 @@ prepare_ps_constants_userbuf(struct NineDevice9 *device)
 
     /* Upload special constants needed to implement PS1.x instructions like TEXBEM,TEXBEML and BEM */
     if (context->ps->bumpenvmat_needed) {
-        memcpy(context->ps_lconstf_temp, cb.user_buffer, cb.buffer_size);
+        memcpy(context->ps_lconstf_temp, cb.user_buffer, 8 * sizeof(float[4]));
         memcpy(&context->ps_lconstf_temp[4 * 8], &device->context.bumpmap_vars, sizeof(device->context.bumpmap_vars));
 
         cb.user_buffer = context->ps_lconstf_temp;
@@ -545,7 +545,7 @@ prepare_ps_constants_userbuf(struct NineDevice9 *device)
         context->rs[D3DRS_FOGENABLE]) {
         float *dst = &context->ps_lconstf_temp[4 * 32];
         if (cb.user_buffer != context->ps_lconstf_temp) {
-            memcpy(context->ps_lconstf_temp, cb.user_buffer, cb.buffer_size);
+            memcpy(context->ps_lconstf_temp, cb.user_buffer, 32 * sizeof(float[4]));
             cb.user_buffer = context->ps_lconstf_temp;
         }
 
@@ -556,7 +556,6 @@ prepare_ps_constants_userbuf(struct NineDevice9 *device)
         } else if (context->rs[D3DRS_FOGTABLEMODE] != D3DFOG_NONE) {
             dst[4] = asfloat(context->rs[D3DRS_FOGDENSITY]);
         }
-        cb.buffer_size = 4 * 4 * 34;
     }
 
     if (!cb.buffer_size)
@@ -603,7 +602,9 @@ prepare_vs(struct NineDevice9 *device, uint8_t shader_changed)
 
     /* likely because we dislike FF */
     if (likely(context->programmable_vs)) {
-        context->cso_shader.vs = NineVertexShader9_GetVariant(vs, &context->cso_shader.vs_const_ranges);
+        context->cso_shader.vs = NineVertexShader9_GetVariant(vs,
+                                                              &context->cso_shader.vs_const_ranges,
+                                                              &context->cso_shader.vs_const_used_size);
     } else {
         vs = device->ff.vs;
         context->cso_shader.vs = vs->ff_cso;
@@ -637,7 +638,9 @@ prepare_ps(struct NineDevice9 *device, uint8_t shader_changed)
         return 0;
 
     if (likely(ps)) {
-        context->cso_shader.ps = NinePixelShader9_GetVariant(ps, &context->cso_shader.ps_const_ranges);
+        context->cso_shader.ps = NinePixelShader9_GetVariant(ps,
+                                                             &context->cso_shader.ps_const_ranges,
+                                                             &context->cso_shader.ps_const_used_size);
     } else {
         ps = device->ff.ps;
         context->cso_shader.ps = ps->ff_cso;
index 37238b8593632ad535634392f8f50acc67e821fc..d8fa2f80c4d80420f573687fc3cbf0f4385a8afa 100644 (file)
@@ -240,8 +240,10 @@ struct nine_context {
     struct {
         void *vs;
         unsigned *vs_const_ranges;
+        unsigned vs_const_used_size;
         void *ps;
         unsigned *ps_const_ranges;
+        unsigned ps_const_used_size;
     } cso_shader;
 
     struct pipe_context *pipe;
index 42ad2c76a7f9b315104539fd9226fe0cb7f4f9b8..4b85c738f7977be9aba348207d878d46eefa4c9e 100644 (file)
@@ -80,13 +80,14 @@ NinePixelShader9_ctor( struct NinePixelShader9 *This,
 
     This->variant.cso = info.cso;
     This->variant.const_ranges = info.const_ranges;
+    This->variant.const_used_size = info.const_used_size;
     This->last_cso = info.cso;
     This->last_const_ranges = info.const_ranges;
+    This->last_const_used_size = info.const_used_size;
     This->last_key = 0;
 
     This->sampler_mask = info.sampler_mask;
     This->rt_mask = info.rt_mask;
-    This->const_used_size = info.const_used_size;
     This->bumpenvmat_needed = info.bumpenvmat_needed;
 
     memcpy(This->int_slots_used, info.int_slots_used, sizeof(This->int_slots_used));
@@ -159,7 +160,9 @@ NinePixelShader9_GetFunction( struct NinePixelShader9 *This,
 }
 
 void *
-NinePixelShader9_GetVariant( struct NinePixelShader9 *This, unsigned **const_ranges )
+NinePixelShader9_GetVariant( struct NinePixelShader9 *This,
+                             unsigned **const_ranges,
+                             unsigned *const_used_size )
 {
     /* GetVariant is called from nine_context, thus we can
      * get pipe directly */
@@ -170,10 +173,11 @@ NinePixelShader9_GetVariant( struct NinePixelShader9 *This, unsigned **const_ran
     key = This->next_key;
     if (key == This->last_key) {
         *const_ranges = This->last_const_ranges;
+        *const_used_size = This->last_const_used_size;
         return This->last_cso;
     }
 
-    cso = nine_shader_variant_get(&This->variant, const_ranges, key);
+    cso = nine_shader_variant_get(&This->variant, const_ranges, const_used_size, key);
     if (!cso) {
         struct NineDevice9 *device = This->base.device;
         struct nine_shader_info info;
@@ -210,14 +214,17 @@ NinePixelShader9_GetVariant( struct NinePixelShader9 *This, unsigned **const_ran
         hr = nine_translate_shader(This->base.device, &info, pipe);
         if (FAILED(hr))
             return NULL;
-        nine_shader_variant_add(&This->variant, key, info.cso, info.const_ranges);
+        nine_shader_variant_add(&This->variant, key, info.cso,
+                                info.const_ranges, info.const_used_size);
         cso = info.cso;
         *const_ranges = info.const_ranges;
+        *const_used_size = info.const_used_size;
     }
 
     This->last_key = key;
     This->last_cso = cso;
     This->last_const_ranges = *const_ranges;
+    This->last_const_used_size = *const_used_size;
 
     return cso;
 }
index 62b5abb596254398e4a5e0650ca58f9545d2bcfd..6bac90be7a96844e759eca35d72623f67697fc34 100644 (file)
@@ -43,8 +43,6 @@ struct NinePixelShader9
         uint8_t version; /* (major << 4) | minor */
     } byte_code;
 
-    unsigned const_used_size; /* in bytes */
-
     uint8_t bumpenvmat_needed;
     uint16_t sampler_mask;
     uint8_t rt_mask;
@@ -63,6 +61,7 @@ struct NinePixelShader9
     uint64_t last_key;
     void *last_cso;
     unsigned *last_const_ranges;
+    unsigned last_const_used_size; /* in bytes */
 
     uint64_t next_key;
 };
@@ -132,7 +131,9 @@ NinePixelShader9_UpdateKey( struct NinePixelShader9 *ps,
 }
 
 void *
-NinePixelShader9_GetVariant( struct NinePixelShader9 *ps, unsigned **const_ranges );
+NinePixelShader9_GetVariant( struct NinePixelShader9 *ps,
+                             unsigned **const_ranges,
+                             unsigned *const_used_size );
 
 /*** public ***/
 
index d14b9702a187d595519fc65e0c989b4c35579259..04c50ae619ff5e64708a5896c61fce444c3bea55 100644 (file)
@@ -95,11 +95,12 @@ NineVertexShader9_ctor( struct NineVertexShader9 *This,
 
     This->variant.cso = info.cso;
     This->variant.const_ranges = info.const_ranges;
+    This->variant.const_used_size = info.const_used_size;
     This->last_cso = info.cso;
     This->last_const_ranges = info.const_ranges;
+    This->last_const_used_size = info.const_used_size;
     This->last_key = (uint32_t) (info.swvp_on << 9);
 
-    This->const_used_size = info.const_used_size;
     This->lconstf = info.lconstf;
     This->sampler_mask = info.sampler_mask;
     This->position_t = info.position_t;
@@ -185,7 +186,9 @@ NineVertexShader9_GetFunction( struct NineVertexShader9 *This,
 }
 
 void *
-NineVertexShader9_GetVariant( struct NineVertexShader9 *This, unsigned **const_ranges )
+NineVertexShader9_GetVariant( struct NineVertexShader9 *This,
+                              unsigned **const_ranges,
+                              unsigned *const_used_size )
 {
     /* GetVariant is called from nine_context, thus we can
      * get pipe directly */
@@ -196,10 +199,11 @@ NineVertexShader9_GetVariant( struct NineVertexShader9 *This, unsigned **const_r
     key = This->next_key;
     if (key == This->last_key) {
         *const_ranges = This->last_const_ranges;
+        *const_used_size = This->last_const_used_size;
         return This->last_cso;
     }
 
-    cso = nine_shader_variant_get(&This->variant, const_ranges, key);
+    cso = nine_shader_variant_get(&This->variant, const_ranges, const_used_size, key);
     if (!cso) {
         struct NineDevice9 *device = This->base.device;
         struct nine_shader_info info;
@@ -223,14 +227,17 @@ NineVertexShader9_GetVariant( struct NineVertexShader9 *This, unsigned **const_r
         hr = nine_translate_shader(This->base.device, &info, pipe);
         if (FAILED(hr))
             return NULL;
-        nine_shader_variant_add(&This->variant, key, info.cso, info.const_ranges);
+        nine_shader_variant_add(&This->variant, key, info.cso,
+                                info.const_ranges, info.const_used_size);
         cso = info.cso;
         *const_ranges = info.const_ranges;
+        *const_used_size = info.const_used_size;
     }
 
     This->last_key = key;
     This->last_cso = cso;
     This->last_const_ranges = *const_ranges;
+    This->last_const_used_size = *const_used_size;
 
     return cso;
 }
index 0b139c5dad1408a78d4d39b53c9f43491cbfb68e..cbbd34979c904a5470be57eaf5138c258dacfaf6 100644 (file)
@@ -55,8 +55,6 @@ struct NineVertexShader9
     boolean point_size; /* if true, set rasterizer.point_size_per_vertex to 1 */
     boolean swvp_only;
 
-    unsigned const_used_size; /* in bytes */
-
     struct nine_lconstf lconstf;
 
     boolean int_slots_used[NINE_MAX_CONST_I];
@@ -73,6 +71,7 @@ struct NineVertexShader9
     uint64_t last_key;
     void *last_cso;
     unsigned *last_const_ranges;
+    unsigned last_const_used_size; /* in bytes */
 
     uint64_t next_key;
 
@@ -124,7 +123,9 @@ NineVertexShader9_UpdateKey( struct NineVertexShader9 *vs,
 }
 
 void *
-NineVertexShader9_GetVariant( struct NineVertexShader9 *vs, unsigned **const_ranges );
+NineVertexShader9_GetVariant( struct NineVertexShader9 *vs,
+                              unsigned **const_ranges,
+                              unsigned *const_used_size );
 
 void *
 NineVertexShader9_GetVariantProcessVertices( struct NineVertexShader9 *vs,