Beispiel #1
0
	void updateUniformBuffers()
	{
		// Vertex shader
		glm::mat4 viewMatrix = glm::mat4();
		uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f);
		viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom));

		float offset = 0.5f;
		int uboIndex = 1;
		uboVS.model = glm::mat4();
		uboVS.model = viewMatrix * glm::translate(uboVS.model, glm::vec3(0, 0, 0));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformData.VS.memory, 0, sizeof(uboVS), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboVS, sizeof(uboVS));
		vkUnmapMemory(device, uniformData.VS.memory);

		// Geometry shader
		uboGS.model = uboVS.model;
		uboGS.projection = uboVS.projection;
		err = vkMapMemory(device, uniformData.GS.memory, 0, sizeof(uboGS), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboGS, sizeof(uboGS));
		vkUnmapMemory(device, uniformData.GS.memory);
	}
Beispiel #2
0
	void updateUniformBuffers()
	{
		// Vertex shader
		glm::mat4 viewMatrix = glm::mat4();
		ubos.vertexShader.projection = glm::perspective(glm::radians(45.0f), (float)(width* ((splitScreen) ? 0.5f : 1.0f)) / (float)height, 0.001f, 256.0f);
		viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom));

		ubos.vertexShader.model = glm::mat4();
		ubos.vertexShader.model = viewMatrix * glm::translate(ubos.vertexShader.model, glm::vec3(0, 0, 0));
		ubos.vertexShader.model = glm::rotate(ubos.vertexShader.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
		ubos.vertexShader.model = glm::rotate(ubos.vertexShader.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
		ubos.vertexShader.model = glm::rotate(ubos.vertexShader.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

		ubos.vertexShader.normal = glm::inverseTranspose(ubos.vertexShader.model);

		if (!paused)
		{
			ubos.vertexShader.lightPos.x = sin(glm::radians(timer * 360.0f)) * 0.5;
			ubos.vertexShader.lightPos.y = cos(glm::radians(timer * 360.0f)) * 0.5;
		}

		ubos.vertexShader.cameraPos = glm::vec4(0.0, 0.0, zoom, 0.0);

		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformData.vertexShader.memory, 0, sizeof(ubos.vertexShader), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &ubos.vertexShader, sizeof(ubos.vertexShader));
		vkUnmapMemory(device, uniformData.vertexShader.memory);

		// Fragment shader
		err = vkMapMemory(device, uniformData.fragmentShader.memory, 0, sizeof(ubos.fragmentShader), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &ubos.fragmentShader, sizeof(ubos.fragmentShader));
		vkUnmapMemory(device, uniformData.fragmentShader.memory);
	}
Beispiel #3
0
	void updateUniformBuffers()
	{
		// 3D object
		glm::mat4 viewMatrix = glm::mat4();
		uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f);
		viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom));

		uboVS.model = glm::mat4();
		uboVS.model = viewMatrix * glm::translate(uboVS.model, glm::vec3(0, 0, 0));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformData.objectVS.memory, 0, sizeof(uboVS), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboVS, sizeof(uboVS));
		vkUnmapMemory(device, uniformData.objectVS.memory);

		// Skysphere
		viewMatrix = glm::mat4();
		uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f);

		uboVS.model = glm::mat4();
		uboVS.model = viewMatrix * glm::translate(uboVS.model, glm::vec3(0, 0, 0));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

		err = vkMapMemory(device, uniformData.skyboxVS.memory, 0, sizeof(uboVS), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboVS, sizeof(uboVS));
		vkUnmapMemory(device, uniformData.skyboxVS.memory);
	}
