Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Vulkan internal values for access from extensions #52203

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions doc/classes/RenderingDevice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@
<description>
</description>
</method>
<method name="get_driver_resource">
<return type="int" />
<argument index="0" name="resource" type="int" enum="RenderingDevice.DriverResource" />
<argument index="1" name="rid" type="RID" />
<argument index="2" name="index" type="int" />
<description>
</description>
</method>
<method name="get_frame_delay" qualifiers="const">
<return type="int" />
<description>
Expand Down Expand Up @@ -646,6 +654,32 @@
</constant>
<constant name="BARRIER_MASK_NO_BARRIER" value="8">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_DEVICE" value="0" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_PHYSICAL_DEVICE" value="1" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_INSTANCE" value="2" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_QUEUE" value="3" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_QUEUE_FAMILY_INDEX" value="4" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_IMAGE" value="5" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_IMAGE_VIEW" value="6" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_IMAGE_NATIVE_TEXTURE_FORMAT" value="7" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_SAMPLER" value="8" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_DESCRIPTOR_SET" value="9" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_BUFFER" value="10" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_COMPUTE_PIPELINE" value="11" enum="DriverResource">
</constant>
<constant name="DRIVER_RESOURCE_VULKAN_RENDER_PIPELINE" value="12" enum="DriverResource">
</constant>
<constant name="DATA_FORMAT_R4G4_UNORM_PACK8" value="0" enum="DataFormat">
</constant>
<constant name="DATA_FORMAT_R4G4B4A4_UNORM_PACK16" value="1" enum="DataFormat">
Expand Down
5 changes: 5 additions & 0 deletions doc/classes/RenderingServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,11 @@
<description>
</description>
</method>
<method name="get_rendering_device" qualifiers="const">
<return type="RenderingDevice" />
<description>
</description>
</method>
<method name="get_rendering_info">
<return type="int" />
<argument index="0" name="info" type="int" enum="RenderingServer.RenderingInfo" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/XRInterfaceExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
Blits our render results to screen optionally applying lens distortion. This can only be called while processing [code]_commit_views[/code].
</description>
</method>
<method name="get_render_target_texture">
<return type="RID" />
<argument index="0" name="render_target" type="RID" />
<description>
</description>
</method>
</methods>
<constants>
</constants>
Expand Down
90 changes: 88 additions & 2 deletions drivers/vulkan/rendering_device_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7588,7 +7588,7 @@ Error RenderingDeviceVulkan::_draw_list_allocate(const Rect2i &p_viewport, uint3
VkCommandPoolCreateInfo cmd_pool_info;
cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmd_pool_info.pNext = nullptr;
cmd_pool_info.queueFamilyIndex = context->get_graphics_queue();
cmd_pool_info.queueFamilyIndex = context->get_graphics_queue_family_index();
cmd_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;

VkResult res = vkCreateCommandPool(device, &cmd_pool_info, nullptr, &split_draw_list_allocators.write[i].command_pool);
Expand Down Expand Up @@ -8846,7 +8846,7 @@ void RenderingDeviceVulkan::initialize(VulkanContext *p_context, bool p_local_de
VkCommandPoolCreateInfo cmd_pool_info;
cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmd_pool_info.pNext = nullptr;
cmd_pool_info.queueFamilyIndex = p_context->get_graphics_queue();
cmd_pool_info.queueFamilyIndex = p_context->get_graphics_queue_family_index();
cmd_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;

VkResult res = vkCreateCommandPool(device, &cmd_pool_info, nullptr, &frames[i].command_pool);
Expand Down Expand Up @@ -9016,6 +9016,92 @@ void RenderingDeviceVulkan::capture_timestamp(const String &p_name) {
frames[frame].timestamp_count++;
}

uint64_t RenderingDeviceVulkan::get_driver_resource(DriverResource p_resource, RID p_rid, uint64_t p_index) {
_THREAD_SAFE_METHOD_

switch (p_resource) {
case DRIVER_RESOURCE_VULKAN_DEVICE: {
return (uint64_t)context->get_device();
}; break;
case DRIVER_RESOURCE_VULKAN_PHYSICAL_DEVICE: {
return (uint64_t)context->get_physical_device();
}; break;
case DRIVER_RESOURCE_VULKAN_INSTANCE: {
return (uint64_t)context->get_instance();
}; break;
case DRIVER_RESOURCE_VULKAN_QUEUE: {
return (uint64_t)context->get_graphics_queue();
}; break;
case DRIVER_RESOURCE_VULKAN_QUEUE_FAMILY_INDEX: {
return context->get_graphics_queue_family_index();
}; break;
case DRIVER_RESOURCE_VULKAN_IMAGE: {
Texture *tex = texture_owner.getornull(p_rid);
ERR_FAIL_NULL_V(tex, 0);

return (uint64_t)tex->image;
}; break;
case DRIVER_RESOURCE_VULKAN_IMAGE_VIEW: {
Texture *tex = texture_owner.getornull(p_rid);
ERR_FAIL_NULL_V(tex, 0);

return (uint64_t)tex->view;
}; break;
case DRIVER_RESOURCE_VULKAN_IMAGE_NATIVE_TEXTURE_FORMAT: {
Texture *tex = texture_owner.getornull(p_rid);
ERR_FAIL_NULL_V(tex, 0);

return vulkan_formats[tex->format];
}; break;
case DRIVER_RESOURCE_VULKAN_SAMPLER: {
VkSampler *sampler = sampler_owner.getornull(p_rid);
ERR_FAIL_NULL_V(sampler, 0);

return uint64_t(*sampler);
}; break;
case DRIVER_RESOURCE_VULKAN_DESCRIPTOR_SET: {
UniformSet *uniform_set = uniform_set_owner.getornull(p_rid);
ERR_FAIL_NULL_V(uniform_set, 0);

return uint64_t(uniform_set->descriptor_set);
}; break;
case DRIVER_RESOURCE_VULKAN_BUFFER: {
Buffer *buffer = nullptr;
if (vertex_buffer_owner.owns(p_rid)) {
buffer = vertex_buffer_owner.getornull(p_rid);
} else if (index_buffer_owner.owns(p_rid)) {
buffer = index_buffer_owner.getornull(p_rid);
} else if (uniform_buffer_owner.owns(p_rid)) {
buffer = uniform_buffer_owner.getornull(p_rid);
} else if (texture_buffer_owner.owns(p_rid)) {
buffer = &texture_buffer_owner.getornull(p_rid)->buffer;
} else if (storage_buffer_owner.owns(p_rid)) {
buffer = storage_buffer_owner.getornull(p_rid);
}

ERR_FAIL_NULL_V(buffer, 0);

return uint64_t(buffer->buffer);
}; break;
case DRIVER_RESOURCE_VULKAN_COMPUTE_PIPELINE: {
ComputePipeline *compute_pipeline = compute_pipeline_owner.getornull(p_rid);
ERR_FAIL_NULL_V(compute_pipeline, 0);

return uint64_t(compute_pipeline->pipeline);
}; break;
case DRIVER_RESOURCE_VULKAN_RENDER_PIPELINE: {
RenderPipeline *render_pipeline = render_pipeline_owner.getornull(p_rid);
ERR_FAIL_NULL_V(render_pipeline, 0);

return uint64_t(render_pipeline->pipeline);
}; break;
default: {
// not supported for this driver
return 0;
}; break;
}
}

uint32_t RenderingDeviceVulkan::get_captured_timestamps_count() const {
return frames[frame].timestamp_result_count;
}
Expand Down
2 changes: 2 additions & 0 deletions drivers/vulkan/rendering_device_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,8 @@ class RenderingDeviceVulkan : public RenderingDevice {
virtual String get_device_name() const;
virtual String get_device_pipeline_cache_uuid() const;

virtual uint64_t get_driver_resource(DriverResource p_resource, RID p_rid = RID(), uint64_t p_index = 0);

RenderingDeviceVulkan();
~RenderingDeviceVulkan();
};
Expand Down
6 changes: 5 additions & 1 deletion drivers/vulkan/vulkan_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2028,7 +2028,11 @@ int VulkanContext::get_swapchain_image_count() const {
return swapchainImageCount;
}

uint32_t VulkanContext::get_graphics_queue() const {
VkQueue VulkanContext::get_graphics_queue() const {
return graphics_queue;
}

uint32_t VulkanContext::get_graphics_queue_family_index() const {
return graphics_queue_family_index;
}

Expand Down
3 changes: 2 additions & 1 deletion drivers/vulkan/vulkan_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ class VulkanContext {
VkPhysicalDevice get_physical_device();
VkInstance get_instance() { return inst; }
int get_swapchain_image_count() const;
uint32_t get_graphics_queue() const;
VkQueue get_graphics_queue() const;
uint32_t get_graphics_queue_family_index() const;

void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height);
int window_get_width(DisplayServer::WindowID p_window = 0);
Expand Down
16 changes: 16 additions & 0 deletions servers/rendering/rendering_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,28 @@ void RenderingDevice::_bind_methods() {

ClassDB::bind_method(D_METHOD("get_memory_usage"), &RenderingDevice::get_memory_usage);

ClassDB::bind_method(D_METHOD("get_driver_resource", "resource", "rid", "index"), &RenderingDevice::get_driver_resource);

BIND_CONSTANT(BARRIER_MASK_RASTER);
BIND_CONSTANT(BARRIER_MASK_COMPUTE);
BIND_CONSTANT(BARRIER_MASK_TRANSFER);
BIND_CONSTANT(BARRIER_MASK_ALL);
BIND_CONSTANT(BARRIER_MASK_NO_BARRIER);

BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_DEVICE);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_PHYSICAL_DEVICE);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_INSTANCE);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_QUEUE);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_QUEUE_FAMILY_INDEX);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE_VIEW);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE_NATIVE_TEXTURE_FORMAT);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_SAMPLER);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_DESCRIPTOR_SET);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_BUFFER);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_COMPUTE_PIPELINE);
BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_RENDER_PIPELINE);

