def update_ubo(): # Upload uniforms data to memory data_ptr = hvk.map_memory(api, device, uniforms_mem, 0, sizeof(uniforms_data_type)) uniforms = uniforms_data_type.from_address(data_ptr.value) ubo_data, light, mat = uniforms.ubo, uniforms.light, uniforms.mat # UBO width, height = window.dimensions() ubo_data[0] = Mat4.perspective(radians(60), width / height, 0.1, 256.0) # Perspective ubo_data[1] = Mat4.from_translation(0.0, 0.0, -zoom) # View model = Mat4.from_rotation(rotation[0], (1.0, 0.0, 0))\ .rotate(rotation[1], (0.0, 1.0, 0.0))\ .rotate(rotation[2], (0.0, 0.0, 1.0)) ubo_data[2] = model # Model ubo_data[3] = model.invert().transpose() # Normal # Light stuff light.viewPos[:3] = view light.pos[:3] = light_direction light.color = light_color light.ambient = light_ambient # Material stuff material = materials[material_index] mat.color = material['color'] mat.strenght = material['strength'] mat.shininess = material['shininess'] hvk.unmap_memory(api, device, uniforms_mem)
def mesh_to_staging(): global staging_mesh_buffer, staging_mesh_memory # Create staging resources staging_mesh_buffer = hvk.create_buffer( api, device, hvk.buffer_create_info(size=total_mesh_size, usage=vk.BUFFER_USAGE_TRANSFER_SRC_BIT)) staging_req = hvk.buffer_memory_requirements(api, device, staging_mesh_buffer) mt_index = find_memory_type( 0, vk.MEMORY_PROPERTY_HOST_COHERENT_BIT | vk.MEMORY_PROPERTY_HOST_VISIBLE_BIT) staging_mesh_memory = hvk.allocate_memory( api, device, hvk.memory_allocate_info(allocation_size=staging_req.size, memory_type_index=mt_index)) hvk.bind_buffer_memory(api, device, staging_mesh_buffer, staging_mesh_memory, 0) # Upload mesh to staging data data_ptr = hvk.map_memory(api, device, staging_mesh_memory, 0, staging_req.size).value memmove(data_ptr + mesh_positions['offset'], byref(mesh_positions['data']), mesh_positions['size']) memmove(data_ptr + mesh_indices['offset'], byref(mesh_indices['data']), mesh_indices['size']) hvk.unmap_memory(api, device, staging_mesh_memory)
def texture_to_staging(): global staging_texture_buffer, staging_texture_memory # Create staging resources staging_texture_buffer = hvk.create_buffer( api, device, hvk.buffer_create_info(size=len(texture.data), usage=vk.BUFFER_USAGE_TRANSFER_SRC_BIT)) staging_req = hvk.buffer_memory_requirements(api, device, staging_texture_buffer) mt_index = find_memory_type( 0, vk.MEMORY_PROPERTY_HOST_COHERENT_BIT | vk.MEMORY_PROPERTY_HOST_VISIBLE_BIT) staging_texture_memory = hvk.allocate_memory( api, device, hvk.memory_allocate_info(allocation_size=staging_req.size, memory_type_index=mt_index)) hvk.bind_buffer_memory(api, device, staging_texture_buffer, staging_texture_memory, 0) # Upload texture to staging data data_ptr = hvk.map_memory(api, device, staging_texture_memory, 0, staging_req.size).value memmove(data_ptr, texture.data_ptr(), len(texture.data)) hvk.unmap_memory(api, device, staging_texture_memory)
def create_descriptor_sets(): global descriptor_pool, descriptor_set, color_buffer, color_mem # Uniform buffer values colors = (0.8, 0.8, 0.0, 1.0) colors_data = (c_float * len(colors))(*colors) colors_data_size = sizeof(colors_data) # Create descriptor pool pool_size = vk.DescriptorPoolSize(type=vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptor_count=1) descriptor_pool = hvk.create_descriptor_pool( api, device, hvk.descriptor_pool_create_info(max_sets=1, pool_sizes=(pool_size, ))) descriptor_set = hvk.allocate_descriptor_sets( api, device, hvk.descriptor_set_allocate_info( descriptor_pool=descriptor_pool, set_layouts=(descriptor_set_layout, )))[0] # Allocate memory for the descriptor color_buffer = hvk.create_buffer( api, device, hvk.buffer_create_info(size=colors_data_size, usage=vk.BUFFER_USAGE_UNIFORM_BUFFER_BIT)) color_buffer_req = hvk.buffer_memory_requirements(api, device, color_buffer) mt_index = find_memory_type( 0, vk.MEMORY_PROPERTY_HOST_COHERENT_BIT | vk.MEMORY_PROPERTY_HOST_VISIBLE_BIT) color_mem = hvk.allocate_memory( api, device, hvk.memory_allocate_info(allocation_size=color_buffer_req.size, memory_type_index=mt_index)) hvk.bind_buffer_memory(api, device, color_buffer, color_mem, 0) # Upload color data to memory data_ptr = hvk.map_memory(api, device, color_mem, 0, colors_data_size).value memmove(data_ptr, byref(colors_data), colors_data_size) hvk.unmap_memory(api, device, color_mem)
def update_ubo(): # Upload uniforms data to memory data_ptr = hvk.map_memory(api, device, uniforms_mem, 0, sizeof(uniforms_data_type)) uniforms = uniforms_data_type.from_address(data_ptr.value) ubo_data = uniforms.ubo # Perspective width, height = window.dimensions() ubo_data[0] = Mat4.perspective(radians(60), width / height, 0.1, 256.0) # View ubo_data[1] = Mat4.from_translation(0.0, 0.0, -zoom) # Model ubo_data[2] = Mat4.from_rotation(rotation, (0.0, -1.0, 0.5)) hvk.unmap_memory(api, device, uniforms_mem)
def map_alloc(self, alloc, offset=None, size=None): engine, api, device = self.ctx offset = offset or 0 size = size or alloc.size pointer = hvk.map_memory(api, device, alloc.device_memory, offset, size) unmap = lambda: hvk.unmap_memory(api, device, alloc.device_memory) return MappedDeviceMemory(alloc, pointer, unmap)
def update_ubo(): # Upload uniforms data to memory data_ptr = hvk.map_memory(api, device, uniforms_mem, 0, sizeof(uniforms_data_type)) uniforms = uniforms_data_type.from_address(data_ptr.value) ubo_data, light = uniforms.ubo, uniforms.light # Perspective width, height = window.dimensions() ubo_data[0] = Mat4.perspective(radians(60), width / height, 0.1, 256.0) # View ubo_data[1] = Mat4.from_translation(0.0, 0.0, -zoom) # Model ubo_data[2] = Mat4.from_rotation(rotation, (0.0, -1.0, 0.5)) # Light stuff light.reverseLightDirection[:3] = Vec3.normalize(reverse_light_direction) hvk.unmap_memory(api, device, uniforms_mem)