2019-04-30 23:00:39 +02:00
# pragma once
2018-08-26 15:11:43 +02:00
# include <drm/drm.h>
# include <drm/drm_fourcc.h>
# include <drm/vc4_drm.h>
# include <vulkan/vulkan.h>
# include "vkExt.h"
# include "AlignedAllocator.h"
# include "PoolAllocator.h"
# include "ConsecutivePoolAllocator.h"
# include "LinearAllocator.h"
2019-05-02 23:00:23 +02:00
# include "map.h"
2018-08-26 15:11:43 +02:00
# include <stdio.h>
# include "CustomAssert.h"
# include <string.h>
# include <stdlib.h>
# include <unistd.h>
# include <stdint.h>
# include <pthread.h>
# include <semaphore.h>
# include "kernelInterface.h"
# include "ControlListUtil.h"
# ifndef min
# define min(a, b) (a < b ? a : b)
# endif
# ifndef max
# define max(a, b) (a > b ? a : b)
# endif
# include "vkCaps.h"
2018-11-17 16:53:13 +01:00
/**
//scope
VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
VK_SYSTEM_ALLOCATION_SCOPE_CACHE
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
* */
2019-04-30 23:00:39 +02:00
# define ALLOCATE(size, alignment, scope) (pAllocator == 0) ? malloc(size) : pAllocator->pfnAllocation(pAllocator->pUserData, size, alignment, scope)
# define FREE(memory) (pAllocator == 0) ? free(memory) : pAllocator->pfnFree(pAllocator->pUserData, memory)
2018-11-17 16:53:13 +01:00
2019-09-07 18:41:46 +02:00
# define UNSUPPORTED(str) fprintf(stderr, "Unsupported: %s\n", str); exit(-1)
2018-11-16 20:39:33 +01:00
typedef struct VkDevice_T _device ;
2018-08-26 15:11:43 +02:00
typedef struct VkQueue_T
{
uint64_t lastEmitSeqno ;
2018-11-16 20:39:33 +01:00
_device * dev ;
2018-08-26 15:11:43 +02:00
} _queue ;
typedef struct VkCommandPool_T
{
PoolAllocator pa ;
ConsecutivePoolAllocator cpa ;
uint32_t queueFamilyIndex ;
2019-09-07 18:41:46 +02:00
uint32_t resetAble ;
2018-08-26 15:11:43 +02:00
} _commandPool ;
typedef enum commandBufferState
{
CMDBUF_STATE_INITIAL = 0 ,
CMDBUF_STATE_RECORDING ,
CMDBUF_STATE_EXECUTABLE ,
CMDBUF_STATE_PENDING ,
CMDBUF_STATE_INVALID ,
CMDBUF_STATE_LAST
} commandBufferState ;
2018-11-16 20:39:33 +01:00
typedef struct VkInstance_T _instance ;
typedef struct VkPhysicalDevice_T
{
//hardware id?
char * path ;
_instance * instance ;
} _physicalDevice ;
2018-08-26 15:11:43 +02:00
typedef struct VkInstance_T
{
2018-11-16 20:39:33 +01:00
_physicalDevice dev ;
2018-08-26 15:11:43 +02:00
//supposedly this should contain all the enabled layers?
int enabledExtensions [ numInstanceExtensions ] ;
int numEnabledExtensions ;
int chipVersion ;
int hasTiling ;
int hasControlFlow ;
int hasEtc1 ;
int hasThreadedFs ;
int hasMadvise ;
} _instance ;
typedef struct VkDevice_T
{
int enabledExtensions [ numDeviceExtensions ] ;
int numEnabledExtensions ;
VkPhysicalDeviceFeatures enabledFeatures ;
_physicalDevice * dev ;
_queue * queues [ numQueueFamilies ] ;
int numQueues [ numQueueFamilies ] ;
} _device ;
2018-09-09 16:45:07 +02:00
typedef struct VkRenderPass_T
{
2018-09-18 22:22:43 +02:00
//collection of:
//attachments, subpasses, dependencies between subpasses
//describes how attachments are used in subpasses
//attachment describes:
//format, sample count, how contents are treated at start/end of a renderpass
//subpasses render to same dimensions and fragments
//framebuffer objects specify views for attachements
VkAttachmentDescription * attachments ;
uint32_t numAttachments ;
VkSubpassDescription * subpasses ;
uint32_t numSubpasses ;
VkSubpassDependency * subpassDependencies ;
uint32_t numSubpassDependencies ;
2018-09-09 16:45:07 +02:00
} _renderpass ;
typedef struct VkDeviceMemory_T
{
uint32_t size ;
uint32_t bo ;
uint32_t memTypeIndex ;
void * mappedPtr ;
uint32_t mappedOffset , mappedSize ;
} _deviceMemory ;
typedef struct VkBuffer_T
{
uint32_t size ;
VkBufferUsageFlags usage ;
_deviceMemory * boundMem ;
uint32_t boundOffset ;
uint32_t alignment ;
uint32_t alignedSize ;
} _buffer ;
2018-09-18 22:22:43 +02:00
typedef struct VkImage_T
{
2018-10-20 16:46:12 +02:00
VkImageType type ; //1d, 2d, 3d
2018-09-18 22:22:43 +02:00
uint32_t fb ; //needed for swapchain
uint32_t width , height , depth ;
uint32_t paddedWidth , paddedHeight ;
uint32_t miplevels , samples ;
uint32_t layers ; //number of views for multiview/stereo
2018-10-20 16:46:12 +02:00
uint32_t size ; //overall size including padding and alignment
2018-09-18 22:22:43 +02:00
uint32_t stride ; //the number of bytes from one row of pixels in memory to the next row of pixels in memory (aka pitch)
uint32_t usageBits ;
uint32_t format ;
uint32_t imageSpace ;
2018-10-20 16:46:12 +02:00
uint32_t tiling ; //T or LT
2018-09-18 22:22:43 +02:00
uint32_t needToClear ;
uint32_t clearColor [ 2 ] ;
uint32_t layout ;
2018-10-20 16:46:12 +02:00
_deviceMemory * boundMem ;
uint32_t boundOffset ;
uint32_t alignment ;
2018-09-18 22:22:43 +02:00
uint32_t concurrentAccess ; //TODO
uint32_t numQueueFamiliesWithAccess ;
uint32_t * queueFamiliesWithAccess ;
uint32_t preTransformMode ;
uint32_t compositeAlpha ;
uint32_t presentMode ;
uint32_t clipped ;
} _image ;
typedef struct VkImageView_T
{
_image * image ;
VkImageViewType viewType ;
VkFormat interpretedFormat ;
VkComponentMapping swizzle ;
VkImageSubresourceRange subresourceRange ;
} _imageView ;
typedef struct VkSwapchain_T
{
_image * images ;
uint32_t numImages ;
uint32_t backbufferIdx ;
VkSurfaceKHR surface ;
} _swapchain ;
typedef struct VkFramebuffer_T
{
_renderpass * renderpass ;
_imageView * attachmentViews ;
uint32_t numAttachmentViews ;
uint32_t width , height , layers ;
} _framebuffer ;
2019-08-18 18:36:57 +02:00
typedef enum RpiAssemblyType {
RPI_ASSEMBLY_TYPE_COORDINATE = 0 ,
RPI_ASSEMBLY_TYPE_VERTEX = 1 ,
RPI_ASSEMBLY_TYPE_FRAGMENT = 2 ,
RPI_ASSEMBLY_TYPE_COMPUTE = 3 ,
RPI_ASSEMBLY_TYPE_MAX ,
} RpiAssemblyType ;
2018-09-18 22:22:43 +02:00
typedef struct VkShaderModule_T
{
2019-08-18 18:36:57 +02:00
uint32_t bos [ RPI_ASSEMBLY_TYPE_MAX ] ;
uint32_t sizes [ RPI_ASSEMBLY_TYPE_MAX ] ;
VkRpiAssemblyMappingEXT * mappings ;
uint32_t numMappings ;
2019-08-25 22:59:18 +02:00
uint32_t hasThreadSwitch ;
2019-09-21 00:05:19 +02:00
uint32_t numVaryings ;
2018-09-18 22:22:43 +02:00
} _shaderModule ;
2019-07-27 22:57:13 +02:00
typedef struct VkDescriptorSetLayout_T
{
//an array of zero or more descriptor bindings
VkDescriptorSetLayoutBinding * bindings ;
uint32_t bindingsCount ;
VkDescriptorSetLayoutCreateFlags flags ;
} _descriptorSetLayout ;
2019-07-08 22:10:22 +02:00
typedef struct VkPipelineLayout_T
{
map descriptorSetBindingMap ;
uint32_t setLayoutCount ;
const _descriptorSetLayout * setLayouts ;
uint32_t pushConstantRangeCount ;
const VkPushConstantRange * pushConstantRanges ;
} _pipelineLayout ;
2018-09-23 21:55:30 +02:00
typedef struct VkPipeline_T
{
2019-07-08 22:10:22 +02:00
_shaderModule * modules [ 6 ] ;
2018-09-23 21:55:30 +02:00
char * names [ 6 ] ;
uint32_t vertexBindingDescriptionCount ;
VkVertexInputBindingDescription * vertexBindingDescriptions ;
uint32_t vertexAttributeDescriptionCount ;
VkVertexInputAttributeDescription * vertexAttributeDescriptions ;
VkPrimitiveTopology topology ;
VkBool32 primitiveRestartEnable ;
uint32_t viewportCount ;
VkViewport * viewports ;
uint32_t scissorCount ;
VkRect2D * scissors ;
VkBool32 depthClampEnable ;
VkBool32 rasterizerDiscardEnable ;
VkPolygonMode polygonMode ;
VkCullModeFlags cullMode ;
VkFrontFace frontFace ;
VkBool32 depthBiasEnable ;
float depthBiasConstantFactor ;
float depthBiasClamp ;
float depthBiasSlopeFactor ;
float lineWidth ;
VkSampleCountFlagBits rasterizationSamples ;
VkBool32 sampleShadingEnable ;
float minSampleShading ;
VkSampleMask sampleMask ;
VkBool32 alphaToCoverageEnable ;
VkBool32 alphaToOneEnable ;
VkBool32 depthTestEnable ;
VkBool32 depthWriteEnable ;
VkCompareOp depthCompareOp ;
VkBool32 depthBoundsTestEnable ;
VkBool32 stencilTestEnable ;
VkStencilOpState front ;
VkStencilOpState back ;
float minDepthBounds ;
float maxDepthBounds ;
VkBool32 logicOpEnable ;
VkLogicOp logicOp ;
uint32_t attachmentCount ;
VkPipelineColorBlendAttachmentState * attachmentBlendStates ;
float blendConstants [ 4 ] ;
uint32_t dynamicStateCount ;
VkDynamicState * dynamicStates ;
2019-07-08 22:10:22 +02:00
_pipelineLayout * layout ;
2018-09-23 21:55:30 +02:00
_renderpass * renderPass ;
uint32_t subpass ;
} _pipeline ;
2018-09-26 22:59:00 +02:00
typedef struct VkCommandBuffer_T
{
2019-08-19 23:12:51 +02:00
_device * dev ; //device from which it was created
2018-09-26 22:59:00 +02:00
//Recorded commands include commands to bind pipelines and descriptor sets to the command buffer, commands to modify dynamic state, commands to draw (for graphics rendering),
//commands to dispatch (for compute), commands to execute secondary command buffers (for primary command buffers only), commands to copy buffers and images, and other commands
2019-07-27 22:57:13 +02:00
//Rpi only supports vertex and pixel shaders
//(coordinate shaders will just use the vertex shader push constants)
//anything else will be ignored I guess
char pushConstantBufferVertex [ 256 ] ;
char pushConstantBufferPixel [ 256 ] ;
2018-09-26 22:59:00 +02:00
ControlList binCl ;
ControlList shaderRecCl ;
uint32_t shaderRecCount ;
ControlList uniformsCl ;
ControlList handlesCl ;
commandBufferState state ;
VkCommandBufferUsageFlags usageFlags ;
_commandPool * cp ;
VkRect2D renderArea ;
_renderpass * renderpass ;
_framebuffer * fbo ;
uint32_t currentSubpass ;
_pipeline * graphicsPipeline ;
_pipeline * computePipeline ;
2019-09-01 20:14:47 +02:00
uint32_t numDrawCallsSubmitted ;
2018-10-21 14:33:02 +02:00
uint32_t vertexBufferDirty ;
uint32_t indexBufferDirty ;
uint32_t viewportDirty ;
uint32_t lineWidthDirty ;
uint32_t depthBiasDirty ;
uint32_t graphicsPipelineDirty ;
uint32_t computePipelineDirty ;
uint32_t subpassDirty ;
uint32_t blendConstantsDirty ;
uint32_t scissorDirty ;
uint32_t depthBoundsDirty ;
uint32_t stencilCompareMaskDirty ;
uint32_t stencilWriteMaskDirty ;
uint32_t stencilReferenceDirty ;
uint32_t descriptorSetDirty ;
uint32_t pushConstantDirty ;
VkViewport viewport ;
VkRect2D scissor ;
float lineWidth ;
float depthBiasConstantFactor ;
float depthBiasClamp ;
float depthBiasSlopeFactor ;
float blendConstants [ 4 ] ;
float minDepthBounds ;
float maxDepthBounds ;
uint32_t stencilCompareMask [ 2 ] ;
uint32_t stencilWriteMask [ 2 ] ;
uint32_t stencilReference [ 2 ] ;
2018-09-26 22:59:00 +02:00
uint32_t vertexBufferOffsets [ 8 ] ;
_buffer * vertexBuffers [ 8 ] ;
} _commandBuffer ;
2018-10-17 21:56:13 +02:00
typedef struct VkFence_T
{
uint64_t seqno ;
uint32_t signaled ;
} _fence ;
2018-10-18 22:17:02 +02:00
typedef struct VkBufferView_T
{
_buffer * buffer ;
VkFormat format ;
VkDeviceSize offset ;
VkDeviceSize range ;
} _bufferView ;
2019-04-30 23:00:39 +02:00
typedef struct VkSampler_T
{
2019-08-18 18:36:57 +02:00
VkFilter minFilter , magFilter ;
VkSamplerMipmapMode mipmapMode ;
2019-08-19 23:12:51 +02:00
VkSamplerAddressMode addressModeU , addressModeV , addressModeW ;
2019-08-18 18:36:57 +02:00
float mipLodBias ;
VkBool32 anisotropyEnable ;
float maxAnisotropy ;
VkBool32 compareEnable ;
VkCompareOp compareOp ;
float minLod , maxLod ;
VkBorderColor borderColor ;
VkBool32 unnormalizedCoordinates ;
2019-04-30 23:00:39 +02:00
} _sampler ;
typedef struct VkDescriptorImage_T
{
uint32_t count ;
VkDescriptorType type ;
VkShaderStageFlags stageFlags ;
_sampler * sampler ;
_imageView * imageView ;
VkImageLayout imageLayout ;
} _descriptorImage ;
typedef struct VkDescriptorBuffer_T
{
uint32_t count ;
VkDescriptorType type ;
VkShaderStageFlags stageFlags ;
_buffer * buffer ;
VkDeviceSize offset ;
VkDeviceSize range ;
} _descriptorBuffer ;
typedef struct VkDescriptorTexelBuffer_T
{
uint32_t count ;
VkDescriptorType type ;
VkShaderStageFlags stageFlags ;
_bufferView * bufferView ;
} _descriptorTexelBuffer ;
typedef struct VkDescriptorSet_T
{
//VkDescriptorSetLayoutCreateFlags flags;
2019-05-02 23:00:23 +02:00
map imageBindingMap ;
map bufferBindingMap ;
map texelBufferBindingMap ;
//pointers into CPAs
2019-04-30 23:00:39 +02:00
//VK_DESCRIPTOR_TYPE_SAMPLER, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
//VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
//VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
_descriptorImage * imageDescriptors ;
//VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
//VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, or VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
_descriptorBuffer * bufferDescriptors ;
//VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER or VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
_descriptorTexelBuffer * texelBufferDescriptors ;
uint32_t imageDescriptorsCount ;
uint32_t bufferDescriptorsCount ;
uint32_t texelBufferDescriptorsCount ;
} _descriptorSet ;
typedef struct VkDescriptorPool_T
{
PoolAllocator descriptorSetPA ;
2019-05-02 23:00:23 +02:00
ConsecutivePoolAllocator mapElementCPA ;
2019-04-30 23:00:39 +02:00
ConsecutivePoolAllocator * imageDescriptorCPA ;
ConsecutivePoolAllocator * bufferDescriptorCPA ;
ConsecutivePoolAllocator * texelBufferDescriptorCPA ;
2019-09-08 00:30:52 +02:00
uint32_t freeAble ;
2019-04-30 23:00:39 +02:00
} _descriptorPool ;
2018-08-26 15:11:43 +02:00
uint32_t getFormatBpp ( VkFormat f ) ;
uint32_t packVec4IntoABGR8 ( const float rgba [ 4 ] ) ;
2018-10-15 23:37:09 +02:00
void createImageBO ( _image * i ) ;
2018-08-26 15:11:43 +02:00
int findInstanceExtension ( char * name ) ;
int findDeviceExtension ( char * name ) ;
2018-10-15 23:37:09 +02:00
void getPaddedTextureDimensionsT ( uint32_t width , uint32_t height , uint32_t bpp , uint32_t * paddedWidth , uint32_t * paddedHeight ) ;
int isDepthStencilFormat ( VkFormat format ) ;
uint32_t getDepthCompareOp ( VkCompareOp op ) ;
uint32_t getTopology ( VkPrimitiveTopology topology ) ;
uint32_t getPrimitiveMode ( VkPrimitiveTopology topology ) ;
uint32_t getFormatByteSize ( VkFormat format ) ;
uint32_t ulog2 ( uint32_t v ) ;
2019-08-18 18:36:57 +02:00
void encodeTextureUniform ( uint32_t * params ,
uint8_t numMipLevels ,
uint8_t textureDataType ,
uint8_t isCubeMap ,
uint32_t cubemapStride ,
uint32_t textureBasePtr ,
uint16_t height ,
uint16_t width ,
uint8_t minFilter ,
uint8_t magFilter ,
uint8_t wrapT ,
uint8_t wrapS ,
uint8_t noAutoLod ) ;
uint8_t getTextureDataType ( VkFormat format ) ;
uint8_t getMinFilterType ( VkFilter minFilter , VkSamplerMipmapMode mipFilter , float maxLod ) ;
uint8_t getWrapMode ( VkSamplerAddressMode mode ) ;
2019-09-01 20:14:47 +02:00
uint32_t getRenderTargetFormatVC4 ( VkFormat format ) ;
2018-10-16 20:34:17 +02:00
void clFit ( VkCommandBuffer cb , ControlList * cl , uint32_t commandSize ) ;
void clDump ( void * cl , uint32_t size ) ;