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); }
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); }
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); }
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); }
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; }
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); }
void VulkanImage::map(UINT32 face, UINT32 mipLevel, PixelData& output) const { VulkanDevice& device = mOwner->getDevice(); VkImageSubresource range; range.mipLevel = mipLevel; range.arrayLayer = face; if (mImageViewCI.subresourceRange.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; else // Depth stencil, but we only map depth range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; VkSubresourceLayout layout; vkGetImageSubresourceLayout(device.getLogical(), mImage, &range, &layout); const UINT32 pixelSize = PixelUtil::getNumElemBytes(output.getFormat()); assert((UINT32)layout.rowPitch % pixelSize == 0); assert((UINT32)layout.depthPitch % pixelSize == 0); output.setRowPitch((UINT32)layout.rowPitch / pixelSize); output.setSlicePitch((UINT32)layout.depthPitch / pixelSize); VkDeviceMemory memory; VkDeviceSize memoryOffset; device.getAllocationInfo(mAllocation, memory, memoryOffset); UINT8* data; VkResult result = vkMapMemory(device.getLogical(), memory, memoryOffset + layout.offset, layout.size, 0, (void**)&data); assert(result == VK_SUCCESS); output.setExternalBuffer(data); }
VkResult DeviceMemory::mapMemory(const VkDeviceSize offset, const VkDeviceSize size, const VkMemoryMapFlags flags) { if (!(memoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) { return VK_ERROR_MEMORY_MAP_FAILED; } if (mapped) { unmapMemory(); } auto result = vkMapMemory(device, deviceMemory, offset, size, flags, &data); if (result == VK_SUCCESS) { mapped = VK_TRUE; } else { data = nullptr; } return result; }
/** * 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; }
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); }
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 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); }
/** * 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); }
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; }
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); }
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; }
void *DeviceMemory::map(VkFlags flags) { void *data; if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL; return data; }
bool NvGLFWContextVK::readFramebufferRGBX32(uint8_t *dest, int32_t& w, int32_t& h) { NvVkRenderTarget& rt = *mainRenderTarget(); w = rt.width(); h = rt.height(); if (!dest) return true; VkFormat format = rt.targetFormat(); if (format == VK_FORMAT_R8G8B8A8_UNORM) { uint32_t size = 4 * w * h; VkBufferImageCopy region; region.bufferOffset = 0; region.bufferRowLength = 0; region.bufferImageHeight = 0; region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; region.imageSubresource.baseArrayLayer = 0; region.imageSubresource.layerCount = 1; region.imageSubresource.mipLevel = 0; region.imageOffset = { 0, 0, 0 }; region.imageExtent = { (uint32_t)w, (uint32_t)h, 1 }; NvVkBuffer dstBuffer; VkResult result = createAndFillBuffer(size, VK_BUFFER_USAGE_FLAG_BITS_MAX_ENUM, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, dstBuffer); VkCommandBuffer cmd = beginTempCmdBuffer(); vkCmdCopyImageToBuffer(cmd, rt.image(), VK_IMAGE_LAYOUT_GENERAL, dstBuffer.buffer, 1, ®ion); result = doneWithTempCmdBufferSubmit(cmd); vkDeviceWaitIdle(device()); uint8_t* ptr = NULL; result = vkMapMemory(device(), dstBuffer.mem, 0, size, 0, (void**)&ptr); uint32_t rowSize = w * 4; ptr += rowSize * (h - 1); for (int32_t i = 0; i < h; i++) { memcpy(dest, ptr, rowSize); dest += rowSize; ptr -= rowSize; } return true; } return false; }
XCamReturn VKDevice::map_mem (VkDeviceMemory mem, VkDeviceSize size, VkDeviceSize offset, void *&ptr) { XCAM_ASSERT (XCAM_IS_VALID_VK_ID (mem)); XCAM_VK_CHECK_RETURN ( ERROR, vkMapMemory (_dev_id, mem, offset, size, 0, &ptr), XCAM_RETURN_ERROR_VULKAN, "vk device map mem failed. size:%lld", size); return XCAM_RETURN_NO_ERROR; }
void* Tensor::map() { void *p; VK_CHECK_RESULT(vkMapMemory(device_, buffer_->getVkMemory(), 0, size_in_byte_, 0, (void **)&p)); return p; }
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);
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); }
xdl_uint8* XdevLVertexBufferVulkan::map(XdevLBufferAccessType bufferAccessType) { assert(m_locked && "XdevLVertexBufferVulkan::map: Was not locked."); assert(!m_mapped && "XdevLVertexBufferVulkan::map: Was mapped already."); xdl_uint8* pData; VkResult result = vkMapMemory(m_device, m_deviceMemory, 0, m_memReqs.size, 0, (void **)&pData); if(VK_SUCCESS != result) { return nullptr; } m_mapped = xdl_true; return pData; }
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); }
WError WImage::MapPixels(void** const pixels, bool bReadOnly) { if (!Valid() || !m_stagingMemory) return WError(W_NOTVALID); VkDevice device = m_app->GetVulkanDevice(); m_readOnlyMap = bReadOnly; VkResult err = vkMapMemory(device, m_stagingMemory, 0, m_mapSize, 0, pixels); if (err) return WError(W_NOTVALID); return WError(W_SUCCEEDED); }
auto mmap(std::uint64_t offset, std::uint64_t count) { munmap(); void *pdata = nullptr; const vk_result res = vkMapMemory(device.get(), *this, offset * sizeof(T), count * sizeof(T), 0, &pdata); if (!res) { throw vk_exception(res); } auto mmap = lib::allocate_unique<vk_mmap<T, host_allocator>>(*this, offset, count, reinterpret_cast<T*>(pdata)); mapped_memory = vk_mmap_type_eraser::create<T>(mmap.get()); return mmap; }
UINT8* VulkanImage::map(UINT32 offset, UINT32 size) const { VulkanDevice& device = mOwner->getDevice(); VkDeviceMemory memory; VkDeviceSize memoryOffset; device.getAllocationInfo(mAllocation, memory, memoryOffset); UINT8* data; VkResult result = vkMapMemory(device.getLogical(), memory, memoryOffset + offset, size, 0, (void**)&data); assert(result == VK_SUCCESS); return data; }
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);
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); }