Skip to content

Commit

Permalink
Merge pull request #736 from w23/E373-tradsky
Browse files Browse the repository at this point in the history
E373: Draw skybox in traditional renderer

Fixes #732
w23 authored Feb 7, 2024
2 parents 5ed4839 + cf966b3 commit 2b19a8c
Showing 18 changed files with 501 additions and 208 deletions.
8 changes: 7 additions & 1 deletion ref/vk/TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
## Next
- [ ] performance profiling and comparison

# Previously
## 2024-02-05 E373
- [x] Skybox for traditional renderer
- [x] Sky pipeline
- [x] Submit SURF_DRAWSKY draw commands
- [x] Use original skybox for trad renderer

## 2024-02-01 E371
- [x] tune A-Trous step widths for different channels
- [x] multiple passes -- core of the paper lol
@@ -11,7 +18,6 @@
- [ ] :x: temporal glitches with dontBlurSamples() and ATrous → no longer reproduces
- [x] add `-vkdbg_shaderprintf` arg to explicitly enable shader debug printfs

# Previously
## 2024-01-29 E370
- [x] bounce > 1 brighness
- [ ] tune A-Trous step widths for different channels
25 changes: 12 additions & 13 deletions ref/vk/r_textures.c
Original file line number Diff line number Diff line change
@@ -5,16 +5,12 @@
#include "vk_const.h"
#include "vk_mapents.h" // wadlist
#include "vk_logs.h"
#include "r_speeds.h"
#include "profiler.h"
#include "unordered_roadmap.h"
#include "stringview.h"

#include "xash3d_mathlib.h"
#include "crtlib.h"
#include "crclib.h" // COM_HashKey
#include "com_strings.h"
#include "eiface.h" // ARRAYSIZE

#include <memory.h>
#include <math.h>
@@ -239,7 +235,7 @@ static void createDefaultTextures( void )
memset(pic->buffer, 0, pic->size);

const qboolean is_placeholder = true;
R_VkTexturesSkyboxUpload( "skybox_placeholder", pic, kColorspaceGamma, is_placeholder );
R_VkTexturesSkyboxUpload( "skybox_placeholder", pic, kColorspaceGamma, kSkyboxPlaceholder );
}
}

@@ -777,7 +773,7 @@ static void skyboxParseInfo( const char *name ) {
Mem_Free(data);
}

