39 case Interpolation::SMOOTH:
41 case Interpolation::FLAT:
43 case Interpolation::NO_PERSPECTIVE:
44 return "noperspective";
152 return "r11f_g11f_b10f";
168 return "lines_adjacency";
172 return "triangles_adjacency";
181 case PrimitiveOut::POINTS:
183 case PrimitiveOut::LINE_STRIP:
185 case PrimitiveOut::TRIANGLE_STRIP:
186 return "triangle_strip";
198 return "depth_greater";
202 return "depth_unchanged";
353 os <<
"layout(binding = " <<
uint32_t(location);
381 array_offset = res.
uniformbuf.name.find_first_of(
"[");
382 name_no_array = (array_offset == -1) ? res.
uniformbuf.name :
384 os <<
"uniform _" << name_no_array <<
" { " << res.
uniformbuf.type_name <<
" "
388 array_offset = res.
storagebuf.name.find_first_of(
"[");
389 name_no_array = (array_offset == -1) ? res.
storagebuf.name :
419 const std::string &prefix,
424 os <<
"layout(location=" << location <<
") " << prefix <<
" " <<
to_string(inout.
interp) <<
" "
431 const std::string &prefix,
436 std::string struct_name = prefix + iface.
name;
439 os <<
"struct " << struct_name <<
" {\n";
444 os <<
"layout(location=" << location <<
") " << prefix <<
" " <<
to_string(qualifier) <<
" "
445 << struct_name <<
" " << iface.
instance_name << suffix <<
";\n";
453 const std::string &prefix,
469 std::stringstream ss;
472 ss <<
"void main_function_();\n";
474 ss <<
"void main() {\n";
476 ss <<
" main_function_();\n";
480 ss <<
"#define main main_function_\n";
488 std::string
result(sources_combined);
501 vk_interface->
init(info);
502 interface = vk_interface;
505 use_batch_compilation_ = is_batch_compilation;
518 vk_descriptor_set_layout_ = VK_NULL_HANDLE;
522 shaderc_shader_kind stage,
526 shaderc_vertex_shader,
527 shaderc_geometry_shader,
528 shaderc_fragment_shader,
529 shaderc_compute_shader),
530 "Only forced ShaderC shader kinds are supported.");
535 if (!use_batch_compilation_) {
543 build_shader_module(sources, shaderc_vertex_shader,
vertex_module);
548 build_shader_module(sources, shaderc_geometry_shader,
geometry_module);
553 build_shader_module(sources, shaderc_fragment_shader,
fragment_module);
558 build_shader_module(sources, shaderc_compute_shader,
compute_module);
568 if (!use_batch_compilation_) {
576 if (info ==
nullptr) {
580 if (do_geometry_shader_injection(info)) {
581 std::string source = workaround_geometry_shader_source_create(*info);
583 sources.
append(
"version");
584 sources.
append(source.c_str());
590 if (!finalize_descriptor_set_layouts(device, vk_interface)) {
593 if (!finalize_pipeline_layout(device.
vk_handle(), vk_interface)) {
598 if (use_batch_compilation_) {
620 if (
result && is_compute_shader_) {
626bool VKShader::finalize_shader_module(
VKShaderModule &shader_module,
const char *stage_name)
630 shaderc_compilation_status_null_result_object,
631 shaderc_compilation_status_success);
643 std::string full_name = std::string(
name) +
"_" + stage_name;
644 shader_module.
finalize(full_name.c_str());
649 return compilation_succeeded;
657bool VKShader::finalize_pipeline_layout(VkDevice vk_device,
662 const uint32_t layout_count = vk_descriptor_set_layout_ == VK_NULL_HANDLE ? 0 : 1;
663 VkPipelineLayoutCreateInfo pipeline_info = {};
664 VkPushConstantRange push_constant_range = {};
665 pipeline_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
666 pipeline_info.flags = 0;
667 pipeline_info.setLayoutCount = layout_count;
668 pipeline_info.pSetLayouts = &vk_descriptor_set_layout_;
674 push_constant_range.offset = 0;
675 push_constant_range.size = push_constants_layout.
size_in_bytes();
676 push_constant_range.stageFlags = is_compute_shader_ ? VK_SHADER_STAGE_COMPUTE_BIT :
677 VK_SHADER_STAGE_ALL_GRAPHICS;
678 pipeline_info.pushConstantRangeCount = 1;
679 pipeline_info.pPushConstantRanges = &push_constant_range;
682 if (vkCreatePipelineLayout(
683 vk_device, &pipeline_info, vk_allocation_callbacks, &
vk_pipeline_layout) != VK_SUCCESS)
691bool VKShader::finalize_descriptor_set_layouts(
VKDevice &vk_device,
703 BLI_assert(vk_descriptor_set_layout_ == VK_NULL_HANDLE);
706 return vk_descriptor_set_layout_ != VK_NULL_HANDLE;
755 std::stringstream ss;
757 ss <<
"\n/* Specialization Constants (pass-through). */\n";
758 uint constant_id = 0;
760 ss <<
"layout (constant_id=" << constant_id++ <<
") const ";
763 ss <<
"int " << sc.
name <<
"=" << std::to_string(sc.
value.
i) <<
";\n";
766 ss <<
"uint " << sc.
name <<
"=" << std::to_string(sc.
value.
u) <<
"u;\n";
769 ss <<
"bool " << sc.
name <<
"=" << (sc.
value.
u ?
"true" :
"false") <<
";\n";
774 ss <<
"uint " << sc.
name <<
"_uint=" << std::to_string(sc.
value.
u) <<
"u;\n";
775 ss <<
"#define " << sc.
name <<
" uintBitsToFloat(" << sc.
name <<
"_uint)\n";
783 ss <<
"\n/* Pass Resources. */\n";
788 ss <<
"\n/* Batch Resources. */\n";
793 ss <<
"\n/* Geometry Resources. */\n";
803 ss <<
"\n/* Push Constants. */\n";
805 ss <<
"layout(push_constant, std430) uniform constants\n";
809 <<
", std140) uniform constants\n";
819 ss <<
"} PushConstants;\n";
821 ss <<
"#define " << uniform.
name <<
" (PushConstants.pc_" << uniform.
name <<
")\n";
831 std::stringstream ss;
832 std::string post_main;
835 ss <<
"\n/* Inputs. */\n";
837 ss <<
"layout(location = " << attr.
index <<
") ";
840 ss <<
"\n/* Interfaces. */\n";
846 ss <<
"layout(location=" << (location++) <<
") out int gpu_Layer;\n ";
851 ss <<
"layout(location=" << (location++) <<
") out int gpu_ViewportIndex;\n";
857 const bool has_geometry_stage = do_geometry_shader_injection(&info) ||
859 const bool retarget_depth = !has_geometry_stage;
860 if (retarget_depth) {
861 post_main +=
"gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n";
864 if (post_main.empty() ==
false) {
865 std::string pre_main;
920 std::stringstream ss;
921 std::string pre_main;
924 ss <<
"\n/* Interfaces. */\n";
933 ss <<
"#define gpu_Layer gl_Layer\n";
938 ss <<
"#define gpu_ViewportIndex gl_ViewportIndex\n";
944 ss <<
"layout(location=" << (location++) <<
") smooth in vec3 gpu_BaryCoord;\n";
945 ss <<
"layout(location=" << (location++) <<
") noperspective in vec3 gpu_BaryCoordNoPersp;\n";
949 ss <<
"layout(early_fragment_tests) in;\n";
953 if (use_gl_frag_depth) {
957 ss <<
"\n/* Sub-pass Inputs. */\n";
959 std::string image_name =
"gpu_subpass_img_";
960 image_name += std::to_string(input.
index);
988 Resource res(Resource::BindType::SAMPLER, input.
index);
989 res.sampler.type = image_type;
991 res.sampler.name = image_name;
994 char swizzle[] =
"xyzw";
997 std::string texel_co = (is_layered_fb) ?
"ivec3(gl_FragCoord.xy, gpu_Layer)" :
998 "ivec2(gl_FragCoord.xy)";
1000 std::stringstream ss_pre;
1002 ss_pre <<
" " << input.
name <<
" = texelFetch(" << image_name <<
", " << texel_co <<
", 0)."
1003 << swizzle <<
";\n";
1005 pre_main += ss_pre.str();
1008 ss <<
"\n/* Outputs. */\n";
1010 ss <<
"layout(location = " <<
output.index;
1013 ss <<
", index = 0";
1016 ss <<
", index = 1";
1026 if (pre_main.empty() ==
false) {
1027 std::string post_main;
1038 std::stringstream ss;
1039 ss <<
"\n/* Geometry Layout. */\n";
1041 if (invocations != -1) {
1042 ss <<
", invocations = " << invocations;
1047 <<
", max_vertices = " << max_verts <<
") out;\n";
1056 if (iface->instance_name == name) {
1065 ss <<
"void gpu_EmitVertex() {\n";
1066 ss <<
" gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n";
1067 ss <<
" EmitVertex();\n";
1073 std::stringstream ss;
1075 ss <<
"\n/* Interfaces. */\n";
1080 const char *suffix = (has_matching_output_iface) ?
"_in[]" :
"[]";
1089 const char *suffix = (has_matching_input_iface) ?
"_out" :
"";
1101 std::stringstream ss;
1102 ss <<
"\n/* Compute Layout. */\n";
1120std::string VKShader::workaround_geometry_shader_source_create(
1123 std::stringstream ss;
1150 int location_in = location;
1151 int location_out = location;
1152 if (do_layer_workaround) {
1153 ss <<
"layout(location=" << (location_in++) <<
") in int gpu_Layer[];\n";
1155 if (do_viewport_workaround) {
1156 ss <<
"layout(location=" << (location_in++) <<
") in int gpu_ViewportIndex[];\n";
1158 if (do_barycentric_workaround) {
1159 ss <<
"layout(location=" << (location_out++) <<
") smooth out vec3 gpu_BaryCoord;\n";
1160 ss <<
"layout(location=" << (location_out++)
1161 <<
") noperspective out vec3 gpu_BaryCoordNoPersp;\n";
1165 ss <<
"void main()\n";
1167 if (do_layer_workaround) {
1168 ss <<
" gl_Layer = gpu_Layer[0];\n";
1170 if (do_viewport_workaround) {
1171 ss <<
" gl_ViewportIndex = gpu_ViewportIndex[0];\n";
1173 for (
auto i : IndexRange(3)) {
1175 for (
auto &inout : iface->
inouts) {
1177 ss <<
" = " << iface->
instance_name <<
"_in[" << i <<
"]." << inout.name <<
";\n";
1180 if (do_barycentric_workaround) {
1181 ss <<
" gpu_BaryCoordNoPersp = gpu_BaryCoord =";
1182 ss <<
" vec3(" <<
int(i == 0) <<
", " <<
int(i == 1) <<
", " <<
int(i == 2) <<
");\n";
1184 ss <<
" gl_Position = gl_in[" << i <<
"].gl_Position;\n";
1185 ss <<
" gpu_EmitVertex();\n";
1217 if (
constants.values.is_empty() && vk_pipeline_base_ != VK_NULL_HANDLE) {
1218 return vk_pipeline_base_;
1229 compute_info, is_static_shader_, vk_pipeline_base_);
1230 if (vk_pipeline_base_ == VK_NULL_HANDLE) {
1231 vk_pipeline_base_ = vk_pipeline;
1244 "GPU_PRIM_POINTS is used with a shader that doesn't set point size before "
1245 "drawing fragments. Calling code should be adapted to use a shader that sets the "
1246 "gl_PointSize before entering the fragment stage. For example `GPU_SHADER_3D_POINT_*`.");
1278 graphics_info, is_static_shader_, vk_pipeline_base_);
1279 if (vk_pipeline_base_ == VK_NULL_HANDLE) {
1280 vk_pipeline_base_ = vk_pipeline;
1293 "Interface can be accessed after the VKShader has been initialized "
1294 "`VKShader::init`");
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
char * BLI_string_join_arrayN(const char *strings[], uint strings_num) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void extend(Span< T > array)
constexpr const T * data() const
constexpr int64_t size() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr bool is_empty() const
void append(const T &value)
void extend(Span< T > array)
ShaderInterface * interface
const char *const name_get() const
struct blender::gpu::Shader::Constants constants
void print_log(Span< const char * > sources, const char *log, const char *stage, bool error, GPULogParser *parser)
GPUStateMutable mutable_state
VkDescriptorSetLayout get_or_create(const VKDescriptorSetLayoutInfo &info, bool &r_created, bool &r_needed)
VKDescriptorSetLayouts & descriptor_set_layouts_get()
VkDevice vk_handle() const
VKDiscardPool & discard_pool_for_current_thread()
const char * glsl_patch_get() const
const VKWorkarounds & workarounds_get() const
void discard_pipeline_layout(VkPipelineLayout vk_pipeline_layout)
VkFormat stencil_attachment_format_get() const
VkFormat depth_attachment_format_get() const
Array< VkViewport, 16 > vk_viewports_get() const
Array< VkRect2D, 16 > vk_render_areas_get() const
Span< VkFormat > color_attachment_formats_get() const
VkPipeline get_or_create_compute_pipeline(VKComputeInfo &compute_info, bool is_static_shader, VkPipeline vk_pipeline_base)
VkPipeline get_or_create_graphics_pipeline(VKGraphicsInfo &graphics_info, bool is_static_shader, VkPipeline vk_pipeline_base)
static bool compile_module(VKShader &shader, shaderc_shader_kind stage, VKShaderModule &shader_module)
const VKPushConstants::Layout & push_constants_layout_get() const
const VKDescriptorSetLayoutInfo & descriptor_set_layout_info_get() const
const VKDescriptorSet::Location descriptor_set_location(const shader::ShaderCreateInfo::Resource &resource) const
void init(const shader::ShaderCreateInfo &info)
shaderc::SpvCompilationResult compilation_result
std::string combined_sources
void finalize(StringRefNull name)
Vector< uint32_t > spirv_binary
VKShaderModule compute_module
void init(const shader::ShaderCreateInfo &info, bool is_batch_compilation) override
std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const override
int program_handle_get() const override
bool compilation_finished
VKShaderModule fragment_module
void transform_feedback_names_set(Span< const char * > name_list, eGPUShaderTFBType geom_type) override
void geometry_shader_from_glsl(MutableSpan< const char * > sources) override
void compute_shader_from_glsl(MutableSpan< const char * > sources) override
std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const override
void warm_cache(int limit) override
VKPushConstants push_constants
std::string resources_declare(const shader::ShaderCreateInfo &info) const override
std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const override
VkPipelineLayout vk_pipeline_layout
VkPipeline ensure_and_get_compute_pipeline()
void transform_feedback_disable() override
void uniform_int(int location, int comp_len, int array_size, const int *data) override
VKShader(const char *name)
VKShaderModule vertex_module
void vertex_shader_from_glsl(MutableSpan< const char * > sources) override
VKShaderModule geometry_module
VkPipeline ensure_and_get_graphics_pipeline(GPUPrimType primitive, VKVertexAttributeObject &vao, VKStateManager &state_manager, VKFrameBuffer &framebuffer)
std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const override
void uniform_float(int location, int comp_len, int array_size, const float *data) override
const VKShaderInterface & interface_get() const
bool transform_feedback_enable(VertBuf *) override
void fragment_shader_from_glsl(MutableSpan< const char * > sources) override
bool finalize(const shader::ShaderCreateInfo *info=nullptr) override
std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const override
Vector< VkVertexInputAttributeDescription > attributes
Vector< VkVertexInputBindingDescription > bindings
additional_info("compositor_sum_float_shared") .push_constant(Type additional_info("compositor_sum_float_shared") .push_constant(Type GPU_RGBA32F
DOF_TILES_FLATTEN_GROUP_SIZE coc_tx GPU_R11F_G11F_B10F
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
out_radiance out_gbuf_normal out_gbuf_closure2 GPU_RG16
SHADOW_TILEMAP_RES tiles_buf[] statistics_buf render_view_buf[SHADOW_VIEW_MAX] GPU_R32UI
RAYTRACE_GROUP_SIZE additional_info("eevee_shared", "eevee_gbuffer_data", "eevee_global_ubo", "eevee_sampling_data", "eevee_utility_texture", "eevee_hiz_data", "draw_view") .specialization_constant(Type RAYTRACE_GROUP_SIZE in_sh_0_tx in_sh_2_tx screen_normal_tx GPU_RGBA8
#define SOURCES_INDEX_VERSION
void MEM_freeN(void *vmemh)
ccl_device_inline float2 interp(const float2 a, const float2 b, float t)
void object_label(GLenum type, GLuint object, const char *name)
BLI_INLINE int to_component_count(const Type &type)
static void print_image_type(std::ostream &os, const ImageType &type, const ShaderCreateInfo::Resource::BindType bind_type)
static std::string combine_sources(Span< const char * > sources)
const char * to_string(ShaderStage stage)
int get_location_count(const Type &type)
static StageInterfaceInfo * find_interface_by_name(const Span< StageInterfaceInfo * > ifaces, const StringRefNull name)
static void print_interface(std::ostream &os, const std::string &prefix, const StageInterfaceInfo &iface, int &location, const StringRefNull &suffix="")
static std::ostream & print_qualifier(std::ostream &os, const Qualifier &qualifiers)
static void print_resource(std::ostream &os, const ShaderCreateInfo::Resource &res)
static Type to_component_type(const Type &type)
static void print_interface_as_struct(std::ostream &os, const std::string &prefix, const StageInterfaceInfo &iface, int &location, const StringRefNull &suffix)
static std::string main_function_wrapper(std::string &pre_main, std::string &post_main)
VkPrimitiveTopology to_vk_primitive_topology(const GPUPrimType prim_type)
static void declare_emit_vertex(std::stringstream &ss)
static void print_interface_as_attributes(std::ostream &os, const std::string &prefix, const StageInterfaceInfo &iface, int &location)
static constexpr GPUSamplerState default_sampler()
Vector< shader::SpecializationConstant::Value > specialization_constants
VkShaderModule vk_shader_module
VkPipelineLayout vk_pipeline_layout
VkFormat stencil_attachment_format
VkFormat depth_attachment_format
Vector< VkFormat > color_attachment_formats
VkShaderModule vk_fragment_module
Vector< VkViewport > viewports
Vector< VkRect2D > scissors
VkShaderModule vk_vertex_module
VkShaderModule vk_geometry_module
Vector< VkVertexInputBindingDescription > bindings
Vector< VkVertexInputAttributeDescription > attributes
VkPrimitiveTopology vk_topology
PreRasterization pre_rasterization
GPUStateMutable mutable_state
VkPipelineLayout vk_pipeline_layout
FragmentShader fragment_shader
Vector< shader::SpecializationConstant::Value > specialization_constants
uint32_t size_in_bytes() const
VKDescriptorSet::Location descriptor_set_location_get() const
StorageType storage_type_get() const
bool shader_output_viewport_index
bool fragment_shader_barycentric
PrimitiveOut primitive_out
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Vector< StageInterfaceInfo * > vertex_out_interfaces_
StringRefNull compute_source_
Vector< SubpassIn > subpass_inputs_
Vector< Resource > geometry_resources_
Self & geometry_layout(PrimitiveIn prim_in, PrimitiveOut prim_out, int max_vertices, int invocations=-1)
Vector< VertIn > vertex_inputs_
bool early_fragment_test_
Vector< Resource > batch_resources_
StringRefNull geometry_source_
Vector< StageInterfaceInfo * > geometry_out_interfaces_
Vector< Resource > pass_resources_
GeometryStageLayout geometry_layout_
StringRefNull fragment_source_
bool do_static_compilation_
ComputeStageLayout compute_layout_
std::string compute_source_generated
Vector< SpecializationConstant > specialization_constants_
Vector< PushConst > push_constants_
Vector< FragOut > fragment_outputs_
StringRefNull instance_name
#define NOT_YET_IMPLEMENTED
#define VK_ALLOCATION_CALLBACKS