Path: blob/21.2-virgl/src/compiler/glsl/gl_nir_link_atomics.c
4545 views
/*1* Copyright © 2018 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "nir.h"24#include "linker_util.h"25#include "gl_nir_linker.h"26#include "compiler/glsl/ir_uniform.h" /* for gl_uniform_storage */27#include "main/context.h"2829/**30* This file do the common link for GLSL atomic counter uniforms, using NIR,31* instead of IR as the counter-part glsl/link_uniforms.cpp32*/3334struct active_atomic_counter_uniform {35unsigned loc;36nir_variable *var;37};3839struct active_atomic_buffer {40struct active_atomic_counter_uniform *uniforms;41unsigned num_uniforms;42unsigned uniform_buffer_size;43unsigned stage_counter_references[MESA_SHADER_STAGES];44unsigned size;45};4647static void48add_atomic_counter(const void *ctx,49struct active_atomic_buffer *buffer,50unsigned uniform_loc,51nir_variable *var)52{53if (buffer->num_uniforms >= buffer->uniform_buffer_size) {54if (buffer->uniform_buffer_size == 0)55buffer->uniform_buffer_size = 1;56else57buffer->uniform_buffer_size *= 2;58buffer->uniforms = reralloc(ctx,59buffer->uniforms,60struct active_atomic_counter_uniform,61buffer->uniform_buffer_size);62}6364struct active_atomic_counter_uniform *uniform =65buffer->uniforms + buffer->num_uniforms;66uniform->loc = uniform_loc;67uniform->var = var;68buffer->num_uniforms++;69}7071static void72process_atomic_variable(const struct glsl_type *t,73struct gl_shader_program *prog,74unsigned *uniform_loc,75nir_variable *var,76struct active_atomic_buffer *buffers,77unsigned *num_buffers,78int *offset,79unsigned shader_stage)80{81/* FIXME: Arrays of arrays get counted separately. For example:82* x1[3][3][2] = 9 uniforms, 18 atomic counters83* x2[3][2] = 3 uniforms, 6 atomic counters84* x3[2] = 1 uniform, 2 atomic counters85*86* However this code marks all the counters as active even when they87* might not be used.88*/89if (glsl_type_is_array(t) &&90glsl_type_is_array(glsl_get_array_element(t))) {91for (unsigned i = 0; i < glsl_get_length(t); i++) {92process_atomic_variable(glsl_get_array_element(t),93prog,94uniform_loc,95var,96buffers, num_buffers,97offset,98shader_stage);99}100} else {101struct active_atomic_buffer *buf = buffers + var->data.binding;102struct gl_uniform_storage *const storage =103&prog->data->UniformStorage[*uniform_loc];104105/* If this is the first time the buffer is used, increment106* the counter of buffers used.107*/108if (buf->size == 0)109(*num_buffers)++;110111add_atomic_counter(buffers, /* ctx */112buf,113*uniform_loc,114var);115116/* When checking for atomic counters we should count every member in117* an array as an atomic counter reference.118*/119if (glsl_type_is_array(t))120buf->stage_counter_references[shader_stage] += glsl_get_length(t);121else122buf->stage_counter_references[shader_stage]++;123buf->size = MAX2(buf->size, *offset + glsl_atomic_size(t));124125storage->offset = *offset;126*offset += glsl_atomic_size(t);127128(*uniform_loc)++;129}130}131132static struct active_atomic_buffer *133find_active_atomic_counters(struct gl_context *ctx,134struct gl_shader_program *prog,135unsigned *num_buffers)136{137struct active_atomic_buffer *buffers =138rzalloc_array(NULL, /* ctx */139struct active_atomic_buffer,140ctx->Const.MaxAtomicBufferBindings);141*num_buffers = 0;142143for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {144struct gl_linked_shader *sh = prog->_LinkedShaders[i];145if (sh == NULL)146continue;147148nir_shader *nir = sh->Program->nir;149150nir_foreach_uniform_variable(var, nir) {151if (!glsl_contains_atomic(var->type))152continue;153154int offset = var->data.offset;155unsigned uniform_loc = var->data.location;156157process_atomic_variable(var->type,158prog,159&uniform_loc,160var,161buffers,162num_buffers,163&offset,164i);165}166}167168return buffers;169}170171static bool172check_atomic_counters_overlap(const nir_variable *x, const nir_variable *y)173{174return ((x->data.offset >= y->data.offset &&175x->data.offset < y->data.offset + glsl_atomic_size(y->type)) ||176(y->data.offset >= x->data.offset &&177y->data.offset < x->data.offset + glsl_atomic_size(x->type)));178}179180static int181cmp_active_counter_offsets(const void *a, const void *b)182{183const struct active_atomic_counter_uniform *const first =184(struct active_atomic_counter_uniform *) a;185const struct active_atomic_counter_uniform *const second =186(struct active_atomic_counter_uniform *) b;187188return first->var->data.offset - second->var->data.offset;189}190191void192gl_nir_link_assign_atomic_counter_resources(struct gl_context *ctx,193struct gl_shader_program *prog)194{195unsigned num_buffers;196unsigned num_atomic_buffers[MESA_SHADER_STAGES] = {0};197struct active_atomic_buffer *abs =198find_active_atomic_counters(ctx, prog, &num_buffers);199200prog->data->AtomicBuffers =201rzalloc_array(prog->data, struct gl_active_atomic_buffer, num_buffers);202prog->data->NumAtomicBuffers = num_buffers;203204unsigned buffer_idx = 0;205for (unsigned binding = 0;206binding < ctx->Const.MaxAtomicBufferBindings;207binding++) {208209/* If the binding was not used, skip.210*/211if (abs[binding].size == 0)212continue;213214struct active_atomic_buffer *ab = abs + binding;215struct gl_active_atomic_buffer *mab =216prog->data->AtomicBuffers + buffer_idx;217218/* Assign buffer-specific fields. */219mab->Binding = binding;220mab->MinimumSize = ab->size;221mab->Uniforms = rzalloc_array(prog->data->AtomicBuffers, GLuint,222ab->num_uniforms);223mab->NumUniforms = ab->num_uniforms;224225/* Assign counter-specific fields. */226for (unsigned j = 0; j < ab->num_uniforms; j++) {227nir_variable *var = ab->uniforms[j].var;228struct gl_uniform_storage *storage =229&prog->data->UniformStorage[ab->uniforms[j].loc];230231mab->Uniforms[j] = ab->uniforms[j].loc;232233storage->atomic_buffer_index = buffer_idx;234storage->offset = var->data.offset;235if (glsl_type_is_array(var->type)) {236const struct glsl_type *without_array =237glsl_without_array(var->type);238storage->array_stride = glsl_atomic_size(without_array);239} else {240storage->array_stride = 0;241}242if (!glsl_type_is_matrix(var->type))243storage->matrix_stride = 0;244}245246/* Assign stage-specific fields. */247for (unsigned stage = 0; stage < MESA_SHADER_STAGES; ++stage) {248if (ab->stage_counter_references[stage]) {249mab->StageReferences[stage] = GL_TRUE;250num_atomic_buffers[stage]++;251} else {252mab->StageReferences[stage] = GL_FALSE;253}254}255256buffer_idx++;257}258259/* Store a list pointers to atomic buffers per stage and store the index260* to the intra-stage buffer list in uniform storage.261*/262for (unsigned stage = 0; stage < MESA_SHADER_STAGES; ++stage) {263if (prog->_LinkedShaders[stage] == NULL ||264num_atomic_buffers[stage] <= 0)265continue;266267struct gl_program *gl_prog = prog->_LinkedShaders[stage]->Program;268gl_prog->info.num_abos = num_atomic_buffers[stage];269gl_prog->sh.AtomicBuffers =270rzalloc_array(gl_prog,271struct gl_active_atomic_buffer *,272num_atomic_buffers[stage]);273274gl_prog->nir->info.num_abos = num_atomic_buffers[stage];275276unsigned intra_stage_idx = 0;277for (unsigned i = 0; i < num_buffers; i++) {278struct gl_active_atomic_buffer *atomic_buffer =279&prog->data->AtomicBuffers[i];280if (!atomic_buffer->StageReferences[stage])281continue;282283gl_prog->sh.AtomicBuffers[intra_stage_idx] = atomic_buffer;284285for (unsigned u = 0; u < atomic_buffer->NumUniforms; u++) {286GLuint uniform_loc = atomic_buffer->Uniforms[u];287struct gl_opaque_uniform_index *opaque =288prog->data->UniformStorage[uniform_loc].opaque + stage;289opaque->index = intra_stage_idx;290opaque->active = true;291}292293intra_stage_idx++;294}295}296297assert(buffer_idx == num_buffers);298299ralloc_free(abs);300}301302void303gl_nir_link_check_atomic_counter_resources(struct gl_context *ctx,304struct gl_shader_program *prog)305{306unsigned num_buffers;307struct active_atomic_buffer *abs =308find_active_atomic_counters(ctx, prog, &num_buffers);309unsigned atomic_counters[MESA_SHADER_STAGES] = {0};310unsigned atomic_buffers[MESA_SHADER_STAGES] = {0};311unsigned total_atomic_counters = 0;312unsigned total_atomic_buffers = 0;313314/* Sum the required resources. Note that this counts buffers and315* counters referenced by several shader stages multiple times316* against the combined limit -- That's the behavior the spec317* requires.318*/319for (unsigned i = 0; i < ctx->Const.MaxAtomicBufferBindings; i++) {320if (abs[i].size == 0)321continue;322323qsort(abs[i].uniforms, abs[i].num_uniforms,324sizeof(struct active_atomic_counter_uniform),325cmp_active_counter_offsets);326327for (unsigned j = 1; j < abs[i].num_uniforms; j++) {328/* If an overlapping counter found, it must be a reference to the329* same counter from a different shader stage.330*/331if (check_atomic_counters_overlap(abs[i].uniforms[j-1].var,332abs[i].uniforms[j].var)333&& strcmp(abs[i].uniforms[j-1].var->name,334abs[i].uniforms[j].var->name) != 0) {335linker_error(prog, "Atomic counter %s declared at offset %d "336"which is already in use.",337abs[i].uniforms[j].var->name,338abs[i].uniforms[j].var->data.offset);339}340}341342for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j) {343const unsigned n = abs[i].stage_counter_references[j];344345if (n) {346atomic_counters[j] += n;347total_atomic_counters += n;348atomic_buffers[j]++;349total_atomic_buffers++;350}351}352}353354/* Check that they are within the supported limits. */355for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {356if (atomic_counters[i] > ctx->Const.Program[i].MaxAtomicCounters)357linker_error(prog, "Too many %s shader atomic counters",358_mesa_shader_stage_to_string(i));359360if (atomic_buffers[i] > ctx->Const.Program[i].MaxAtomicBuffers)361linker_error(prog, "Too many %s shader atomic counter buffers",362_mesa_shader_stage_to_string(i));363}364365if (total_atomic_counters > ctx->Const.MaxCombinedAtomicCounters)366linker_error(prog, "Too many combined atomic counters");367368if (total_atomic_buffers > ctx->Const.MaxCombinedAtomicBuffers)369linker_error(prog, "Too many combined atomic buffers");370371ralloc_free(abs);372}373374375