1 /* 2 * Copyright © 2020 Valve Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 */ 24 #ifndef ACO_TEST_HELPERS_H 25 #define ACO_TEST_HELPERS_H 26 27 #include "vulkan/vulkan.h" 28 29 #include "framework.h" 30 #include <functional> 31 32 enum QoShaderDeclType { 33 QoShaderDeclType_ubo, 34 QoShaderDeclType_ssbo, 35 QoShaderDeclType_img_buf, 36 QoShaderDeclType_img, 37 QoShaderDeclType_tex_buf, 38 QoShaderDeclType_combined, 39 QoShaderDeclType_tex, 40 QoShaderDeclType_samp, 41 QoShaderDeclType_in, 42 QoShaderDeclType_out, 43 }; 44 45 struct QoShaderDecl { 46 const char* name; 47 const char* type; 48 QoShaderDeclType decl_type; 49 // TODO: array size? 50 unsigned location; 51 unsigned component; 52 unsigned binding; 53 unsigned set; 54 }; 55 56 struct QoShaderModuleCreateInfo { 57 void* pNext; 58 size_t spirvSize; 59 const void* pSpirv; 60 uint32_t declarationCount; 61 const QoShaderDecl* pDeclarations; 62 VkShaderStageFlagBits stage; 63 }; 64 65 extern ac_shader_config config; 66 extern aco_shader_info info; 67 extern std::unique_ptr<aco::Program> program; 68 extern aco::Builder bld; 69 extern aco::Temp inputs[16]; 70 71 namespace aco { 72 struct ra_test_policy; 73 } 74 75 void create_program(enum amd_gfx_level gfx_level, aco::Stage stage, unsigned wave_size = 64, 76 enum radeon_family family = CHIP_UNKNOWN); 77 bool setup_cs(const char* input_spec, enum amd_gfx_level gfx_level, 78 enum radeon_family family = CHIP_UNKNOWN, const char* subvariant = "", 79 unsigned wave_size = 64); 80 81 void finish_program(aco::Program* program, bool endpgm = true); 82 void finish_validator_test(); 83 void finish_opt_test(); 84 void finish_setup_reduce_temp_test(); 85 void finish_ra_test(aco::ra_test_policy, bool lower = false); 86 void finish_optimizer_postRA_test(); 87 void finish_to_hw_instr_test(); 88 void finish_schedule_vopd_test(); 89 void finish_waitcnt_test(); 90 void finish_insert_nops_test(bool endpgm = true); 91 void finish_form_hard_clause_test(); 92 void finish_assembler_test(); 93 94 void writeout(unsigned i, aco::Temp tmp = aco::Temp(0, aco::s1)); 95 void writeout(unsigned i, aco::Builder::Result res); 96 void writeout(unsigned i, aco::Operand op); 97 void writeout(unsigned i, aco::Operand op0, aco::Operand op1); 98 99 aco::Temp fneg(aco::Temp src, aco::Builder b = bld); 100 aco::Temp fabs(aco::Temp src, aco::Builder b = bld); 101 aco::Temp f2f32(aco::Temp src, aco::Builder b = bld); 102 aco::Temp f2f16(aco::Temp src, aco::Builder b = bld); 103 aco::Temp u2u16(aco::Temp src, aco::Builder b = bld); 104 aco::Temp fadd(aco::Temp src0, aco::Temp src1, aco::Builder b = bld); 105 aco::Temp fmul(aco::Temp src0, aco::Temp src1, aco::Builder b = bld); 106 aco::Temp fma(aco::Temp src0, aco::Temp src1, aco::Temp src2, aco::Builder b = bld); 107 aco::Temp fsat(aco::Temp src, aco::Builder b = bld); 108 aco::Temp fmin(aco::Temp src0, aco::Temp src1, aco::Builder b = bld); 109 aco::Temp fmax(aco::Temp src0, aco::Temp src1, aco::Builder b = bld); 110 aco::Temp ext_ushort(aco::Temp src, unsigned idx, aco::Builder b = bld); 111 aco::Temp ext_ubyte(aco::Temp src, unsigned idx, aco::Builder b = bld); 112 void emit_divergent_if_else(aco::Program* prog, aco::Builder& b, aco::Operand cond, 113 std::function<void()> then, std::function<void()> els); 114 115 /* vulkan helpers */ 116 VkDevice get_vk_device(enum amd_gfx_level gfx_level); 117 VkDevice get_vk_device(enum radeon_family family); 118 119 void print_pipeline_ir(VkDevice device, VkPipeline pipeline, VkShaderStageFlagBits stages, 120 const char* name, bool remove_encoding = false); 121 122 VkShaderModule __qoCreateShaderModule(VkDevice dev, const QoShaderModuleCreateInfo* info); 123 124 class PipelineBuilder { 125 public: 126 /* inputs */ 127 VkDevice device; 128 VkFormat color_outputs[16]; 129 VkFormat ds_output; 130 VkPrimitiveTopology topology; 131 VkSampleCountFlagBits samples; 132 bool sample_shading_enable; 133 float min_sample_shading; 134 uint32_t patch_size; 135 VkPipelineVertexInputStateCreateInfo vs_input; 136 VkVertexInputBindingDescription vs_bindings[16]; 137 VkVertexInputAttributeDescription vs_attributes[16]; 138 VkPushConstantRange push_constant_range; 139 uint64_t desc_layouts_used; 140 unsigned num_desc_bindings[64]; 141 VkDescriptorSetLayoutBinding desc_bindings[64][64]; 142 VkPipelineShaderStageCreateInfo stages[5]; 143 VkShaderStageFlags owned_stages; 144 145 /* outputs */ 146 VkGraphicsPipelineCreateInfo gfx_pipeline_info; 147 VkComputePipelineCreateInfo cs_pipeline_info; 148 VkDescriptorSetLayout desc_layouts[64]; 149 VkPipelineLayout pipeline_layout; 150 VkRenderPass render_pass; 151 VkPipeline pipeline; 152 153 PipelineBuilder(VkDevice dev); 154 ~PipelineBuilder(); 155 156 PipelineBuilder(const PipelineBuilder&) = delete; 157 PipelineBuilder& operator=(const PipelineBuilder&) = delete; 158 159 void add_desc_binding(VkShaderStageFlags stage_flags, uint32_t layout, uint32_t binding, 160 VkDescriptorType type, uint32_t count = 1); 161 162 void add_vertex_binding(uint32_t binding, uint32_t stride, 163 VkVertexInputRate rate = VK_VERTEX_INPUT_RATE_VERTEX); 164 void add_vertex_attribute(uint32_t location, uint32_t binding, VkFormat format, uint32_t offset); 165 166 void add_resource_decls(QoShaderModuleCreateInfo* module); 167 void add_io_decls(QoShaderModuleCreateInfo* module); 168 169 void add_stage(VkShaderStageFlagBits stage, VkShaderModule module, const char* name = "main"); 170 void add_stage(VkShaderStageFlagBits stage, QoShaderModuleCreateInfo module, 171 const char* name = "main"); 172 void add_vsfs(VkShaderModule vs, VkShaderModule fs); 173 void add_vsfs(QoShaderModuleCreateInfo vs, QoShaderModuleCreateInfo fs); 174 void add_cs(VkShaderModule cs); 175 void add_cs(QoShaderModuleCreateInfo cs); 176 177 bool is_compute(); 178 179 void create_pipeline(); 180 181 void print_ir(VkShaderStageFlagBits stages, const char* name, bool remove_encoding = false); 182 183 private: 184 void create_compute_pipeline(); 185 void create_graphics_pipeline(); 186 }; 187 188 #endif /* ACO_TEST_HELPERS_H */ 189