• 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 #include "nouveau_bo.h"
16 
17 #include "vk_shader.h"
18 
19 struct nak_shader_bin;
20 struct nvk_device;
21 struct nvk_physical_device;
22 struct nvk_pipeline_compilation_ctx;
23 struct vk_descriptor_set_layout;
24 struct vk_graphics_pipeline_state;
25 struct vk_pipeline_cache;
26 struct vk_pipeline_layout;
27 struct vk_pipeline_robustness_state;
28 struct vk_shader_module;
29 
30 #define GF100_SHADER_HEADER_SIZE (20 * 4)
31 #define TU102_SHADER_HEADER_SIZE (32 * 4)
32 #define NVC0_MAX_SHADER_HEADER_SIZE TU102_SHADER_HEADER_SIZE
33 
34 static inline uint32_t
nvk_cbuf_binding_for_stage(gl_shader_stage stage)35 nvk_cbuf_binding_for_stage(gl_shader_stage stage)
36 {
37    return stage;
38 }
39 
40 enum ENUM_PACKED nvk_cbuf_type {
41    NVK_CBUF_TYPE_INVALID = 0,
42    NVK_CBUF_TYPE_ROOT_DESC,
43    NVK_CBUF_TYPE_SHADER_DATA,
44    NVK_CBUF_TYPE_DESC_SET,
45    NVK_CBUF_TYPE_DYNAMIC_UBO,
46    NVK_CBUF_TYPE_UBO_DESC,
47 };
48 
49 struct nvk_cbuf {
50    enum nvk_cbuf_type type;
51    uint8_t desc_set;
52    uint8_t dynamic_idx;
53    uint32_t desc_offset;
54 };
55 
56 struct nvk_cbuf_map {
57    uint32_t cbuf_count;
58    struct nvk_cbuf cbufs[16];
59 };
60 
61 struct nvk_shader {
62    struct vk_shader vk;
63 
64    struct nak_shader_info info;
65    struct nvk_cbuf_map cbuf_map;
66 
67    /* Only relevant for fragment shaders */
68    float min_sample_shading;
69 
70    struct nak_shader_bin *nak;
71    const void *code_ptr;
72    uint32_t code_size;
73 
74    const void *data_ptr;
75    uint32_t data_size;
76 
77    uint32_t upload_size;
78    uint64_t upload_addr;
79 
80    /* Address of the shader header (or start of the shader code) for compute
81     * shaders.
82     *
83     * Prior to Volta, this is relative to the start of the shader heap. On
84     * Volta and later, it's an absolute address.
85     */
86    uint64_t hdr_addr;
87 
88    /* Address of the start of the shader data section */
89    uint64_t data_addr;
90 };
91 
92 extern const struct vk_device_shader_ops nvk_device_shader_ops;
93 
94 VkShaderStageFlags nvk_nak_stages(const struct nv_device_info *info);
95 
96 uint64_t
97 nvk_physical_device_compiler_flags(const struct nvk_physical_device *pdev);
98 
99 static inline nir_address_format
nvk_buffer_addr_format(VkPipelineRobustnessBufferBehaviorEXT robustness)100 nvk_buffer_addr_format(VkPipelineRobustnessBufferBehaviorEXT robustness)
101 {
102    switch (robustness) {
103    case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT:
104       return nir_address_format_64bit_global_32bit_offset;
105    case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_EXT:
106    case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_2_EXT:
107       return nir_address_format_64bit_bounded_global;
108    default:
109       unreachable("Invalid robust buffer access behavior");
110    }
111 }
112 
113 bool
114 nvk_nir_lower_descriptors(nir_shader *nir,
115                           const struct vk_pipeline_robustness_state *rs,
116                           uint32_t set_layout_count,
117                           struct vk_descriptor_set_layout * const *set_layouts,
118                           struct nvk_cbuf_map *cbuf_map_out);
119 void
120 nvk_lower_nir(struct nvk_device *dev, nir_shader *nir,
121               const struct vk_pipeline_robustness_state *rs,
122               bool is_multiview,
123               uint32_t set_layout_count,
124               struct vk_descriptor_set_layout * const *set_layouts,
125               struct nvk_cbuf_map *cbuf_map_out);
126 
127 VkResult
128 nvk_shader_upload(struct nvk_device *dev, struct nvk_shader *shader);
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