Vulkan: Clearing Framebuffer + Scissors #106044

Merged
Jeroen Bakker merged 49 commits from Jeroen-Bakker/blender:vulkan-framebuffer-clear into main 2023-03-28 11:51:45 +02:00
Member

This PR adds support for clearing framebuffers and scissor testing.
Tweaks had to be made to VKTexture to keep track of its layout
on the device. Based on the actual call the layout can switch
to a more optimum layout.

For example during upload of a texture the texture will be converted
to a transfer destination optimized layout. When reading from the
texture it will be converted to a transfer source optimized layout.

The order of the attachments in the framebuffer follows the next rules

  • When only color attachments are there the color attachments will
    be placed in the slot they are defined. This way it will match
    the ShaderCreateInfo binding location.
  • When a stencil/depth attachment is added it will be placed
    right after the color attachments. When there isn't a color
    attachment it will be the first attachment.

Depth attachment in Vulkan doesn't have a specific binding location
as there can only be one in the framebuffer.

To demonstrate what is possible. This is the image created by the
GPUVulkanTest.framebuffer_scissor_test test case. Drawing with
scissors!

This PR adds support for clearing framebuffers and scissor testing. Tweaks had to be made to VKTexture to keep track of its layout on the device. Based on the actual call the layout can switch to a more optimum layout. For example during upload of a texture the texture will be converted to a transfer destination optimized layout. When reading from the texture it will be converted to a transfer source optimized layout. The order of the attachments in the framebuffer follows the next rules - When only color attachments are there the color attachments will be placed in the slot they are defined. This way it will match the ShaderCreateInfo binding location. - When a stencil/depth attachment is added it will be placed right after the color attachments. When there isn't a color attachment it will be the first attachment. Depth attachment in Vulkan doesn't have a specific binding location as there can only be one in the framebuffer. To demonstrate what is possible. This is the image created by the `GPUVulkanTest.framebuffer_scissor_test` test case. Drawing with scissors! ![](https://)
Jeroen Bakker added this to the 3.6 LTS milestone 2023-03-23 14:44:53 +01:00
Jeroen Bakker added the
Interest
Vulkan
label 2023-03-23 14:44:53 +01:00
Jeroen Bakker self-assigned this 2023-03-23 14:44:53 +01:00
Jeroen Bakker added 36 commits 2023-03-23 14:44:57 +01:00
Jeroen Bakker added a new dependency 2023-03-23 14:45:21 +01:00
Jeroen Bakker added a new dependency 2023-03-23 14:45:39 +01:00
Jeroen Bakker added this to the EEVEE & Viewport project 2023-03-23 14:48:57 +01:00
Jeroen Bakker changed title from Vulkan: Clearing Framebuffer to WIP: Vulkan: Clearing Framebuffer 2023-03-23 15:03:06 +01:00
Jeroen Bakker added 1 commit 2023-03-24 07:53:06 +01:00
Jeroen Bakker added 2 commits 2023-03-24 08:13:17 +01:00
Jeroen Bakker added 1 commit 2023-03-24 08:18:57 +01:00
Jeroen Bakker added 1 commit 2023-03-24 14:11:18 +01:00
Jeroen Bakker added 1 commit 2023-03-24 15:22:59 +01:00
Jeroen Bakker added 1 commit 2023-03-24 15:48:39 +01:00
Jeroen Bakker requested review from Bastien Montagne 2023-03-24 16:00:38 +01:00
Jeroen Bakker requested review from Clément Foucault 2023-03-24 16:00:45 +01:00
Jeroen Bakker changed title from WIP: Vulkan: Clearing Framebuffer to Vulkan: Clearing Framebuffer 2023-03-24 16:00:53 +01:00
Jeroen Bakker changed title from Vulkan: Clearing Framebuffer to Vulkan: Clearing Framebuffer + Scissors 2023-03-24 16:01:16 +01:00
Jeroen Bakker reviewed 2023-03-24 16:02:53 +01:00
@ -68,0 +82,4 @@
* \{ */
public:
/**
* Change the current layout setting, without actually changing the layout.
Author
Member

Update the current layout attribute, without actually changing the layout.

Update the current layout attribute, without actually changing the layout.
Jeroen-Bakker marked this conversation as resolved
Kazashi Yoshioka reviewed 2023-03-24 20:00:20 +01:00
@ -0,0 +64,4 @@
float4 *read_data2 = static_cast<float4 *>(GPU_texture_read(texture2, GPU_DATA_FLOAT, 0));
for (float4 pixel_color : Span<float4>(read_data1, size.x * size.y)) {
EXPECT_EQ(pixel_color, clear_color);
}
Contributor

This Check may result in an error.

Sometimes it clears correctly.


\blender\source\blender\gpu\tests\framebuffer_test.cc(66): error: Expected equality of these values:
pixel_color
Which is: (-1.9984e+18, -1.9984e+18, -1.9984e+18, -1.9984e+18)
clear_color
Which is: (0.1, 0.2, 0.5, 1)


A possible cause of this suspicious behavior is that the layout is only general.

This Check may result in an error. Sometimes it clears correctly. ----- \blender\source\blender\gpu\tests\framebuffer_test.cc(66): error: Expected equality of these values: pixel_color Which is: (-1.9984e+18, -1.9984e+18, -1.9984e+18, -1.9984e+18) clear_color Which is: (0.1, 0.2, 0.5, 1) ----- A possible cause of this suspicious behavior is that the layout is only general.
Jeroen-Bakker marked this conversation as resolved
Clément Foucault requested changes 2023-03-26 16:14:03 +02:00
@ -0,0 +80,4 @@
GPUTexture *texture1 = GPU_texture_create_2d(
__func__, UNPACK2(size), 1, GPU_RGBA32F, usage, nullptr);
GPUTexture *texture2 = GPU_texture_create_2d(
__func__, UNPACK2(size), 1, GPU_RGBA32F, usage, nullptr);

Would be best to test if clearing attachments with different formats work.
And also with integer format.

Would be best to test if clearing attachments with different formats work. And also with integer format.
Jeroen-Bakker marked this conversation as resolved
@ -62,0 +111,4 @@
VkClearAttachment clear_attachment = {};
clear_attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
clear_attachment.colorAttachment = color_slot;
copy_v4_v4(clear_attachment.clearValue.color.float32, clear_colors[color_index]);

I believe that doesn't work if the attachement is a integer texture. If I remember correctly we expect the data to be converted to (U)INT. So would still suggest to cast the floats to int so that 1.0f > 1.

After looking up the usage, it seem to be only used for object IDs and thoses are cleared to 0.

EDIT: In GL, we reinterpret the data:

    switch (data_format) {
      case GPU_DATA_FLOAT:
        glClearBufferfv(GL_COLOR, slot, (GLfloat *)clear_value);
        break;
      case GPU_DATA_UINT:
        glClearBufferuiv(GL_COLOR, slot, (GLuint *)clear_value);
        break;
      case GPU_DATA_INT:
        glClearBufferiv(GL_COLOR, slot, (GLint *)clear_value);
        break;
      default:
        BLI_assert_msg(0, "Unhandled data format");
        break;
    }
I believe that doesn't work if the attachement is a integer texture. If I remember correctly we expect the data to be converted to (U)INT. So would still suggest to cast the floats to int so that `1.0f > 1`. After looking up the usage, it seem to be only used for object IDs and thoses are cleared to 0. EDIT: In GL, we reinterpret the data: ``` switch (data_format) { case GPU_DATA_FLOAT: glClearBufferfv(GL_COLOR, slot, (GLfloat *)clear_value); break; case GPU_DATA_UINT: glClearBufferuiv(GL_COLOR, slot, (GLuint *)clear_value); break; case GPU_DATA_INT: glClearBufferiv(GL_COLOR, slot, (GLint *)clear_value); break; default: BLI_assert_msg(0, "Unhandled data format"); break; }
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 2 commits 2023-03-27 14:55:05 +02:00
Jeroen Bakker added 1 commit 2023-03-27 14:57:54 +02:00
Jeroen Bakker added 1 commit 2023-03-27 15:03:14 +02:00
Jeroen Bakker added 1 commit 2023-03-27 15:13:50 +02:00
Jeroen Bakker reviewed 2023-03-27 15:25:10 +02:00
@ -0,0 +64,4 @@
uint4 *read_data2 = static_cast<uint4 *>(GPU_texture_read(texture2, GPU_DATA_UINT, 0));
uint4 clear_color_uint(1036831949, 1045220557, 1056964608, 1065353216);
for (uint4 pixel_color : Span<uint4>(read_data2, size.x * size.y)) {
EXPECT_EQ(pixel_color, clear_color_uint);
Author
Member

The issue was a read out of bounds. Can you recheck if it still fails for you.

The root cause was that read_data1 was read, but was already freed. It should have checked read_data2.

The issue was a read out of bounds. Can you recheck if it still fails for you. The root cause was that `read_data1` was read, but was already freed. It should have checked `read_data2`.
Jeroen-Bakker marked this conversation as resolved
@ -62,0 +111,4 @@
VkClearAttachment clear_attachment = {};
clear_attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
clear_attachment.colorAttachment = color_slot;
copy_v4_v4(clear_attachment.clearValue.color.float32, clear_colors[color_index]);
Author
Member

Yes you're right, I misread the specifications how they handled SINT/UINT texture formats. Althought VkClearColorValue is an union and would have the same effect as the OpenGL code. I cleaned the code up so this is more visible.

I think we should change the API as the API uses a float parameter here, which is confusing when clearing other type of buffers. In that case I can move to_vk_clear_color_value to vk_common.cc and use that one. Something to do outside this patch.

Yes you're right, I misread the specifications how they handled SINT/UINT texture formats. Althought [`VkClearColorValue`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkClearColorValue.html) is an union and would have the same effect as the OpenGL code. I cleaned the code up so this is more visible. I think we should change the API as the API uses a float parameter here, which is confusing when clearing other type of buffers. In that case I can move `to_vk_clear_color_value` to `vk_common.cc` and use that one. Something to do outside this patch.
Jeroen-Bakker marked this conversation as resolved
Clément Foucault approved these changes 2023-03-27 17:14:18 +02:00
Jeroen Bakker added 1 commit 2023-03-28 10:51:27 +02:00
Jeroen Bakker merged commit aa69ec7f80 into main 2023-03-28 11:51:45 +02:00
Jeroen Bakker deleted branch vulkan-framebuffer-clear 2023-03-28 11:51:45 +02:00
Kazashi Yoshioka reviewed 2023-03-28 23:02:42 +02:00
@ -99,0 +330,4 @@
VkSubpassDescription subpass = {};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = color_attachment_len;
subpass.pColorAttachments = attachment_references.begin();
Contributor

In case of windows machine, this part does not pass.
attachment_references.data();
image_views.data();
No problem here.

In case of windows machine, this part does not pass. `attachment_references.data();` `image_views.data();` No problem here.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Reference: blender/blender#106044
No description provided.