• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3  * SPDX-License-Identifier: MIT
4  */
5 #ifndef NVK_SHADER_H
6 #define NVK_SHADER_H 1
7 
8 #include "nvk_private.h"
9 #include "nvk_device_memory.h"
10 
11 #include "vk_pipeline_cache.h"
12 
13 #include "nak.h"
14 #include "nir.h"
15 
16 #include "vk_shader.h"
17 
18 struct nak_shader_bin;
19 struct nvk_device;
20 struct nvk_physical_device;
21 struct nvk_pipeline_compilation_ctx;
22 struct vk_descriptor_set_layout;
23 struct vk_graphics_pipeline_state;
24 struct vk_pipeline_cache;
25 struct vk_pipeline_layout;
26 struct vk_pipeline_robustness_state;
27 struct vk_shader_module;
28 
29 #define GF100_SHADER_HEADER_SIZE (20 * 4)
30 #define TU102_SHADER_HEADER_SIZE (32 * 4)
31 #define NVC0_MAX_SHADER_HEADER_SIZE TU102_SHADER_HEADER_SIZE
32 
33 #define NVK_SHADER_STAGE_VTGM_BITS \
34    (VK_SHADER_STAGE_VERTEX_BIT | \
35     VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | \
36     VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | \
37     VK_SHADER_STAGE_GEOMETRY_BIT)
38 
39 #define NVK_SHADER_STAGE_GRAPHICS_BITS \
40    (NVK_SHADER_STAGE_VTGM_BITS | VK_SHADER_STAGE_FRAGMENT_BIT)
41 
42 static inline gl_shader_stage
nvk_last_vtgm_shader_stage(VkShaderStageFlags stages)43 nvk_last_vtgm_shader_stage(VkShaderStageFlags stages)
44 {
45    stages &= ~VK_SHADER_STAGE_FRAGMENT_BIT;
46    stages = 1 << (util_last_bit(stages) - 1);
47    return vk_to_mesa_shader_stage(stages);
48 }
49 
50 static inline uint32_t
nvk_cbuf_binding_for_stage(gl_shader_stage stage)51 nvk_cbuf_binding_for_stage(gl_shader_stage stage)
52 {
53    return stage;
54 }
55 
56 enum ENUM_PACKED nvk_cbuf_type {
57    NVK_CBUF_TYPE_INVALID = 0,
58    NVK_CBUF_TYPE_ROOT_DESC,
59    NVK_CBUF_TYPE_SHADER_DATA,
60    NVK_CBUF_TYPE_DESC_SET,
61    NVK_CBUF_TYPE_DYNAMIC_UBO,
62    NVK_CBUF_TYPE_UBO_DESC,
63 };
64 
65 PRAGMA_DIAGNOSTIC_PUSH
66 PRAGMA_DIAGNOSTIC_ERROR(-Wpadded)
67 struct nvk_cbuf {
68    enum nvk_cbuf_type type;
69    uint8_t desc_set;
70    uint8_t dynamic_idx;
71    uint8_t _pad;
72    uint32_t desc_offset;
73 };
74 PRAGMA_DIAGNOSTIC_POP
75 static_assert(sizeof(struct nvk_cbuf) == 8, "This struct has no holes");
76 
77 struct nvk_cbuf_map {
78    uint32_t cbuf_count;
79    struct nvk_cbuf cbufs[16];
80 };
81 
82 uint16_t
83 nvk_max_shader_push_dw(struct nvk_physical_device *pdev,
84                        gl_shader_stage stage, bool last_vtgm);
85 
86 struct nvk_shader {
87    struct vk_shader vk;
88 
89    struct nak_shader_info info;
90    struct nvk_cbuf_map cbuf_map;
91 
92    /* Only relevant for fragment shaders */
93    bool sample_shading_enable;
94    float min_sample_shading;
95 
96    struct nak_shader_bin *nak;
97    const void *code_ptr;
98    uint32_t code_size;
99 
100    const void *data_ptr;
101    uint32_t data_size;
102 
103    uint32_t upload_size;
104    uint64_t upload_addr;
105 
106    /* Address of the shader header (or start of the shader code) for compute
107     * shaders.
108     *
109     * Prior to Volta, this is relative to the start of the shader heap. On
110     * Volta and later, it's an absolute address.
111     */
112    uint64_t hdr_addr;
113 
114    /* Address of the start of the shader data section */
115    uint64_t data_addr;
116 
117    uint16_t push_dw_count;
118    uint16_t vtgm_push_dw_count;
119    uint32_t *push_dw;
120 };
121 
122 VK_DEFINE_NONDISP_HANDLE_CASTS(nvk_shader, vk.base, VkShaderEXT,
123                                VK_OBJECT_TYPE_SHADER_EXT);
124 
125 extern const struct vk_device_shader_ops nvk_device_shader_ops;
126 
127 VkShaderStageFlags nvk_nak_stages(const struct nv_device_info *info);
128 
129 uint64_t
130 nvk_physical_device_compiler_flags(const struct nvk_physical_device *pdev);
131 
132 nir_address_format
133 nvk_ubo_addr_format(const struct nvk_physical_device *pdev,
134                     const struct vk_pipeline_robustness_state *rs);
135 nir_address_format
136 nvk_ssbo_addr_format(const struct nvk_physical_device *pdev,
137                      const struct vk_pipeline_robustness_state *rs);
138 
139 bool
140 nvk_nir_lower_descriptors(nir_shader *nir,
141                           const struct nvk_physical_device *pdev,
142                           VkShaderCreateFlagsEXT shader_flags,
143                           const struct vk_pipeline_robustness_state *rs,
144                           uint32_t set_layout_count,
145                           struct vk_descriptor_set_layout * const *set_layouts,
146                           struct nvk_cbuf_map *cbuf_map_out);
147 
148 VkResult
149 nvk_compile_nir_shader(struct nvk_device *dev, nir_shader *nir,
150                        const VkAllocationCallbacks *alloc,
151                        struct nvk_shader **shader_out);
152 
153 uint32_t mesa_to_nv9097_shader_type(gl_shader_stage stage);
154 uint32_t nvk_pipeline_bind_group(gl_shader_stage stage);
155 
156 /* Codegen wrappers.
157  *
158  * TODO: Delete these once NAK supports everything.
159  */
160 uint64_t nvk_cg_get_prog_debug(void);
161 uint64_t nvk_cg_get_prog_optimize(void);
162 
163 const nir_shader_compiler_options *
164 nvk_cg_nir_options(const struct nvk_physical_device *pdev,
165                    gl_shader_stage stage);
166 
167 void nvk_cg_preprocess_nir(nir_shader *nir);
168 void nvk_cg_optimize_nir(nir_shader *nir);
169 
170 VkResult nvk_cg_compile_nir(struct nvk_physical_device *pdev, nir_shader *nir,
171                             const struct nak_fs_key *fs_key,
172                             struct nvk_shader *shader);
173 
174 #endif
175