Beispiel #4
0
	void updateUniformBuffers()
	{
		// Tessellation eval
		glm::mat4 viewMatrix = glm::mat4();
		uboTE.projection = glm::perspective(glm::radians(45.0f), (float)(width* ((splitScreen) ? 0.5f : 1.0f)) / (float)height, 0.1f, 256.0f);
		viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom));

		float offset = 0.5f;
		int uboIndex = 1;
		uboTE.model = glm::mat4();
		uboTE.model = viewMatrix * glm::translate(uboTE.model, glm::vec3(0, 0, 0));
		uboTE.model = glm::rotate(uboTE.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
		uboTE.model = glm::rotate(uboTE.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
		uboTE.model = glm::rotate(uboTE.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformDataTE.memory, 0, sizeof(uboTE), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboTE, sizeof(uboTE));
		vkUnmapMemory(device, uniformDataTE.memory);

		// Tessellation control
		err = vkMapMemory(device, uniformDataTC.memory, 0, sizeof(uboTC), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboTC, sizeof(uboTC));
		vkUnmapMemory(device, uniformDataTC.memory);
	}
Beispiel #5
0
		/**
		* Create a buffer on the device
		*
		* @param usageFlags Usage flag bitmask for the buffer (i.e. index, vertex, uniform buffer)
		* @param memoryPropertyFlags Memory properties for this buffer (i.e. device local, host visible, coherent)
		* @param size Size of the buffer in byes
		* @param buffer Pointer to the buffer handle acquired by the function
		* @param memory Pointer to the memory handle acquired by the function
		* @param data Pointer to the data that should be copied to the buffer after creation (optional, if not set, no data is copied over)
		*
		* @return VK_SUCCESS if buffer handle and memory have been created and (optionally passed) data has been copied
		*/
		VkResult createBuffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, VkDeviceSize size, VkBuffer *buffer, VkDeviceMemory *memory, void *data = nullptr)
		{
			// Create the buffer handle
			VkBufferCreateInfo bufferCreateInfo = vkTools::initializers::bufferCreateInfo(usageFlags, size);
			bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
			VK_CHECK_RESULT(vkCreateBuffer(logicalDevice, &bufferCreateInfo, nullptr, buffer));

			// Create the memory backing up the buffer handle
			VkMemoryRequirements memReqs;
			VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
			vkGetBufferMemoryRequirements(logicalDevice, *buffer, &memReqs);
			memAlloc.allocationSize = memReqs.size;
			// Find a memory type index that fits the properties of the buffer
			memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags);
			VK_CHECK_RESULT(vkAllocateMemory(logicalDevice, &memAlloc, nullptr, memory));
			
			// If a pointer to the buffer data has been passed, map the buffer and copy over the data
			if (data != nullptr)
			{
				void *mapped;
				VK_CHECK_RESULT(vkMapMemory(logicalDevice, *memory, 0, size, 0, &mapped));
				memcpy(mapped, data, size);
				vkUnmapMemory(logicalDevice, *memory);
			}

			// Attach the memory to the buffer object
			VK_CHECK_RESULT(vkBindBufferMemory(logicalDevice, *buffer, *memory, 0));

			return VK_SUCCESS;
		}
VkBool32 VulkanExampleBase::createBuffer(
	VkBufferUsageFlags usage,
	VkDeviceSize size,
	void * data,
	VkBuffer *buffer,
	VkDeviceMemory *memory)
{
	VkMemoryRequirements memReqs;
	VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
	VkBufferCreateInfo bufferCreateInfo = vkTools::initializers::bufferCreateInfo(usage, size);

	VkResult err = vkCreateBuffer(device, &bufferCreateInfo, nullptr, buffer);
	assert(!err);
	vkGetBufferMemoryRequirements(device, *buffer, &memReqs);
	memAlloc.allocationSize = memReqs.size;
	getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &memAlloc.memoryTypeIndex);
	err = vkAllocateMemory(device, &memAlloc, nullptr, memory);
	assert(!err);
	if (data != nullptr)
	{
		void *mapped;
		err = vkMapMemory(device, *memory, 0, size, 0, &mapped);
		assert(!err);
		memcpy(mapped, data, size);
		vkUnmapMemory(device, *memory);
	}
	err = vkBindBufferMemory(device, *buffer, *memory, 0);
	assert(!err);
	return true;
}
Beispiel #7
0
/**
 * Create the entity.
 */
