rtx: add material support stub

This commit is contained in:
Ivan 'provod' Avdeev 2021-11-14 13:53:50 -08:00 committed by Ivan Avdeev
parent 24ebc1fab2
commit e8359b12e8
11 changed files with 91 additions and 26 deletions

View File

@ -2,7 +2,7 @@
- [ ] remove surface visibility cache
- [ ] rtx: remove lbsp
- [ ] rtx: rename point lights to lampochki
- [ ] rtx: move entity parsing to its own module
- [x] rtx: move entity parsing to its own module
- [ ] rtx: rename emissive surface to surface lights
- [ ] rtx: configuration that includes texture name -> pbr params mapping, etc. Global, per-map, ...
- [ ] rtx: simple convolution denoise (bilateral?)

View File

@ -26,7 +26,8 @@ void main() {
const uint vi3 = uint(indices[first_index_offset+2]) + kusochki[kusok_index].vertex_offset;
const vec2 texture_uv = vertices[vi1].gl_tc * (1. - bary.x - bary.y) + vertices[vi2].gl_tc * bary.x + vertices[vi3].gl_tc * bary.y + push_constants.time * kusochki[kusok_index].uv_speed;
const uint tex_index = kusochki[kusok_index].texture;
// TODO mips
const uint tex_index = kusochki[kusok_index].tex_base_color;
const vec4 texture_color = texture(textures[nonuniformEXT(tex_index)], texture_uv);
const vec3 color = texture_color.rgb * kusochki[kusok_index].color.rgb * texture_color.a * kusochki[kusok_index].color.a;

View File

@ -20,10 +20,10 @@ void main() {
const uint vi3 = uint(indices[first_index_offset+2]) + kusochki[kusok_index].vertex_offset;
const vec2 texture_uv = vertices[vi1].gl_tc * (1. - bary.x - bary.y) + vertices[vi2].gl_tc * bary.x + vertices[vi3].gl_tc * bary.y;
const uint tex_index = kusochki[kusok_index].texture;
const uint tex_index = kusochki[kusok_index].tex_base_color;
const vec4 texture_color = texture(textures[nonuniformEXT(tex_index)], texture_uv);
if (texture_color.a < 0.1) {
ignoreIntersectionEXT;
}
}
}

View File

