iris: always include an extra constbuf0 if using UBOs
[mesa.git] / src / gallium / drivers / iris / iris_program_cache.c
index 6f88062f269f35008417125f1a9ced36d6a4bacf..849f96906ca5da3ed9b945532980eb576106d66a 100644 (file)
@@ -4,22 +4,30 @@
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
- * on the rights to use, copy, modify, merge, publish, distribute, sub
- * license, and/or sell copies of the Software, and to permit persons to whom
- * the Software is furnished to do so, subject to the following conditions:
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
  *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
  *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
- * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  */
+
+/**
+ * @file iris_program_cache.c
+ *
+ * The in-memory program cache.  This is basically a hash table mapping
+ * API-specified shaders and a state key to a compiled variant.  It also
+ * takes care of uploading shader assembly into a BO for use on the GPU.
+ */
+
 #include <stdio.h>
 #include <errno.h>
 #include "pipe/p_defines.h"
@@ -42,24 +50,6 @@ struct keybox {
    uint8_t data[0];
 };
 
-static uint32_t
-key_size_for_cache(enum iris_program_cache_id cache_id)
-{
-   static const unsigned key_sizes[] = {
-      [IRIS_CACHE_VS]         = sizeof(struct brw_vs_prog_key),
-      [IRIS_CACHE_TCS]        = sizeof(struct brw_tcs_prog_key),
-      [IRIS_CACHE_TES]        = sizeof(struct brw_tes_prog_key),
-      [IRIS_CACHE_GS]         = sizeof(struct brw_gs_prog_key),
-      [IRIS_CACHE_FS]         = sizeof(struct brw_wm_prog_key),
-      [IRIS_CACHE_CS]         = sizeof(struct brw_cs_prog_key),
-   };
-
-   /* BLORP keys aren't all the same size. */
-   assert(cache_id != IRIS_CACHE_BLORP);
-
-   return key_sizes[cache_id];
-}
-
 static struct keybox *
 make_keybox(void *mem_ctx,
             enum iris_program_cache_id cache_id,
@@ -93,23 +83,6 @@ keybox_equals(const void *void_a, const void *void_b)
    return memcmp(a->data, b->data, a->size) == 0;
 }
 
-static uint64_t
-dirty_flag_for_cache(enum iris_program_cache_id cache_id)
-{
-   assert(cache_id <= MESA_SHADER_STAGES);
-
-   // XXX: ugly...
-   // XXX: move this flagging out to a higher level, allow comparison of
-   // XXX: new and old programs to decide what bits to twiddle
-   // XXX: CLIP: toggle if barycentric modes has any NONPERSPECTIVE or not
-   if (cache_id == IRIS_CACHE_FS)
-      return IRIS_DIRTY_WM | IRIS_DIRTY_FS | IRIS_DIRTY_CLIP;
-   if (cache_id == IRIS_CACHE_VS)
-      return IRIS_DIRTY_VS | IRIS_DIRTY_VF_SGVS;
-
-   return IRIS_DIRTY_VS << cache_id;
-}
-
 static unsigned
 get_program_string_id(enum iris_program_cache_id cache_id, const void *key)
 {
@@ -131,11 +104,11 @@ get_program_string_id(enum iris_program_cache_id cache_id, const void *key)
    }
 }
 
