def write_descriptor_sets(): ubo_buffer_info = vk.DescriptorBufferInfo(buffer=uniforms_buffer, offset=0, range=sizeof(ubo_data_type)) light_buffer_info = vk.DescriptorBufferInfo(buffer=uniforms_buffer, offset=sizeof(ubo_data_type), range=sizeof(light_data_type)) sampler_image_info = vk.DescriptorImageInfo( sampler=texture_sampler, image_view=texture_view, image_layout=texture_image_layout) write_set_ubo = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=0, descriptor_type=vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info=(ubo_buffer_info, )) write_set_light = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=1, descriptor_type=vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info=(light_buffer_info, )) write_sampler = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=2, descriptor_type=vk.DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, image_info=(sampler_image_info, )) hvk.update_descriptor_sets(api, device, (write_set_ubo, write_set_light, write_sampler), ())
def write_descriptor_sets(): ubo_buffer_info = vk.DescriptorBufferInfo(buffer=uniforms_buffer, offset=0, range=sizeof(ubo_data_type)) light_buffer_info = vk.DescriptorBufferInfo(buffer=uniforms_buffer, offset=sizeof(ubo_data_type), range=sizeof(light_data_type)) mat_buffer_info = vk.DescriptorBufferInfo(buffer=uniforms_buffer, offset=light_buffer_info.offset + light_buffer_info.range, range=sizeof(mat_data_type)) write_set_ubo = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=0, descriptor_type=vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info=(ubo_buffer_info, )) write_set_light = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=1, descriptor_type=vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info=(light_buffer_info, )) write_set_mat = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=2, descriptor_type=vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info=(mat_buffer_info, )) hvk.update_descriptor_sets(api, device, (write_set_ubo, write_set_light, write_set_mat), ())
def write_descriptor_sets(): color_buffer_info = vk.DescriptorBufferInfo(buffer=color_buffer, offset=0, range=vk.WHOLE_SIZE) write_set = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=0, descriptor_type=vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info=(color_buffer_info, )) hvk.update_descriptor_sets(api, device, (write_set, ), ())
def write_compute_descriptor_sets(): noise_info = vk.DescriptorImageInfo(sampler=noise_sampler, image_view=noise_view, image_layout=vk.IMAGE_LAYOUT_GENERAL) write_set_ubo = hvk.write_descriptor_set( dst_set=compute_descriptor_set, dst_binding=0, descriptor_type=vk.DESCRIPTOR_TYPE_STORAGE_IMAGE, image_info=(noise_info, )) hvk.update_descriptor_sets(api, device, (write_set_ubo, ), ())
def write_descriptor_sets(): ubo_buffer_info = vk.DescriptorBufferInfo(buffer=uniforms_buffer, offset=0, range=sizeof(ubo_data_type)) sampler_image_info = vk.DescriptorImageInfo( sampler=noise_sampler, image_view=noise_view, image_layout=vk.IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) write_set_ubo = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=0, descriptor_type=vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER, buffer_info=(ubo_buffer_info, )) write_sampler = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=1, descriptor_type=vk.DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, image_info=(sampler_image_info, )) hvk.update_descriptor_sets(api, device, (write_set_ubo, write_sampler), ())
def _update_uniforms(self, objects, shaders): uniforms_alloc = self.uniforms_alloc base_offset = map_size = None buffer_update_list = [] data_samplers, data_images = self.samplers, self.images image_write_sets = [] def read_buffer_offets(uniforms, dobj, obj, uniform_name): nonlocal buffer_update_list, base_offset, map_size # Skips image uniforms buffer_offset_range = dobj.write_sets[uniform_name][ "buffer_offset_range"] if buffer_offset_range is None: return # Fetch the new uniform value buffer_value = getattr(uniforms, uniform_name) # Update the mapping info from the size and offset of the uniforms value offset, size = buffer_offset_range offset_size = offset + size if base_offset is None or offset < base_offset: base_offset = offset if map_size is None or offset_size > map_size: map_size = size buffer_update_list.append((buffer_value, offset)) def read_image_write_sets(uniforms, dobj, obj, uniform_name): nonlocal image_write_sets, data_images, data_samplers write_set_info = dobj.write_sets[uniform_name] if write_set_info["buffer_offset_range"] is not None: return # Fetch the new image CombinedSampler value image_sampler = getattr(uniforms, uniform_name) data_sampler = data_samplers[image_sampler.sampler_id] data_image = data_images[image_sampler.image_id] data_view = data_image.views.get(image_sampler.view_name, None) if data_view is None: raise ValueError( f"No view named \"{view_name}\" for image \"{data_image.image.name}\" of component {obj.name}" ) # Fetch the write set associated with the updated uniform value write_set = write_set_info["write_set"] # Update the image info image_info = write_set.image_info[0] image_info.sampler = data_sampler.sampler_handle image_info.image_layout = data_image.layout image_info.image_view = data_view image_write_sets.append(write_set) def process_uniforms(items): for obj, data_obj in items: uniforms = obj.uniforms for uniform_name in uniforms.updated_member_names: # Find the offsets in the uniforms buffer for the updated buffer uniforms read_buffer_offets(uniforms, data_obj, obj, uniform_name) # Fetch the write sets for the updated image uniforms read_image_write_sets(uniforms, data_obj, obj, uniform_name) uniforms.updated_member_names.clear() process_uniforms(objects) process_uniforms(shaders) # Update the image uniforms _, api, device = self.ctx hvk.update_descriptor_sets(api, device, image_write_sets, ()) # Update the uniform buffers mem = self.engine.memory_manager with mem.map_alloc(uniforms_alloc, base_offset, map_size) as mapping: for value, offset in buffer_update_list: mapping.write_typed_data(value, offset - base_offset)
def _setup_descriptor_write_sets(self): _, api, device = self.ctx shaders, computes, data_samplers, data_images = self.shaders, self.computes, self.samplers, self.images uniform_buffer = self.uniforms_buffer uniform_offset = 0 write_sets_to_update = [] def generate_write_set(obj, wst, descriptor_set): nonlocal uniform_buffer, uniform_offset name, dtype, drange, binding = wst['name'], wst[ 'descriptor_type'], wst['range'], wst['binding'] if dtype == vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER: buffer_info = vk.DescriptorBufferInfo(buffer=uniform_buffer, offset=uniform_offset, range=drange) write_set = hvk.write_descriptor_set( dst_set=descriptor_set, dst_binding=binding, descriptor_type=dtype, buffer_info=(buffer_info, )) uniform_offset += drange elif dtype in (vk.DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, vk.DESCRIPTOR_TYPE_STORAGE_IMAGE): image_id, view_name, sampler_id = getattr(obj.uniforms, name) data_image = data_images[image_id] data_sampler = data_samplers[sampler_id] data_view = data_image.views.get(view_name, None) if data_view is None: raise ValueError( f"No view named \"{view_name}\" for image \"{data_image.image.name}\"" ) image_info = vk.DescriptorImageInfo( sampler=data_sampler.sampler_handle, image_view=data_view, image_layout=data_image.layout) write_set = hvk.write_descriptor_set(dst_set=descriptor_set, dst_binding=binding, descriptor_type=dtype, image_info=(image_info, )) # Clear the accessed uniforms names for images because we don't actually need to mark them as updated obj.uniforms.updated_member_names.remove(name) else: raise ValueError(f"Unknown descriptor type: {dtype}") return write_set def map_write_sets(data_obj, obj, layouts): write_sets = {} for descriptor_set, descriptor_layout in zip( data_obj.descriptor_sets, layouts): for wst in descriptor_layout.write_set_templates: name = wst["name"] buffer_offset_range = None if wst["buffer"]: buffer_offset_range = (uniform_offset, wst["range"]) write_set = generate_write_set(obj, wst, descriptor_set) write_sets_to_update.append(write_set) write_sets[name] = { "buffer_offset_range": buffer_offset_range, "write_set": write_set } return write_sets for data_shader in shaders: data_shader.write_sets = map_write_sets(data_shader, data_shader.shader, data_shader.global_layouts) for data_compute in computes: data_compute.write_sets = map_write_sets( data_compute, data_compute.compute, data_compute.global_layouts) for shader_index, objects in self._group_objects_by_shaders(): data_shader = shaders[shader_index] # Local descriptor sets for data_obj in objects: data_obj.write_sets = map_write_sets(data_obj, data_obj.obj, data_shader.local_layouts) hvk.update_descriptor_sets(api, device, write_sets_to_update, ())