rt: read textures and pass it to gbuffer

102us, 56(64)v, 48(128)s, 16/16o
This commit is contained in:
Ivan Avdeev 2022-01-07 23:22:19 -08:00
parent 4b540d28eb
commit 93b0766142
9 changed files with 76 additions and 22 deletions

View File

@ -69,8 +69,8 @@ void main() {
const float material_index = imageLoad(src_diffuse_gi, pix).a;
//imageStore(dest, pix, vec4(aces_tonemap(base_color.rgb), 0.)); return;
//imageStore(dest, pix, vec4((base_color.rgb), 0.)); return;
imageStore(dest, pix, vec4(fract(imageLoad(src_position_t, pix).rgb / 10.), 0.)); return;
imageStore(dest, pix, vec4((base_color.rgb), 0.)); return;
//imageStore(dest, pix, vec4(fract(imageLoad(src_position_t, pix).rgb / 10.), 0.)); return;
//imageStore(dest, pix, vec4((imageLoad(src_diffuse_gi, pix).rgb), 0.)); return;
//imageStore(dest, pix, vec4(aces_tonemap(imageLoad(src_diffuse_gi, pix).rgb), 0.)); return;
//imageStore(dest, pix, vec4(aces_tonemap(imageLoad(src_specular, pix).rgb), 0.)); return;

View File

@ -1,10 +1,14 @@
#version 460 core
#extension GL_GOOGLE_include_directive : require
#extension GL_EXT_nonuniform_qualifier : enable
#include "ray_primary_common.glsl"
#include "ray_kusochki.glsl"
layout (constant_id = 6) const uint MAX_TEXTURES = 4096;
layout(set = 0, binding = 6) uniform sampler2D textures[MAX_TEXTURES];
layout(location = PAYLOAD_LOCATION_PRIMARY) rayPayloadInEXT RayPayloadPrimary payload;
hitAttributeEXT vec2 bary;
@ -19,14 +23,16 @@ vec2 baryMix(vec2 v1, vec2 v2, vec2 v3, vec2 bary) {
struct Geometry {
vec3 pos;
vec2 uv;
int kusok_index;
};
Geometry readHitGeometry() {
Geometry geom;
const int instance_kusochki_offset = gl_InstanceCustomIndexEXT;
const int kusok_index = instance_kusochki_offset + gl_GeometryIndexEXT;
const Kusok kusok = kusochki[kusok_index];
geom.kusok_index = instance_kusochki_offset + gl_GeometryIndexEXT;
const Kusok kusok = kusochki[geom.kusok_index];
const uint first_index_offset = kusok.index_offset + gl_PrimitiveID * 3;
const uint vi1 = uint(indices[first_index_offset+0]) + kusok.vertex_offset;
@ -48,5 +54,15 @@ void main() {
const Geometry geom = readHitGeometry();
payload.hit_t = vec4(geom.pos, gl_HitTEXT);
payload.uv = geom.uv;
const Kusok kusok = kusochki[geom.kusok_index];
const uint tex_base_color = kusok.tex_base_color;
if ((tex_base_color & KUSOK_MATERIAL_FLAG_SKYBOX) != 0) {
// FIXME read skybox
payload.base_color_a = vec4(1.,0.,1.,1.);
} else {
// FIXME mips
payload.base_color_a = texture(textures[nonuniformEXT(tex_base_color)], geom.uv) * kusok.color;
}
}

View File

@ -4,7 +4,7 @@
#include "ray_primary_common.glsl"
layout(set = 0, binding = 0, rgba8) uniform image2D out_image_base_color_r;
layout(set = 0, binding = 6, rgba32f) uniform image2D out_image_position_t;
layout(set = 0, binding = 7, rgba32f) uniform image2D out_image_position_t;
layout(set = 0, binding = 1) uniform accelerationStructureEXT tlas;
layout(set = 0, binding = 2) uniform UBO {
@ -30,8 +30,6 @@ void main() {
origin, 0., direction, L,
PAYLOAD_LOCATION_PRIMARY);
const vec4 out_base_color_r = vec4(fract(payload.uv), 0., 0.);
imageStore(out_image_position_t, ivec2(gl_LaunchIDEXT.xy), payload.hit_t);
imageStore(out_image_base_color_r, ivec2(gl_LaunchIDEXT.xy), out_base_color_r);
imageStore(out_image_base_color_r, ivec2(gl_LaunchIDEXT.xy), payload.base_color_a);
}

View File

@ -6,7 +6,7 @@
struct RayPayloadPrimary {
vec4 hit_t;
vec2 uv;
vec4 base_color_a;
};
#define PAYLOAD_LOCATION_PRIMARY 0

View File

@ -23,10 +23,10 @@ enum {
RtPrim_Desc_Kusochki = 3,
RtPrim_Desc_Indices = 4,
RtPrim_Desc_Vertices = 5,
//RtPrim_Desc_Textures = 6,
RtPrim_Desc_Textures = 6,
// TODO set 1
RtPrim_Desc_Out_PositionT = 6,
RtPrim_Desc_Out_PositionT = 7,
RtPrim_Desc_COUNT
};
@ -77,14 +77,14 @@ static void initDescriptors( void ) {
INIT_BINDING(RtPrim_Desc_Indices, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR);
INIT_BINDING(RtPrim_Desc_Vertices, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR);
/* g_ray_primary.desc.bindings[RtPrim_Desc_Textures] = (VkDescriptorSetLayoutBinding){ */
/* .binding = RtPrim_Desc_Textures, */
/* .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, */
/* .descriptorCount = MAX_TEXTURES, */
/* .stageFlags = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR, */
/* // FIXME on AMD using immutable samplers leads to nearest filtering ???! */
/* .pImmutableSamplers = NULL, //samplers, */
/* }; */
g_ray_primary.desc.bindings[RtPrim_Desc_Textures] = (VkDescriptorSetLayoutBinding){
.binding = RtPrim_Desc_Textures,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = MAX_TEXTURES,
.stageFlags = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR,
// FIXME on AMD using immutable samplers leads to nearest filtering ???!
.pImmutableSamplers = NULL, //samplers,
};
#undef INIT_BINDING
@ -259,7 +259,7 @@ static void updateDescriptors( const xvk_ray_trace_primary_t* args ) {
#undef DESC_SET_BUFFER
//g_ray_primary.desc.values[RtPrim_Desc_Textures].image_array = args->in.all_textures;
g_ray_primary.desc.values[RtPrim_Desc_Textures].image_array = args->in.all_textures;
VK_DescriptorsWrite(&g_ray_primary.desc.riptors);
}

View File

@ -15,7 +15,7 @@ typedef struct {
VkAccelerationStructureKHR tlas;
vk_buffer_region_t ubo;
vk_buffer_region_t kusochki, indices, vertices;
//VkDescriptorImageInfo *all_textures; // MAX_TEXTURES
VkDescriptorImageInfo *all_textures; // [MAX_TEXTURES]
} in;
struct {

View File

@ -992,6 +992,7 @@ LIST_GBUFFER_IMAGES(GBUFFER_WRITE_BARRIER)
.offset = 0,
.size = args->geometry_data.size,
},
.all_textures = tglob.dii_all_textures,
},
.out = {
.position_t = current_frame->position_t.view,

View File

@ -47,6 +47,18 @@ void initTextures( void )
/* FIXME
gEngine.Cmd_AddCommand( "texturelist", R_TextureList_f, "display loaded textures list" );
*/
{
const vk_texture_t *const default_texture = vk_textures + tglob.defaultTexture;
for (int i = 0; i < MAX_TEXTURES; ++i) {
const vk_texture_t *const tex = findTexture(i);;
tglob.dii_all_textures[i] = (VkDescriptorImageInfo){
.imageView = tex->vk.image.view != VK_NULL_HANDLE ? tex->vk.image.view : default_texture->vk.image.view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.sampler = vk_core.default_sampler,
};
}
}
}
static void unloadSkybox( void );
@ -726,6 +738,15 @@ int VK_LoadTexture( const char *name, const byte *buf, size_t size, int flags )
return 0;
}
{
const int index = tex - vk_textures;
tglob.dii_all_textures[index] = (VkDescriptorImageInfo){
.imageView = tex->vk.image.view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.sampler = vk_core.default_sampler,
};
}
/* FIXME
VK_ApplyTextureParams( tex ); // update texture filter, wrap etc
*/
@ -814,6 +835,12 @@ void VK_FreeTexture( unsigned int texnum ) {
XVK_ImageDestroy(&tex->vk.image);
memset(tex, 0, sizeof(*tex));
tglob.dii_all_textures[texnum] = (VkDescriptorImageInfo){
.imageView = vk_textures[tglob.defaultTexture].vk.image.view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.sampler = vk_core.default_sampler,
};
}
int VK_LoadTextureFromBuffer( const char *name, rgbdata_t *pic, texFlags_t flags, qboolean update )
@ -850,6 +877,15 @@ int VK_LoadTextureFromBuffer( const char *name, rgbdata_t *pic, texFlags_t flags
return 0;
}
{
const int index = tex - vk_textures;
tglob.dii_all_textures[index] = (VkDescriptorImageInfo){
.imageView = tex->vk.image.view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.sampler = vk_core.default_sampler,
};
}
/* FIXME
VK_ApplyTextureParams( tex ); // update texture filter, wrap etc
*/

View File

@ -1,6 +1,7 @@
#pragma once
#include "vk_core.h"
#include "vk_image.h"
#include "vk_const.h"
#include "xash3d_types.h"
#include "const.h"
@ -45,6 +46,8 @@ typedef struct vk_textures_global_s
vk_texture_t skybox_cube;
vk_texture_t cubemap_placeholder;
VkDescriptorImageInfo dii_all_textures[MAX_TEXTURES];
} vk_textures_global_t;
// TODO rename this consistently