// File: rossy[P]crossc/SPIRV-Cross/spirv_msl.cpp
// Function: spirv_cross::CompilerMSL::get_metal_resource_index(spirv_cross::SPIRVariable&, spirv_cross::SPIRType::BaseType)
uint32_t CompilerMSL::get_metal_resource_index(SPIRVariable &var, SPIRType::BaseType basetype)
{
	auto &execution = get_entry_point();
	auto &var_dec = ir.meta[var.self].decoration;
	uint32_t var_desc_set = (var.storage == StorageClassPushConstant) ? kPushConstDescSet : var_dec.set;
	uint32_t var_binding = (var.storage == StorageClassPushConstant) ? kPushConstBinding : var_dec.binding;

	// If a matching binding has been specified, find and use it
	for (auto p_res_bind : resource_bindings)
	{
		if (p_res_bind->stage == execution.model && p_res_bind->desc_set == var_desc_set &&
		    p_res_bind->binding == var_binding)
		{

			p_res_bind->used_by_shader = true;
			switch (basetype)
			{
			case SPIRType::Struct:
				return p_res_bind->msl_buffer;
			case SPIRType::Image:
				return p_res_bind->msl_texture;
			case SPIRType::Sampler:
				return p_res_bind->msl_sampler;
			default:
				return 0;
			}
		}
	}

	// If there is no explicit mapping of bindings to MSL, use the declared binding.
	if (has_decoration(var.self, DecorationBinding))
		return get_decoration(var.self, DecorationBinding);

	uint32_t binding_stride = 1;
	auto &type = get<SPIRType>(var.basetype);
	for (uint32_t i = 0; i < uint32_t(type.array.size()); i++)
		binding_stride *= type.array_size_literal[i] ? type.array[i] : get<SPIRConstant>(type.array[i]).scalar();

	// If a binding has not been specified, revert to incrementing resource indices
	uint32_t resource_index;
	switch (basetype)
	{
	case SPIRType::Struct:
		resource_index = next_metal_resource_index.msl_buffer;
		next_metal_resource_index.msl_buffer += binding_stride;
		break;
	case SPIRType::Image:
		resource_index = next_metal_resource_index.msl_texture;
		next_metal_resource_index.msl_texture += binding_stride;
		break;
	case SPIRType::Sampler:
		resource_index = next_metal_resource_index.msl_sampler;
		next_metal_resource_index.msl_sampler += binding_stride;
		break;
	default:
		resource_index = 0;
		break;
	}
	return resource_index;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x90(%rbp)
callq 0x25150
movq -0x90(%rbp), %rdi
movq %rax, -0x28(%rbp)
addq $0x8, %rdi
addq $0x30, %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %esi
callq 0xead0
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x9, 0x10(%rax)
jne 0x106212
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x94(%rbp)
jmp 0x106222
movq -0x30(%rbp), %rax
movl 0xac(%rax), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x9, 0x10(%rax)
jne 0x10623f
xorl %eax, %eax
movl %eax, -0x98(%rbp)
jmp 0x10624f
movq -0x30(%rbp), %rax
movl 0xb0(%rax), %eax
movl %eax, -0x98(%rbp)
movq -0x90(%rbp), %rax
movl -0x98(%rbp), %ecx
movl %ecx, -0x38(%rbp)
addq $0x8f8, %rax            # imm = 0x8F8
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x1179a0
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
callq 0x1179d0
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x117a00
testb $0x1, %al
jne 0x106299
jmp 0x106352
leaq -0x48(%rbp), %rdi
callq 0x117a40
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl (%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0xb8(%rcx), %eax
jne 0x106342
movq -0x58(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x34(%rbp), %eax
jne 0x106342
movq -0x58(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x38(%rbp), %eax
jne 0x106342
movq -0x58(%rbp), %rax
movb $0x1, 0x18(%rax)
movl -0x1c(%rbp), %eax
movl %eax, -0x9c(%rbp)
subl $0xc, %eax
je 0x106309
jmp 0x1062ef
movl -0x9c(%rbp), %eax
subl $0xd, %eax
je 0x106318
jmp 0x1062fc
movl -0x9c(%rbp), %eax
subl $0xf, %eax
je 0x106327
jmp 0x106336
movq -0x58(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x106517
movq -0x58(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x106517
movq -0x58(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x106517
movl $0x0, -0x4(%rbp)
jmp 0x106517
jmp 0x106344
leaq -0x48(%rbp), %rdi
callq 0x117a60
jmp 0x106283
movq -0x90(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x21, %edx
callq 0x1f560
testb $0x1, %al
jne 0x106370
jmp 0x106390
movq -0x90(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x21, %edx
callq 0x23520
movl %eax, -0x4(%rbp)
jmp 0x106517
movq -0x90(%rbp), %rdi
movl $0x1, -0x5c(%rbp)
movq -0x18(%rbp), %rax
movl 0xc(%rax), %esi
callq 0xea90
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0xa0(%rbp)
movq -0x68(%rbp), %rdi
addq $0x20, %rdi
callq 0x8650
movq %rax, %rcx
movl -0xa0(%rbp), %eax
cmpl %ecx, %eax
jae 0x10646e
movq -0x68(%rbp), %rdi
addq $0x38, %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x117a80
movq %rax, -0x80(%rbp)
movq %rdx, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x1d8d0
testb $0x1, %al
jne 0x106405
jmp 0x106421
movq -0x68(%rbp), %rdi
addq $0x20, %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x8630
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
jmp 0x106453
movq -0x68(%rbp), %rdi
addq $0x20, %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x8630
movq -0x90(%rbp), %rdi
movl (%rax), %esi
callq 0x31910
movq %rax, %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x2f560
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
imull -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1063b5
movl -0x1c(%rbp), %eax
movl %eax, -0xa8(%rbp)
subl $0xc, %eax
je 0x106498
jmp 0x10647e
movl -0xa8(%rbp), %eax
subl $0xd, %eax
je 0x1064bc
jmp 0x10648b
movl -0xa8(%rbp), %eax
subl $0xf, %eax
je 0x1064e0
jmp 0x106504
movq -0x90(%rbp), %rax
movl 0x91c(%rax), %ecx
movl %ecx, -0x84(%rbp)
movl -0x5c(%rbp), %ecx
addl 0x91c(%rax), %ecx
movl %ecx, 0x91c(%rax)
jmp 0x10650e
movq -0x90(%rbp), %rax
movl 0x920(%rax), %ecx
movl %ecx, -0x84(%rbp)
movl -0x5c(%rbp), %ecx
addl 0x920(%rax), %ecx
movl %ecx, 0x920(%rax)
jmp 0x10650e
movq -0x90(%rbp), %rax
movl 0x924(%rax), %ecx
movl %ecx, -0x84(%rbp)
movl -0x5c(%rbp), %ecx
addl 0x924(%rax), %ecx
movl %ecx, 0x924(%rax)
jmp 0x10650e
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)