33 if (name !=
nullptr) {
43 for (
const int i :
IndexRange(ARGUMENT_ENCODERS_CACHE_SIZE)) {
44 if (arg_encoders_[i].encoder != nil) {
45 id<MTLArgumentEncoder> enc = arg_encoders_[i].encoder;
58 total_attributes_ = 0;
60 total_uniform_blocks_ = 0;
61 max_uniformbuf_index_ = 0;
62 total_storage_blocks_ = 0;
63 max_storagebuf_index_ = 0;
66 max_texture_index_ = -1;
67 enabled_attribute_mask_ = 0;
68 total_vert_stride_ = 0;
69 sampler_use_argument_buffer_ =
false;
70 for (
int i = 0; i <
ARRAY_SIZE(sampler_argument_buffer_bind_index_); i++) {
71 sampler_argument_buffer_bind_index_[i] = -1;
87 for (
const int i :
IndexRange(ARGUMENT_ENCODERS_CACHE_SIZE)) {
88 arg_encoders_[i].encoder = nil;
89 arg_encoders_[i].buffer_index = -1;
99 int matrix_element_count)
104 input_attr.
location = attribute_location;
107 input_attr.
offset = offset;
109 input_attr.
index = total_attributes_;
111 total_vert_stride_ =
max_ii(total_vert_stride_, offset + size);
112 enabled_attribute_mask_ |= (1 << attribute_location);
122 if ((size % 16) != 0) {
123 size += 16 - (size % 16);
135 max_uniformbuf_index_ =
max_ii(max_uniformbuf_index_, buffer_index);
136 return (total_uniform_blocks_++);
146 if ((size % 16) != 0) {
147 size += 16 - (size % 16);
159 max_storagebuf_index_ =
max_ii(max_storagebuf_index_, buffer_index);
160 return (total_storage_blocks_++);
169 push_constant_block_.
size = 0;
181 "Cannot add uniform '%s' to shader interface '%s' as the uniform limit of %d has been "
196 if ((current_offset % data_type_alignment) != 0) {
197 current_offset += data_type_alignment - (current_offset % data_type_alignment);
208 push_constant_block_.
current_offset = current_offset + data_type_size;
211 if ((push_constant_block_.
size % 16) != 0) {
212 push_constant_block_.
size += 16 - (push_constant_block_.
size % 16);
219 current_offset + data_type_size <= push_constant_block_.
size,
220 "Uniform size and offset sits outside the specified size range for the uniform block");
228 bool is_texture_sampler,
230 int tex_buffer_ssbo_location)
237 BLI_assert_msg(
tex.used ==
false,
"Texture slot already in-use by another binding");
238 tex.name_offset = name_offset;
239 tex.slot_index = texture_slot;
240 tex.location = location;
242 tex.sampler_format = sampler_format;
243 tex.is_texture_sampler = is_texture_sampler;
244 tex.stage_mask = stage_mask;
246 tex.texture_buffer_ssbo_location = tex_buffer_ssbo_location;
248 max_texture_index_ =
max_ii(max_texture_index_, texture_slot);
251 BLI_assert_msg(
false,
"Exceeding maximum supported texture count.");
253 "Could not add additional texture with index %d to shader interface. Maximum "
254 "supported texture count is %d",
264 constants_.append(constant);
281 if (uni !=
nullptr) {
285 MTL_LOG_INFO(
"Mapped builtin uniform '%s' NB: '%s' to location: %d",
297 if (uni !=
nullptr) {
301 MTL_LOG_INFO(
"Mapped builtin uniform block '%s' to location %d",
332 for (
const int attr_index :
IndexRange(total_attributes_)) {
341 current_input->
binding = attr_index;
348 for (
const int ubo_index :
IndexRange(total_uniform_blocks_)) {
366 for (
const int uniform_index :
IndexRange(total_uniforms_)) {
370 current_input->
location = uniform_index;
371 current_input->
binding = uniform_index;
383 for (
int texture_index = 0; texture_index <= max_texture_index_; texture_index++) {
389 "Texture binding slot should match array index for texture.");
399 current_input->
location = texture_index + total_uniforms_;
411 for (
const int ssbo_index :
IndexRange(total_storage_blocks_)) {
421 if (info !=
nullptr) {
427 BLI_assert_msg(0,
"Resource type is not supported for Geometry frequency");
439 current_input->
location = const_index;
449 for (
int texture_index = 0; texture_index <= max_texture_index_; texture_index++) {
452 char uniform_name[256];
454 BLI_snprintf(uniform_name, 256,
"%s_metadata", tex_name);
457 "Could not find expected metadata uniform slot for buffer-backed texture.");
458 if (uni !=
nullptr) {
469 uint32_t argument_buffer_bind_index_vert,
470 uint32_t argument_buffer_bind_index_frag,
471 uint32_t argument_buffer_bind_index_compute)
473 sampler_use_argument_buffer_ = use_argument_buffer;
475 argument_buffer_bind_index_vert;
477 argument_buffer_bind_index_frag;
479 argument_buffer_bind_index_compute;
487 return attributes_[index];
492 return total_attributes_;
497 return total_constants_;
502 return total_vert_stride_;
507 return enabled_attribute_mask_;
515 return uniforms_[index];
520 return total_uniforms_;
533 return push_constant_block_;
538 return total_uniform_blocks_;
543 return (block_index < total_uniform_blocks_);
548 return (block_index < total_uniform_blocks_) ? ubos_[block_index].
size : 0;
556 return ssbos_[index];
561 return total_storage_blocks_;
566 return (block_index < total_storage_blocks_);
571 return (block_index < total_storage_blocks_) ? ssbos_[block_index].
size : 0;
585 return textures_[index];
590 return total_textures_;
595 return max_texture_index_;
600 return sampler_use_argument_buffer_;
611 for (
const int i :
IndexRange(ARGUMENT_ENCODERS_CACHE_SIZE)) {
612 encoder = arg_encoders_[i].buffer_index == buffer_index ? arg_encoders_[i].encoder : encoder;
619 for (
const int i :
IndexRange(ARGUMENT_ENCODERS_CACHE_SIZE)) {
620 if (arg_encoders_[i].encoder == nil) {
621 arg_encoders_[i].encoder = encoder;
622 arg_encoders_[i].buffer_index = buffer_index;
633 return MTLVertexFormatChar;
635 return MTLVertexFormatUChar;
637 return MTLVertexFormatUChar;
639 return MTLVertexFormatChar2;
641 return MTLVertexFormatUChar2;
643 return MTLVertexFormatUChar2;
645 return MTLVertexFormatShort;
647 return MTLVertexFormatUShort;
649 return MTLVertexFormatChar3;
651 return MTLVertexFormatUChar3;
653 return MTLVertexFormatUChar3;
655 return MTLVertexFormatChar4;
657 return MTLVertexFormatUChar4;
659 return MTLVertexFormatInt;
661 return MTLVertexFormatUInt;
663 return MTLVertexFormatUChar4;
665 return MTLVertexFormatShort2;
667 return MTLVertexFormatUShort2;
669 return MTLVertexFormatFloat;
673 BLI_assert_msg(
false,
"Unsupported raw vertex attribute types in Blender.");
674 return MTLVertexFormatInvalid;
677 return MTLVertexFormatShort3;
679 return MTLVertexFormatUShort3;
681 return MTLVertexFormatShort4;
683 return MTLVertexFormatUShort4;
685 return MTLVertexFormatInt2;
687 return MTLVertexFormatUInt2;
689 return MTLVertexFormatFloat2;
691 return MTLVertexFormatInt;
693 return MTLVertexFormatUInt;
703 BLI_assert_msg(
false,
"Unsupported raw vertex attribute types in Blender.");
704 return MTLVertexFormatInvalid;
707 return MTLVertexFormatInt3;
709 return MTLVertexFormatInt4;
711 return MTLVertexFormatUInt3;
713 return MTLVertexFormatUInt4;
715 return MTLVertexFormatFloat3;
717 return MTLVertexFormatFloat4;
719 return MTLVertexFormatInt2;
721 return MTLVertexFormatUInt2;
728 BLI_assert_msg(
false,
"Unsupported raw vertex attribute types in Blender.");
729 return MTLVertexFormatInvalid;
732 return MTLVertexFormatInt3;
734 return MTLVertexFormatInt4;
736 return MTLVertexFormatUInt3;
738 return MTLVertexFormatUInt4;
742 return MTLVertexFormatUInt1010102Normalized;
744 return MTLVertexFormatInt1010102Normalized;
748 return MTLVertexFormatInvalid;
#define BLI_assert_msg(a, msg)
BLI_INLINE unsigned int BLI_hash_string(const char *str)
MINLINE int max_ii(int a, int b)
#define STRNCPY(dst, src)
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
uint32_t get_total_attributes() const
bool has_storage_block(uint32_t block_index) const
uint32_t add_storage_block(uint32_t name_offset, uint32_t buffer_index, uint32_t location, uint32_t size, ShaderStage stage_mask=ShaderStage::ANY)
uint32_t add_uniform_block(uint32_t name_offset, uint32_t buffer_index, uint32_t location, uint32_t size, ShaderStage stage_mask=ShaderStage::ANY)
void add_uniform(uint32_t name_offset, eMTLDataType type, int array_len=1)
const MTLShaderBufferBlock & get_push_constant_block() const
MTLShaderInterface(const char *name)
const MTLShaderTexture & get_texture(uint index) const
void insert_argument_encoder(int buffer_index, id encoder)
const MTLShaderBufferBlock & get_uniform_block(uint index) const
uint32_t get_total_textures() const
uint32_t get_storage_block_size(uint32_t block_index) const
const char * get_name_at_offset(uint32_t offset) const
bool has_uniform_block(uint32_t block_index) const
bool uses_argument_buffer_for_samplers() const
uint32_t get_enabled_attribute_mask() const
int get_argument_buffer_bind_index(ShaderStage stage) const
const MTLShaderInputAttribute & get_attribute(uint index) const
const MTLShaderBufferBlock & get_storage_block(uint index) const
const MTLShaderUniform & get_uniform(uint index) const
void add_input_attribute(uint32_t name_offset, uint32_t attribute_location, MTLVertexFormat format, uint32_t buffer_index, uint32_t size, uint32_t offset, int matrix_element_count=1)
id< MTLArgumentEncoder > find_argument_encoder(int buffer_index) const
uint32_t get_total_uniform_blocks() const
uint32_t get_uniform_block_size(uint32_t block_index) const
void add_texture(uint32_t name_offset, uint32_t texture_slot, uint32_t location, eGPUTextureType tex_binding_type, eGPUSamplerFormat sampler_format, bool is_texture_sampler, ShaderStage stage_mask=ShaderStage::FRAGMENT, int tex_buffer_ssbo_location=-1)
uint32_t get_total_uniforms() const
uint32_t get_total_constants() const
void add_constant(uint32_t name_offset)
void add_push_constant_block(uint32_t name_offset)
void set_sampler_properties(bool use_argument_buffer, uint32_t argument_buffer_bind_index_vert, uint32_t argument_buffer_bind_index_frag, uint32_t argument_buffer_bind_index_compute)
uint32_t get_total_storage_blocks() const
uint32_t get_total_vertex_stride() const
void prepare_common_shader_inputs(const shader::ShaderCreateInfo *info=nullptr)
uint32_t get_max_texture_index() const
uint32_t get_max_buffer_index() const
const ShaderInput * ubo_get(const char *name) const
static const char * builtin_uniform_block_name(GPUUniformBlockBuiltin u)
int32_t builtin_blocks_[GPU_NUM_UNIFORM_BLOCKS]
int32_t builtins_[GPU_NUM_UNIFORMS]
const ShaderInput * uniform_get(const char *name) const
static const char * builtin_uniform_name(GPUUniformBuiltin u)
void *(* MEM_callocN)(size_t len, const char *str)
#define MTL_MAX_BUFFER_BINDINGS
#define MTL_MAX_TEXTURE_SLOTS
#define MTL_MAX_VERTEX_INPUT_ATTRIBUTES
#define MTL_MAX_UNIFORMS_PER_BLOCK
#define MTL_LOG_INFO(info,...)
#define MTL_LOG_WARNING(info,...)
uint mtl_get_data_type_alignment(eMTLDataType type)
@ MTL_DATATYPE_INT1010102_NORM
@ MTL_DATATYPE_UINT1010102_NORM
uint mtl_get_data_type_size(eMTLDataType type)
uint get_shader_stage_index(ShaderStage stage)
MTLVertexFormat mtl_datatype_to_vertex_type(eMTLDataType type)
int texture_buffer_ssbo_location
int buffer_metadata_uniform_loc
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Vector< Resource > geometry_resources_