@ -59,6 +59,10 @@ void computeAnisotropicEllipseAxes(in vec3 P, in vec3 f,
v2 * txcoords[2] - interpolatedTexCoordsAtIntersection;
}
vec4 sampleTexture(uint tex_index, vec2 uv, vec4 uv_lods) {
return textureGrad(textures[nonuniformEXT(tex_index)], uv, uv_lods.xy, uv_lods.zw);
}
void main() {
payload.t_offset += gl_HitTEXT;
@ -93,7 +97,6 @@ void main() {
};
const vec2 texture_uv_stationary = vertices[vi1].gl_tc * (1. - bary.x - bary.y) + vertices[vi2].gl_tc * bary.x + vertices[vi3].gl_tc * bary.y;
const vec2 texture_uv = texture_uv_stationary + push_constants.time * kusochki[kusok_index].uv_speed;
const uint tex_index = kusochki[kusok_index].texture;
const vec3 real_geom_normal = normalize(cross(pos[2]-pos[0], pos[1]-pos[0]));
const float geom_normal_sign = sign(dot(real_geom_normal, -gl_WorldRayDirectionEXT));
@ -106,7 +109,9 @@ void main() {
const float ray_cone_width = payload.pixel_cone_spread_angle * payload.t_offset;
vec4 uv_lods;
computeAnisotropicEllipseAxes(hit_pos, normal, gl_WorldRayDirectionEXT, ray_cone_width, pos, uvs, texture_uv_stationary, uv_lods.xy, uv_lods.zw);
const vec4 tex_color = textureGrad(textures[nonuniformEXT(tex_index)], texture_uv, uv_lods.xy, uv_lods.zw);
const uint tex_index = kusochki[kusok_index].tex_base_color;
const vec4 tex_color = sampleTexture(tex_index, texture_uv, uv_lods);
//const vec3 base_color = pow(tex_color.rgb, vec3(2.));
const vec3 base_color = ((push_constants.flags & PUSH_FLAG_LIGHTMAP_ONLY) != 0) ? vec3(1.) : tex_color.rgb;// pow(tex_color.rgb, vec3(2.));
/* tex_color = pow(tex_color, vec4(2.)); */
@ -120,7 +125,8 @@ void main() {
payload.normal = normal * geom_normal_sign;
payload.geometry_normal = geom_normal;
payload.emissive = kusochki[kusok_index].emissive * base_color; // TODO emissive should have a special texture
payload.roughness = kusochki[kusok_index].roughness;
payload.kusok_index = kusok_index;
payload.material_index = nonuniformEXT(tex_index);
payload.roughness = sampleTexture(kusochki[kusok_index].tex_roughness, texture_uv, uv_lods).r;
payload.metalness = sampleTexture(kusochki[kusok_index].tex_metalness, texture_uv, uv_lods).r;
payload.material_index = tex_index;
}

View File

@ -404,7 +404,7 @@ void main() {
MaterialProperties material;
material.baseColor = payload_opaque.base_color;
material.metalness = 0.f; // TODO
material.metalness = payload_opaque.metalness;
material.emissive = payload_opaque.emissive;
material.roughness = payload_opaque.roughness;

View File

@ -13,6 +13,7 @@ struct RayPayloadOpaque {
float transmissiveness;
vec3 emissive;
float roughness;
float metalness;
int kusok_index;
uint material_index;
};

View File

@ -39,18 +39,17 @@ struct Kusok {
uint triangles;
// Material
uint texture;
uint tex_base_color;
// TODO the color is per-model, not per-kusok
vec4 color;
vec3 emissive;
//PAD(1)
float roughness;
uint tex_roughness;
vec2 uv_speed; // for conveyors
PAD(2)
uint tex_metalness;
uint tex_normalmap;
};
struct PointLight {

35
ref_vk/vk_materials.c Normal file
View File

@ -0,0 +1,35 @@
#include "vk_materials.h"
#include "vk_textures.h"
#include "vk_const.h"
static struct {
xvk_material_t materials[MAX_TEXTURES];
} g_materials;
void XVK_ReloadMaterials( void ) {
for (int i = 0; i < MAX_TEXTURES; ++i) {
xvk_material_t *const mat = g_materials.materials + i;
const vk_texture_t *const tex = findTexture( i );
if (tex) {
mat->base_color = i;
mat->metalness = tglob.blackTexture;
mat->roughness = tglob.whiteTexture;
mat->normalmap = tglob.blackTexture;
} else {
mat->base_color = -1;
}
}
}
xvk_material_t* XVK_GetMaterialForTextureIndex( int tex_index ) {
xvk_material_t *mat = NULL;
ASSERT(tex_index >= 0);
ASSERT(tex_index < MAX_TEXTURES);
mat = g_materials.materials + tex_index;
if (mat->base_color >= 0)
return mat;
return NULL;
}

12
ref_vk/vk_materials.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
typedef struct {
int base_color;
int roughness;
int metalness;
int normalmap;
} xvk_material_t;
void XVK_ReloadMaterials( void );
xvk_material_t* XVK_GetMaterialForTextureIndex( int tex_index );

View File

@ -2,6 +2,7 @@
#include "vk_rtx.h"
#include "vk_textures.h"
#include "vk_materials.h"
#include "vk_render.h"
#include "vk_light.h"
@ -133,7 +134,7 @@ void XVK_RayModel_Validate( void ) {
for (int j = 0; j < num_geoms; j++) {
const vk_kusok_data_t *kusok = kusochki + j;
const vk_texture_t *tex = findTexture(kusok->texture);
const vk_texture_t *tex = findTexture(kusok->tex_base_color);
ASSERT(tex);
ASSERT(tex->vk.image_view != VK_NULL_HANDLE);
@ -216,8 +217,8 @@ vk_ray_model_t* VK_RayModelCreate( vk_ray_model_init_t args ) {
kusochki[i].index_offset = mg->index_offset;
kusochki[i].triangles = prim_count;
kusochki[i].texture = mg->texture;
kusochki[i].roughness = mg->material == kXVkMaterialWater ? 0. : 1.; // FIXME
//kusochki[i].texture = mg->texture;
//kusochki[i].roughness = mg->material == kXVkMaterialWater ? 0. : 1.; // FIXME
VectorSet(kusochki[i].emissive, 0, 0, 0 );
mg->kusok_index = i + kusochki_count_offset;
@ -362,17 +363,23 @@ void VK_RayFrameAddModel( vk_ray_model_t *model, const vk_render_model_t *render
const vk_render_geometry_t *geom = render_model->geometries + i;
const vk_emissive_surface_t *esurf = render_model->static_map ? NULL : VK_LightsAddEmissiveSurface( geom, transform_row, false );
vk_kusok_data_t *kusok = (vk_kusok_data_t*)(g_ray_model_state.kusochki_buffer.mapped) + geom->kusok_index;
kusok->texture = geom->texture;
const xvk_material_t *const mat = XVK_GetMaterialForTextureIndex( geom->texture );
ASSERT(mat);
kusok->tex_base_color = mat->base_color;
kusok->tex_roughness = mat->roughness;
kusok->tex_metalness = mat->metalness;
kusok->tex_normalmap = mat->normalmap;
// HACK until there is proper specular
// FIXME also this erases previour roughness unconditionally
if (HACK_reflective) {
kusok->roughness = 0.f;
} else if (geom->material == kXVkMaterialChrome) {
kusok->roughness = .1f;
} else {
kusok->roughness = 1.f;
}
// if (HACK_reflective) {
// kusok->roughness = 0.f;
// } else if (geom->material == kXVkMaterialChrome) {
// kusok->roughness = .1f;
// } else {
// kusok->roughness = 1.f;
// }
Vector4Copy(color, kusok->color);

View File

@ -13,6 +13,7 @@
#include "vk_rtx.h"
#include "vk_textures.h"
#include "vk_cvar.h"
#include "vk_materials.h"
#include "camera.h"
#include "com_strings.h"
@ -107,8 +108,11 @@ void R_NewMap( void )
VK_LightsNewMap();
if (vk_core.rtx)
if (vk_core.rtx) {
VK_RayNewMap();
XVK_ReloadMaterials();
}
// RTX map loading requires command buffer for building blases
if (vk_core.rtx)