-static struct iris_compiled_shader *
+struct iris_compiled_shader *
 iris_find_cached_shader(struct iris_context *ice,
                         enum iris_program_cache_id cache_id,
-                        const void *key,
-                        uint32_t key_size)
+                        uint32_t key_size,
+                        const void *key)
 {
    struct keybox *keybox =
       make_keybox(ice->shaders.cache, cache_id, key, key_size);
@@ -147,31 +120,6 @@ iris_find_cached_shader(struct iris_context *ice,
    return entry ? entry->data : NULL;
 }
 
-/**
- * Looks for a program in the cache and binds it.
- *
- * If no program was found, returns false and leaves the binding alone.
- */
-bool
-iris_bind_cached_shader(struct iris_context *ice,
-                        enum iris_program_cache_id cache_id,
-                        const void *key)
-{
-   unsigned key_size = key_size_for_cache(cache_id);
-   struct iris_compiled_shader *shader =
-      iris_find_cached_shader(ice, cache_id, key, key_size);
-
-   if (!shader)
-      return false;
-
-   if (memcmp(shader, ice->shaders.prog[cache_id], sizeof(*shader)) != 0) {
-      ice->shaders.prog[cache_id] = shader;
-      ice->state.dirty |= dirty_flag_for_cache(cache_id);
-   }
-
-   return true;
-}
-
 const void *
 iris_find_previous_compile(const struct iris_context *ice,
                            enum iris_program_cache_id cache_id,
@@ -209,17 +157,18 @@ find_existing_assembly(struct hash_table *cache,
    return NULL;
 }
 
-static struct iris_compiled_shader *
+struct iris_compiled_shader *
 iris_upload_shader(struct iris_context *ice,
                    enum iris_program_cache_id cache_id,
                    uint32_t key_size,
                    const void *key,
                    const void *assembly,
                    struct brw_stage_prog_data *prog_data,
-                   uint32_t *streamout)
+                   uint32_t *streamout,
+                   enum brw_param_builtin *system_values,
+                   unsigned num_system_values,
+                   unsigned num_cbufs)
 {
-   struct iris_screen *screen = (void *) ice->ctx.screen;
-   struct gen_device_info *devinfo = &screen->devinfo;
    struct hash_table *cache = ice->shaders.cache;
    struct iris_compiled_shader *shader =
       rzalloc_size(cache, sizeof(struct iris_compiled_shader) +
@@ -247,14 +196,18 @@ iris_upload_shader(struct iris_context *ice,
 
    shader->prog_data = prog_data;
    shader->streamout = streamout;
+   shader->system_values = system_values;
+   shader->num_system_values = num_system_values;
+   shader->num_cbufs = num_cbufs;
 
    ralloc_steal(shader, shader->prog_data);
    ralloc_steal(shader->prog_data, prog_data->param);
    ralloc_steal(shader->prog_data, prog_data->pull_param);
    ralloc_steal(shader, shader->streamout);
+   ralloc_steal(shader, shader->system_values);
 
    /* Store the 3DSTATE shader packets and other derived state. */
-   ice->vtbl.store_derived_program_state(devinfo, cache_id, shader);
+   ice->vtbl.store_derived_program_state(ice, cache_id, shader);
 
    struct keybox *keybox = make_keybox(cache, cache_id, key, key_size);
    _mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
@@ -262,29 +215,6 @@ iris_upload_shader(struct iris_context *ice,
    return shader;
 }
 
-/**
- * Upload a new shader to the program cache, and bind it for use.
- *
- * \param prog_data must be ralloc'd and will be stolen.
- */
-void
-iris_upload_and_bind_shader(struct iris_context *ice,
-                            enum iris_program_cache_id cache_id,
-                            const void *key,
-                            const void *assembly,
-                            struct brw_stage_prog_data *prog_data,
-                            uint32_t *streamout)
-{
-   assert(cache_id != IRIS_CACHE_BLORP);
-
-   struct iris_compiled_shader *shader =
-      iris_upload_shader(ice, cache_id, key_size_for_cache(cache_id), key,
-                         assembly, prog_data, streamout);
-
-   ice->shaders.prog[cache_id] = shader;
-   ice->state.dirty |= dirty_flag_for_cache(cache_id);
-}
-
 bool
 iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
                          const void *key, uint32_t key_size,
@@ -294,7 +224,7 @@ iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
    struct iris_context *ice = blorp->driver_ctx;
    struct iris_batch *batch = blorp_batch->driver_batch;
    struct iris_compiled_shader *shader =
-      iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key, key_size);
+      iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key_size, key);
 
    if (!shader)
       return false;
@@ -326,7 +256,7 @@ iris_blorp_upload_shader(struct blorp_batch *blorp_batch,
 
    struct iris_compiled_shader *shader =
       iris_upload_shader(ice, IRIS_CACHE_BLORP, key_size, key, kernel,
-                         prog_data, NULL);
+                         prog_data, NULL, NULL, 0, 0);
 
    struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
    *kernel_out =