VkcEntity::VkcEntity(const VkcDevice *device) : VkcEntity()
{
    // Load the model.
    vertices.append({-1.0f,  0.0f,  1.0f,    0.0f,  1.0f,    0.0f, -1.0f,  0.0f});
    vertices.append({-1.0f,  0.0f, -1.0f,    0.0f,  0.0f,    0.0f, -1.0f,  0.0f});
    vertices.append({ 1.0f,  0.0f,  1.0f,    1.0f,  1.0f,    0.0f, -1.0f,  0.0f});
    vertices.append({ 1.0f,  0.0f, -1.0f,    1.0f,  0.0f,    0.0f, -1.0f,  0.0f});

    indices = {0, 1, 2, 2, 1, 3};

    // Load position, scale and rotation data.
    // /@todo

    // Create buffer.
    buffer.create(vertices.size() * sizeof(VkVertex) + indices.size() * sizeof(uint32_t),
                  VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, device);

    // Map buffer memory to host.
    uint8_t *data;
    vkMapMemory(device->logical, buffer.memory, 0, VK_WHOLE_SIZE, 0,(void**) &data);

    // Copy data to the buffer.
    uint32_t offset = 0;
    memcpy(data + offset, vertices.data(), vertices.size() * sizeof(VkVertex));

    offset += vertices.size() * sizeof(VkVertex);
    memcpy(data + offset, indices.data(), indices.size() * sizeof(uint32_t));

    // Unmap memory.
    vkUnmapMemory(device->logical, buffer.memory);
}
Beispiel #8
0
void VulkanGear::updateUniformBuffer(glm::mat4 perspective, glm::vec3 rotation, float zoom, float timer)
{
	ubo.projection = perspective;

	ubo.view = glm::lookAt(
		glm::vec3(0, 0, -zoom),
		glm::vec3(-1.0, -1.5, 0),
		glm::vec3(0, 1, 0)
		);
	ubo.view = glm::rotate(ubo.view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
	ubo.view = glm::rotate(ubo.view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));

	ubo.model = glm::mat4();
	ubo.model = glm::translate(ubo.model, pos);
	rotation.z = (rotSpeed * timer) + rotOffset;
	ubo.model = glm::rotate(ubo.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

	ubo.normal = glm::inverseTranspose(ubo.view * ubo.model);

	ubo.lightPos = glm::vec3(0.0f, 0.0f, 2.5f);
	ubo.lightPos.x = sin(glm::radians(timer)) * 8.0f;
	ubo.lightPos.z = cos(glm::radians(timer)) * 8.0f;

	uint8_t *pData;
	VK_CHECK_RESULT(vkMapMemory(device, uniformData.memory, 0, sizeof(ubo), 0, (void **)&pData));
	memcpy(pData, &ubo, sizeof(ubo));
	vkUnmapMemory(device, uniformData.memory);
}
Beispiel #9
0
void op3d::Engine::createIndexBuffer()
{
    VkDeviceSize bufferSize = sizeof(indices[0]) * indices.size();

    VDeleter<VkBuffer> stagingBuffer{device, vkDestroyBuffer};
    VDeleter<VkDeviceMemory> stagingBufferMemory{device, vkFreeMemory};
    createBuffer(bufferSize,
                 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
                 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
                 stagingBuffer,
                 stagingBufferMemory);

    void* data;
    vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
    memcpy(data, indices.data(), (std::size_t)bufferSize);
    vkUnmapMemory(device, stagingBufferMemory);

    createBuffer(bufferSize,
                 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
                 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
                 indexBuffer,
                 indexBufferMemory);

    copyBuffer(stagingBuffer, indexBuffer, bufferSize);
}
Beispiel #10
0
	void updateUniformBuffers()
	{
		uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f);

		uboVS.view = glm::lookAt(
			glm::vec3(0, 0, -zoom),
			glm::vec3(0, 0, 0),
			glm::vec3(0, 1, 0)
			);

		uboVS.model = glm::mat4();
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
		uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

		uboVS.normal = glm::inverseTranspose(uboVS.view * uboVS.model);

		uboVS.lightPos = lightPos;

		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformData.meshVS.memory, 0, sizeof(uboVS), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboVS, sizeof(uboVS));
		vkUnmapMemory(device, uniformData.meshVS.memory);
	}
