xash3d-fwgs/ref/vk/vk_overlay.c

300 lines
8.3 KiB
C
Raw Normal View History

2022-05-30 22:04:19 +02:00
#include "vk_overlay.h"
2021-01-13 20:54:34 +01:00
2021-01-16 22:22:31 +01:00
#include "vk_buffer.h"
2021-01-13 20:54:34 +01:00
#include "vk_core.h"
#include "vk_common.h"
#include "vk_textures.h"
#include "vk_framectl.h"
#include "vk_renderstate.h"
2021-01-23 22:23:25 +01:00
#include "vk_pipeline.h"
#include "vk_descriptor.h"
2021-01-13 20:54:34 +01:00
#include "com_strings.h"
#include "eiface.h"
typedef struct vertex_2d_s {
float x, y;
float u, v;
color_rgba8_t color;
2021-01-13 20:54:34 +01:00
} vertex_2d_t;
// TODO should these be dynamic?
#define MAX_PICS 16384
#define MAX_VERTICES (MAX_PICS * 6)
#define MAX_BATCHES 256
2021-01-16 22:22:31 +01:00
typedef struct {
uint32_t vertex_offset, vertex_count;
int texture;
int blending_mode;
} batch_t;
static struct {
2021-01-23 22:23:25 +01:00
VkPipelineLayout pipeline_layout;
VkPipeline pipelines[kRenderTransAdd + 1];
2021-01-16 22:22:31 +01:00
vk_buffer_t pics_buffer;
r_flipping_buffer_t pics_buffer_alloc;
qboolean exhausted_this_frame;
2021-01-16 22:22:31 +01:00
batch_t batch[MAX_BATCHES];
int batch_count;
2021-01-16 22:22:31 +01:00
// TODO texture bindings?
} g2d;
static vertex_2d_t* allocQuadVerts(int blending_mode, int texnum) {
const uint32_t pics_offset = R_FlippingBuffer_Alloc(&g2d.pics_buffer_alloc, 6, 1);
vertex_2d_t* const ptr = ((vertex_2d_t*)(g2d.pics_buffer.mapped)) + pics_offset;
batch_t *batch = g2d.batch + (g2d.batch_count-1);
if (pics_offset == ALO_ALLOC_FAILED) {
if (!g2d.exhausted_this_frame) {
gEngine.Con_Printf(S_ERROR "2d: ran out of vertex memory\n");
g2d.exhausted_this_frame = true;
}
return NULL;
}
if (batch->texture != texnum
|| batch->blending_mode != blending_mode
|| batch->vertex_offset > pics_offset) {
if (batch->vertex_count != 0) {
if (g2d.batch_count == MAX_BATCHES) {
if (!g2d.exhausted_this_frame) {
gEngine.Con_Printf(S_ERROR "2d: ran out of batch memory\n");
g2d.exhausted_this_frame = true;
}
2021-02-06 22:13:08 +01:00
return NULL;
}
++g2d.batch_count;
batch++;
}
batch->vertex_offset = pics_offset;
batch->vertex_count = 0;
batch->texture = texnum;
batch->blending_mode = blending_mode;
}
batch->vertex_count += 6;
ASSERT(batch->vertex_count + batch->vertex_offset <= MAX_VERTICES);
2021-02-06 22:13:08 +01:00
return ptr;
}
void R_DrawStretchPic( float x, float y, float w, float h, float s1, float t1, float s2, float t2, int texnum )
{
2022-05-30 22:04:19 +02:00
vertex_2d_t *const p = allocQuadVerts(vk_renderstate.blending_mode, texnum);
2021-02-06 22:13:08 +01:00
if (!p) {
/* gEngine.Con_Printf(S_ERROR "VK FIXME %s(%f, %f, %f, %f, %f, %f, %f, %f, %d(%s))\n", __FUNCTION__, */
/* x, y, w, h, s1, t1, s2, t2, texnum, findTexture(texnum)->name); */
2021-01-16 22:22:31 +01:00
return;
}
{
2022-02-05 06:55:42 +01:00
// TODO do this in shader bro
2021-11-02 17:18:09 +01:00
const float vw = vk_frame.width;
const float vh = vk_frame.height;
2021-01-16 22:22:31 +01:00
const float x1 = (x / vw)*2.f - 1.f;
const float y1 = (y / vh)*2.f - 1.f;
const float x2 = ((x + w) / vw)*2.f - 1.f;
const float y2 = ((y + h) / vh)*2.f - 1.f;
const color_rgba8_t color = vk_renderstate.tri_color;
2021-01-16 22:22:31 +01:00
p[0] = (vertex_2d_t){x1, y1, s1, t1, color};
p[1] = (vertex_2d_t){x1, y2, s1, t2, color};
p[2] = (vertex_2d_t){x2, y1, s2, t1, color};
p[3] = (vertex_2d_t){x2, y1, s2, t1, color};
p[4] = (vertex_2d_t){x1, y2, s1, t2, color};
p[5] = (vertex_2d_t){x2, y2, s2, t2, color};
2021-01-16 22:22:31 +01:00
}
}
2021-02-06 22:13:08 +01:00
static void drawFill( float x, float y, float w, float h, int r, int g, int b, int a, int blending_mode )
{
const color_rgba8_t prev_color = vk_renderstate.tri_color;
const int prev_blending = vk_renderstate.blending_mode;
vk_renderstate.blending_mode = blending_mode;
vk_renderstate.tri_color = (color_rgba8_t){r, g, b, a};
R_DrawStretchPic(x, y, w, h, 0, 0, 1, 1, VK_FindTexture(REF_WHITE_TEXTURE));
vk_renderstate.tri_color = prev_color;
vk_renderstate.blending_mode = prev_blending;
}
2022-05-30 22:04:19 +02:00
static void clearAccumulated( void ) {
R_FlippingBuffer_Flip(&g2d.pics_buffer_alloc);
g2d.batch_count = 1;
g2d.batch[0].texture = -1;
g2d.batch[0].vertex_offset = 0;
g2d.batch[0].vertex_count = 0;
g2d.exhausted_this_frame = false;
2021-01-16 22:22:31 +01:00
}
2021-01-23 22:23:25 +01:00
static qboolean createPipelines( void )
2021-01-13 20:54:34 +01:00
{
2021-01-23 22:23:25 +01:00
{
/* VkPushConstantRange push_const = { */
/* .offset = 0, */
/* .size = sizeof(AVec3f), */
/* .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, */
/* }; */
VkDescriptorSetLayout descriptor_layouts[] = {
vk_desc.one_texture_layout,
2021-01-23 22:23:25 +01:00
};
VkPipelineLayoutCreateInfo plci = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = ARRAYSIZE(descriptor_layouts),
.pSetLayouts = descriptor_layouts,
/* .pushConstantRangeCount = 1, */
/* .pPushConstantRanges = &push_const, */
};
XVK_CHECK(vkCreatePipelineLayout(vk_core.device, &plci, NULL, &g2d.pipeline_layout));
}
{
const VkVertexInputAttributeDescription attribs[] = {
2021-01-23 22:23:25 +01:00
{.binding = 0, .location = 0, .format = VK_FORMAT_R32G32_SFLOAT, .offset = offsetof(vertex_2d_t, x)},
{.binding = 0, .location = 1, .format = VK_FORMAT_R32G32_SFLOAT, .offset = offsetof(vertex_2d_t, u)},
{.binding = 0, .location = 2, .format = VK_FORMAT_R8G8B8A8_UNORM, .offset = offsetof(vertex_2d_t, color)},
};
const vk_shader_stage_t shader_stages[] = {
{
2021-01-23 22:23:25 +01:00
.stage = VK_SHADER_STAGE_VERTEX_BIT,
.filename = "2d.vert.spv",
2021-01-23 22:23:25 +01:00
}, {
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
.filename = "2d.frag.spv",
2021-01-23 22:23:25 +01:00
}};
vk_pipeline_graphics_create_info_t pci = {
2021-01-23 22:23:25 +01:00
.layout = g2d.pipeline_layout,
.attribs = attribs,
.num_attribs = ARRAYSIZE(attribs),
.stages = shader_stages,
.num_stages = ARRAYSIZE(shader_stages),
.vertex_stride = sizeof(vertex_2d_t),
.depthTestEnable = VK_FALSE,
.depthWriteEnable = VK_FALSE,
.depthCompareOp = VK_COMPARE_OP_ALWAYS,
.cullMode = VK_CULL_MODE_NONE,
2021-01-23 22:23:25 +01:00
};
for (int i = 0; i < ARRAYSIZE(g2d.pipelines); ++i)
{
switch (i)
{
case kRenderNormal:
pci.blendEnable = VK_FALSE;
break;
case kRenderTransColor:
case kRenderTransTexture:
pci.blendEnable = VK_TRUE;
pci.colorBlendOp = VK_BLEND_OP_ADD;
pci.srcAlphaBlendFactor = pci.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
pci.dstAlphaBlendFactor = pci.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
break;
case kRenderTransAlpha:
pci.blendEnable = VK_FALSE;
// FIXME pglEnable( GL_ALPHA_TEST );
break;
case kRenderGlow:
case kRenderTransAdd:
pci.blendEnable = VK_TRUE;
pci.colorBlendOp = VK_BLEND_OP_ADD;
pci.srcAlphaBlendFactor = pci.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
pci.dstAlphaBlendFactor = pci.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
break;
}
g2d.pipelines[i] = VK_PipelineGraphicsCreate(&pci);
2021-01-23 22:23:25 +01:00
if (!g2d.pipelines[i])
{
// TODO complain
return false;
}
}
2021-01-23 22:23:25 +01:00
}
return true;
}
2022-05-30 22:04:19 +02:00
qboolean R_VkOverlay_Init( void ) {
2021-01-23 22:23:25 +01:00
if (!createPipelines())
return false;
// TODO this doesn't need to be host visible, could use staging too
if (!VK_BufferCreate("2d pics_buffer", &g2d.pics_buffer, sizeof(vertex_2d_t) * MAX_VERTICES,
2021-01-16 22:22:31 +01:00
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ))
// FIXME cleanup
2021-01-16 22:22:31 +01:00
return false;
R_FlippingBuffer_Init(&g2d.pics_buffer_alloc, MAX_VERTICES);
2021-01-13 20:54:34 +01:00
return true;
}
2022-05-30 22:04:19 +02:00
void R_VkOverlay_Shutdown( void ) {
VK_BufferDestroy(&g2d.pics_buffer);
2021-01-23 22:23:25 +01:00
for (int i = 0; i < ARRAYSIZE(g2d.pipelines); ++i)
vkDestroyPipeline(vk_core.device, g2d.pipelines[i], NULL);
vkDestroyPipelineLayout(vk_core.device, g2d.pipeline_layout, NULL);
2021-01-13 20:54:34 +01:00
}
2022-05-30 22:04:19 +02:00
void R_VkOverlay_DrawAndFlip( VkCommandBuffer cmdbuf ) {
DEBUG_BEGIN(cmdbuf, "2d overlay");
{
const VkDeviceSize offset = 0;
vkCmdBindVertexBuffers(cmdbuf, 0, 1, &g2d.pics_buffer.buffer, &offset);
}
for (int i = 0; i < g2d.batch_count && g2d.batch[i].vertex_count > 0; ++i)
{
vk_texture_t *texture = findTexture(g2d.batch[i].texture);
const VkPipeline pipeline = g2d.pipelines[g2d.batch[i].blending_mode];
if (texture->vk.descriptor)
{
vkCmdBindPipeline(cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdBindDescriptorSets(cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, g2d.pipeline_layout, 0, 1, &texture->vk.descriptor, 0, NULL);
vkCmdDraw(cmdbuf, g2d.batch[i].vertex_count, 1, g2d.batch[i].vertex_offset, 0);
} // FIXME else what?
}
DEBUG_END(cmdbuf);
clearAccumulated();
}
void R_DrawStretchRaw( float x, float y, float w, float h, int cols, int rows, const byte *data, qboolean dirty )
{
PRINT_NOT_IMPLEMENTED();
}
void R_DrawTileClear( int texnum, int x, int y, int w, int h )
{
PRINT_NOT_IMPLEMENTED_ARGS("%s", findTexture(texnum)->name );
}
void CL_FillRGBA( float x, float y, float w, float h, int r, int g, int b, int a )
{
drawFill(x, y, w, h, r, g, b, a, kRenderTransAdd);
}
void CL_FillRGBABlend( float x, float y, float w, float h, int r, int g, int b, int a )
{
drawFill(x, y, w, h, r, g, b, a, kRenderTransColor);
}