Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | EpEp |
So I’ve managed to get a compute pipeline working, but now I’m trying to render a triangle using a render pipeline. I’ve wrote the following code by referencing the source code and as far as I can see it should work. I very limited experience with GPU related things so I might very well be missing something completely.
extends Node3D
@onready var rd := RenderingServer.get_rendering_device()
func _ready():
var shader_file: RDShaderFile = load("res://RenderTest.glsl")
var shader_spirv: RDShaderSPIRV = shader_file.get_spirv()
var shader := rd.shader_create_from_spirv(shader_spirv)
# Create format
var vertex_attrs = [RDVertexAttribute.new()]
vertex_attrs[0].format = RenderingDevice.DATA_FORMAT_R8G8B8_SNORM
vertex_attrs[0].location = 0
var vertex_format = rd.vertex_format_create(vertex_attrs)
# Create Vertex buffers
var points := PackedFloat32Array([
0, 0, 0,
0, 1, 0,
1, 1, 0,
])
var points_bytes := points.to_byte_array()
var vertex_buffers := [rd.vertex_buffer_create(points_bytes.size(), points_bytes)]
# Create vertex array
var vertex_array := rd.vertex_array_create(3, vertex_format, vertex_buffers)
# Blend
var blend = RDPipelineColorBlendState.new()
blend.attachments.push_back(RDPipelineColorBlendStateAttachment.new())
# Create empty framebuffer size of viewport
var rect_size = get_viewport().get_visible_rect().size
var screen_size = Vector2i(rect_size.x, rect_size.y)
var framebuffer = rd.framebuffer_create_empty(screen_size)
# Define pipe
var pipeline := rd.render_pipeline_create(
shader,
rd.screen_get_framebuffer_format(),
vertex_format,
RenderingDevice.RENDER_PRIMITIVE_TRIANGLES,
RDPipelineRasterizationState.new(),
RDPipelineMultisampleState.new(),
RDPipelineDepthStencilState.new(),
blend,
0
)
# Create render pipeline
if rd.render_pipeline_is_valid(pipeline):
var draw_list := rd.draw_list_begin(framebuffer, RenderingDevice.INITIAL_ACTION_DROP, RenderingDevice.FINAL_ACTION_DISCARD, RenderingDevice.INITIAL_ACTION_DROP, RenderingDevice.FINAL_ACTION_DISCARD)
# Bind pipe
rd.draw_list_bind_render_pipeline(draw_list, pipeline)
# Bind vertex array
rd.draw_list_bind_vertex_array(draw_list, vertex_array)
# Render
rd.draw_list_draw(draw_list, false, 1)
rd.draw_list_end()
else:
print("Invalid pipe")
And this is the shader file, I have limitex experience with shaders but as far as I understand this should be sufficient to render a mesh. Everything executes without errors, but no triangle is displayed…
#[vertex]
#version 450 core
layout(location = 0) in vec3 vertex_attrib;
void main()
{
gl_Position = vec4(vertex_attrib, 1.0);
}
#[fragment]
#version 450 core
layout (location = 0) out vec4 frag_color;
void main()
{
//Default color
frag_color = vec4(1,1,1,1);
}
Note that the function “vertex_array_create” is not currently (commit cf194847) bound, as pre this gitHub issue, so I added the following line of code to rendering_device.cpp, thus making it accessible, and recompiled. As far as I can see this works fine.
ClassDB::bind_method(D_METHOD("vertex_array_create", "vertex_count", "vertex_format", "buffers"), &RenderingDevice::_vertex_array_create)
Note that the function “vertexarraycreate” is not currently (commit cf194847) bound, as pre this gitHub issue, so I added the following line of code to rendering_device.cpp, thus making it accessible, and recompiled. As far as I can see this works fine.
The method is bound as of 4.0.stable: https://github.com/godotengine/godot/blob/92b7a9603aa2395be6bf361067096538ba393c45/servers/rendering/rendering_device.cpp#L729
Calinou | 2023-04-06 17:08