void vkeGameRendererDynamic::setMaterialData(VkeMaterial::List *inData){
	m_materials = inData;

	if (m_materials != NULL){

		uint32_t cnt = m_materials->count();
		uint32_t sz = sizeof(VkeMaterialUniform) * cnt;

		VkBufferUsageFlags usageFlags = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;

		bufferCreate(&m_material_buffer_staging, sz, (VkBufferUsageFlagBits)usageFlags);
		bufferAlloc(&m_material_buffer_staging, &m_material_staging, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT);

		VkeMaterialUniform *uniforms = NULL;

		VKA_CHECK_ERROR(vkMapMemory(getDefaultDevice(), m_material_staging, 0, sz, 0, (void **)&uniforms), "Could not map buffer memory.\n");

		for (uint32_t i = 0; i < cnt; ++i){
			VkeMaterial *mat = m_materials->getMaterial(i);
			mat->initVKBufferData(m_material_buffer_staging);
			mat->updateVKBufferData(uniforms);
		}

		vkUnmapMemory(getDefaultDevice(), m_material_staging);
	}

}
void VertexBuffer::uploadData(std::vector<Vertex>& vertexData) {
	//create the buffer
	VkBufferCreateInfo createInfo = {};
	createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
	createInfo.size = sizeof(Vertex) * vertexData.size();
	createInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
	createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
	if (vkCreateBuffer(deviceHelper.getDevice(), &createInfo, nullptr, buffer.replace()) != VK_SUCCESS) {
		throw std::runtime_error("Could not create buffer.");
	}
	VkMemoryRequirements memReqs = {};
	vkGetBufferMemoryRequirements(deviceHelper.getDevice(), buffer, &memReqs);
	//allocate the memory
	VkMemoryAllocateInfo allocInfo = {};
	allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
	allocInfo.allocationSize = memReqs.size;
	allocInfo.memoryTypeIndex = deviceHelper.findMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
	if (vkAllocateMemory(deviceHelper.getDevice(), &allocInfo, nullptr, bufferMemory.replace()) != VK_SUCCESS) {
		throw std::runtime_error("Could not allocate buffer memory.");
	}
	vkBindBufferMemory(deviceHelper.getDevice(), buffer, bufferMemory, 0);
	//upload the data
	void* dataPtr;
	vkMapMemory(deviceHelper.getDevice(), bufferMemory, 0, createInfo.size, 0, &dataPtr);
	memcpy(dataPtr, vertexData.data(), createInfo.size);
	vkUnmapMemory(deviceHelper.getDevice(), bufferMemory);

}
Beispiel #13
0
bool game_buffer_editor_record_vulkan_commands(game_buffer *game_buffer, vulkan *vulkan) {
  game_buffer_editor *editor = game_buffer->editor;
  ImDrawData *draw_data = ImGui::GetDrawData();
  VkResult vk_result = {};
  {
    uint64 vertices_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
    uint64 indices_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
    uint64 map_size = round_to_multi(vertices_size + indices_size, vulkan->physical_device_non_coherent_atom_size);
    assert(map_size <= editor->imgui_vertex_index_vulkan_buffer.size);
    uint8 *buf_ptr = nullptr;
    if ((vk_result = vkMapMemory(vulkan->device, editor->imgui_vertex_index_vulkan_buffer.device_memory, 0, map_size, 0, (void **)&buf_ptr)) != VK_SUCCESS) {
      return false;
    }
    assert((uintptr_t)buf_ptr % 16 == 0);
    for (int i = 0; i < draw_data->CmdListsCount; i += 1) {
      ImDrawList *dlist = draw_data->CmdLists[i];
      memcpy(buf_ptr, dlist->VtxBuffer.Data, dlist->VtxBuffer.Size * sizeof(ImDrawVert));
      buf_ptr += dlist->VtxBuffer.Size * sizeof(ImDrawVert);
    }
    for (int i = 0; i < draw_data->CmdListsCount; i += 1) {
      ImDrawList *dlist = draw_data->CmdLists[i];
      memcpy(buf_ptr, dlist->IdxBuffer.Data, dlist->IdxBuffer.Size * sizeof(ImDrawIdx));
      buf_ptr += dlist->IdxBuffer.Size * sizeof(ImDrawIdx);
    }
    VkMappedMemoryRange memory_range = { VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE };
    memory_range.memory = editor->imgui_vertex_index_vulkan_buffer.device_memory;
    memory_range.offset = map_size;
    if ((vk_result = vkFlushMappedMemoryRanges(vulkan->device, 1, &memory_range)) != VK_SUCCESS) {
      return false;
    }
    vkUnmapMemory(vulkan->device, editor->imgui_vertex_index_vulkan_buffer.device_memory);
  }
  vkCmdBindPipeline(vulkan->swap_chain_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, editor->imgui_vulkan_pipeline);
  VkViewport viewport = { 0, 0, (float)game_buffer->vulkan_framebuffer_image_width, (float)game_buffer->vulkan_framebuffer_image_height, 0, 1 };
  vkCmdSetViewport(vulkan->swap_chain_cmd_buffer, 0, 1, &viewport);
  vec2 push_consts = { (float)game_buffer->vulkan_framebuffer_image_width, (float)game_buffer->vulkan_framebuffer_image_height };
  vkCmdPushConstants(vulkan->swap_chain_cmd_buffer, editor->imgui_vulkan_pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(push_consts), &push_consts);
  VkDeviceSize vertex_offset = 0;
  VkDeviceSize index_offset = draw_data->TotalVtxCount * sizeof(ImDrawVert);
  for (int i = 0; i < draw_data->CmdListsCount; i += 1) {
    ImDrawList *dlist = draw_data->CmdLists[i];
    vkCmdBindVertexBuffers(vulkan->swap_chain_cmd_buffer, 0, 1, &editor->imgui_vertex_index_vulkan_buffer.buffer, &vertex_offset);
    vertex_offset += dlist->VtxBuffer.Size * sizeof(ImDrawVert);
    for (int i = 0; i < dlist->CmdBuffer.Size; i += 1) {
      ImDrawCmd *dcmd = &dlist->CmdBuffer.Data[i];
      VkRect2D scissor = { { (int)dcmd->ClipRect.x, (int)dcmd->ClipRect.y }, { (uint)dcmd->ClipRect.z, (uint)dcmd->ClipRect.w } };
      vkCmdSetScissor(vulkan->swap_chain_cmd_buffer, 0, 1, &scissor);
      if (dcmd->TextureId == editor->imgui_font_atlas_vulkan_image.image) {
        vkCmdBindDescriptorSets(vulkan->swap_chain_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, editor->imgui_vulkan_pipeline_layout, 0, 1, &editor->imgui_vulkan_descriptor_sets[0], 0, nullptr);
      }
      else {
        vkCmdBindDescriptorSets(vulkan->swap_chain_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, editor->imgui_vulkan_pipeline_layout, 0, 1, &editor->imgui_vulkan_descriptor_sets[1], 0, nullptr);
      }
      vkCmdBindIndexBuffer(vulkan->swap_chain_cmd_buffer, editor->imgui_vertex_index_vulkan_buffer.buffer, index_offset, VK_INDEX_TYPE_UINT16);
      vkCmdDrawIndexed(vulkan->swap_chain_cmd_buffer, dcmd->ElemCount, 1, 0, 0, 0);
      index_offset += dcmd->ElemCount * sizeof(ImDrawIdx);
    }
  }
  return true;
}
            bool VKMaterial::VUpdate() 
            {
                //TODO: Figure out what data is going into what buffers

                if (m_shaderVariables.size() == 0)
                    return true;

                VkDevice device = VKRenderer::RendererInstance->GetVKDevice();

                uint8_t* pData;
                
                std::vector<Math::Matrix4> variableList;

                std::map <std::string, ShaderVariable*>::iterator it;
                for (it = m_shaderVariables.begin(); it != m_shaderVariables.end(); it++)
                    variableList.push_back(*(Math::Matrix4*)(it->second->GetData()));
                
                VkResult err = vkMapMemory(device, m_uniformVSBuffer.memory, 0, sizeof(m_shaderVariables), 0, (void**)&pData);
                assert(!err);
                
                memcpy(pData, variableList.data(), sizeof(Math::Matrix4));

                vkUnmapMemory(device, m_uniformVSBuffer.memory);

                return true;
            }
	xdl_int XdevLVertexBufferVulkan::unmap() {
		assert(m_mapped && "XdevLVertexBufferVulkan::map: Was not mapped.");

		vkUnmapMemory(m_device, m_deviceMemory);
		m_mapped = xdl_false;
		return RET_SUCCESS;
	}
		/**
		* Unmap a mapped memory range
		*
		* @note Does not return a result as vkUnmapMemory can't fail
		*/
		void unmap()
		{
			if (mapped)
			{
				vkUnmapMemory(device, memory);
				mapped = nullptr;
			}
		}
	void destroyUniformData(VkDevice device, vkTools::UniformData * uniformData)
	{
		if (uniformData->mapped != nullptr) {
			vkUnmapMemory(device, uniformData->memory);
		}
		vkDestroyBuffer(device, uniformData->buffer, nullptr);
		vkFreeMemory(device, uniformData->memory, nullptr);
	}