BIND_ENUM_CONSTANT(DATA_FORMAT_R4G4_UNORM_PACK8);
BIND_ENUM_CONSTANT(DATA_FORMAT_R4G4B4A4_UNORM_PACK16);
BIND_ENUM_CONSTANT(DATA_FORMAT_B4G4R4A4_UNORM_PACK16);
Expand Down
20 changes: 20 additions & 0 deletions servers/rendering/rendering_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ class RenderingDevice : public Object {
DEVICE_DIRECTX
};

enum DriverResource {
DRIVER_RESOURCE_VULKAN_DEVICE = 0,
DRIVER_RESOURCE_VULKAN_PHYSICAL_DEVICE,
DRIVER_RESOURCE_VULKAN_INSTANCE,
DRIVER_RESOURCE_VULKAN_QUEUE,
DRIVER_RESOURCE_VULKAN_QUEUE_FAMILY_INDEX,
DRIVER_RESOURCE_VULKAN_IMAGE,
DRIVER_RESOURCE_VULKAN_IMAGE_VIEW,
DRIVER_RESOURCE_VULKAN_IMAGE_NATIVE_TEXTURE_FORMAT,
DRIVER_RESOURCE_VULKAN_SAMPLER,
DRIVER_RESOURCE_VULKAN_DESCRIPTOR_SET,
DRIVER_RESOURCE_VULKAN_BUFFER,
DRIVER_RESOURCE_VULKAN_COMPUTE_PIPELINE,
DRIVER_RESOURCE_VULKAN_RENDER_PIPELINE,
//next driver continue enum from 1000 to keep order
};

