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 "framework.h"
28 #include "vulkan/vulkan.h"
29 
30 enum QoShaderDeclType {
31    QoShaderDeclType_ubo,
32    QoShaderDeclType_ssbo,
33    QoShaderDeclType_img_buf,
34    QoShaderDeclType_img,
35    QoShaderDeclType_tex_buf,
36    QoShaderDeclType_combined,
37    QoShaderDeclType_tex,
38    QoShaderDeclType_samp,
39    QoShaderDeclType_in,
40    QoShaderDeclType_out,
41 };
42 
43 struct QoShaderDecl {
44    const char *name;
45    const char *type;
46    QoShaderDeclType decl_type;
47    //TODO: array size?
48    unsigned location;
49    unsigned component;
50    unsigned binding;
51    unsigned set;
52 };
53 
54 struct QoShaderModuleCreateInfo {
55     void *pNext;
56     size_t spirvSize;
57     const void *pSpirv;
58     uint32_t declarationCount;
59     const QoShaderDecl *pDeclarations;
60     VkShaderStageFlagBits stage;
61 };
62 
63 extern ac_shader_config config;
64 extern radv_shader_info info;
65 extern std::unique_ptr<aco::Program> program;
66 extern aco::Builder bld;
67 extern aco::Temp inputs[16];
68 
69 namespace aco {
70 struct ra_test_policy;
71 }
72 
73 void create_program(enum chip_class chip_class, aco::Stage stage,
74                     unsigned wave_size=64, enum radeon_family family=CHIP_UNKNOWN);
75 bool setup_cs(const char *input_spec, enum chip_class chip_class,
76               enum radeon_family family=CHIP_UNKNOWN, const char* subvariant = "",
77               unsigned wave_size=64);
78 
79 void finish_program(aco::Program *program);
80 void finish_validator_test();
81 void finish_opt_test();
82 void finish_ra_test(aco::ra_test_policy, bool lower=false);
83 void finish_optimizer_postRA_test();
84 void finish_to_hw_instr_test();
85 void finish_insert_nops_test();
86 void finish_form_hard_clause_test();
87 void finish_assembler_test();
88 
89 void writeout(unsigned i, aco::Temp tmp=aco::Temp(0, aco::s1));
90 void writeout(unsigned i, aco::Builder::Result res);
91 void writeout(unsigned i, aco::Operand op);
92 void writeout(unsigned i, aco::Operand op0, aco::Operand op1);
93 
94 aco::Temp fneg(aco::Temp src);
95 aco::Temp fabs(aco::Temp src);
96 
97 /* vulkan helpers */
98 VkDevice get_vk_device(enum chip_class chip_class);
99 VkDevice get_vk_device(enum radeon_family family);
100 
101 void print_pipeline_ir(VkDevice device, VkPipeline pipeline, VkShaderStageFlagBits stages,
102                        const char *name, bool remove_encoding=false);
103 
104 VkShaderModule __qoCreateShaderModule(VkDevice dev, const QoShaderModuleCreateInfo *info);
105 
106 class PipelineBuilder {
107 public:
108    /* inputs */
109    VkDevice device;
110    VkFormat color_outputs[16];
111    VkFormat ds_output;
112    VkPrimitiveTopology topology;
113    VkSampleCountFlagBits samples;
114    bool sample_shading_enable;
115    float min_sample_shading;
116    uint32_t patch_size;
117    VkPipelineVertexInputStateCreateInfo vs_input;
118    VkVertexInputBindingDescription vs_bindings[16];
119    VkVertexInputAttributeDescription vs_attributes[16];
120    VkPushConstantRange push_constant_range;
121    uint64_t desc_layouts_used;
122    unsigned num_desc_bindings[64];
123    VkDescriptorSetLayoutBinding desc_bindings[64][64];
124    VkPipelineShaderStageCreateInfo stages[5];
125    VkShaderStageFlags owned_stages;
126 
127    /* outputs */
128    VkGraphicsPipelineCreateInfo gfx_pipeline_info;
129    VkComputePipelineCreateInfo cs_pipeline_info;
130    VkDescriptorSetLayout desc_layouts[64];
131    VkPipelineLayout pipeline_layout;
132    VkRenderPass render_pass;
133    VkPipeline pipeline;
134 
135    PipelineBuilder(VkDevice dev);
136    ~PipelineBuilder();
137 
138    PipelineBuilder(const PipelineBuilder&) = delete;
139    PipelineBuilder& operator = (const PipelineBuilder&) = delete;
140 
141    void add_desc_binding(VkShaderStageFlags stage_flags, uint32_t layout,
142                          uint32_t binding, VkDescriptorType type, uint32_t count=1);
143 
144    void add_vertex_binding(uint32_t binding, uint32_t stride, VkVertexInputRate rate=VK_VERTEX_INPUT_RATE_VERTEX);
145    void add_vertex_attribute(uint32_t location, uint32_t binding, VkFormat format, uint32_t offset);
146 
147    void add_resource_decls(QoShaderModuleCreateInfo *module);
148    void add_io_decls(QoShaderModuleCreateInfo *module);
149 
150    void add_stage(VkShaderStageFlagBits stage, VkShaderModule module, const char *name="main");
151    void add_stage(VkShaderStageFlagBits stage, QoShaderModuleCreateInfo module, const char *name="main");
152    void add_vsfs(VkShaderModule vs, VkShaderModule fs);
153    void add_vsfs(QoShaderModuleCreateInfo vs, QoShaderModuleCreateInfo fs);
154    void add_cs(VkShaderModule cs);
155    void add_cs(QoShaderModuleCreateInfo cs);
156 
157    bool is_compute();
158 
159    void create_pipeline();
160 
161    void print_ir(VkShaderStageFlagBits stages, const char *name, bool remove_encoding=false);
162 private:
163    void create_compute_pipeline();
164    void create_graphics_pipeline();
165 };
166 
167 #endif /* ACO_TEST_HELPERS_H */
168