Beispiel #18
0
void StagingTexture2DBuffer::Unmap()
{
  _assert_(m_map_pointer);

  vkUnmapMemory(g_vulkan_context->GetDevice(), m_memory);
  m_map_pointer = nullptr;
  m_map_offset = 0;
  m_map_size = 0;
}
Beispiel #19
0
	void VulkanImage::unmap()
	{
		VulkanDevice& device = mOwner->getDevice();

		VkDeviceMemory memory;
		VkDeviceSize memoryOffset;
		device.getAllocationInfo(mAllocation, memory, memoryOffset);

		vkUnmapMemory(device.getLogical(), memory);
	}
Beispiel #20
0
		lightPos.y = -50.0f + sin(glm::radians(timer * 360.0f)) * 20.0f;
		lightPos.z = 25.0f + sin(glm::radians(timer * 360.0f)) * 5.0f;
	}

	void updateUniformBuffers()
	{
		// Shadow map debug quad
		float AR = (float)height / (float)width;

		uboVSquad.projection = glm::ortho(0.0f, 2.5f / AR, 0.0f, 2.5f, -1.0f, 1.0f);
		uboVSquad.model = glm::mat4();

		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformDataVS.memory, 0, sizeof(uboVSquad), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboVSquad, sizeof(uboVSquad));
		vkUnmapMemory(device, uniformDataVS.memory);

		// 3D scene
		uboVSscene.projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, zNear, zFar);

		uboVSscene.view = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
		uboVSscene.view = glm::rotate(uboVSscene.view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
		uboVSscene.view = glm::rotate(uboVSscene.view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
		uboVSscene.view = glm::rotate(uboVSscene.view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

		uboVSscene.model = glm::mat4();

		uboVSscene.lightPos = lightPos;

		// Render scene from light's point of view
		if (lightPOV)
		{
			uboVSscene.projection = glm::perspective(glm::radians(lightFOV), (float)width / (float)height, zNear, zFar);
			uboVSscene.view = glm::lookAt(lightPos, glm::vec3(0.0f), glm::vec3(0, 1, 0));
		}

	
		uboVSscene.depthBiasMVP = uboOffscreenVS.depthMVP;

		pData;
		err = vkMapMemory(device, uniformData.scene.memory, 0, sizeof(uboVSscene), 0, (void **)&pData);
Beispiel #21
0
	void updateUniformBuffers()
	{
		computeUbo.deltaT = frameTimer * 5.0f;
		computeUbo.destX = sin(glm::radians(timer*360.0)) * 0.75f;
		computeUbo.destY = 0;
		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformData.computeShader.ubo.memory, 0, sizeof(computeUbo), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &computeUbo, sizeof(computeUbo));
		vkUnmapMemory(device, uniformData.computeShader.ubo.memory);
	}
	void updateUniformBuffers()
	{
		// Vertex shader
		glm::mat4 viewMatrix = glm::mat4();
		uboVS.projection = glm::perspective(deg_to_rad(60.0f), (float)width / (float)height, 0.1f, 256.0f);
		viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom));

		glm::mat4 rotMatrix = glm::mat4();
		rotMatrix = glm::rotate(rotMatrix, deg_to_rad(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
		rotMatrix = glm::rotate(rotMatrix, deg_to_rad(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
		rotMatrix = glm::rotate(rotMatrix, deg_to_rad(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

		uboVS.model = glm::mat4();
		uboVS.model = viewMatrix * rotMatrix;;

		uboVS.visible = 1.0f;

		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformData.vsScene.memory, 0, sizeof(uboVS), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboVS, sizeof(uboVS));
		vkUnmapMemory(device, uniformData.vsScene.memory);

		// teapot
		// Toggle color depending on visibility
		uboVS.visible = (passedSamples[0] > 0) ? 1.0f : 0.0f;
		uboVS.model = viewMatrix * rotMatrix * glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, -10.0f));
		err = vkMapMemory(device, uniformData.teapot.memory, 0, sizeof(uboVS), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboVS, sizeof(uboVS));
		vkUnmapMemory(device, uniformData.teapot.memory);

		// sphere
		// Toggle color depending on visibility
		uboVS.visible = (passedSamples[1] > 0) ? 1.0f : 0.0f;
		uboVS.model = viewMatrix * rotMatrix * glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, 10.0f));
		err = vkMapMemory(device, uniformData.sphere.memory, 0, sizeof(uboVS), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboVS, sizeof(uboVS));
		vkUnmapMemory(device, uniformData.sphere.memory);
	}
Beispiel #23
0
StreamBuffer::~StreamBuffer()
{
  g_command_buffer_mgr->RemoveFencePointCallback(this);

  if (m_host_pointer)
    vkUnmapMemory(g_vulkan_context->GetDevice(), m_memory);

  if (m_buffer != VK_NULL_HANDLE)
    g_command_buffer_mgr->DeferBufferDestruction(m_buffer);
  if (m_memory != VK_NULL_HANDLE)
    g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);
}
Beispiel #24
0
WError WMaterial::SaveToStream(WFile* file, std::ostream& outputStream) {
	if (!Valid())
		return WError(W_NOTVALID);

	VkDevice device = m_app->GetVulkanDevice();

	// write the UBO data
	uint tmp = m_uniformBuffers.size();
	outputStream.write((char*)&tmp, sizeof(tmp));
	for (uint i = 0; i < m_uniformBuffers.size(); i++) {
		UNIFORM_BUFFER_INFO* UBO = &m_uniformBuffers[i];
		outputStream.write((char*)&UBO->descriptor.range, sizeof(UBO->descriptor.range));
		void* data;
		VkResult vkRes = vkMapMemory(device, m_uniformBuffers[i].memory, 0, UBO->descriptor.range, 0, (void **)&data);
		if (vkRes)
			return WError(W_UNABLETOMAPBUFFER);
		outputStream.write((char*)data, UBO->descriptor.range);
		vkUnmapMemory(device, m_uniformBuffers[0].memory);
	}

	// write the texture data
	tmp = m_sampler_info.size();
	outputStream.write((char*)&tmp, sizeof(tmp));
	std::streampos texturesOffset = outputStream.tellp();
	for (uint i = 0; i < m_sampler_info.size(); i++) {
		SAMPLER_INFO* SI = &m_sampler_info[i];
		tmp = 0;
		outputStream.write((char*)&tmp, sizeof(tmp));
		outputStream.write((char*)&SI->sampler_info->binding_index, sizeof(SI->sampler_info->binding_index));
	}
	outputStream.write((char*)&tmp, sizeof(tmp)); // effect id
	_MarkFileEnd(file, outputStream.tellp());

	// save dependencies
	for (uint i = 0; i < m_sampler_info.size(); i++) {
		SAMPLER_INFO* SI = &m_sampler_info[i];
		if (SI->img) {
			WError err = file->SaveAsset(SI->img, &tmp);
			if (!err)
				return err;
			outputStream.seekp(texturesOffset + std::streamoff(i * (2 * sizeof(uint))));
			outputStream.write((char*)&tmp, sizeof(tmp));
		}
	}
	WError err = file->SaveAsset(m_effect, &tmp);
	if (!err)
		return err;
	outputStream.seekp(texturesOffset + std::streamoff(m_sampler_info.size() * (2 * sizeof(uint))));
	outputStream.write((char*)&tmp, sizeof(tmp));

	return WError(W_SUCCEEDED);
}
Beispiel #25
0
void VulkanPushBuffer::Unmap() {
	_dbg_assert_(G3D, writePtr_ != 0);
	/*
	// Should not need this since we use coherent memory.
	VkMappedMemoryRange range{ VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE };
	range.offset = 0;
	range.size = offset_;
	range.memory = buffers_[buf_].deviceMemory;
	vkFlushMappedMemoryRanges(device_, 1, &range);
	*/
	vkUnmapMemory(device_, buffers_[buf_].deviceMemory);
	writePtr_ = nullptr;
}
Beispiel #26
0
 void Buffer::unmap()
 {
     if (mpStagingResource)
     {
         mpStagingResource->unmap();
         mpStagingResource = nullptr;
     }
     // We only unmap staging buffers
     else if (mDynamicData.pData == nullptr && mBindFlags == BindFlags::None)
     {
         assert(mCpuAccess == CpuAccess::Read);
         vkUnmapMemory(gpDevice->getApiHandle(), mApiHandle);
     }
 }