enum ShaderStage {
SHADER_STAGE_VERTEX,
SHADER_STAGE_FRAGMENT,
Expand Down Expand Up @@ -1183,6 +1200,8 @@ class RenderingDevice : public Object {
virtual String get_device_name() const = 0;
virtual String get_device_pipeline_cache_uuid() const = 0;

virtual uint64_t get_driver_resource(DriverResource p_resource, RID p_rid = RID(), uint64_t p_index = 0) = 0;

static RenderingDevice *get_singleton();
RenderingDevice();

Expand Down Expand Up @@ -1217,6 +1236,7 @@ class RenderingDevice : public Object {
Vector<int64_t> _draw_list_switch_to_next_pass_split(uint32_t p_splits);
};

VARIANT_ENUM_CAST(RenderingDevice::DriverResource)
VARIANT_ENUM_CAST(RenderingDevice::ShaderStage)
VARIANT_ENUM_CAST(RenderingDevice::ShaderLanguage)
VARIANT_ENUM_CAST(RenderingDevice::CompareOperator)
Expand Down
6 changes: 6 additions & 0 deletions servers/rendering_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,11 @@ ShaderLanguage::DataType RenderingServer::global_variable_type_get_shader_dataty
}
}

RenderingDevice *RenderingServer::get_rendering_device() const {
// return the rendering device we're using globally
return RenderingDevice::get_singleton();
}

