[Piglit] [v2 3/7] ext_memory_object: Support for drawing with vulkan

Andres Rodriguez andresx7 at gmail.com
Tue Dec 19 01:11:59 UTC 2017



On 2017-12-18 08:17 AM, Topi Pohjolainen wrote:
> Signed-off-by: Topi Pohjolainen <topi.pohjolainen at intel.com>
> ---
>   tests/spec/ext_memory_object/vk_common.c | 322 +++++++++++++++++++++++++++++++
>   tests/spec/ext_memory_object/vk_common.h |  38 ++++
>   2 files changed, 360 insertions(+)
> 
> diff --git a/tests/spec/ext_memory_object/vk_common.c b/tests/spec/ext_memory_object/vk_common.c
> index ab871d501..2a02d058c 100644
> --- a/tests/spec/ext_memory_object/vk_common.c
> +++ b/tests/spec/ext_memory_object/vk_common.c
> @@ -243,3 +243,325 @@ vk_cleanup(void)
>   	if (inst != VK_NULL_HANDLE)
>   		vkDestroyInstance(inst, &test_alloc_cb);
>   }
> +
> +VkRenderPass
> +vk_create_render_pass(VkFormat format, unsigned num_samples,
> +		      VkImageLayout sub_pass_layout,
> +		      VkImageLayout final_layout)
> +{
> +        const VkRenderPassCreateInfo info = {
> +                .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
> +		.attachmentCount = 1,
> +		.pAttachments = (VkAttachmentDescription[]) { {
> +			.samples = (VkSampleCountFlagBits)num_samples,
> +    			.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
> +    			.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
> +    			.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
> +    			.finalLayout = final_layout,
> +			.format = format,
> +		}, },
> +		.subpassCount = 1,
> +		.pSubpasses = (VkSubpassDescription[]) { {
> +			.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
> +			.colorAttachmentCount = 1,
> +			.pColorAttachments = (VkAttachmentReference[]) {
> +			{
> +				.layout = sub_pass_layout,
> +			}, },
> +			.preserveAttachmentCount = 1,
> +			.pPreserveAttachments = (uint32_t[]) { 0 },
> +		} },
> +	};
> +
> +	VkRenderPass pass = VK_NULL_HANDLE;
> +    	if (vkCreateRenderPass(dev, &info, NULL, &pass) != VK_SUCCESS)
> +	        pass = VK_NULL_HANDLE;
> +
> +	return pass;
> +}
> +
> +VkShaderModule
> +vk_add_shader(const void *code, size_t size)
> +{
> +	const VkShaderModuleCreateInfo info = {
> +	     .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
> +	     .codeSize = size,
> +	     .pCode = code
> +	};
> +
> +	VkShaderModule module = VK_NULL_HANDLE;
> +	if (vkCreateShaderModule(dev, &info, NULL, &module) != VK_SUCCESS)
> +	        module = VK_NULL_HANDLE;
> +
> +	return module;
> +}
> +
> +VkPipeline
> +vk_create_pipeline(unsigned w, unsigned h, unsigned num_samples,
> +		   VkPrimitiveTopology topology,
> +		   unsigned attr_stride, VkFormat attr_format,
> +		   VkRenderPass render_pass,
> +		   VkShaderModule fs, VkShaderModule vs)
> +{
> +        const VkPipelineVertexInputStateCreateInfo vi_info = {
> +		.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
> +		.vertexBindingDescriptionCount = 1,
> +		.pVertexBindingDescriptions =
> +			(VkVertexInputBindingDescription[]) { {
> +				.binding = 0,
> +				.stride = attr_stride,
> +				.inputRate = VK_VERTEX_INPUT_RATE_VERTEX
> +		}, },
> +		.vertexAttributeDescriptionCount = 1,
> +		.pVertexAttributeDescriptions =
> +			(VkVertexInputAttributeDescription[]) { {
> +				.location = 0,
> +				.binding = 0,
> +				.format = attr_format,
> +				.offset = 0
> +		}, }
> +	};
> +	const VkPipelineInputAssemblyStateCreateInfo ia_info = {
> +		.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
> +		.topology = topology,
> +		.primitiveRestartEnable = false
> +	};
> +	const VkViewport viewport = { 0.0, 0.0, w, h, 0.0, 1.0 };
> +	const VkRect2D scissor = { { 0, 0 }, {w, h } };
> +	const VkPipelineViewportStateCreateInfo vp_info = {
> +		.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
> +		.viewportCount = 1,
> +		.pViewports = &viewport,
> +		.scissorCount = 1,
> +		.pScissors = &scissor
> +	};
> +	const VkPipelineRasterizationStateCreateInfo rs_info = {
> +		.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
> +    		.depthClampEnable = false,
> +		.rasterizerDiscardEnable = false,
> +		.polygonMode = VK_POLYGON_MODE_FILL,
> +		.cullMode = VK_CULL_MODE_NONE,
> +		.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
> +		.depthBiasConstantFactor = 0.0f,
> +		.depthBiasClamp = 0.0f,
> +		.depthBiasSlopeFactor = 0.0f,
> +		.lineWidth = 1.0f
> +	};
> +	const VkPipelineMultisampleStateCreateInfo ms_info = {
> +		.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
> +		.rasterizationSamples = num_samples
> +	};
> +	const VkPipelineDepthStencilStateCreateInfo ds_info = {
> +		.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
> +		.depthTestEnable = false,
> +		.depthWriteEnable = false,
> +		.depthBoundsTestEnable = false,
> +		.stencilTestEnable = false,
> +		.front = {
> +			.compareMask = ~0,  /* default in OpenGL ES 3.1 */
> +			.writeMask = ~0,    /* default in OpenGL ES 3.1 */
> +			.reference = 0,     /* default in OpenGL ES 3.1 */
> +		},
> +		.back = {
> +			.compareMask = ~0,  /* default in OpenGL ES 3.1 */
> +			.writeMask = ~0,    /* default in OpenGL ES 3.1 */
> +			.reference = 0,     /* default in OpenGL ES 3.1 */
> +		},
> +		.minDepthBounds = 0.0f, /* default in OpenGL ES 3.1 */
> +		.maxDepthBounds = 1.0f  /* default in OpenGL ES 3.1 */
> +	};
> +	const VkPipelineColorBlendStateCreateInfo cb_info = {
> +		.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
> +    		.attachmentCount = 1,
> +    		.pAttachments = (VkPipelineColorBlendAttachmentState []) { {
> +			.blendEnable = false,
> +    			.colorWriteMask = (VK_COLOR_COMPONENT_R_BIT |
> +					   VK_COLOR_COMPONENT_G_BIT |
> +					   VK_COLOR_COMPONENT_B_BIT |
> +					   VK_COLOR_COMPONENT_A_BIT)
> +    		} },
> +    		.blendConstants = {0.0f, 0.0f, 0.0f, 0.0f} /* default in OpenGL ES 3.1 */
> +	};
> +	const VkPipelineShaderStageCreateInfo stage_info[] = {
> +		{
> +			.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
> +                	.stage = VK_SHADER_STAGE_VERTEX_BIT,
> +                	.module = vs,
> +                	.pName = "main",
> +		},
> +		{
> +			.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
> +			.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
> +			.module = fs,
> +			.pName = "main",
> +		}
> +	};
> +	const VkGraphicsPipelineCreateInfo pipeline_info = {
> +		.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
> +		.pInputAssemblyState = &ia_info,
> +		.pViewportState = &vp_info,
> +		.pRasterizationState = &rs_info,
> +		.pMultisampleState = &ms_info,
> +		.pDepthStencilState = &ds_info,
> +		.pColorBlendState = &cb_info,
> +		.stageCount = ARRAY_SIZE(stage_info),
> +		.pStages = stage_info,
> +		.pVertexInputState = &vi_info,
> +		.renderPass = render_pass,
> +		.subpass = 0,
> +	};
> +
> +	VkPipeline pipeline = VK_NULL_HANDLE;
> +	if (vkCreateGraphicsPipelines(dev, pipeline_cache, 1, &pipeline_info,
> +				      NULL, &pipeline) != VK_SUCCESS)
> +		pipeline = VK_NULL_HANDLE;
> +
> +	return pipeline;
> +}
> +
> +static uint32_t
> +vk_get_memory_type_index(const VkMemoryRequirements *mem_reqs,
> +			 VkMemoryPropertyFlagBits properties)
> +{
> +	VkPhysicalDeviceMemoryProperties props;
> +
> +	vkGetPhysicalDeviceMemoryProperties(phys_dev, &props);
> +
> +	for (uint32_t i = 0; i < props.memoryTypeCount; i++) {
> +		const VkMemoryType *type = &props.memoryTypes[i];
> +
> +		if ((mem_reqs->memoryTypeBits & (1 << i)) &&
> +		    (type->propertyFlags & properties) == properties) {
> +			return i;
> +			break;
> +		}
> +	}
> +
> +	return UINT32_MAX;
> +}
> +
> +VkDeviceMemory
> +vk_alloc_mem(VkDevice dev, const VkMemoryRequirements *mem_regs,

s/mem_regs/mem_reqs

> +	     VkMemoryPropertyFlagBits props)
> +{
> +	const VkMemoryAllocateInfo info = {
> +		.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
> +                .allocationSize = mem_regs->size,
> +		.memoryTypeIndex = vk_get_memory_type_index(mem_regs, props),
> +	};
> +	VkDeviceMemory mem = VK_NULL_HANDLE;
> +
> +	if (info.memoryTypeIndex == UINT32_MAX)
> +		return VK_NULL_HANDLE;
> +
> +	if (vkAllocateMemory(dev, &info, NULL, &mem) != VK_SUCCESS ||
> +	    mem == VK_NULL_HANDLE)
> +		return VK_NULL_HANDLE;
> +
> +	return mem;
> +}
> +
> +static VkDeviceMemory
> +vk_alloc_buffer(VkDevice dev, VkBuffer buffer, VkMemoryPropertyFlagBits props)
> +{
> +	VkMemoryRequirements mem_regs;

See above.

> +	vkGetBufferMemoryRequirements(dev, buffer, &mem_regs);
> +
> +	return vk_alloc_mem(dev, &mem_regs, props);
> +}
> +
> +void
> +vk_setup_vertex_buffer(const void *vertices, unsigned size,
> +		       struct vk_vertex_buffer *vb)
> +{
> +	const VkBufferCreateInfo info = {
> +		.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
> +		.size = size,
> +		.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
> +	};
> +	void *map;
> +
> +	vb->buf = VK_NULL_HANDLE;
> +	vb->mem = VK_NULL_HANDLE;
> +
> +	if (vkCreateBuffer(dev, &info, NULL, &vb->buf) != VK_SUCCESS ||
> +	    vb->buf == VK_NULL_HANDLE)
> +		goto fail;
> +
> +	vb->mem = vk_alloc_buffer(
> +			dev, vb->buf, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
> +	if (vb->mem == VK_NULL_HANDLE)
> +		goto fail;
> +
> +	if (vkBindBufferMemory(dev, vb->buf, vb->mem, 0) != VK_SUCCESS)
> +		goto fail;
> +
> +	if (vkMapMemory(dev, vb->mem, 0, info.size, 0, &map) != VK_SUCCESS)
> +		goto fail;
> +
> +	memcpy(map, vertices, info.size);


map is leaked here.

> +
> +	return;
> +
> +fail:
> +	if (vb->mem != VK_NULL_HANDLE)
> +		vkFreeMemory(dev, vb->mem, NULL);
> +
> +	if (vb->buf != VK_NULL_HANDLE)
> +		vkDestroyBuffer(dev, vb->buf, NULL);
> +	
> +	vb->buf = VK_NULL_HANDLE;
> +	vb->mem = VK_NULL_HANDLE;
> +}
> +
> +void
> +vk_begin_render_pass(VkRenderPass render_pass, VkFramebuffer fb,
> +		     unsigned w, unsigned h)
> +{
> +	const VkRenderPassBeginInfo info = {
> +		.renderPass = render_pass,
> +		.framebuffer = fb,
> +		.renderArea = { { 0, 0 }, { w, h} },
> +		.clearValueCount = 1,
> +		.pClearValues = (VkClearValue[]) {
> +			{ .color = { .float32 = {1.0, 0.0, 0.0, 1.0} } },
> +		}
> +	};
> +
> +	vkCmdBeginRenderPass(cmd_buf, &info, VK_SUBPASS_CONTENTS_INLINE);
> +}
> +
> +bool
> +vk_draw(VkPipeline pipeline, VkBuffer vb, VkFence fence)
> +{
> +	const VkSubmitInfo submit_info = {
> +		.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
> +		.commandBufferCount = 1,
> +		.pCommandBuffers = &cmd_buf,
> +	};
> +	const VkDeviceSize dev_size = 0;
> +
> +	vkCmdBindVertexBuffers(cmd_buf, 0, 1, &vb, &dev_size);
> +    	vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
> +    	vkCmdDraw(cmd_buf, 4, 1, 0, 0);
> +	vkCmdEndRenderPass(cmd_buf);
> +
> +	if (vkEndCommandBuffer(cmd_buf) != VK_SUCCESS)
> +		return false;
> +
> +	return vkQueueSubmit(queue, 1, &submit_info, fence) == VK_SUCCESS;
> +}
> +
> +VkFence
> +vk_create_fence(VkDevice dev)
> +{
> +        const VkFenceCreateInfo info = {
> +                .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
> +        };
> +        VkFence fence = VK_NULL_HANDLE;
> +
> +        if (vkCreateFence(dev, &info, NULL, &fence) != VK_SUCCESS)
> +                fence = VK_NULL_HANDLE;
> +
> +        return fence;
> +}
> diff --git a/tests/spec/ext_memory_object/vk_common.h b/tests/spec/ext_memory_object/vk_common.h
> index 8ddf9c8be..590735253 100644
> --- a/tests/spec/ext_memory_object/vk_common.h
> +++ b/tests/spec/ext_memory_object/vk_common.h
> @@ -29,10 +29,48 @@
>   #define VK_PROTOTYPES
>   #include <vulkan/vulkan.h>
>   
> +struct vk_vertex_buffer {
> +	VkBuffer buf;
> +	VkDeviceMemory mem;
> +};
> +
>   VkDevice
>   vk_init(void);
>   
>   void
>   vk_cleanup(void);
>   
> +VkRenderPass
> +vk_create_render_pass(VkFormat format, unsigned num_samples,
> +		      VkImageLayout sub_pass_layout,
> +		      VkImageLayout final_layout);
> +
> +VkShaderModule
> +vk_add_shader(const void *code, size_t size);
> +
> +VkPipeline
> +vk_create_pipeline(unsigned w, unsigned h, unsigned num_samples,
> +		   VkPrimitiveTopology topology,
> +		   unsigned attr_stride, VkFormat attr_format,
> +		   VkRenderPass render_pass,
> +		   VkShaderModule fs, VkShaderModule vs);
> +
> +VkDeviceMemory
> +vk_alloc_mem(VkDevice dev, const VkMemoryRequirements *mem_reqs,
> +	     VkMemoryPropertyFlagBits props);
> +
> +void
> +vk_setup_vertex_buffer(const void *vertices, unsigned size,
> +		       struct vk_vertex_buffer *vb);
> +
> +void
> +vk_begin_render_pass(VkRenderPass render_pass, VkFramebuffer fb,
> +		     unsigned w, unsigned h);
> +
> +bool
> +vk_draw(VkPipeline pipeline, VkBuffer vb, VkFence fence);
> +
> +VkFence
> +vk_create_fence(VkDevice dev);
> +
>   #endif
> 


More information about the Piglit mailing list