static qboolean skyboxLoadF(const char *fmt, ...) {
static qboolean skyboxLoadF(skybox_slot_e slot, const char *fmt, ...) {
qboolean success = false;
char buffer[MAX_STRING];

@@ -805,7 +801,7 @@ static qboolean skyboxLoadF(const char *fmt, ...) {

{
const qboolean is_placeholder = false;
success = R_VkTexturesSkyboxUpload( buffer, pic, kColorspaceGamma, is_placeholder );
success = R_VkTexturesSkyboxUpload( buffer, pic, kColorspaceGamma, slot );
}

if (success)
@@ -854,15 +850,18 @@ static qboolean skyboxTryLoad( const char *skyboxname, qboolean force_reload ) {
if (!force_reload && svCmp(basename, g_textures.skybox.current_name) == 0)
return true;

// Try loading newer "PBR" upscaled skybox first
if (skyboxLoadF("pbr/env/%.*s", basename.len, basename.s))
goto success;
// Unload previous skybox
skyboxUnload();

// Try loading original game skybox
if (skyboxLoadF("gfx/env/%.*s", basename.len, basename.s))
const qboolean original = skyboxLoadF(kSkyboxOriginal, "gfx/env/%.*s", basename.len, basename.s);

// Try loading newer "PBR" upscaled skybox
const qboolean patched = skyboxLoadF(kSkyboxPatched, "pbr/env/%.*s", basename.len, basename.s);

if (original || patched)
goto success;

skyboxUnload();
return false;

success:
@@ -872,7 +871,7 @@ static qboolean skyboxTryLoad( const char *skyboxname, qboolean force_reload ) {

static const char *k_skybox_default = "desert";

void skyboxSetup( const char *skyboxname, qboolean is_custom, qboolean force_reload ) {
static void skyboxSetup( const char *skyboxname, qboolean is_custom, qboolean force_reload ) {
DEBUG("%s: skyboxname='%s' is_custom=%d force_reload=%d", __FUNCTION__, skyboxname, is_custom, force_reload);

if (!skyboxTryLoad(skyboxname, force_reload)) {
8 changes: 8 additions & 0 deletions ref/vk/r_textures.h
Original file line number Diff line number Diff line change
@@ -58,6 +58,14 @@ typedef enum {
kColorspaceGamma,
} colorspace_hint_e;

typedef enum {
kSkyboxPlaceholder,
kSkyboxOriginal,
kSkyboxPatched,

kSkybox_COUNT,
} skybox_slot_e;

int R_TextureUploadFromFileExAcquire( const char *filename, colorspace_hint_e colorspace, qboolean force_reload );

int R_TextureFindByNameF( const char *fmt, ...);
30 changes: 30 additions & 0 deletions ref/vk/shaders/sky.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#version 450

layout(set = 0, binding = 0) uniform UBO {
mat4 mvp;
mat4 inv_proj;
mat4 inv_view;
vec2 resolution;
} ubo;

layout(set = 0, binding = 1) uniform samplerCube skybox;

layout(location = 0) out vec4 out_color;

vec3 clipToWorldSpace(vec3 clip) {
const vec4 eye_space = ubo.inv_proj * vec4(clip, 1.);
return (ubo.inv_view * vec4(eye_space.xyz / eye_space.w, 1.)).xyz;
}

vec3 getDirection(in vec2 uv) {
uv = uv * 2. - 1.;
const vec3 world_near = clipToWorldSpace(vec3(uv, 0.));
const vec3 world_far = clipToWorldSpace(vec3(uv, 1.));
return normalize(world_far - world_near);
}

void main() {
const vec2 uv = gl_FragCoord.xy / ubo.resolution;
const vec3 direction = getDirection(uv);
out_color = texture(skybox, direction);
}
14 changes: 14 additions & 0 deletions ref/vk/shaders/sky.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 450

layout(set=0,binding=0) uniform UBO {
mat4 mvp;
mat4 inv_proj;
mat4 inv_view;
vec2 resolution;
} ubo;

layout(location=0) in vec3 a_pos;

void main() {
gl_Position = ubo.mvp * vec4(a_pos.xyz, 1.);
}
2 changes: 1 addition & 1 deletion ref/vk/vk_brush.c
Original file line number Diff line number Diff line change
@@ -1412,8 +1412,8 @@ static qboolean fillBrushSurfaces(fill_geometries_args_t args) {
model_geometry->index_offset = index_offset;

if ( type == BrushSurface_Sky ) {
#define TEX_BASE_SKYBOX 0x0f000000u // FIXME ray_interop.h
model_geometry->material.tex_base_color = TEX_BASE_SKYBOX;
model_geometry->ye_olde_texture = TEX_BASE_SKYBOX;
} else {
ASSERT(!FBitSet( surf->flags, SURF_DRAWTILED ));
VK_CreateSurfaceLightmap( surf, args.mod );
1 change: 1 addition & 0 deletions ref/vk/vk_descriptor.c
Original file line number Diff line number Diff line change
@@ -189,6 +189,7 @@ void VK_DescriptorsWrite(const vk_descriptors_t *desc, int set_slot)
switch (binding->descriptorType) {
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
// TODO
ASSERT(wds[i].descriptorCount == 1);
wds[i].pBufferInfo = &desc->values[i].buffer;
5 changes: 0 additions & 5 deletions ref/vk/vk_descriptor.h
Original file line number Diff line number Diff line change
@@ -52,8 +52,3 @@ typedef struct {
void VK_DescriptorsCreate(vk_descriptors_t *desc);
void VK_DescriptorsWrite(const vk_descriptors_t *desc, int set_slot);
void VK_DescriptorsDestroy(const vk_descriptors_t *desc);

// typedef enum {
// VK_DescType_SingleTexture,
// } vk_desc_type_t;
// VkDescriptorSet VK_DescriptorGetSet( vk_desc_type_t type );
18 changes: 4 additions & 14 deletions ref/vk/vk_framectl.c
Original file line number Diff line number Diff line change
@@ -300,7 +300,10 @@ static void enqueueRendering( vk_combuf_t* combuf, qboolean draw ) {
}

if (!vk_frame.rtx_enabled)
VK_RenderEnd( cmdbuf, draw );
VK_RenderEnd( cmdbuf, draw,
g_frame.current.framebuffer.width, g_frame.current.framebuffer.height,
g_frame.current.index
);

R_VkOverlay_DrawAndFlip( cmdbuf, draw );

@@ -335,19 +338,6 @@ static void submit( vk_combuf_t* combuf, qboolean wait, qboolean draw ) {
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
};

#define BOUNDED_ARRAY(NAME, TYPE, MAX_SIZE) \
struct { \
TYPE items[MAX_SIZE]; \
int count; \
} NAME

#define BOUNDED_ARRAY_APPEND(var, item) \
do { \
ASSERT(var.count < COUNTOF(var.items)); \
var.items[var.count++] = item; \
} while(0)

// TODO for RT renderer we only touch framebuffer at the very end of rendering/cmdbuf.
// Can we postpone waitinf for framebuffer semaphore until we actually need it.
BOUNDED_ARRAY(waitophores, VkSemaphore, 2) = {0};
3 changes: 2 additions & 1 deletion ref/vk/vk_image.c
Original file line number Diff line number Diff line change
@@ -118,7 +118,8 @@ void R_VkImageDestroy(r_vk_image_t *img) {
if (img->view != VK_NULL_HANDLE)
vkDestroyImageView(vk_core.device, img->view, NULL);

vkDestroyImage(vk_core.device, img->image, NULL);
if (img->image != VK_NULL_HANDLE)
vkDestroyImage(vk_core.device, img->image, NULL);

VK_DevMemFree(&img->devmem);
*img = (r_vk_image_t){0};
1 change: 1 addition & 0 deletions ref/vk/vk_mapents.c
Original file line number Diff line number Diff line change
@@ -869,6 +869,7 @@ void XVK_ParseMapEntities( void ) {
for (int i = 0; i < g_map_entities.smoothing.groups_count; ++i)
g_map_entities.smoothing.groups[i].count = 0;
g_map_entities.smoothing.groups_count = 0;
g_map_entities.remove_all_sky_surfaces = 0;

parseEntities( map->entities, false );
orientSpotlights();
1 change: 1 addition & 0 deletions ref/vk/vk_ray_model.c
Original file line number Diff line number Diff line change
@@ -459,6 +459,7 @@ qboolean RT_DynamicModelInit(void) {
void RT_DynamicModelShutdown(void) {
for (int i = 0; i < MATERIAL_MODE_COUNT; ++i) {
RT_BlasDestroy(g_dyn.groups[i].blas);
g_dyn.groups[i].blas = NULL;
}
}

Loading

0 comments on commit 2b19a8c

Please sign in to comment.