Beispiel #27
0
void WImage::UnmapPixels() {
	VkDevice device = m_app->GetVulkanDevice();
	vkUnmapMemory(device, m_stagingMemory);

	if (!m_readOnlyMap) {
		VkResult err = m_app->BeginCommandBuffer();
		if (err)
			return;

		VkBufferImageCopy bufferCopyRegion = {};
		// Setup buffer copy regions for each mip level
		bufferCopyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
		bufferCopyRegion.imageSubresource.mipLevel = 0;
		bufferCopyRegion.imageSubresource.baseArrayLayer = 0;
		bufferCopyRegion.imageSubresource.layerCount = 1;
		bufferCopyRegion.imageExtent.width = m_width;
		bufferCopyRegion.imageExtent.height = m_height;
		bufferCopyRegion.imageExtent.depth = 1;
		bufferCopyRegion.bufferOffset = 0;

		// Image barrier for optimal image (target)
		// Optimal image will be used as destination for the copy
		vkTools::setImageLayout(
			m_app->GetCommandBuffer(),
			m_image,
			VK_IMAGE_ASPECT_COLOR_BIT,
			VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
			VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);

		// Copy mip levels from staging buffer
		vkCmdCopyBufferToImage(
			m_app->GetCommandBuffer(),
			m_stagingBuffer,
			m_image,
			VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
			1,
			&bufferCopyRegion
		);

		// Change texture image layout to shader read after all mip levels have been copied
		vkTools::setImageLayout(
			m_app->GetCommandBuffer(),
			m_image,
			VK_IMAGE_ASPECT_COLOR_BIT,
			VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
			VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);

		err = m_app->EndCommandBuffer();
	}
}
	void updateUniformBuffers()
	{
		uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f);

		uboVS.view = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
		uboVS.view = glm::rotate(uboVS.view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
		uboVS.view = glm::rotate(uboVS.view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
		uboVS.view = glm::rotate(uboVS.view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformData.vsScene.memory, 0, sizeof(uboVS), 0, (void **)&pData);
		assert(!err);
		memcpy(pData, &uboVS, sizeof(uboVS));
		vkUnmapMemory(device, uniformData.vsScene.memory);
	}
Beispiel #29
0
		memcpy(pData, &uboVSscene, sizeof(uboVSscene));
		vkUnmapMemory(device, uniformData.scene.memory);
	}

	void updateUniformBufferOffscreen()
	{
		// Matrix from light's point of view
		glm::mat4 depthProjectionMatrix = glm::perspective(glm::radians(lightFOV), 1.0f, zNear, zFar);
		glm::mat4 depthViewMatrix = glm::lookAt(lightPos, glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
		glm::mat4 depthModelMatrix = glm::mat4();

		uboOffscreenVS.depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;

		uint8_t *pData;
		VkResult err = vkMapMemory(device, uniformDataOffscreenVS.memory, 0, sizeof(uboOffscreenVS), 0, (void **)&pData);
	xdl_int XdevLVertexBufferVulkan::upload(xdl_uint8* src, xdl_uint size) {
		if(m_memReqs.size > size) {
			return RET_FAILED;
		}

		xdl_uint8* pData;
		VkResult result = vkMapMemory(m_device, m_deviceMemory, 0, m_memReqs.size, 0, (void **)&pData);
		if(VK_SUCCESS != result) {
			return RET_FAILED;
		}

		memcpy(pData, src, size);

		vkUnmapMemory(m_device, m_deviceMemory);
		return RET_FAILED;
	}