* 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 <i915_drm.h>
#include "pipe/p_defines.h"
#include "pipe/p_state.h"
#include "pipe/p_context.h"
#include "pipe/p_screen.h"
#include "util/u_atomic.h"
+#include "util/u_upload_mgr.h"
#include "compiler/nir/nir.h"
#include "compiler/nir/nir_builder.h"
#include "intel/compiler/brw_compiler.h"
#include "intel/compiler/brw_eu.h"
#include "intel/compiler/brw_nir.h"
#include "iris_context.h"
+#include "iris_resource.h"
struct keybox {
uint8_t size;
uint8_t data[0];
};
-static struct keybox *
-make_keybox(struct iris_program_cache *cache,
- enum iris_program_cache_id cache_id,
- const void *key)
+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_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),
- //[IRIS_CACHE_BLORP_BLIT] = sizeof(struct brw_blorp_blit_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,
+ const void *key,
+ uint32_t key_size)
+{
struct keybox *keybox =
- ralloc_size(cache->table, sizeof(struct keybox) + key_sizes[cache_id]);
+ ralloc_size(mem_ctx, sizeof(struct keybox) + key_size);
keybox->cache_id = cache_id;
- keybox->size = key_sizes[cache_id];
- memcpy(keybox->data, key, key_sizes[cache_id]);
+ keybox->size = key_size;
+ memcpy(keybox->data, key, key_size);
return keybox;
}
dirty_flag_for_cache(enum iris_program_cache_id cache_id)
{
assert(cache_id <= MESA_SHADER_STAGES);
- return IRIS_DIRTY_VS << cache_id;
+
+ // 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 | IRIS_DIRTY_SBE;
+ if (cache_id == IRIS_CACHE_VS)
+ return IRIS_DIRTY_VS | IRIS_DIRTY_VF_SGVS;
+
+ /* For compute, prog_data->threads needs to be uploaded as constants. */
+ if (cache_id == IRIS_CACHE_CS)
+ return IRIS_DIRTY_CS | IRIS_DIRTY_CONSTANTS_CS;
+
+ return IRIS_DIRTY_VS << cache_id | IRIS_DIRTY_BINDINGS_VS << cache_id;
}
static unsigned
}
}
+static 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)
+{
+ struct keybox *keybox =
+ make_keybox(ice->shaders.cache, cache_id, key, key_size);
+ struct hash_entry *entry =
+ _mesa_hash_table_search(ice->shaders.cache, keybox);
+
+ ralloc_free(keybox);
+
+ return entry ? entry->data : NULL;
+}
+
/**
* Looks for a program in the cache and binds it.
*
enum iris_program_cache_id cache_id,
const void *key)
{
- struct iris_program_cache *cache = &ice->shaders.cache;
-
- struct keybox *keybox = make_keybox(cache, cache_id, key);
-
- struct hash_entry *entry = _mesa_hash_table_search(cache->table, keybox);
+ 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 (!entry)
+ if (!shader)
return false;
- struct iris_compiled_shader *shader = entry->data;
-
- if (cache_id <= MESA_SHADER_STAGES &&
+ // XXX: why memcmp?
+ if (!ice->shaders.prog[cache_id] ||
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;
}
-static void
-recreate_cache_bo(struct iris_context *ice, uint32_t size)
+void
+iris_unbind_shader(struct iris_context *ice,
+ enum iris_program_cache_id cache_id)
{
- struct iris_program_cache *cache = &ice->shaders.cache;
- struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
- struct iris_bo *old_bo = cache->bo;
- void *old_map = cache->map;
-
- cache->bo = iris_bo_alloc(screen->bufmgr, "program cache", size, 64);
- cache->bo->kflags = EXEC_OBJECT_CAPTURE | EXEC_OBJECT_PINNED;
- cache->map = iris_bo_map(&ice->dbg, cache->bo,
- MAP_READ | MAP_WRITE | MAP_ASYNC | MAP_PERSISTENT);
-
- /* Copy any existing data that needs to be saved. */
- if (old_bo) {
- perf_debug(&ice->dbg,
- "Copying to larger program cache: %u kB -> %u kB\n",
- (unsigned) old_bo->size / 1024,
- (unsigned) cache->bo->size / 1024);
-
- /* Put the new BO just past the old one */
- cache->bo->gtt_offset = ALIGN(old_bo->gtt_offset + old_bo->size, 4096);
-
- memcpy(cache->map, old_map, cache->next_offset);
-
- iris_bo_unreference(old_bo);
- iris_bo_unmap(old_bo);
- } else {
- /* Put the initial cache BO...somewhere. */
- cache->bo->gtt_offset = 4096 * 10;
+ if (ice->shaders.prog[cache_id]) {
+ ice->shaders.prog[cache_id] = NULL;
+ ice->state.dirty |= dirty_flag_for_cache(cache_id);
}
-
- ice->state.dirty |= IRIS_DIRTY_STATE_BASE_ADDRESS;
}
const void *
-iris_find_previous_compile(struct iris_program_cache *cache,
+iris_find_previous_compile(const struct iris_context *ice,
enum iris_program_cache_id cache_id,
unsigned program_string_id)
{
- hash_table_foreach(cache->table, entry) {
+ hash_table_foreach(ice->shaders.cache, entry) {
const struct keybox *keybox = entry->key;
if (keybox->cache_id == cache_id &&
get_program_string_id(cache_id, keybox->data) == program_string_id) {
* in our backend. This saves space in the program cache buffer.
*/
static const struct iris_compiled_shader *
-find_existing_assembly(const struct iris_program_cache *cache,
+find_existing_assembly(struct hash_table *cache,
const void *assembly,
unsigned assembly_size)
{
- hash_table_foreach(cache->table, entry) {
+ hash_table_foreach(cache, entry) {
const struct iris_compiled_shader *existing = entry->data;
if (existing->prog_data->program_size == assembly_size &&
- memcmp(cache->map + existing->prog_offset,
- assembly, assembly_size) == 0)
+ memcmp(existing->map, assembly, assembly_size) == 0)
return existing;
}
return NULL;
}
-static uint32_t
-upload_new_assembly(struct iris_context *ice,
- const void *assembly,
- unsigned size)
-{
- struct iris_program_cache *cache = &ice->shaders.cache;
-
- /* Allocate space in the cache BO for our new program. */
- if (cache->next_offset + size > cache->bo->size) {
- uint32_t new_size = cache->bo->size * 2;
-
- while (cache->next_offset + size > new_size)
- new_size *= 2;
-
- recreate_cache_bo(ice, new_size);
- }
-
- uint32_t offset = cache->next_offset;
-
- /* Programs are always 64-byte aligned, so set up the next one now */
- cache->next_offset = ALIGN(offset + size, 64);
-
- /* Copy data to the buffer */
- memcpy(cache->map + offset, assembly, size);
-
- return offset;
-}
-
-/**
- * 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)
+static 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)
{
struct iris_screen *screen = (void *) ice->ctx.screen;
struct gen_device_info *devinfo = &screen->devinfo;
- struct iris_program_cache *cache = &ice->shaders.cache;
+ struct hash_table *cache = ice->shaders.cache;
struct iris_compiled_shader *shader =
- ralloc_size(cache->table, sizeof(struct iris_compiled_shader) +
- iris_derived_program_state_size(cache_id));
+ rzalloc_size(cache, sizeof(struct iris_compiled_shader) +
+ ice->vtbl.derived_program_state_size(cache_id));
const struct iris_compiled_shader *existing =
find_existing_assembly(cache, assembly, prog_data->program_size);
* backend.
*/
if (existing) {
- shader->prog_offset = existing->prog_offset;
+ pipe_resource_reference(&shader->assembly.res, existing->assembly.res);
+ shader->assembly.offset = existing->assembly.offset;
+ shader->map = existing->map;
} else {
- shader->prog_offset =
- upload_new_assembly(ice, assembly, prog_data->program_size);
+ shader->assembly.res = NULL;
+ u_upload_alloc(ice->shaders.uploader, 0, prog_data->program_size, 64,
+ &shader->assembly.offset, &shader->assembly.res,
+ &shader->map);
+ memcpy(shader->map, assembly, prog_data->program_size);
}
shader->prog_data = prog_data;
+ shader->streamout = streamout;
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);
/* Store the 3DSTATE shader packets and other derived state. */
- iris_set_derived_program_state(devinfo, cache_id, shader);
+ ice->vtbl.store_derived_program_state(devinfo, cache_id, shader);
- struct keybox *keybox = make_keybox(cache, cache_id, key);
- _mesa_hash_table_insert(cache->table, keybox, shader);
+ struct keybox *keybox = make_keybox(cache, cache_id, key, key_size);
+ _mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
- if (cache_id <= MESA_SHADER_STAGES) {
- ice->shaders.prog[cache_id] = shader;
- ice->state.dirty |= dirty_flag_for_cache(cache_id);
- }
+ 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_init_program_cache(struct iris_context *ice)
+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)
{
- struct iris_program_cache *cache = &ice->shaders.cache;
+ assert(cache_id != IRIS_CACHE_BLORP);
- cache->table = _mesa_hash_table_create(ice, keybox_hash, keybox_equals);
+ struct iris_compiled_shader *shader =
+ iris_upload_shader(ice, cache_id, key_size_for_cache(cache_id), key,
+ assembly, prog_data, streamout);
- recreate_cache_bo(ice, 16384);
+ 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,
+ uint32_t *kernel_out, void *prog_data_out)
+{
+ struct blorp_context *blorp = blorp_batch->blorp;
+ 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);
+
+ if (!shader)
+ return false;
+
+ struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
+ *kernel_out =
+ iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
+ *((void **) prog_data_out) = shader->prog_data;
+
+ iris_use_pinned_bo(batch, bo, false);
+
+ return true;
+}
+
+bool
+iris_blorp_upload_shader(struct blorp_batch *blorp_batch,
+ const void *key, uint32_t key_size,
+ const void *kernel, UNUSED uint32_t kernel_size,
+ const struct brw_stage_prog_data *prog_data_templ,
+ UNUSED uint32_t prog_data_size,
+ uint32_t *kernel_out, void *prog_data_out)
+{
+ struct blorp_context *blorp = blorp_batch->blorp;
+ struct iris_context *ice = blorp->driver_ctx;
+ struct iris_batch *batch = blorp_batch->driver_batch;
+
+ void *prog_data = ralloc_size(NULL, prog_data_size);
+ memcpy(prog_data, prog_data_templ, prog_data_size);
+
+ struct iris_compiled_shader *shader =
+ iris_upload_shader(ice, IRIS_CACHE_BLORP, key_size, key, kernel,
+ prog_data, NULL);
+
+ struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
+ *kernel_out =
+ iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
+ *((void **) prog_data_out) = shader->prog_data;
+
+ iris_use_pinned_bo(batch, bo, false);
+
+ return true;
}
void
-iris_destroy_program_cache(struct iris_context *ice)
+iris_init_program_cache(struct iris_context *ice)
{
- struct iris_program_cache *cache = &ice->shaders.cache;
-
- /* This can be NULL if context creation failed early on. */
- if (cache->bo) {
- iris_bo_unmap(cache->bo);
- iris_bo_unreference(cache->bo);
- cache->bo = NULL;
- cache->map = NULL;
- }
+ ice->shaders.cache =
+ _mesa_hash_table_create(ice, keybox_hash, keybox_equals);
- cache->next_offset = 0;
+ ice->shaders.uploader =
+ u_upload_create(&ice->ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
+ IRIS_RESOURCE_FLAG_SHADER_MEMZONE);
+}
+void
+iris_destroy_program_cache(struct iris_context *ice)
+{
for (int i = 0; i < MESA_SHADER_STAGES; i++) {
ice->shaders.prog[i] = NULL;
}
- ralloc_free(cache->table);
+ hash_table_foreach(ice->shaders.cache, entry) {
+ struct iris_compiled_shader *shader = entry->data;
+ pipe_resource_reference(&shader->assembly.res, NULL);
+ }
+
+ u_upload_destroy(ice->shaders.uploader);
+
+ ralloc_free(ice->shaders.cache);
}
static const char *
cache_name(enum iris_program_cache_id cache_id)
{
- if (cache_id == IRIS_CACHE_BLORP_BLIT)
+ if (cache_id == IRIS_CACHE_BLORP)
return "BLORP";
return _mesa_shader_stage_to_string(cache_id);
void
iris_print_program_cache(struct iris_context *ice)
{
- struct iris_program_cache *cache = &ice->shaders.cache;
struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
const struct gen_device_info *devinfo = &screen->devinfo;
- hash_table_foreach(cache->table, entry) {
+ hash_table_foreach(ice->shaders.cache, entry) {
const struct keybox *keybox = entry->key;
struct iris_compiled_shader *shader = entry->data;
fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));
- brw_disassemble(devinfo, cache->map, shader->prog_offset,
+ brw_disassemble(devinfo, shader->map, 0,
shader->prog_data->program_size, stderr);
}
}