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