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 static inline uint32_t
nvk_cbuf_binding_for_stage(gl_shader_stage stage)34 nvk_cbuf_binding_for_stage(gl_shader_stage stage)
35 {
36 return stage;
37 }
38
39 enum ENUM_PACKED nvk_cbuf_type {
40 NVK_CBUF_TYPE_INVALID = 0,
41 NVK_CBUF_TYPE_ROOT_DESC,
42 NVK_CBUF_TYPE_SHADER_DATA,
43 NVK_CBUF_TYPE_DESC_SET,
44 NVK_CBUF_TYPE_DYNAMIC_UBO,
45 NVK_CBUF_TYPE_UBO_DESC,
46 };
47
48 PRAGMA_DIAGNOSTIC_PUSH
49 PRAGMA_DIAGNOSTIC_ERROR(-Wpadded)
50 struct nvk_cbuf {
51 enum nvk_cbuf_type type;
52 uint8_t desc_set;
53 uint8_t dynamic_idx;
54 uint8_t _pad;
55 uint32_t desc_offset;
56 };
57 PRAGMA_DIAGNOSTIC_POP
58 static_assert(sizeof(struct nvk_cbuf) == 8, "This struct has no holes");
59
60 struct nvk_cbuf_map {
61 uint32_t cbuf_count;
62 struct nvk_cbuf cbufs[16];
63 };
64
65 struct nvk_shader {
66 struct vk_shader vk;
67
68 struct nak_shader_info info;
69 struct nvk_cbuf_map cbuf_map;
70
71 /* Only relevant for fragment shaders */
72 float min_sample_shading;
73
74 struct nak_shader_bin *nak;
75 const void *code_ptr;
76 uint32_t code_size;
77
78 const void *data_ptr;
79 uint32_t data_size;
80
81 uint32_t upload_size;
82 uint64_t upload_addr;
83
84 /* Address of the shader header (or start of the shader code) for compute
85 * shaders.
86 *
87 * Prior to Volta, this is relative to the start of the shader heap. On
88 * Volta and later, it's an absolute address.
89 */
90 uint64_t hdr_addr;
91
92 /* Address of the start of the shader data section */
93 uint64_t data_addr;
94 };
95
96 extern const struct vk_device_shader_ops nvk_device_shader_ops;
97
98 VkShaderStageFlags nvk_nak_stages(const struct nv_device_info *info);
99
100 uint64_t
101 nvk_physical_device_compiler_flags(const struct nvk_physical_device *pdev);
102
103 nir_address_format
104 nvk_ubo_addr_format(const struct nvk_physical_device *pdev,
105 const struct vk_pipeline_robustness_state *rs);
106 nir_address_format
107 nvk_ssbo_addr_format(const struct nvk_physical_device *pdev,
108 const struct vk_pipeline_robustness_state *rs);
109
110 bool
111 nvk_nir_lower_descriptors(nir_shader *nir,
112 const struct nvk_physical_device *pdev,
113 const struct vk_pipeline_robustness_state *rs,
114 uint32_t set_layout_count,
115 struct vk_descriptor_set_layout * const *set_layouts,
116 struct nvk_cbuf_map *cbuf_map_out);
117 void
118 nvk_lower_nir(struct nvk_device *dev, nir_shader *nir,
119 const struct vk_pipeline_robustness_state *rs,
120 bool is_multiview,
121 uint32_t set_layout_count,
122 struct vk_descriptor_set_layout * const *set_layouts,
123 struct nvk_cbuf_map *cbuf_map_out);
124
125 VkResult
126 nvk_compile_nir_shader(struct nvk_device *dev, nir_shader *nir,
127 const VkAllocationCallbacks *alloc,
128 struct nvk_shader **shader_out);
129
130 /* Codegen wrappers.
131 *
132 * TODO: Delete these once NAK supports everything.
133 */
134 uint64_t nvk_cg_get_prog_debug(void);
135 uint64_t nvk_cg_get_prog_optimize(void);
136
137 const nir_shader_compiler_options *
138 nvk_cg_nir_options(const struct nvk_physical_device *pdev,
139 gl_shader_stage stage);
140
141 void nvk_cg_preprocess_nir(nir_shader *nir);
142 void nvk_cg_optimize_nir(nir_shader *nir);
143
144 VkResult nvk_cg_compile_nir(struct nvk_physical_device *pdev, nir_shader *nir,
145 const struct nak_fs_key *fs_key,
146 struct nvk_shader *shader);
147
148 #endif
149