Path: blob/21.2-virgl/src/compiler/spirv/tests/helpers.h
4547 views
/*1* Copyright © 2020 Valve 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*/22#ifndef SPIRV_TEST_HELPERS_H23#define SPIRV_TEST_HELPERS_H2425#include <gtest/gtest.h>26#include "compiler/spirv/nir_spirv.h"27#include "compiler/nir/nir.h"2829class spirv_test : public ::testing::Test {30protected:31spirv_test()32: shader(NULL)33{34glsl_type_singleton_init_or_ref();35}3637~spirv_test()38{39ralloc_free(shader);40glsl_type_singleton_decref();41}4243void get_nir(size_t num_words, const uint32_t *words)44{45spirv_to_nir_options spirv_options;46memset(&spirv_options, 0, sizeof(spirv_options));47spirv_options.environment = NIR_SPIRV_VULKAN;48spirv_options.caps.vk_memory_model = true;49spirv_options.caps.vk_memory_model_device_scope = true;50spirv_options.ubo_addr_format = nir_address_format_32bit_index_offset;51spirv_options.ssbo_addr_format = nir_address_format_32bit_index_offset;52spirv_options.phys_ssbo_addr_format = nir_address_format_64bit_global;53spirv_options.push_const_addr_format = nir_address_format_32bit_offset;54spirv_options.shared_addr_format = nir_address_format_32bit_offset;5556nir_shader_compiler_options nir_options;57memset(&nir_options, 0, sizeof(nir_options));58nir_options.use_scoped_barrier = true;5960shader = spirv_to_nir(words, num_words, NULL, 0,61MESA_SHADER_COMPUTE, "main", &spirv_options, &nir_options);62}6364nir_intrinsic_instr *find_intrinsic(nir_intrinsic_op op, unsigned index=0)65{66nir_function_impl *impl = nir_shader_get_entrypoint(shader);67nir_foreach_block(block, impl) {68nir_foreach_instr(instr, block) {69if (instr->type != nir_instr_type_intrinsic ||70nir_instr_as_intrinsic(instr)->intrinsic != op)71continue;72if (index == 0)73return nir_instr_as_intrinsic(instr);74else75index--;76}77}7879return NULL;80}8182nir_shader *shader;83};8485#endif /* SPIRV_TEST_HELPERS_H */868788