2019-06-22 16:34:26 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* rendering_device_vulkan.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2019-06-22 16:34:26 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
#ifndef RENDERING_DEVICE_VULKAN_H
|
|
|
|
#define RENDERING_DEVICE_VULKAN_H
|
|
|
|
|
|
|
|
#include "core/os/thread_safe.h"
|
2020-12-19 13:18:08 +00:00
|
|
|
#include "core/templates/local_vector.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/templates/oa_hash_map.h"
|
|
|
|
#include "core/templates/rid_owner.h"
|
2020-03-27 18:21:27 +00:00
|
|
|
#include "servers/rendering/rendering_device.h"
|
2019-07-28 22:58:32 +00:00
|
|
|
|
2019-06-24 19:13:06 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-03-27 16:30:18 +00:00
|
|
|
#ifndef _DEBUG
|
2019-06-24 19:13:06 +00:00
|
|
|
#define _DEBUG
|
|
|
|
#endif
|
2020-03-27 16:30:18 +00:00
|
|
|
#endif
|
2019-06-07 16:07:57 +00:00
|
|
|
#include "vk_mem_alloc.h"
|
2020-06-03 08:45:44 +00:00
|
|
|
|
2021-08-12 11:24:54 +00:00
|
|
|
#ifdef USE_VOLK
|
|
|
|
#include <volk.h>
|
|
|
|
#else
|
2019-06-07 16:07:57 +00:00
|
|
|
#include <vulkan/vulkan.h>
|
2021-08-12 11:24:54 +00:00
|
|
|
#endif
|
2020-06-03 08:45:44 +00:00
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
class VulkanContext;
|
|
|
|
|
|
|
|
class RenderingDeviceVulkan : public RenderingDevice {
|
|
|
|
_THREAD_SAFE_CLASS_
|
|
|
|
|
|
|
|
// Miscellaneous tables that map
|
|
|
|
// our enums to enums used
|
|
|
|
// by vulkan.
|
|
|
|
|
|
|
|
VkPhysicalDeviceLimits limits;
|
|
|
|
static const VkFormat vulkan_formats[DATA_FORMAT_MAX];
|
|
|
|
static const char *named_formats[DATA_FORMAT_MAX];
|
|
|
|
static const VkCompareOp compare_operators[COMPARE_OP_MAX];
|
|
|
|
static const VkStencilOp stencil_operations[STENCIL_OP_MAX];
|
|
|
|
static const VkSampleCountFlagBits rasterization_sample_count[TEXTURE_SAMPLES_MAX];
|
|
|
|
static const VkLogicOp logic_operations[RenderingDevice::LOGIC_OP_MAX];
|
|
|
|
static const VkBlendFactor blend_factors[RenderingDevice::BLEND_FACTOR_MAX];
|
|
|
|
static const VkBlendOp blend_operations[RenderingDevice::BLEND_OP_MAX];
|
|
|
|
static const VkSamplerAddressMode address_modes[SAMPLER_REPEAT_MODE_MAX];
|
|
|
|
static const VkBorderColor sampler_border_colors[SAMPLER_BORDER_COLOR_MAX];
|
2019-06-25 22:49:52 +00:00
|
|
|
static const VkImageType vulkan_image_type[TEXTURE_TYPE_MAX];
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
// Functions used for format
|
|
|
|
// validation, and ensures the
|
|
|
|
// user passes valid data.
|
|
|
|
|
|
|
|
static int get_format_vertex_size(DataFormat p_format);
|
|
|
|
static uint32_t get_image_format_pixel_size(DataFormat p_format);
|
|
|
|
static void get_compressed_image_format_block_dimensions(DataFormat p_format, uint32_t &r_w, uint32_t &r_h);
|
|
|
|
uint32_t get_compressed_image_format_block_byte_size(DataFormat p_format);
|
|
|
|
static uint32_t get_compressed_image_format_pixel_rshift(DataFormat p_format);
|
2020-04-01 23:20:12 +00:00
|
|
|
static uint32_t get_image_format_required_size(DataFormat p_format, uint32_t p_width, uint32_t p_height, uint32_t p_depth, uint32_t p_mipmaps, uint32_t *r_blockw = nullptr, uint32_t *r_blockh = nullptr, uint32_t *r_depth = nullptr);
|
2019-06-07 16:07:57 +00:00
|
|
|
static uint32_t get_image_required_mipmaps(uint32_t p_width, uint32_t p_height, uint32_t p_depth);
|
2019-08-20 20:54:03 +00:00
|
|
|
static bool format_has_stencil(DataFormat p_format);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/***************************/
|
|
|
|
/**** ID INFRASTRUCTURE ****/
|
|
|
|
/***************************/
|
|
|
|
|
|
|
|
enum IDType {
|
|
|
|
ID_TYPE_FRAMEBUFFER_FORMAT,
|
2019-06-10 17:12:24 +00:00
|
|
|
ID_TYPE_VERTEX_FORMAT,
|
2019-06-07 16:07:57 +00:00
|
|
|
ID_TYPE_DRAW_LIST,
|
|
|
|
ID_TYPE_SPLIT_DRAW_LIST,
|
2019-09-25 19:44:44 +00:00
|
|
|
ID_TYPE_COMPUTE_LIST,
|
2019-06-07 16:07:57 +00:00
|
|
|
ID_TYPE_MAX,
|
|
|
|
ID_BASE_SHIFT = 58 //5 bits for ID types
|
|
|
|
};
|
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
VkDevice device = VK_NULL_HANDLE;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
Map<RID, Set<RID>> dependency_map; //IDs to IDs that depend on it
|
|
|
|
Map<RID, Set<RID>> reverse_dependency_map; //same as above, but in reverse
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
void _add_dependency(RID p_id, RID p_depends_on);
|
|
|
|
void _free_dependencies(RID p_id);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/*****************/
|
|
|
|
/**** TEXTURE ****/
|
|
|
|
/*****************/
|
|
|
|
|
|
|
|
// In Vulkan, the concept of textures does not exist,
|
2020-07-16 16:54:15 +00:00
|
|
|
// instead there is the image (the memory pretty much,
|
2019-06-07 16:07:57 +00:00
|
|
|
// the view (how the memory is interpreted) and the
|
|
|
|
// sampler (how it's sampled from the shader).
|
|
|
|
//
|
|
|
|
// Texture here includes the first two stages, but
|
|
|
|
// It's possible to create textures sharing the image
|
|
|
|
// but with different views. The main use case for this
|
|
|
|
// is textures that can be read as both SRGB/Linear,
|
|
|
|
// or slices of a texture (a mipmap, a layer, a 3D slice)
|
|
|
|
// for a framebuffer to render into it.
|
|
|
|
|
|
|
|
struct Texture {
|
2020-11-23 16:38:46 +00:00
|
|
|
VkImage image = VK_NULL_HANDLE;
|
|
|
|
VmaAllocation allocation = nullptr;
|
2019-06-07 16:07:57 +00:00
|
|
|
VmaAllocationInfo allocation_info;
|
2020-11-23 16:38:46 +00:00
|
|
|
VkImageView view = VK_NULL_HANDLE;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
TextureType type;
|
|
|
|
DataFormat format;
|
|
|
|
TextureSamples samples;
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t width = 0;
|
|
|
|
uint32_t height = 0;
|
|
|
|
uint32_t depth = 0;
|
|
|
|
uint32_t layers = 0;
|
|
|
|
uint32_t mipmaps = 0;
|
|
|
|
uint32_t usage_flags = 0;
|
|
|
|
uint32_t base_mipmap = 0;
|
|
|
|
uint32_t base_layer = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-19 20:03:19 +00:00
|
|
|
Vector<DataFormat> allowed_shared_formats;
|
|
|
|
|
2019-09-25 19:44:44 +00:00
|
|
|
VkImageLayout layout;
|
|
|
|
|
2021-02-02 19:51:36 +00:00
|
|
|
uint64_t used_in_frame = 0;
|
|
|
|
bool used_in_transfer = false;
|
|
|
|
bool used_in_raster = false;
|
|
|
|
bool used_in_compute = false;
|
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t read_aspect_mask = 0;
|
|
|
|
uint32_t barrier_aspect_mask = 0;
|
|
|
|
bool bound = false; //bound to framebffer
|
2019-06-10 17:12:24 +00:00
|
|
|
RID owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<Texture, true> texture_owner;
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t texture_upload_region_size_px = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> _texture_get_data_from_image(Texture *tex, VkImage p_image, VmaAllocation p_allocation, uint32_t p_layer, bool p_2d = false);
|
2021-07-07 21:06:06 +00:00
|
|
|
Error _texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, uint32_t p_post_barrier, bool p_use_setup_queue);
|
2019-06-25 22:49:52 +00:00
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
/*****************/
|
|
|
|
/**** SAMPLER ****/
|
|
|
|
/*****************/
|
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
RID_Owner<VkSampler> sampler_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/***************************/
|
|
|
|
/**** BUFFER MANAGEMENT ****/
|
|
|
|
/***************************/
|
|
|
|
|
|
|
|
// These are temporary buffers on CPU memory that hold
|
|
|
|
// the information until the CPU fetches it and places it
|
|
|
|
// either on GPU buffers, or images (textures). It ensures
|
Fix various typos
Found via `codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,expct,fave,findn,gird,hist,inh,inout,leapyear,lod,nd,numer,ois,ony,paket,ro,seeked,sinc,switchs,te,uint,varn,vew`
2022-01-02 18:47:52 +00:00
|
|
|
// updates are properly synchronized with whatever the
|
2019-06-07 16:07:57 +00:00
|
|
|
// GPU is doing.
|
|
|
|
//
|
|
|
|
// The logic here is as follows, only 3 of these
|
|
|
|
// blocks are created at the beginning (one per frame)
|
|
|
|
// they can each belong to a frame (assigned to current when
|
|
|
|
// used) and they can only be reused after the same frame is
|
|
|
|
// recycled.
|
|
|
|
//
|
|
|
|
// When CPU requires to allocate more than what is available,
|
|
|
|
// more of these buffers are created. If a limit is reached,
|
|
|
|
// then a fence will ensure will wait for blocks allocated
|
|
|
|
// in previous frames are processed. If that fails, then
|
|
|
|
// another fence will ensure everything pending for the current
|
|
|
|
// frame is processed (effectively stalling).
|
|
|
|
//
|
|
|
|
// See the comments in the code to understand better how it works.
|
|
|
|
|
|
|
|
struct StagingBufferBlock {
|
2020-11-23 16:38:46 +00:00
|
|
|
VkBuffer buffer = VK_NULL_HANDLE;
|
|
|
|
VmaAllocation allocation = nullptr;
|
|
|
|
uint64_t frame_used = 0;
|
|
|
|
uint32_t fill_amount = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Vector<StagingBufferBlock> staging_buffer_blocks;
|
2020-11-23 16:38:46 +00:00
|
|
|
int staging_buffer_current = 0;
|
|
|
|
uint32_t staging_buffer_block_size = 0;
|
|
|
|
uint64_t staging_buffer_max_size = 0;
|
|
|
|
bool staging_buffer_used = false;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
Error _staging_buffer_allocate(uint32_t p_amount, uint32_t p_required_align, uint32_t &r_alloc_offset, uint32_t &r_alloc_size, bool p_can_segment = true, bool p_on_draw_command_buffer = false);
|
|
|
|
Error _insert_staging_block();
|
|
|
|
|
|
|
|
struct Buffer {
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t size = 0;
|
|
|
|
uint32_t usage = 0;
|
|
|
|
VkBuffer buffer = VK_NULL_HANDLE;
|
|
|
|
VmaAllocation allocation = nullptr;
|
2019-06-07 16:07:57 +00:00
|
|
|
VkDescriptorBufferInfo buffer_info; //used for binding
|
2020-06-25 13:33:28 +00:00
|
|
|
Buffer() {
|
|
|
|
}
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Error _buffer_allocate(Buffer *p_buffer, uint32_t p_size, uint32_t p_usage, VmaMemoryUsage p_mapping);
|
|
|
|
Error _buffer_free(Buffer *p_buffer);
|
|
|
|
Error _buffer_update(Buffer *p_buffer, size_t p_offset, const uint8_t *p_data, size_t p_data_size, bool p_use_draw_command_buffer = false, uint32_t p_required_align = 32);
|
|
|
|
|
2019-10-31 22:54:21 +00:00
|
|
|
void _full_barrier(bool p_sync_with_draw);
|
2019-06-24 19:13:06 +00:00
|
|
|
void _memory_barrier(VkPipelineStageFlags p_src_stage_mask, VkPipelineStageFlags p_dst_stage_mask, VkAccessFlags p_src_access, VkAccessFlags p_dst_sccess, bool p_sync_with_draw);
|
2019-09-07 01:51:27 +00:00
|
|
|
void _buffer_memory_barrier(VkBuffer buffer, uint64_t p_from, uint64_t p_size, VkPipelineStageFlags p_src_stage_mask, VkPipelineStageFlags p_dst_stage_mask, VkAccessFlags p_src_access, VkAccessFlags p_dst_sccess, bool p_sync_with_draw);
|
2019-06-24 19:13:06 +00:00
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
/*********************/
|
|
|
|
/**** FRAMEBUFFER ****/
|
|
|
|
/*********************/
|
|
|
|
|
|
|
|
// In Vulkan, framebuffers work similar to how they
|
|
|
|
// do in OpenGL, with the exception that
|
|
|
|
// the "format" (vkRenderPass) is not dynamic
|
|
|
|
// and must be more or less the same as the one
|
|
|
|
// used for the render pipelines.
|
|
|
|
|
|
|
|
struct FramebufferFormatKey {
|
|
|
|
Vector<AttachmentFormat> attachments;
|
2021-06-24 13:58:36 +00:00
|
|
|
Vector<FramebufferPass> passes;
|
2021-05-07 13:19:04 +00:00
|
|
|
uint32_t view_count = 1;
|
2019-06-07 16:07:57 +00:00
|
|
|
bool operator<(const FramebufferFormatKey &p_key) const {
|
2021-05-07 13:19:04 +00:00
|
|
|
if (view_count != p_key.view_count) {
|
|
|
|
return view_count < p_key.view_count;
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:58:36 +00:00
|
|
|
uint32_t pass_size = passes.size();
|
|
|
|
uint32_t key_pass_size = p_key.passes.size();
|
|
|
|
if (pass_size != key_pass_size) {
|
|
|
|
return pass_size < key_pass_size;
|
|
|
|
}
|
|
|
|
const FramebufferPass *pass_ptr = passes.ptr();
|
|
|
|
const FramebufferPass *key_pass_ptr = p_key.passes.ptr();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < pass_size; i++) {
|
|
|
|
{ //compare color attachments
|
|
|
|
uint32_t attachment_size = pass_ptr[i].color_attachments.size();
|
|
|
|
uint32_t key_attachment_size = key_pass_ptr[i].color_attachments.size();
|
|
|
|
if (attachment_size != key_attachment_size) {
|
|
|
|
return attachment_size < key_attachment_size;
|
|
|
|
}
|
|
|
|
const int32_t *pass_attachment_ptr = pass_ptr[i].color_attachments.ptr();
|
|
|
|
const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].color_attachments.ptr();
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < attachment_size; j++) {
|
|
|
|
if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
|
|
|
|
return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{ //compare input attachments
|
|
|
|
uint32_t attachment_size = pass_ptr[i].input_attachments.size();
|
|
|
|
uint32_t key_attachment_size = key_pass_ptr[i].input_attachments.size();
|
|
|
|
if (attachment_size != key_attachment_size) {
|
|
|
|
return attachment_size < key_attachment_size;
|
|
|
|
}
|
|
|
|
const int32_t *pass_attachment_ptr = pass_ptr[i].input_attachments.ptr();
|
|
|
|
const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].input_attachments.ptr();
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < attachment_size; j++) {
|
|
|
|
if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
|
|
|
|
return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{ //compare resolve attachments
|
|
|
|
uint32_t attachment_size = pass_ptr[i].resolve_attachments.size();
|
|
|
|
uint32_t key_attachment_size = key_pass_ptr[i].resolve_attachments.size();
|
|
|
|
if (attachment_size != key_attachment_size) {
|
|
|
|
return attachment_size < key_attachment_size;
|
|
|
|
}
|
|
|
|
const int32_t *pass_attachment_ptr = pass_ptr[i].resolve_attachments.ptr();
|
|
|
|
const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].resolve_attachments.ptr();
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < attachment_size; j++) {
|
|
|
|
if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
|
|
|
|
return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{ //compare preserve attachments
|
|
|
|
uint32_t attachment_size = pass_ptr[i].preserve_attachments.size();
|
|
|
|
uint32_t key_attachment_size = key_pass_ptr[i].preserve_attachments.size();
|
|
|
|
if (attachment_size != key_attachment_size) {
|
|
|
|
return attachment_size < key_attachment_size;
|
|
|
|
}
|
|
|
|
const int32_t *pass_attachment_ptr = pass_ptr[i].preserve_attachments.ptr();
|
|
|
|
const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].preserve_attachments.ptr();
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < attachment_size; j++) {
|
|
|
|
if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
|
|
|
|
return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pass_ptr[i].depth_attachment != key_pass_ptr[i].depth_attachment) {
|
|
|
|
return pass_ptr[i].depth_attachment < key_pass_ptr[i].depth_attachment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
int as = attachments.size();
|
|
|
|
int bs = p_key.attachments.size();
|
|
|
|
if (as != bs) {
|
|
|
|
return as < bs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AttachmentFormat *af_a = attachments.ptr();
|
|
|
|
const AttachmentFormat *af_b = p_key.attachments.ptr();
|
|
|
|
for (int i = 0; i < as; i++) {
|
|
|
|
const AttachmentFormat &a = af_a[i];
|
|
|
|
const AttachmentFormat &b = af_b[i];
|
|
|
|
if (a.format != b.format) {
|
|
|
|
return a.format < b.format;
|
|
|
|
}
|
|
|
|
if (a.samples != b.samples) {
|
|
|
|
return a.samples < b.samples;
|
|
|
|
}
|
|
|
|
if (a.usage_flags != b.usage_flags) {
|
|
|
|
return a.usage_flags < b.usage_flags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; //equal
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-24 13:58:36 +00:00
|
|
|
VkRenderPass _render_pass_create(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, InitialAction p_initial_action, FinalAction p_final_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, uint32_t p_view_count = 1, Vector<TextureSamples> *r_samples = nullptr);
|
2019-06-07 16:07:57 +00:00
|
|
|
// This is a cache and it's never freed, it ensures
|
|
|
|
// IDs for a given format are always unique.
|
2019-06-10 17:12:24 +00:00
|
|
|
Map<FramebufferFormatKey, FramebufferFormatID> framebuffer_format_cache;
|
2019-06-07 16:07:57 +00:00
|
|
|
struct FramebufferFormat {
|
2019-06-10 17:12:24 +00:00
|
|
|
const Map<FramebufferFormatKey, FramebufferFormatID>::Element *E;
|
2020-11-23 16:38:46 +00:00
|
|
|
VkRenderPass render_pass = VK_NULL_HANDLE; //here for constructing shaders, never used, see section (7.2. Render Pass Compatibility from Vulkan spec)
|
2021-06-24 13:58:36 +00:00
|
|
|
Vector<TextureSamples> pass_samples;
|
2021-05-07 13:19:04 +00:00
|
|
|
uint32_t view_count = 1; // number of views
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
Map<FramebufferFormatID, FramebufferFormat> framebuffer_formats;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
struct Framebuffer {
|
2020-11-23 16:38:46 +00:00
|
|
|
FramebufferFormatID format_id = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
struct VersionKey {
|
2019-10-03 20:39:08 +00:00
|
|
|
InitialAction initial_color_action;
|
|
|
|
FinalAction final_color_action;
|
|
|
|
InitialAction initial_depth_action;
|
|
|
|
FinalAction final_depth_action;
|
2021-05-07 13:19:04 +00:00
|
|
|
uint32_t view_count;
|
2021-06-24 13:58:36 +00:00
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
bool operator<(const VersionKey &p_key) const {
|
2019-10-03 20:39:08 +00:00
|
|
|
if (initial_color_action == p_key.initial_color_action) {
|
|
|
|
if (final_color_action == p_key.final_color_action) {
|
|
|
|
if (initial_depth_action == p_key.initial_depth_action) {
|
2021-05-07 13:19:04 +00:00
|
|
|
if (final_depth_action == p_key.final_depth_action) {
|
|
|
|
return view_count < p_key.view_count;
|
|
|
|
} else {
|
|
|
|
return final_depth_action < p_key.final_depth_action;
|
|
|
|
}
|
2019-10-03 20:39:08 +00:00
|
|
|
} else {
|
|
|
|
return initial_depth_action < p_key.initial_depth_action;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return final_color_action < p_key.final_color_action;
|
|
|
|
}
|
2019-06-07 16:07:57 +00:00
|
|
|
} else {
|
2019-10-03 20:39:08 +00:00
|
|
|
return initial_color_action < p_key.initial_color_action;
|
2019-06-07 16:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t storage_mask = 0;
|
2019-06-10 17:12:24 +00:00
|
|
|
Vector<RID> texture_ids;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
struct Version {
|
2020-11-23 16:38:46 +00:00
|
|
|
VkFramebuffer framebuffer = VK_NULL_HANDLE;
|
|
|
|
VkRenderPass render_pass = VK_NULL_HANDLE; //this one is owned
|
2021-06-24 13:58:36 +00:00
|
|
|
uint32_t subpass_count = 1;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Map<VersionKey, Version> framebuffers;
|
|
|
|
Size2 size;
|
2021-05-07 13:19:04 +00:00
|
|
|
uint32_t view_count;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<Framebuffer, true> framebuffer_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/***********************/
|
|
|
|
/**** VERTEX BUFFER ****/
|
|
|
|
/***********************/
|
|
|
|
|
|
|
|
// Vertex buffers in Vulkan are similar to how
|
|
|
|
// they work in OpenGL, except that instead of
|
2020-07-16 16:54:15 +00:00
|
|
|
// an attribute index, there is a buffer binding
|
2019-06-07 16:07:57 +00:00
|
|
|
// index (for binding the buffers in real-time)
|
|
|
|
// and a location index (what is used in the shader).
|
|
|
|
//
|
|
|
|
// This mapping is done here internally, and it's not
|
|
|
|
// exposed.
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<Buffer, true> vertex_buffer_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
struct VertexDescriptionKey {
|
2020-04-21 15:16:45 +00:00
|
|
|
Vector<VertexAttribute> vertex_formats;
|
2019-06-24 19:13:06 +00:00
|
|
|
bool operator==(const VertexDescriptionKey &p_key) const {
|
|
|
|
int vdc = vertex_formats.size();
|
|
|
|
int vdck = p_key.vertex_formats.size();
|
|
|
|
|
|
|
|
if (vdc != vdck) {
|
|
|
|
return false;
|
2019-06-07 16:07:57 +00:00
|
|
|
} else {
|
2020-04-21 15:16:45 +00:00
|
|
|
const VertexAttribute *a_ptr = vertex_formats.ptr();
|
|
|
|
const VertexAttribute *b_ptr = p_key.vertex_formats.ptr();
|
2019-06-07 16:07:57 +00:00
|
|
|
for (int i = 0; i < vdc; i++) {
|
2020-04-21 15:16:45 +00:00
|
|
|
const VertexAttribute &a = a_ptr[i];
|
|
|
|
const VertexAttribute &b = b_ptr[i];
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
if (a.location != b.location) {
|
2019-06-24 19:13:06 +00:00
|
|
|
return false;
|
2019-06-07 16:07:57 +00:00
|
|
|
}
|
|
|
|
if (a.offset != b.offset) {
|
2019-06-24 19:13:06 +00:00
|
|
|
return false;
|
2019-06-07 16:07:57 +00:00
|
|
|
}
|
|
|
|
if (a.format != b.format) {
|
2019-06-24 19:13:06 +00:00
|
|
|
return false;
|
2019-06-07 16:07:57 +00:00
|
|
|
}
|
|
|
|
if (a.stride != b.stride) {
|
2019-06-24 19:13:06 +00:00
|
|
|
return false;
|
2019-06-07 16:07:57 +00:00
|
|
|
}
|
2020-02-19 13:01:19 +00:00
|
|
|
if (a.frequency != b.frequency) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-07 16:07:57 +00:00
|
|
|
}
|
2019-06-24 19:13:06 +00:00
|
|
|
return true; //they are equal
|
2019-06-07 16:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 19:13:06 +00:00
|
|
|
|
|
|
|
uint32_t hash() const {
|
|
|
|
int vdc = vertex_formats.size();
|
|
|
|
uint32_t h = hash_djb2_one_32(vdc);
|
2020-04-21 15:16:45 +00:00
|
|
|
const VertexAttribute *ptr = vertex_formats.ptr();
|
2019-06-24 19:13:06 +00:00
|
|
|
for (int i = 0; i < vdc; i++) {
|
2020-04-21 15:16:45 +00:00
|
|
|
const VertexAttribute &vd = ptr[i];
|
2019-06-24 19:13:06 +00:00
|
|
|
h = hash_djb2_one_32(vd.location, h);
|
|
|
|
h = hash_djb2_one_32(vd.offset, h);
|
|
|
|
h = hash_djb2_one_32(vd.format, h);
|
|
|
|
h = hash_djb2_one_32(vd.stride, h);
|
|
|
|
h = hash_djb2_one_32(vd.frequency, h);
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VertexDescriptionHash {
|
|
|
|
static _FORCE_INLINE_ uint32_t hash(const VertexDescriptionKey &p_key) {
|
|
|
|
return p_key.hash();
|
|
|
|
}
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is a cache and it's never freed, it ensures that
|
|
|
|
// ID used for a specific format always remain the same.
|
2019-06-24 19:13:06 +00:00
|
|
|
HashMap<VertexDescriptionKey, VertexFormatID, VertexDescriptionHash> vertex_format_cache;
|
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
struct VertexDescriptionCache {
|
2020-04-21 15:16:45 +00:00
|
|
|
Vector<VertexAttribute> vertex_formats;
|
2020-11-23 16:38:46 +00:00
|
|
|
VkVertexInputBindingDescription *bindings = nullptr;
|
|
|
|
VkVertexInputAttributeDescription *attributes = nullptr;
|
2019-06-07 16:07:57 +00:00
|
|
|
VkPipelineVertexInputStateCreateInfo create_info;
|
|
|
|
};
|
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
Map<VertexFormatID, VertexDescriptionCache> vertex_formats;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
struct VertexArray {
|
2019-06-10 17:12:24 +00:00
|
|
|
RID buffer;
|
2020-11-23 16:38:46 +00:00
|
|
|
VertexFormatID description = 0;
|
|
|
|
int vertex_count = 0;
|
|
|
|
uint32_t max_instances_allowed = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
Vector<VkBuffer> buffers; //not owned, just referenced
|
|
|
|
Vector<VkDeviceSize> offsets;
|
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<VertexArray, true> vertex_array_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
struct IndexBuffer : public Buffer {
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t max_index = 0; //used for validation
|
|
|
|
uint32_t index_count = 0;
|
|
|
|
VkIndexType index_type = VK_INDEX_TYPE_NONE_NV;
|
|
|
|
bool supports_restart_indices = false;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<IndexBuffer, true> index_buffer_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
struct IndexArray {
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t max_index = 0; //remember the maximum index here too, for validation
|
2019-06-07 16:07:57 +00:00
|
|
|
VkBuffer buffer; //not owned, inherited from index buffer
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t offset = 0;
|
|
|
|
uint32_t indices = 0;
|
|
|
|
VkIndexType index_type = VK_INDEX_TYPE_NONE_NV;
|
|
|
|
bool supports_restart_indices = false;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<IndexArray, true> index_array_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/****************/
|
|
|
|
/**** SHADER ****/
|
|
|
|
/****************/
|
|
|
|
|
2019-07-27 13:23:24 +00:00
|
|
|
// Vulkan specifies a really complex behavior for the application
|
|
|
|
// in order to tell when descriptor sets need to be re-bound (or not).
|
|
|
|
// "When binding a descriptor set (see Descriptor Set Binding) to set
|
|
|
|
// number N, if the previously bound descriptor sets for sets zero
|
|
|
|
// through N-1 were all bound using compatible pipeline layouts,
|
|
|
|
// then performing this binding does not disturb any of the lower numbered sets.
|
|
|
|
// If, additionally, the previous bound descriptor set for set N was
|
|
|
|
// bound using a pipeline layout compatible for set N, then the bindings
|
|
|
|
// in sets numbered greater than N are also not disturbed."
|
|
|
|
// As a result, we need to figure out quickly when something is no longer "compatible".
|
|
|
|
// in order to avoid costly rebinds.
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MAX_UNIFORM_SETS = 16
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UniformInfo {
|
2020-11-23 16:38:46 +00:00
|
|
|
UniformType type = UniformType::UNIFORM_TYPE_MAX;
|
|
|
|
int binding = 0;
|
|
|
|
uint32_t stages = 0;
|
|
|
|
int length = 0; //size of arrays (in total elements), or ubos (in bytes * total elements)
|
2019-07-27 13:23:24 +00:00
|
|
|
|
|
|
|
bool operator!=(const UniformInfo &p_info) const {
|
|
|
|
return (binding != p_info.binding || type != p_info.type || stages != p_info.stages || length != p_info.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const UniformInfo &p_info) const {
|
|
|
|
if (binding != p_info.binding) {
|
|
|
|
return binding < p_info.binding;
|
|
|
|
}
|
|
|
|
if (type != p_info.type) {
|
|
|
|
return type < p_info.type;
|
|
|
|
}
|
|
|
|
if (stages != p_info.stages) {
|
|
|
|
return stages < p_info.stages;
|
|
|
|
}
|
|
|
|
return length < p_info.length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UniformSetFormat {
|
|
|
|
Vector<UniformInfo> uniform_info;
|
|
|
|
bool operator<(const UniformSetFormat &p_format) const {
|
|
|
|
uint32_t size = uniform_info.size();
|
|
|
|
uint32_t psize = p_format.uniform_info.size();
|
|
|
|
|
|
|
|
if (size != psize) {
|
|
|
|
return size < psize;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UniformInfo *infoptr = uniform_info.ptr();
|
|
|
|
const UniformInfo *pinfoptr = p_format.uniform_info.ptr();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
|
|
|
if (infoptr[i] != pinfoptr[i]) {
|
|
|
|
return infoptr[i] < pinfoptr[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Always grows, never shrinks, ensuring unique IDs, but we assume
|
|
|
|
// the amount of formats will never be a problem, as the amount of shaders
|
|
|
|
// in a game is limited.
|
|
|
|
Map<UniformSetFormat, uint32_t> uniform_set_format_cache;
|
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
// Shaders in Vulkan are just pretty much
|
|
|
|
// precompiled blocks of SPIR-V bytecode. They
|
|
|
|
// are most likely not really compiled to host
|
|
|
|
// assembly until a pipeline is created.
|
|
|
|
//
|
|
|
|
// When supplying the shaders, this implementation
|
|
|
|
// will use the reflection abilities of glslang to
|
|
|
|
// understand and cache everything required to
|
|
|
|
// create and use the descriptor sets (Vulkan's
|
|
|
|
// biggest pain).
|
|
|
|
//
|
|
|
|
// Additionally, hashes are created for every set
|
|
|
|
// to do quick validation and ensuring the user
|
|
|
|
// does not submit something invalid.
|
|
|
|
|
|
|
|
struct Shader {
|
|
|
|
struct Set {
|
|
|
|
Vector<UniformInfo> uniform_info;
|
2020-11-23 16:38:46 +00:00
|
|
|
VkDescriptorSetLayout descriptor_set_layout = VK_NULL_HANDLE;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t vertex_input_mask = 0; //inputs used, this is mostly for validation
|
2021-06-24 13:58:36 +00:00
|
|
|
uint32_t fragment_output_mask = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-08 20:10:52 +00:00
|
|
|
struct PushConstant {
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t push_constant_size = 0;
|
|
|
|
uint32_t push_constants_vk_stage = 0;
|
2019-06-08 20:10:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PushConstant push_constant;
|
|
|
|
|
2021-02-02 19:51:36 +00:00
|
|
|
uint32_t compute_local_size[3] = { 0, 0, 0 };
|
|
|
|
|
2021-07-09 19:48:28 +00:00
|
|
|
struct SpecializationConstant {
|
|
|
|
PipelineSpecializationConstant constant;
|
|
|
|
uint32_t stage_flags = 0;
|
|
|
|
};
|
|
|
|
|
2019-09-25 19:44:44 +00:00
|
|
|
bool is_compute = false;
|
2020-11-23 16:38:46 +00:00
|
|
|
int max_output = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
Vector<Set> sets;
|
2019-07-27 13:23:24 +00:00
|
|
|
Vector<uint32_t> set_formats;
|
2019-06-07 16:07:57 +00:00
|
|
|
Vector<VkPipelineShaderStageCreateInfo> pipeline_stages;
|
2021-07-09 19:48:28 +00:00
|
|
|
Vector<SpecializationConstant> specialization_constants;
|
2020-11-23 16:38:46 +00:00
|
|
|
VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
|
2021-08-16 17:51:29 +00:00
|
|
|
String name; //used for debug
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2019-07-27 13:23:24 +00:00
|
|
|
String _shader_uniform_debug(RID p_shader, int p_set = -1);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<Shader, true> shader_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/******************/
|
|
|
|
/**** UNIFORMS ****/
|
|
|
|
/******************/
|
|
|
|
|
|
|
|
// Descriptor sets require allocation from a pool.
|
|
|
|
// The documentation on how to use pools properly
|
|
|
|
// is scarce, and the documentation is strange.
|
|
|
|
//
|
|
|
|
// Basically, you can mix and match pools as you
|
|
|
|
// like, but you'll run into fragmentation issues.
|
|
|
|
// Because of this, the recommended approach is to
|
Fix various typos with codespell
Found via `codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,fave,findn,hist,inout,leapyear,lod,nd,numer,ois,ony,paket,seeked,sinc,switchs,te,uint`
2021-07-07 15:17:32 +00:00
|
|
|
// create a pool for every descriptor set type, as
|
|
|
|
// this prevents fragmentation.
|
2019-06-07 16:07:57 +00:00
|
|
|
//
|
|
|
|
// This is implemented here as a having a list of
|
|
|
|
// pools (each can contain up to 64 sets) for each
|
|
|
|
// set layout. The amount of sets for each type
|
|
|
|
// is used as the key.
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MAX_DESCRIPTOR_POOL_ELEMENT = 65535
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DescriptorPoolKey {
|
|
|
|
union {
|
|
|
|
struct {
|
2020-06-03 08:45:44 +00:00
|
|
|
uint16_t uniform_type[UNIFORM_TYPE_MAX]; // Using 16 bits because, for sending arrays, each element is a pool set.
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
struct {
|
|
|
|
uint64_t key1;
|
|
|
|
uint64_t key2;
|
|
|
|
uint64_t key3;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
bool operator<(const DescriptorPoolKey &p_key) const {
|
|
|
|
if (key1 != p_key.key1) {
|
|
|
|
return key1 < p_key.key1;
|
|
|
|
}
|
|
|
|
if (key2 != p_key.key2) {
|
|
|
|
return key2 < p_key.key2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key3 < p_key.key3;
|
|
|
|
}
|
|
|
|
DescriptorPoolKey() {
|
|
|
|
key1 = 0;
|
|
|
|
key2 = 0;
|
|
|
|
key3 = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DescriptorPool {
|
|
|
|
VkDescriptorPool pool;
|
|
|
|
uint32_t usage;
|
|
|
|
};
|
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
Map<DescriptorPoolKey, Set<DescriptorPool *>> descriptor_pools;
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t max_descriptors_per_pool = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
DescriptorPool *_descriptor_pool_allocate(const DescriptorPoolKey &p_key);
|
|
|
|
void _descriptor_pool_free(const DescriptorPoolKey &p_key, DescriptorPool *p_pool);
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<Buffer, true> uniform_buffer_owner;
|
|
|
|
RID_Owner<Buffer, true> storage_buffer_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
//texture buffer needs a view
|
|
|
|
struct TextureBuffer {
|
|
|
|
Buffer buffer;
|
2020-11-23 16:38:46 +00:00
|
|
|
VkBufferView view = VK_NULL_HANDLE;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<TextureBuffer, true> texture_buffer_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
// This structure contains the descriptor set. They _need_ to be allocated
|
|
|
|
// for a shader (and will be erased when this shader is erased), but should
|
|
|
|
// work for other shaders as long as the hash matches. This covers using
|
|
|
|
// them in shader variants.
|
|
|
|
//
|
|
|
|
// Keep also in mind that you can share buffers between descriptor sets, so
|
|
|
|
// the above restriction is not too serious.
|
|
|
|
|
|
|
|
struct UniformSet {
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t format = 0;
|
2019-06-10 17:12:24 +00:00
|
|
|
RID shader_id;
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t shader_set = 0;
|
|
|
|
DescriptorPool *pool = nullptr;
|
2019-06-07 16:07:57 +00:00
|
|
|
DescriptorPoolKey pool_key;
|
2020-11-23 16:38:46 +00:00
|
|
|
VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
|
2019-07-27 13:23:24 +00:00
|
|
|
//VkPipelineLayout pipeline_layout; //not owned, inherited from shader
|
2020-12-19 13:18:08 +00:00
|
|
|
struct AttachableTexture {
|
|
|
|
uint32_t bind;
|
|
|
|
RID texture;
|
|
|
|
};
|
|
|
|
|
|
|
|
LocalVector<AttachableTexture> attachable_textures; //used for validation
|
2019-09-25 19:44:44 +00:00
|
|
|
Vector<Texture *> mutable_sampled_textures; //used for layout change
|
|
|
|
Vector<Texture *> mutable_storage_textures; //used for layout change
|
2021-07-07 22:55:20 +00:00
|
|
|
UniformSetInvalidatedCallback invalidated_callback = nullptr;
|
|
|
|
void *invalidated_callback_userdata = nullptr;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Owner<UniformSet, true> uniform_set_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/*******************/
|
|
|
|
/**** PIPELINES ****/
|
|
|
|
/*******************/
|
|
|
|
|
|
|
|
// Render pipeline contains ALL the
|
|
|
|
// information required for drawing.
|
|
|
|
// This includes all the rasterizer state
|
|
|
|
// as well as shader used, framebuffer format,
|
|
|
|
// etc.
|
|
|
|
// While the pipeline is just a single object
|
|
|
|
// (VkPipeline) a lot of values are also saved
|
|
|
|
// here to do validation (vulkan does none by
|
|
|
|
// default) and warn the user if something
|
|
|
|
// was not supplied as intended.
|
|
|
|
|
|
|
|
struct RenderPipeline {
|
2019-07-29 15:59:18 +00:00
|
|
|
//Cached values for validation
|
2019-07-27 13:23:24 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
struct Validation {
|
2020-11-23 16:38:46 +00:00
|
|
|
FramebufferFormatID framebuffer_format = 0;
|
2021-06-24 13:58:36 +00:00
|
|
|
uint32_t render_pass = 0;
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t dynamic_state = 0;
|
|
|
|
VertexFormatID vertex_format = 0;
|
|
|
|
bool uses_restart_indices = false;
|
|
|
|
uint32_t primitive_minimum = 0;
|
|
|
|
uint32_t primitive_divisor = 0;
|
2019-07-27 13:23:24 +00:00
|
|
|
} validation;
|
|
|
|
#endif
|
2019-06-07 16:07:57 +00:00
|
|
|
//Actual pipeline
|
2019-07-27 13:23:24 +00:00
|
|
|
RID shader;
|
|
|
|
Vector<uint32_t> set_formats;
|
2020-11-23 16:38:46 +00:00
|
|
|
VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; // not owned, needed for push constants
|
|
|
|
VkPipeline pipeline = VK_NULL_HANDLE;
|
|
|
|
uint32_t push_constant_size = 0;
|
|
|
|
uint32_t push_constant_stages = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2019-09-25 19:44:44 +00:00
|
|
|
RID_Owner<RenderPipeline, true> render_pipeline_owner;
|
|
|
|
|
|
|
|
struct ComputePipeline {
|
|
|
|
RID shader;
|
|
|
|
Vector<uint32_t> set_formats;
|
2020-11-23 16:38:46 +00:00
|
|
|
VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; // not owned, needed for push constants
|
|
|
|
VkPipeline pipeline = VK_NULL_HANDLE;
|
|
|
|
uint32_t push_constant_size = 0;
|
|
|
|
uint32_t push_constant_stages = 0;
|
2021-02-02 19:51:36 +00:00
|
|
|
uint32_t local_group_size[3] = { 0, 0, 0 };
|
2019-09-25 19:44:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RID_Owner<ComputePipeline, true> compute_pipeline_owner;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/*******************/
|
|
|
|
/**** DRAW LIST ****/
|
|
|
|
/*******************/
|
|
|
|
|
|
|
|
// Draw list contains both the command buffer
|
|
|
|
// used for drawing as well as a LOT of
|
|
|
|
// information used for validation. This
|
|
|
|
// validation is cheap so most of it can
|
|
|
|
// also run in release builds.
|
|
|
|
|
|
|
|
// When using split command lists, this is
|
|
|
|
// implemented internally using secondary command
|
|
|
|
// buffers. As they can be created in threads,
|
2021-08-13 02:45:44 +00:00
|
|
|
// each needs its own command pool.
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
struct SplitDrawListAllocator {
|
2020-11-23 16:38:46 +00:00
|
|
|
VkCommandPool command_pool = VK_NULL_HANDLE;
|
2019-06-07 16:07:57 +00:00
|
|
|
Vector<VkCommandBuffer> command_buffers; //one for each frame
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<SplitDrawListAllocator> split_draw_list_allocators;
|
|
|
|
|
|
|
|
struct DrawList {
|
2020-11-23 16:38:46 +00:00
|
|
|
VkCommandBuffer command_buffer = VK_NULL_HANDLE; // If persistent, this is owned, otherwise it's shared with the ringbuffer.
|
2019-06-24 19:13:06 +00:00
|
|
|
Rect2i viewport;
|
2021-06-24 13:58:36 +00:00
|
|
|
bool viewport_set = false;
|
2019-07-27 13:23:24 +00:00
|
|
|
|
|
|
|
struct SetState {
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t pipeline_expected_format = 0;
|
|
|
|
uint32_t uniform_set_format = 0;
|
|
|
|
VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
|
2019-07-27 13:23:24 +00:00
|
|
|
RID uniform_set;
|
2020-06-03 08:45:44 +00:00
|
|
|
bool bound = false;
|
2019-07-27 13:23:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct State {
|
|
|
|
SetState sets[MAX_UNIFORM_SETS];
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t set_count = 0;
|
2019-07-27 13:23:24 +00:00
|
|
|
RID pipeline;
|
|
|
|
RID pipeline_shader;
|
2020-06-03 08:45:44 +00:00
|
|
|
VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
|
2019-07-27 13:23:24 +00:00
|
|
|
RID vertex_array;
|
|
|
|
RID index_array;
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t pipeline_push_constant_stages = 0;
|
2019-07-27 13:23:24 +00:00
|
|
|
} state;
|
|
|
|
|
2020-06-03 08:45:44 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2019-06-07 16:07:57 +00:00
|
|
|
struct Validation {
|
2020-06-03 08:45:44 +00:00
|
|
|
bool active = true; // Means command buffer was not closed, so you can keep adding things.
|
|
|
|
// Actual render pass values.
|
|
|
|
uint32_t dynamic_state = 0;
|
|
|
|
VertexFormatID vertex_format = INVALID_ID;
|
|
|
|
uint32_t vertex_array_size = 0;
|
|
|
|
uint32_t vertex_max_instances_allowed = 0xFFFFFFFF;
|
|
|
|
bool index_buffer_uses_restart_indices = false;
|
|
|
|
uint32_t index_array_size = 0;
|
|
|
|
uint32_t index_array_max_index = 0;
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t index_array_offset = 0;
|
2019-07-27 13:23:24 +00:00
|
|
|
Vector<uint32_t> set_formats;
|
|
|
|
Vector<bool> set_bound;
|
|
|
|
Vector<RID> set_rids;
|
2020-06-03 08:45:44 +00:00
|
|
|
// Last pipeline set values.
|
|
|
|
bool pipeline_active = false;
|
|
|
|
uint32_t pipeline_dynamic_state = 0;
|
|
|
|
VertexFormatID pipeline_vertex_format = INVALID_ID;
|
2019-07-27 13:23:24 +00:00
|
|
|
RID pipeline_shader;
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t invalid_set_from = 0;
|
|
|
|
bool pipeline_uses_restart_indices = false;
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t pipeline_primitive_divisor = 0;
|
|
|
|
uint32_t pipeline_primitive_minimum = 0;
|
2019-07-27 13:23:24 +00:00
|
|
|
Vector<uint32_t> pipeline_set_formats;
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t pipeline_push_constant_size = 0;
|
|
|
|
bool pipeline_push_constant_supplied = false;
|
2019-06-07 16:07:57 +00:00
|
|
|
} validation;
|
2019-07-12 13:18:30 +00:00
|
|
|
#else
|
|
|
|
struct Validation {
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t vertex_array_size = 0;
|
|
|
|
uint32_t index_array_size = 0;
|
2019-07-12 13:18:30 +00:00
|
|
|
uint32_t index_array_offset;
|
|
|
|
} validation;
|
2019-07-27 13:23:24 +00:00
|
|
|
#endif
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
DrawList *draw_list = nullptr; // One for regular draw lists, multiple for split.
|
2021-06-24 13:58:36 +00:00
|
|
|
uint32_t draw_list_subpass_count = 0;
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t draw_list_count = 0;
|
2021-06-24 13:58:36 +00:00
|
|
|
VkRenderPass draw_list_render_pass;
|
|
|
|
VkFramebuffer draw_list_vkframebuffer;
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
FramebufferFormatID draw_list_framebuffer_format = INVALID_ID;
|
|
|
|
#endif
|
|
|
|
uint32_t draw_list_current_subpass = 0;
|
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
bool draw_list_split = false;
|
2019-06-10 17:12:24 +00:00
|
|
|
Vector<RID> draw_list_bound_textures;
|
2020-06-25 13:33:28 +00:00
|
|
|
Vector<RID> draw_list_storage_textures;
|
2020-11-23 16:38:46 +00:00
|
|
|
bool draw_list_unbind_color_textures = false;
|
|
|
|
bool draw_list_unbind_depth_textures = false;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-10-03 20:39:08 +00:00
|
|
|
void _draw_list_insert_clear_region(DrawList *draw_list, Framebuffer *framebuffer, Point2i viewport_offset, Point2i viewport_size, bool p_clear_color, const Vector<Color> &p_clear_colors, bool p_clear_depth, float p_depth, uint32_t p_stencil);
|
2021-06-24 13:58:36 +00:00
|
|
|
Error _draw_list_setup_framebuffer(Framebuffer *p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, VkFramebuffer *r_framebuffer, VkRenderPass *r_render_pass, uint32_t *r_subpass_count);
|
2020-06-25 13:33:28 +00:00
|
|
|
Error _draw_list_render_pass_begin(Framebuffer *framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_colors, float p_clear_depth, uint32_t p_clear_stencil, Point2i viewport_offset, Point2i viewport_size, VkFramebuffer vkframebuffer, VkRenderPass render_pass, VkCommandBuffer command_buffer, VkSubpassContents subpass_contents, const Vector<RID> &p_storage_textures);
|
2019-06-10 17:12:24 +00:00
|
|
|
_FORCE_INLINE_ DrawList *_get_draw_list_ptr(DrawListID p_id);
|
2021-01-26 00:52:58 +00:00
|
|
|
Buffer *_get_buffer_from_owner(RID p_buffer, VkPipelineStageFlags &dst_stage_mask, VkAccessFlags &dst_access, uint32_t p_post_barrier);
|
2021-06-24 13:58:36 +00:00
|
|
|
Error _draw_list_allocate(const Rect2i &p_viewport, uint32_t p_splits, uint32_t p_subpass);
|
|
|
|
void _draw_list_free(Rect2i *r_last_viewport = nullptr);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-09-25 19:44:44 +00:00
|
|
|
/**********************/
|
|
|
|
/**** COMPUTE LIST ****/
|
|
|
|
/**********************/
|
|
|
|
|
|
|
|
struct ComputeList {
|
2020-11-23 16:38:46 +00:00
|
|
|
VkCommandBuffer command_buffer = VK_NULL_HANDLE; // If persistent, this is owned, otherwise it's shared with the ringbuffer.
|
2019-09-25 19:44:44 +00:00
|
|
|
|
|
|
|
struct SetState {
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t pipeline_expected_format = 0;
|
|
|
|
uint32_t uniform_set_format = 0;
|
|
|
|
VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
|
2019-09-25 19:44:44 +00:00
|
|
|
RID uniform_set;
|
2020-06-03 08:45:44 +00:00
|
|
|
bool bound = false;
|
2019-09-25 19:44:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct State {
|
|
|
|
Set<Texture *> textures_to_sampled_layout;
|
|
|
|
SetState sets[MAX_UNIFORM_SETS];
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t set_count = 0;
|
2019-09-25 19:44:44 +00:00
|
|
|
RID pipeline;
|
|
|
|
RID pipeline_shader;
|
2021-02-02 19:51:36 +00:00
|
|
|
uint32_t local_group_size[3] = { 0, 0, 0 };
|
2020-06-03 08:45:44 +00:00
|
|
|
VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
|
|
|
|
uint32_t pipeline_push_constant_stages = 0;
|
2021-02-02 19:51:36 +00:00
|
|
|
bool allow_draw_overlap;
|
2019-09-25 19:44:44 +00:00
|
|
|
} state;
|
|
|
|
|
2020-06-03 08:45:44 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2019-09-25 19:44:44 +00:00
|
|
|
struct Validation {
|
2020-06-03 08:45:44 +00:00
|
|
|
bool active = true; // Means command buffer was not closed, so you can keep adding things.
|
2019-09-25 19:44:44 +00:00
|
|
|
Vector<uint32_t> set_formats;
|
|
|
|
Vector<bool> set_bound;
|
|
|
|
Vector<RID> set_rids;
|
2020-06-03 08:45:44 +00:00
|
|
|
// Last pipeline set values.
|
|
|
|
bool pipeline_active = false;
|
2019-09-25 19:44:44 +00:00
|
|
|
RID pipeline_shader;
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t invalid_set_from = 0;
|
2019-09-25 19:44:44 +00:00
|
|
|
Vector<uint32_t> pipeline_set_formats;
|
2020-06-03 08:45:44 +00:00
|
|
|
uint32_t pipeline_push_constant_size = 0;
|
|
|
|
bool pipeline_push_constant_supplied = false;
|
2019-09-25 19:44:44 +00:00
|
|
|
} validation;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
ComputeList *compute_list = nullptr;
|
2019-09-25 19:44:44 +00:00
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
/**************************/
|
|
|
|
/**** FRAME MANAGEMENT ****/
|
|
|
|
/**************************/
|
|
|
|
|
|
|
|
// This is the frame structure. There are normally
|
|
|
|
// 3 of these (used for triple buffering), or 2
|
|
|
|
// (double buffering). They are cycled constantly.
|
|
|
|
//
|
|
|
|
// It contains two command buffers, one that is
|
|
|
|
// used internally for setting up (creating stuff)
|
|
|
|
// and another used mostly for drawing.
|
|
|
|
//
|
|
|
|
// They also contains a list of things that need
|
|
|
|
// to be disposed of when deleted, which can't
|
|
|
|
// happen immediately due to the asynchronous
|
|
|
|
// nature of the GPU. They will get deleted
|
|
|
|
// when the frame is cycled.
|
|
|
|
|
|
|
|
struct Frame {
|
|
|
|
//list in usage order, from last to free to first to free
|
|
|
|
List<Buffer> buffers_to_dispose_of;
|
|
|
|
List<Texture> textures_to_dispose_of;
|
|
|
|
List<Framebuffer> framebuffers_to_dispose_of;
|
|
|
|
List<VkSampler> samplers_to_dispose_of;
|
|
|
|
List<Shader> shaders_to_dispose_of;
|
|
|
|
List<VkBufferView> buffer_views_to_dispose_of;
|
|
|
|
List<UniformSet> uniform_sets_to_dispose_of;
|
2019-09-25 19:44:44 +00:00
|
|
|
List<RenderPipeline> render_pipelines_to_dispose_of;
|
|
|
|
List<ComputePipeline> compute_pipelines_to_dispose_of;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
VkCommandPool command_pool = VK_NULL_HANDLE;
|
|
|
|
VkCommandBuffer setup_command_buffer = VK_NULL_HANDLE; //used at the beginning of every frame for set-up
|
|
|
|
VkCommandBuffer draw_command_buffer = VK_NULL_HANDLE; //used at the beginning of every frame for set-up
|
2019-09-20 20:58:06 +00:00
|
|
|
|
|
|
|
struct Timestamp {
|
|
|
|
String description;
|
2020-11-23 16:38:46 +00:00
|
|
|
uint64_t value = 0;
|
2019-09-20 20:58:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
VkQueryPool timestamp_pool;
|
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
String *timestamp_names = nullptr;
|
|
|
|
uint64_t *timestamp_cpu_values = nullptr;
|
|
|
|
uint32_t timestamp_count = 0;
|
|
|
|
String *timestamp_result_names = nullptr;
|
|
|
|
uint64_t *timestamp_cpu_result_values = nullptr;
|
|
|
|
uint64_t *timestamp_result_values = nullptr;
|
|
|
|
uint32_t timestamp_result_count = 0;
|
|
|
|
uint64_t index = 0;
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t max_timestamp_query_elements = 0;
|
2019-09-20 20:58:06 +00:00
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
Frame *frames = nullptr; //frames available, for main device they are cycled (usually 3), for local devices only 1
|
|
|
|
int frame = 0; //current frame
|
|
|
|
int frame_count = 0; //total amount of frames
|
|
|
|
uint64_t frames_drawn = 0;
|
2020-04-18 23:30:57 +00:00
|
|
|
RID local_device;
|
|
|
|
bool local_device_processing = false;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-24 19:13:06 +00:00
|
|
|
void _free_pending_resources(int p_frame);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
VmaAllocator allocator = nullptr;
|
2022-02-12 11:47:08 +00:00
|
|
|
VmaPool small_allocs_pool = nullptr;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
VulkanContext *context = nullptr;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2021-07-02 23:14:19 +00:00
|
|
|
uint64_t image_memory = 0;
|
|
|
|
uint64_t buffer_memory = 0;
|
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
void _free_internal(RID p_id);
|
2019-08-26 20:43:58 +00:00
|
|
|
void _flush(bool p_current_frame);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
bool screen_prepared = false;
|
2019-06-24 19:13:06 +00:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void _free_rids(T &p_owner, const char *p_type);
|
|
|
|
|
2020-04-18 23:30:57 +00:00
|
|
|
void _finalize_command_bufers();
|
|
|
|
void _begin_frame();
|
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
public:
|
2020-03-17 06:33:00 +00:00
|
|
|
virtual RID texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data = Vector<Vector<uint8_t>>());
|
2019-06-10 17:12:24 +00:00
|
|
|
virtual RID texture_create_shared(const TextureView &p_view, RID p_with_texture);
|
2019-08-26 20:43:58 +00:00
|
|
|
|
2021-08-03 07:07:32 +00:00
|
|
|
virtual RID texture_create_shared_from_slice(const TextureView &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, uint32_t p_mipmaps = 1, TextureSliceType p_slice_type = TEXTURE_SLICE_2D);
|
2021-01-26 00:52:58 +00:00
|
|
|
virtual Error texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, uint32_t p_post_barrier = BARRIER_MASK_ALL);
|
2020-02-17 21:06:54 +00:00
|
|
|
virtual Vector<uint8_t> texture_get_data(RID p_texture, uint32_t p_layer);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-16 02:45:24 +00:00
|
|
|
virtual bool texture_is_format_supported_for_usage(DataFormat p_format, uint32_t p_usage) const;
|
2019-06-24 19:13:06 +00:00
|
|
|
virtual bool texture_is_shared(RID p_texture);
|
|
|
|
virtual bool texture_is_valid(RID p_texture);
|
2021-08-19 01:52:06 +00:00
|
|
|
virtual Size2i texture_size(RID p_texture);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2021-01-26 00:52:58 +00:00
|
|
|
virtual Error texture_copy(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer, uint32_t p_post_barrier = BARRIER_MASK_ALL);
|
|
|
|
virtual Error texture_clear(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers, uint32_t p_post_barrier = BARRIER_MASK_ALL);
|
|
|
|
virtual Error texture_resolve_multisample(RID p_from_texture, RID p_to_texture, uint32_t p_post_barrier = BARRIER_MASK_ALL);
|
2019-07-27 13:23:24 +00:00
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
/*********************/
|
|
|
|
/**** FRAMEBUFFER ****/
|
|
|
|
/*********************/
|
|
|
|
|
2021-05-07 13:19:04 +00:00
|
|
|
virtual FramebufferFormatID framebuffer_format_create(const Vector<AttachmentFormat> &p_format, uint32_t p_view_count = 1);
|
2021-06-24 13:58:36 +00:00
|
|
|
virtual FramebufferFormatID framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, Vector<FramebufferPass> &p_passes, uint32_t p_view_count = 1);
|
2021-01-17 16:25:38 +00:00
|
|
|
virtual FramebufferFormatID framebuffer_format_create_empty(TextureSamples p_samples = TEXTURE_SAMPLES_1);
|
2021-06-24 13:58:36 +00:00
|
|
|
virtual TextureSamples framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass = 0);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2021-05-07 13:19:04 +00:00
|
|
|
virtual RID framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
|
2021-06-24 13:58:36 +00:00
|
|
|
virtual RID framebuffer_create_multipass(const Vector<RID> &p_texture_attachments, Vector<FramebufferPass> &p_passes, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
|
2021-01-17 16:25:38 +00:00
|
|
|
virtual RID framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples = TEXTURE_SAMPLES_1, FramebufferFormatID p_format_check = INVALID_ID);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
virtual FramebufferFormatID framebuffer_get_format(RID p_framebuffer);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/*****************/
|
|
|
|
/**** SAMPLER ****/
|
|
|
|
/*****************/
|
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
virtual RID sampler_create(const SamplerState &p_state);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/**********************/
|
|
|
|
/**** VERTEX ARRAY ****/
|
|
|
|
/**********************/
|
|
|
|
|
2020-12-16 14:07:08 +00:00
|
|
|
virtual RID vertex_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_as_storage = false);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
// Internally reference counted, this ID is warranted to be unique for the same description, but needs to be freed as many times as it was allocated
|
2020-04-21 15:16:45 +00:00
|
|
|
virtual VertexFormatID vertex_format_create(const Vector<VertexAttribute> &p_vertex_formats);
|
2019-06-10 17:12:24 +00:00
|
|
|
virtual RID vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const Vector<RID> &p_src_buffers);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
virtual RID index_buffer_create(uint32_t p_size_indices, IndexBufferFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_restart_indices = false);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
virtual RID index_array_create(RID p_index_buffer, uint32_t p_index_offset, uint32_t p_index_count);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/****************/
|
|
|
|
/**** SHADER ****/
|
|
|
|
/****************/
|
|
|
|
|
Implement Binary Shader Compilation
* Added an extra stage before compiling shader, which is generating a binary blob.
* On Vulkan, this allows caching the SPIRV reflection information, which is expensive to parse.
* On other (future) RenderingDevices, it allows caching converted binary data, such as DXIL or MSL.
This PR makes the shader cache include the reflection information, hence editor startup times are significantly improved.
I tested this well and it appears to work, and I added a lot of consistency checks, but because it includes writing and reading binary information, rare bugs may pop up, so be aware.
There was not much of a choice for storing the reflection information, given shaders can be a lot, take a lot of space and take time to parse.
2021-07-25 14:22:55 +00:00
|
|
|
virtual String shader_get_binary_cache_key() const;
|
2021-08-16 17:51:29 +00:00
|
|
|
virtual Vector<uint8_t> shader_compile_binary_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name = "");
|
Implement Binary Shader Compilation
* Added an extra stage before compiling shader, which is generating a binary blob.
* On Vulkan, this allows caching the SPIRV reflection information, which is expensive to parse.
* On other (future) RenderingDevices, it allows caching converted binary data, such as DXIL or MSL.
This PR makes the shader cache include the reflection information, hence editor startup times are significantly improved.
I tested this well and it appears to work, and I added a lot of consistency checks, but because it includes writing and reading binary information, rare bugs may pop up, so be aware.
There was not much of a choice for storing the reflection information, given shaders can be a lot, take a lot of space and take time to parse.
2021-07-25 14:22:55 +00:00
|
|
|
|
|
|
|
virtual RID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary);
|
|
|
|
|
2019-08-18 22:40:52 +00:00
|
|
|
virtual uint32_t shader_get_vertex_input_attribute_mask(RID p_shader);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/*****************/
|
|
|
|
/**** UNIFORM ****/
|
|
|
|
/*****************/
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
virtual RID uniform_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>());
|
2020-06-25 13:33:28 +00:00
|
|
|
virtual RID storage_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>(), uint32_t p_usage = 0);
|
2020-02-17 21:06:54 +00:00
|
|
|
virtual RID texture_buffer_create(uint32_t p_size_elements, DataFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>());
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
virtual RID uniform_set_create(const Vector<Uniform> &p_uniforms, RID p_shader, uint32_t p_shader_set);
|
2019-06-16 02:45:24 +00:00
|
|
|
virtual bool uniform_set_is_valid(RID p_uniform_set);
|
2021-07-07 22:55:20 +00:00
|
|
|
virtual void uniform_set_set_invalidation_callback(RID p_uniform_set, UniformSetInvalidatedCallback p_callback, void *p_userdata);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2021-01-26 00:52:58 +00:00
|
|
|
virtual Error buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const void *p_data, uint32_t p_post_barrier = BARRIER_MASK_ALL); //works for any buffer
|
|
|
|
virtual Error buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size, uint32_t p_post_barrier = BARRIER_MASK_ALL);
|
2020-02-17 21:06:54 +00:00
|
|
|
virtual Vector<uint8_t> buffer_get_data(RID p_buffer);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/*************************/
|
|
|
|
/**** RENDER PIPELINE ****/
|
|
|
|
/*************************/
|
|
|
|
|
2021-07-09 19:48:28 +00:00
|
|
|
virtual RID render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const PipelineRasterizationState &p_rasterization_state, const PipelineMultisampleState &p_multisample_state, const PipelineDepthStencilState &p_depth_stencil_state, const PipelineColorBlendState &p_blend_state, int p_dynamic_state_flags = 0, uint32_t p_for_render_pass = 0, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
|
2019-06-24 19:13:06 +00:00
|
|
|
virtual bool render_pipeline_is_valid(RID p_pipeline);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-09-25 19:44:44 +00:00
|
|
|
/**************************/
|
|
|
|
/**** COMPUTE PIPELINE ****/
|
|
|
|
/**************************/
|
|
|
|
|
2021-07-09 19:48:28 +00:00
|
|
|
virtual RID compute_pipeline_create(RID p_shader, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
|
2019-09-25 19:44:44 +00:00
|
|
|
virtual bool compute_pipeline_is_valid(RID p_pipeline);
|
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
/****************/
|
|
|
|
/**** SCREEN ****/
|
|
|
|
/****************/
|
|
|
|
|
2020-03-04 01:51:12 +00:00
|
|
|
virtual int screen_get_width(DisplayServer::WindowID p_screen = 0) const;
|
|
|
|
virtual int screen_get_height(DisplayServer::WindowID p_screen = 0) const;
|
2019-06-10 17:12:24 +00:00
|
|
|
virtual FramebufferFormatID screen_get_framebuffer_format() const;
|
2019-06-07 16:07:57 +00:00
|
|
|
|
|
|
|
/********************/
|
|
|
|
/**** DRAW LISTS ****/
|
|
|
|
/********************/
|
|
|
|
|
2020-03-04 01:51:12 +00:00
|
|
|
virtual DrawListID draw_list_begin_for_screen(DisplayServer::WindowID p_screen = 0, const Color &p_clear_color = Color());
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2020-06-25 13:33:28 +00:00
|
|
|
virtual DrawListID draw_list_begin(RID p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth = 1.0, uint32_t p_clear_stencil = 0, const Rect2 &p_region = Rect2(), const Vector<RID> &p_storage_textures = Vector<RID>());
|
|
|
|
virtual Error draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, DrawListID *r_split_ids, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth = 1.0, uint32_t p_clear_stencil = 0, const Rect2 &p_region = Rect2(), const Vector<RID> &p_storage_textures = Vector<RID>());
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
virtual void draw_list_bind_render_pipeline(DrawListID p_list, RID p_render_pipeline);
|
|
|
|
virtual void draw_list_bind_uniform_set(DrawListID p_list, RID p_uniform_set, uint32_t p_index);
|
|
|
|
virtual void draw_list_bind_vertex_array(DrawListID p_list, RID p_vertex_array);
|
|
|
|
virtual void draw_list_bind_index_array(DrawListID p_list, RID p_index_array);
|
2019-06-24 19:13:06 +00:00
|
|
|
virtual void draw_list_set_line_width(DrawListID p_list, float p_width);
|
2020-04-20 02:19:21 +00:00
|
|
|
virtual void draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-10-03 20:39:08 +00:00
|
|
|
virtual void draw_list_draw(DrawListID p_list, bool p_use_indices, uint32_t p_instances = 1, uint32_t p_procedural_vertices = 0);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-06-10 17:12:24 +00:00
|
|
|
virtual void draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect);
|
|
|
|
virtual void draw_list_disable_scissor(DrawListID p_list);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2021-06-26 10:49:25 +00:00
|
|
|
virtual uint32_t draw_list_get_current_pass();
|
2021-06-24 13:58:36 +00:00
|
|
|
virtual DrawListID draw_list_switch_to_next_pass();
|
|
|
|
virtual Error draw_list_switch_to_next_pass_split(uint32_t p_splits, DrawListID *r_split_ids);
|
|
|
|
|
2021-01-26 00:52:58 +00:00
|
|
|
virtual void draw_list_end(uint32_t p_post_barrier = BARRIER_MASK_ALL);
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-09-25 19:44:44 +00:00
|
|
|
/***********************/
|
|
|
|
/**** COMPUTE LISTS ****/
|
|
|
|
/***********************/
|
|
|
|
|
2021-02-02 19:51:36 +00:00
|
|
|
virtual ComputeListID compute_list_begin(bool p_allow_draw_overlap = false);
|
2019-09-25 19:44:44 +00:00
|
|
|
virtual void compute_list_bind_compute_pipeline(ComputeListID p_list, RID p_compute_pipeline);
|
|
|
|
virtual void compute_list_bind_uniform_set(ComputeListID p_list, RID p_uniform_set, uint32_t p_index);
|
2020-04-20 02:19:21 +00:00
|
|
|
virtual void compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size);
|
2019-10-03 20:39:08 +00:00
|
|
|
virtual void compute_list_add_barrier(ComputeListID p_list);
|
|
|
|
|
2019-09-25 19:44:44 +00:00
|
|
|
virtual void compute_list_dispatch(ComputeListID p_list, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups);
|
2021-02-02 19:51:36 +00:00
|
|
|
virtual void compute_list_dispatch_threads(ComputeListID p_list, uint32_t p_x_threads, uint32_t p_y_threads, uint32_t p_z_threads);
|
2020-06-25 13:33:28 +00:00
|
|
|
virtual void compute_list_dispatch_indirect(ComputeListID p_list, RID p_buffer, uint32_t p_offset);
|
2021-01-26 00:52:58 +00:00
|
|
|
virtual void compute_list_end(uint32_t p_post_barrier = BARRIER_MASK_ALL);
|
2019-09-25 19:44:44 +00:00
|
|
|
|
2021-01-26 00:52:58 +00:00
|
|
|
virtual void barrier(uint32_t p_from = BARRIER_MASK_ALL, uint32_t p_to = BARRIER_MASK_ALL);
|
2020-06-25 13:33:28 +00:00
|
|
|
virtual void full_barrier();
|
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
/**************/
|
|
|
|
/**** FREE ****/
|
|
|
|
/**************/
|
|
|
|
|
2019-06-16 02:45:24 +00:00
|
|
|
virtual void free(RID p_id);
|
|
|
|
|
2019-09-20 20:58:06 +00:00
|
|
|
/****************/
|
|
|
|
/**** Timing ****/
|
|
|
|
/****************/
|
|
|
|
|
2021-01-26 00:52:58 +00:00
|
|
|
virtual void capture_timestamp(const String &p_name);
|
2019-09-20 20:58:06 +00:00
|
|
|
virtual uint32_t get_captured_timestamps_count() const;
|
|
|
|
virtual uint64_t get_captured_timestamps_frame() const;
|
|
|
|
virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const;
|
|
|
|
virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const;
|
|
|
|
virtual String get_captured_timestamp_name(uint32_t p_index) const;
|
|
|
|
|
|
|
|
/****************/
|
|
|
|
/**** Limits ****/
|
|
|
|
/****************/
|
|
|
|
|
2019-07-10 20:44:55 +00:00
|
|
|
virtual int limit_get(Limit p_limit);
|
|
|
|
|
2019-06-24 19:13:06 +00:00
|
|
|
virtual void prepare_screen_for_drawing();
|
2020-04-18 23:30:57 +00:00
|
|
|
void initialize(VulkanContext *p_context, bool p_local_device = false);
|
2019-06-07 16:07:57 +00:00
|
|
|
void finalize();
|
|
|
|
|
2020-04-18 23:30:57 +00:00
|
|
|
virtual void swap_buffers(); //for main device
|
|
|
|
|
|
|
|
virtual void submit(); //for local device
|
|
|
|
virtual void sync(); //for local device
|
2019-06-07 16:07:57 +00:00
|
|
|
|
2019-09-20 20:58:06 +00:00
|
|
|
virtual uint32_t get_frame_delay() const;
|
|
|
|
|
2020-04-18 23:30:57 +00:00
|
|
|
virtual RenderingDevice *create_local_device();
|
|
|
|
|
2021-07-02 23:14:19 +00:00
|
|
|
virtual uint64_t get_memory_usage(MemoryType p_type) const;
|
2020-05-01 12:34:23 +00:00
|
|
|
|
2021-01-24 06:21:54 +00:00
|
|
|
virtual void set_resource_name(RID p_id, const String p_name);
|
|
|
|
|
|
|
|
virtual void draw_command_begin_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1));
|
|
|
|
virtual void draw_command_insert_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1));
|
|
|
|
virtual void draw_command_end_label();
|
|
|
|
|
2021-02-02 19:51:36 +00:00
|
|
|
virtual String get_device_vendor_name() const;
|
|
|
|
virtual String get_device_name() const;
|
2021-12-10 16:01:51 +00:00
|
|
|
virtual RenderingDevice::DeviceType get_device_type() const;
|
2021-02-02 19:51:36 +00:00
|
|
|
virtual String get_device_pipeline_cache_uuid() const;
|
|
|
|
|
2021-08-29 02:52:19 +00:00
|
|
|
virtual uint64_t get_driver_resource(DriverResource p_resource, RID p_rid = RID(), uint64_t p_index = 0);
|
|
|
|
|
2019-06-07 16:07:57 +00:00
|
|
|
RenderingDeviceVulkan();
|
2020-04-18 23:30:57 +00:00
|
|
|
~RenderingDeviceVulkan();
|
2019-06-07 16:07:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RENDERING_DEVICE_VULKAN_H
|