Buffer CreateUniformBuffer(PhysicalDevice physicalDevice) { var uniformBufferData = new AreaUniformBuffer { width = surfaceCapabilities.CurrentExtent.Width, height = surfaceCapabilities.CurrentExtent.Height }; return(CreateBuffer(physicalDevice, uniformBufferData, BufferUsageFlags.UniformBuffer, typeof(AreaUniformBuffer))); }
public void UpdateSize() { if (width == originalWidth) { width = originalWidth / 2; height = originalHeight / 2; } else { width = originalWidth; height = originalHeight; } var uniformBufferData = new AreaUniformBuffer(width, height); var length = 1; var size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(AreaUniformBuffer)) * length; VkDevice device = this.device; VkDeviceMemory deviceMemory = this.vkDeviceMemory; { IntPtr memPtr = IntPtr.Zero; vkAPI.vkMapMemory(device, deviceMemory, 0, (UInt64)size, 0, &memPtr).Check(); System.Runtime.InteropServices.Marshal.StructureToPtr(uniformBufferData, memPtr, false); //GCHandle pin = GCHandle.Alloc(uniformBufferData, GCHandleType.Pinned); //IntPtr address = pin.AddrOfPinnedObject(); //var src = (byte*)address; //var dst = (byte*)memPtr; //for (int i = 0; i < size; i++) { // dst[i] = src[i]; //} //pin.Free(); //// Flush to make changes visible to the host //var memoryRange = new VkMappedMemoryRange(); //{ // memoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; // memoryRange.memory = deviceMemory; // memoryRange.size = this.uniformSize; //} //vkAPI.vkFlushMappedMemoryRanges(device, 1, &memoryRange); vkAPI.vkUnmapMemory(device, deviceMemory); } }
public void Init(IntPtr hwnd, IntPtr processHandle) { if (this.isInitialized) { return; } this.instance = InitInstance(); InitDebugCallback(this.instance); this.surface = InitSurface(this.instance, hwnd, processHandle); this.vkPhysicalDevice = InitPhysicalDevice(this.instance); VkSurfaceFormatKHR surfaceFormat = SelectFormat(this.vkPhysicalDevice, this.surface); this.device = CreateDevice(this.vkPhysicalDevice, this.surface); this.vkQueue = this.device.GetQueue(0, 0); VkSurfaceCapabilitiesKHR surfaceCapabilities; //this.vkPhysicalDevice.GetSurfaceCapabilitiesKhr(this.vkSurface, out surfaceCapabilities); vkAPI.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this.vkPhysicalDevice, this.surface, &surfaceCapabilities).Check(); this.swapchain = CreateSwapchain(this.device, this.surface, surfaceFormat, surfaceCapabilities); this.vkImages = this.device.GetSwapchainImages(this.swapchain); this.renderPass = CreateRenderPass(this.device, surfaceFormat); this.framebuffers = CreateFramebuffers(this.device, this.vkImages, surfaceFormat, this.renderPass, surfaceCapabilities); this.vkFence = this.device.CreateFence(); this.vkSemaphore = this.device.CreateSemaphore(); // buffers for vertex data. VkBuffer vertexBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, Vertices, VkBufferUsageFlagBits.VertexBuffer, typeof(float)); VkBuffer indexBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, Indexes, VkBufferUsageFlagBits.IndexBuffer, typeof(short)); var uniformBufferData = new AreaUniformBuffer(1, 1); this.originalWidth = 1; this.width = this.originalWidth; this.originalHeight = 1; this.height = this.originalHeight; this.uniformBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, uniformBufferData, VkBufferUsageFlagBits.UniformBuffer, typeof(AreaUniformBuffer)); this.descriptorSetLayout = CreateDescriptorSetLayout(this.device); this.vkPipelineLayout = CreatePipelineLayout(this.device, this.descriptorSetLayout); VkPipeline pipeline = CreatePipeline(this.device, surfaceCapabilities, this.renderPass, this.vkPipelineLayout); this.descriptorSet = CreateDescriptorSet(this.device, this.descriptorSetLayout); UpdateDescriptorSets(this.device, this.uniformBuffer, this.descriptorSet); this.commandBuffers = CreateCommandBuffers( this.device, this.renderPass, surfaceCapabilities, this.vkImages, this.framebuffers, pipeline, vertexBuffer, indexBuffer, (uint)Indexes.Length, this.vkPipelineLayout, this.descriptorSet); this.isInitialized = true; }