SHARE
TWEET

Untitled

a guest Mar 9th, 2018 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Bad
  2. static int CreateUniformDescription(VkDescriptorPool* pDescPool, VkDescriptorSetLayout* pDescLayout, VkDescriptorSet* pDescSet,
  3.     VkDeviceMemory* pDevMemory, VkBuffer* pUniBuffer)
  4. {
  5.     // Setup Layout Binding for the Uniform buffer that contains "camera"
  6.     VkDescriptorSetLayoutBinding layoutBinding = {};
  7.     layoutBinding.binding = 0;
  8.     layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  9.     layoutBinding.descriptorCount = 1;
  10.     layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  11.  
  12.     // Setup the Description Layout Info
  13.     VkDescriptorSetLayoutCreateInfo descLayoutInfo = {};
  14.     descLayoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  15.     descLayoutInfo.pBindings = &layoutBinding;
  16.     descLayoutInfo.bindingCount = 1;
  17.  
  18.     // Create Description Layout
  19.     VkDescriptorSetLayout descLayout = {};
  20.     VkResult res = vkCreateDescriptorSetLayout(_VkSystem.device, &descLayoutInfo, 0, &descLayout);
  21.     if (res != VK_SUCCESS)
  22.         return 1;
  23.  
  24.     // Setup Descriptor Pool Size
  25.     VkDescriptorPoolSize descPoolSize = {};
  26.     descPoolSize.descriptorCount = 1;
  27.     descPoolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  28.  
  29.     // Setup Descriptor Pool Info
  30.     VkDescriptorPoolCreateInfo descPoolInfo = {};
  31.     descPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
  32.     descPoolInfo.maxSets = 1;
  33.     descPoolInfo.poolSizeCount = 1;
  34.     descPoolInfo.pPoolSizes = &descPoolSize;
  35.  
  36.     // Create Descriptor Pool
  37.     VkDescriptorPool descPool = {};
  38.     res = vkCreateDescriptorPool(_VkSystem.device, &descPoolInfo, 0, &descPool);
  39.     if (res != VK_SUCCESS)
  40.         return 1;
  41.  
  42.     // Setup Descriptor Allocate Info
  43.     VkDescriptorSetAllocateInfo allocInfo = {};
  44.     allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  45.     allocInfo.descriptorPool = descPool;
  46.     allocInfo.pSetLayouts = &descLayout;
  47.     allocInfo.descriptorSetCount = 1;
  48.  
  49.     // Allocate Descriptor set/s
  50.     VkDescriptorSet descSet = {};
  51.     res = vkAllocateDescriptorSets(_VkSystem.device, &allocInfo, &descSet);
  52.     if (res != VK_SUCCESS)
  53.         return 1;
  54.  
  55.     // Create some random big buffer for the Uniform block
  56.     int uniBlockSize = sizeof(float) * 2; // vec2
  57.     VkDeviceMemory devMemory2;
  58.     VkBuffer uniBuffer;
  59.     CreateBuffer(uniBlockSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, &uniBuffer, &devMemory2);
  60.  
  61.     VkMappedMemoryRange range;
  62.  
  63.     void* pBuffer;
  64.     vkMapMemory(_VkSystem.device, devMemory2, 0, uniBlockSize, 0, &pBuffer);
  65.  
  66.     ((float*)pBuffer)[0] = 0.1f; // x
  67.     ((float*)pBuffer)[1] = 0.5f; // y
  68.  
  69.     vkUnmapMemory(_VkSystem.device, devMemory2);
  70.  
  71.     // Setup Descriptor Buffer Info
  72.     VkDescriptorBufferInfo bufferInfo = {};
  73.     bufferInfo.buffer = uniBuffer;
  74.     bufferInfo.range = uniBlockSize;
  75.  
  76.     // Setup Write Descriptor Set
  77.     VkWriteDescriptorSet writeDescSet = {};
  78.     writeDescSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  79.     writeDescSet.descriptorCount = 1;
  80.     writeDescSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  81.     writeDescSet.dstSet = descSet;
  82.     writeDescSet.pBufferInfo = &bufferInfo;
  83.  
  84.     // Update Descriptor set/s
  85.     vkUpdateDescriptorSets(_VkSystem.device, 1, &writeDescSet, 0, 0);
  86.  
  87.     *pDescLayout = descLayout;
  88.     *pDescSet = descSet;
  89.     *pDevMemory = devMemory2;
  90.     *pUniBuffer = uniBuffer;
  91.     *pDescPool = descPool;
  92.  
  93.     return res;
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top