mirror of
https://github.com/korenkonder/ReDIVA.git
synced 2026-10-04 04:37:55 +03:00
Separation and small optimization. v0.7.1.0
This commit is contained in:
+160
-98
@@ -4,12 +4,18 @@
|
||||
*/
|
||||
|
||||
#include "render_texture.hpp"
|
||||
#include "Vulkan/buffer.hpp"
|
||||
#include "gl_state.hpp"
|
||||
#include "render_context.hpp"
|
||||
#include "texture.hpp"
|
||||
|
||||
static shader_glsl render_texture_shader;
|
||||
static GLuint render_texture_vao;
|
||||
static GLuint render_texture_vbo;
|
||||
|
||||
static VkVertexInputAttributeDescription render_texture_vertex_input_attribute_descriptions[16];
|
||||
static VkVertexInputBindingDescription render_texture_vertex_input_binding_descriptions[2];
|
||||
static Vulkan::Buffer render_texture_vertex_data;
|
||||
static Vulkan::Buffer render_texture_dummy_data;
|
||||
|
||||
static bool render_texture_data_initialized;
|
||||
static uint32_t render_texture_counter;
|
||||
|
||||
@@ -43,7 +49,7 @@ void render_texture::draw(bool depth) {
|
||||
if (depth && depth_texture->tex)
|
||||
gl_state_active_bind_texture_2d(1, depth_texture->tex);
|
||||
gl_state_bind_vertex_array(render_texture_vao);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
gl_state_bind_vertex_array(0);
|
||||
}
|
||||
|
||||
@@ -145,130 +151,186 @@ int32_t render_texture::set_color_depth_textures(GLuint color_texture,
|
||||
return error;
|
||||
}
|
||||
|
||||
void render_texture::draw_custom(shader_set_data* set) {
|
||||
void render_texture::draw(shader_set_data* set) {
|
||||
gl_state_bind_vertex_array(render_texture_vao);
|
||||
set->draw_arrays(GL_TRIANGLE_STRIP, 0, 3);
|
||||
gl_state_bind_vertex_array(0);
|
||||
set->draw_arrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
|
||||
void render_texture::draw_custom_glsl() {
|
||||
void render_texture::draw_custom() {
|
||||
gl_state_bind_vertex_array(render_texture_vao);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
|
||||
gl_state_bind_vertex_array(0);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
|
||||
void render_texture::draw_params(shader_set_data* set, int32_t width, int32_t height,
|
||||
void render_texture::draw_quad(shader_set_data* set, int32_t width, int32_t height,
|
||||
float_t scale, float_t param_x, float_t param_y, float_t param_z, float_t param_w) {
|
||||
extern render_context* rctx_ptr;
|
||||
|
||||
float_t w = (float_t)max_def(width, 1);
|
||||
float_t h = (float_t)max_def(height, 1);
|
||||
set->local_vert_set(0, scale / w, scale / h, w, h);
|
||||
set->local_frag_set(0, scale / w, scale / h, w, h);
|
||||
set->local_vert_set(3, param_x, param_y, param_z, param_w);
|
||||
set->local_frag_set(3, param_x, param_y, param_z, param_w);
|
||||
quad_shader_data quad = {};
|
||||
quad.g_texcoord_modifier = { 0.5f, 0.5f, 0.5f, 0.5f }; // x * 0.5 + 0.5
|
||||
quad.g_texel_size = { scale / w, scale / h, w, h };
|
||||
quad.g_color = { param_x, param_y, param_z, param_w };
|
||||
quad.g_texture_lod = 0.0f;
|
||||
|
||||
rctx_ptr->quad_ubo.WriteMapMemory(quad);
|
||||
rctx_ptr->quad_ubo.Bind(0);
|
||||
gl_state_bind_vertex_array(render_texture_vao);
|
||||
set->draw_arrays(GL_TRIANGLE_STRIP, 0, 3);
|
||||
gl_state_bind_vertex_array(0);
|
||||
set->draw_arrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
|
||||
void render_texture::shader_set(shader_glsl* shader) {
|
||||
if (shader)
|
||||
shader->use();
|
||||
else
|
||||
render_texture_shader.use();
|
||||
}
|
||||
|
||||
void render_texture_data_init() {
|
||||
void render_texture_opengl_data_init() {
|
||||
if (render_texture_data_initialized)
|
||||
return;
|
||||
|
||||
static const char* render_texture_vert_shader =
|
||||
"#version 430 core\n"
|
||||
"out VertexData {\n"
|
||||
" vec2 texcoord;\n"
|
||||
"} result;\n"
|
||||
"\n"
|
||||
"void main() {\n"
|
||||
" gl_Position.x = -1.0 + float(gl_VertexID / 2) * 4.0;\n"
|
||||
" gl_Position.y = 1.0 - float(gl_VertexID % 2) * 4.0;\n"
|
||||
" gl_Position.z = 0.0;\n"
|
||||
" gl_Position.w = 1.0;\n"
|
||||
" result.texcoord = gl_Position.xy * 0.5 + 0.5;\n"
|
||||
"}\n";
|
||||
|
||||
static const char* render_texture_frag_shader =
|
||||
"#version 430 core\n"
|
||||
"layout(location = 0) out vec4 result;\n"
|
||||
"\n"
|
||||
"in VertexData {\n"
|
||||
" vec2 texcoord;\n"
|
||||
"} frg;\n"
|
||||
"\n"
|
||||
"layout(binding = 0) uniform sampler2D g_color;\n"
|
||||
"\n"
|
||||
"void main() {\n"
|
||||
" result = texture(g_color, frg.texcoord);\n"
|
||||
"}\n";
|
||||
|
||||
shader_glsl_param param = {};
|
||||
param.name = "Render Texture";
|
||||
render_texture_shader.load(render_texture_vert_shader,
|
||||
render_texture_frag_shader, 0, ¶m);
|
||||
|
||||
const float_t verts_quad[] = {
|
||||
-1.0f, 1.0f, 0.0f, 1.0f,
|
||||
-1.0f, -3.0f, 0.0f, -1.0f,
|
||||
3.0f, 1.0f, 2.0f, 1.0f,
|
||||
};
|
||||
|
||||
glGenVertexArrays(1, &render_texture_vao);
|
||||
gl_state_bind_vertex_array(render_texture_vao, true);
|
||||
|
||||
glGenBuffers(1, &render_texture_vbo);
|
||||
gl_state_bind_array_buffer(render_texture_vbo, true);
|
||||
if (GLAD_GL_VERSION_4_4)
|
||||
glBufferStorage(GL_ARRAY_BUFFER, sizeof(verts_quad), verts_quad, 0);
|
||||
else
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verts_quad), verts_quad, GL_STATIC_DRAW);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 16, (void*)0); // Pos
|
||||
glVertexAttrib4f(3, 1.0f, 1.0f, 1.0f, 1.0f); // Color0
|
||||
glVertexAttrib4f(4, 1.0f, 1.0f, 1.0f, 1.0f); // Color1
|
||||
glEnableVertexAttribArray(8);
|
||||
glVertexAttribPointer(8, 2, GL_FLOAT, GL_FALSE, 16, (void*)8); // TexCoord0
|
||||
glEnableVertexAttribArray(9);
|
||||
glVertexAttribPointer(9, 2, GL_FLOAT, GL_FALSE, 16, (void*)8); // TexCoord1
|
||||
glEnableVertexAttribArray(10);
|
||||
glVertexAttribPointer(10, 2, GL_FLOAT, GL_FALSE, 16, (void*)8); // TexCoord2
|
||||
glEnableVertexAttribArray(11);
|
||||
glVertexAttribPointer(11, 2, GL_FLOAT, GL_FALSE, 16, (void*)8); // TexCoord3
|
||||
glEnableVertexAttribArray(12);
|
||||
glVertexAttribPointer(12, 2, GL_FLOAT, GL_FALSE, 16, (void*)8); // TexCoord4
|
||||
glEnableVertexAttribArray(13);
|
||||
glVertexAttribPointer(13, 2, GL_FLOAT, GL_FALSE, 16, (void*)8); // TexCoord5
|
||||
glEnableVertexAttribArray(14);
|
||||
glVertexAttribPointer(14, 2, GL_FLOAT, GL_FALSE, 16, (void*)8); // TexCoord6
|
||||
glEnableVertexAttribArray(15);
|
||||
glVertexAttribPointer(15, 2, GL_FLOAT, GL_FALSE, 16, (void*)8); // TexCoord7
|
||||
gl_state_bind_array_buffer(0);
|
||||
gl_state_bind_vertex_array(0);
|
||||
|
||||
render_texture_data_initialized = true;
|
||||
}
|
||||
|
||||
void render_texture_data_free() {
|
||||
void render_texture_opengl_data_free() {
|
||||
if (!render_texture_data_initialized)
|
||||
return;
|
||||
|
||||
render_texture_shader.unload();
|
||||
|
||||
glDeleteBuffers(1, &render_texture_vbo);
|
||||
glDeleteVertexArrays(1, &render_texture_vao);
|
||||
|
||||
render_texture_data_initialized = false;
|
||||
}
|
||||
|
||||
void render_texture_vulkan_data_init(VkDevice device,
|
||||
VmaAllocator allocator, VkCommandPool command_pool, VkQueue queue) {
|
||||
if (render_texture_data_initialized)
|
||||
return;
|
||||
|
||||
const float_t vertex_data[] = {
|
||||
-1.0f, 1.0f, 0.0f, 1.0f,
|
||||
-1.0f, -3.0f, 0.0f, -1.0f,
|
||||
3.0f, 1.0f, 2.0f, 1.0f,
|
||||
};
|
||||
|
||||
const float_t dummy_data[] = {
|
||||
0.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f, 1.0f,
|
||||
};
|
||||
|
||||
const size_t vertex_data_size = sizeof(vertex_data);
|
||||
const size_t dummy_data_size = sizeof(dummy_data);
|
||||
|
||||
VkVertexInputAttributeDescription* attribute_descriptions = render_texture_vertex_input_attribute_descriptions;
|
||||
attribute_descriptions[0] = { 0, 0, VK_FORMAT_R32G32_SFLOAT, 0 };
|
||||
attribute_descriptions[1] = { 1, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[2] = { 2, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[3] = { 3, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 8 };
|
||||
attribute_descriptions[4] = { 4, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 8 };
|
||||
attribute_descriptions[5] = { 5, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[6] = { 6, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[7] = { 7, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[8] = { 8, 0, VK_FORMAT_R32G32_SFLOAT, 0 };
|
||||
attribute_descriptions[9] = { 9, 0, VK_FORMAT_R32G32_SFLOAT, 0 };
|
||||
attribute_descriptions[10] = { 10, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[11] = { 11, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[12] = { 12, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[13] = { 13, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[14] = { 14, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
attribute_descriptions[15] = { 15, 1, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(float_t) * 4 };
|
||||
|
||||
VkVertexInputBindingDescription* binding_descriptions = render_texture_vertex_input_binding_descriptions;
|
||||
binding_descriptions[0] = { 0, sizeof(float_t) * 4, VK_VERTEX_INPUT_RATE_VERTEX };
|
||||
binding_descriptions[1] = { 1, sizeof(float_t) * 4, VK_VERTEX_INPUT_RATE_INSTANCE };
|
||||
|
||||
render_texture_vertex_data.Create(allocator, vertex_data_size,
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT
|
||||
| VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||
VMA_MEMORY_USAGE_AUTO,
|
||||
VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT);
|
||||
|
||||
render_texture_dummy_data.Create(allocator, dummy_data_size,
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT
|
||||
| VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||
VMA_MEMORY_USAGE_AUTO,
|
||||
VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT);
|
||||
|
||||
VkCommandBufferAllocateInfo alloc_info = {};
|
||||
alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
alloc_info.commandPool = command_pool;
|
||||
alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
alloc_info.commandBufferCount = 1;
|
||||
|
||||
VkCommandBuffer command_buffer;
|
||||
vkAllocateCommandBuffers(device, &alloc_info, &command_buffer);
|
||||
|
||||
VkCommandBufferBeginInfo begin_info = {};
|
||||
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||
|
||||
Vulkan::Buffer staging_buffer;
|
||||
staging_buffer.Create(allocator, max_def(vertex_data_size, dummy_data_size),
|
||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
VMA_MEMORY_USAGE_AUTO,
|
||||
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
|
||||
VMA_ALLOCATION_CREATE_MAPPED_BIT);
|
||||
|
||||
vkBeginCommandBuffer(command_buffer, &begin_info);
|
||||
|
||||
staging_buffer.WriteMapMemory(vertex_data, 0, vertex_data_size);
|
||||
|
||||
VkBufferCopy copy_region = {};
|
||||
copy_region.size = vertex_data_size;
|
||||
vkCmdCopyBuffer(command_buffer, staging_buffer.data, render_texture_vertex_data.data, 1, ©_region);
|
||||
|
||||
staging_buffer.WriteMapMemory(dummy_data, 0, dummy_data_size);
|
||||
|
||||
copy_region = {};
|
||||
copy_region.size = dummy_data_size;
|
||||
vkCmdCopyBuffer(command_buffer, staging_buffer.data, render_texture_dummy_data.data, 1, ©_region);
|
||||
|
||||
vkEndCommandBuffer(command_buffer);
|
||||
|
||||
staging_buffer.Destroy();
|
||||
|
||||
VkSubmitInfo submit_info = {};
|
||||
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
submit_info.commandBufferCount = 1;
|
||||
submit_info.pCommandBuffers = &command_buffer;
|
||||
|
||||
vkQueueSubmit(queue, 1, &submit_info, 0);
|
||||
vkQueueWaitIdle(queue);
|
||||
|
||||
vkFreeCommandBuffers(device, command_pool, 1, &command_buffer);
|
||||
|
||||
render_texture_data_initialized = true;
|
||||
}
|
||||
|
||||
void render_texture_vulkan_data_get_vertex_input_attribute_descriptions(
|
||||
const VkVertexInputAttributeDescription*& attribute_descriptions, uint32_t& attribute_description_count) {
|
||||
attribute_descriptions = render_texture_vertex_input_attribute_descriptions;
|
||||
attribute_description_count = sizeof(render_texture_vertex_input_attribute_descriptions)
|
||||
/ sizeof(VkVertexInputAttributeDescription);
|
||||
}
|
||||
|
||||
void render_texture_vulkan_data_get_vertex_input_binding_descriptions(
|
||||
const VkVertexInputBindingDescription*& binding_descriptions, uint32_t& binding_description_count) {
|
||||
binding_descriptions = render_texture_vertex_input_binding_descriptions;
|
||||
binding_description_count = sizeof(render_texture_vertex_input_binding_descriptions)
|
||||
/ sizeof(VkVertexInputBindingDescription);
|
||||
}
|
||||
|
||||
void render_texture_vulkan_data_free(VkDevice device, VmaAllocator allocator) {
|
||||
if (!render_texture_data_initialized)
|
||||
return;
|
||||
|
||||
render_texture_dummy_data.Destroy();
|
||||
render_texture_vertex_data.Destroy();
|
||||
|
||||
memset(render_texture_vertex_input_binding_descriptions,
|
||||
0, sizeof(render_texture_vertex_input_binding_descriptions));
|
||||
memset(render_texture_vertex_input_attribute_descriptions,
|
||||
0, sizeof(render_texture_vertex_input_attribute_descriptions));
|
||||
|
||||
render_texture_data_initialized = false;
|
||||
}
|
||||
|
||||
static int32_t render_texture_framebuffer_init(render_texture* rt, int32_t max_level) {
|
||||
glGenFramebuffers(max_level + 1, rt->fbos);
|
||||
return -(glGetError() != GL_ZERO);
|
||||
|
||||
Reference in New Issue
Block a user