RenderingDevice *RenderingServer::create_local_rendering_device() const {
return RenderingDevice::get_singleton()->create_local_device();
}
Expand Down Expand Up @@ -2714,6 +2719,7 @@ void RenderingServer::_bind_methods() {

ClassDB::bind_method(D_METHOD("force_sync"), &RenderingServer::sync);
ClassDB::bind_method(D_METHOD("force_draw", "swap_buffers", "frame_step"), &RenderingServer::draw, DEFVAL(true), DEFVAL(0.0));
ClassDB::bind_method(D_METHOD("get_rendering_device"), &RenderingServer::get_rendering_device);
ClassDB::bind_method(D_METHOD("create_local_rendering_device"), &RenderingServer::create_local_rendering_device);
}

Expand Down
1 change: 1 addition & 0 deletions servers/rendering_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,7 @@ class RenderingServer : public Object {

virtual void set_print_gpu_profile(bool p_enable) = 0;

RenderingDevice *get_rendering_device() const;
RenderingDevice *create_local_rendering_device() const;

bool is_render_loop_enabled() const;
Expand Down
2 changes: 1 addition & 1 deletion servers/xr/xr_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/*************************************************************************/

#include "xr_interface.h"
#include "servers/rendering/renderer_compositor.h"
// #include "servers/rendering/renderer_compositor.h"

void XRInterface::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_name"), &XRInterface::get_name);
Expand Down
26 changes: 23 additions & 3 deletions servers/xr/xr_interface_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
/*************************************************************************/

#include "xr_interface_extension.h"
#include "servers/rendering/renderer_compositor.h"
#include "servers/rendering/renderer_storage.h"
#include "servers/rendering/rendering_server_globals.h"

void XRInterfaceExtension::_bind_methods() {
GDVIRTUAL_BIND(_get_name);
Expand All @@ -41,8 +42,6 @@ void XRInterfaceExtension::_bind_methods() {

GDVIRTUAL_BIND(_get_tracking_status);

ClassDB::bind_method(D_METHOD("add_blit", "render_target", "src_rect", "dst_rect", "use_layer", "layer", "apply_lens_distortion", "eye_center", "k1", "k2", "upscale", "aspect_ratio"), &XRInterfaceExtension::add_blit);

GDVIRTUAL_BIND(_get_render_target_size);
GDVIRTUAL_BIND(_get_view_count);
GDVIRTUAL_BIND(_get_camera_transform);
Expand All @@ -60,6 +59,11 @@ void XRInterfaceExtension::_bind_methods() {
GDVIRTUAL_BIND(_get_anchor_detection_is_enabled);
GDVIRTUAL_BIND(_set_anchor_detection_is_enabled, "enabled");
GDVIRTUAL_BIND(_get_camera_feed_id);

// helper methods
ClassDB::bind_method(D_METHOD("add_blit", "render_target", "src_rect", "dst_rect", "use_layer", "layer", "apply_lens_distortion", "eye_center", "k1", "k2", "upscale", "aspect_ratio"), &XRInterfaceExtension::add_blit);
ClassDB::bind_method(D_METHOD("get_render_target_texture", "render_target"), &XRInterfaceExtension::get_render_target_texture);
// ClassDB::bind_method(D_METHOD("get_render_target_depth", "render_target"), &XRInterfaceExtension::get_render_target_depth);
}

StringName XRInterfaceExtension::get_name() const {
Expand Down Expand Up @@ -240,3 +244,19 @@ void XRInterfaceExtension::process() {
void XRInterfaceExtension::notification(int p_what) {
GDVIRTUAL_CALL(_notification, p_what);
}

RID XRInterfaceExtension::get_render_target_texture(RID p_render_target) {
RendererStorage *storage = RSG::storage;
ERR_FAIL_NULL_V_MSG(storage, RID(), "Renderer storage not setup");

return storage->render_target_get_texture(p_render_target);
}

/*
RID XRInterfaceExtension::get_render_target_depth(RID p_render_target) {
RendererStorage *storage = RSG::storage;
ERR_FAIL_NULL_V_MSG(storage, RID(), "Renderer storage not setup");

return storage->render_target_get_depth(p_render_target);
}
*/
4 changes: 4 additions & 0 deletions servers/xr/xr_interface_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class XRInterfaceExtension : public XRInterface {

GDVIRTUAL0(_process);
GDVIRTUAL1(_notification, int);

/* access to some internals we need */
RID get_render_target_texture(RID p_render_target);
// RID get_render_target_depth(RID p_render_target);
};

#endif // !XR_INTERFACE_EXTENSION_H