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
30enum 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
43struct 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
54struct 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
63extern ac_shader_config config;
64extern aco_shader_info info;
65extern std::unique_ptr<aco::Program> program;
66extern aco::Builder bld;
67extern aco::Temp inputs[16];
68
69namespace aco {
70struct ra_test_policy;
71}
72
73void create_program(enum amd_gfx_level gfx_level, aco::Stage stage,
74                    unsigned wave_size=64, enum radeon_family family=CHIP_UNKNOWN);
75bool setup_cs(const char *input_spec, enum amd_gfx_level gfx_level,
76              enum radeon_family family=CHIP_UNKNOWN, const char* subvariant = "",
77              unsigned wave_size=64);
78
79void finish_program(aco::Program *program);
80void finish_validator_test();
81void finish_opt_test();
82void finish_ra_test(aco::ra_test_policy, bool lower=false);
83void finish_optimizer_postRA_test();
84void finish_to_hw_instr_test();
85void finish_insert_nops_test();
86void finish_form_hard_clause_test();
87void finish_assembler_test();
88
89void writeout(unsigned i, aco::Temp tmp=aco::Temp(0, aco::s1));
90void writeout(unsigned i, aco::Builder::Result res);
91void writeout(unsigned i, aco::Operand op);
92void writeout(unsigned i, aco::Operand op0, aco::Operand op1);
93
94aco::Temp fneg(aco::Temp src, aco::Builder b=bld);
95aco::Temp fabs(aco::Temp src, aco::Builder b=bld);
96aco::Temp f2f32(aco::Temp src, aco::Builder b=bld);
97aco::Temp f2f16(aco::Temp src, aco::Builder b=bld);
98aco::Temp u2u16(aco::Temp src, aco::Builder b=bld);
99aco::Temp fadd(aco::Temp src0, aco::Temp src1, aco::Builder b=bld);
100aco::Temp fmul(aco::Temp src0, aco::Temp src1, aco::Builder b=bld);
101aco::Temp fma(aco::Temp src0, aco::Temp src1, aco::Temp src2, aco::Builder b=bld);
102aco::Temp fsat(aco::Temp src, aco::Builder b=bld);
103aco::Temp ext_ushort(aco::Temp src, unsigned idx, aco::Builder b=bld);
104aco::Temp ext_ubyte(aco::Temp src, unsigned idx, aco::Builder b=bld);
105
106/* vulkan helpers */
107VkDevice get_vk_device(enum amd_gfx_level gfx_level);
108VkDevice get_vk_device(enum radeon_family family);
109
110void print_pipeline_ir(VkDevice device, VkPipeline pipeline, VkShaderStageFlagBits stages,
111                       const char *name, bool remove_encoding=false);
112
113VkShaderModule __qoCreateShaderModule(VkDevice dev, const QoShaderModuleCreateInfo *info);
114
115class PipelineBuilder {
116public:
117   /* inputs */
118   VkDevice device;
119   VkFormat color_outputs[16];
120   VkFormat ds_output;
121   VkPrimitiveTopology topology;
122   VkSampleCountFlagBits samples;
123   bool sample_shading_enable;
124   float min_sample_shading;
125   uint32_t patch_size;
126   VkPipelineVertexInputStateCreateInfo vs_input;
127   VkVertexInputBindingDescription vs_bindings[16];
128   VkVertexInputAttributeDescription vs_attributes[16];
129   VkPushConstantRange push_constant_range;
130   uint64_t desc_layouts_used;
131   unsigned num_desc_bindings[64];
132   VkDescriptorSetLayoutBinding desc_bindings[64][64];
133   VkPipelineShaderStageCreateInfo stages[5];
134   VkShaderStageFlags owned_stages;
135
136   /* outputs */
137   VkGraphicsPipelineCreateInfo gfx_pipeline_info;
138   VkComputePipelineCreateInfo cs_pipeline_info;
139   VkDescriptorSetLayout desc_layouts[64];
140   VkPipelineLayout pipeline_layout;
141   VkRenderPass render_pass;
142   VkPipeline pipeline;
143
144   PipelineBuilder(VkDevice dev);
145   ~PipelineBuilder();
146
147   PipelineBuilder(const PipelineBuilder&) = delete;
148   PipelineBuilder& operator = (const PipelineBuilder&) = delete;
149
150   void add_desc_binding(VkShaderStageFlags stage_flags, uint32_t layout,
151                         uint32_t binding, VkDescriptorType type, uint32_t count=1);
152
153   void add_vertex_binding(uint32_t binding, uint32_t stride, VkVertexInputRate rate=VK_VERTEX_INPUT_RATE_VERTEX);
154   void add_vertex_attribute(uint32_t location, uint32_t binding, VkFormat format, uint32_t offset);
155
156   void add_resource_decls(QoShaderModuleCreateInfo *module);
157   void add_io_decls(QoShaderModuleCreateInfo *module);
158
159   void add_stage(VkShaderStageFlagBits stage, VkShaderModule module, const char *name="main");
160   void add_stage(VkShaderStageFlagBits stage, QoShaderModuleCreateInfo module, const char *name="main");
161   void add_vsfs(VkShaderModule vs, VkShaderModule fs);
162   void add_vsfs(QoShaderModuleCreateInfo vs, QoShaderModuleCreateInfo fs);
163   void add_cs(VkShaderModule cs);
164   void add_cs(QoShaderModuleCreateInfo cs);
165
166   bool is_compute();
167
168   void create_pipeline();
169
170   void print_ir(VkShaderStageFlagBits stages, const char *name, bool remove_encoding=false);
171private:
172   void create_compute_pipeline();
173   void create_graphics_pipeline();
174};
175
176#endif /* ACO_TEST_HELPERS_H */
177