Skip to content

Commit

Permalink
Merge pull request #134 from gameknife/feature/compress_gpuvertex
Browse files Browse the repository at this point in the history
Feature/compress gpuvertex
  • Loading branch information
gameknife authored Dec 28, 2024
2 parents 78fe695 + 1f83227 commit bd6f9a0
Show file tree
Hide file tree
Showing 42 changed files with 744 additions and 274 deletions.
1 change: 0 additions & 1 deletion assets/shaders/GBufferPass.frag
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "common/Material.glsl"

layout(binding = 1) readonly buffer MaterialArray { Material[] Materials; };
//layout(binding = 3) uniform sampler2D[] TextureSamplers;
layout(set = 1, binding = 0) uniform sampler2D TextureSamplers[];

layout(location = 0) in vec3 FragColor;
Expand Down
24 changes: 15 additions & 9 deletions assets/shaders/GBufferPass.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
#include "common/Material.glsl"
#include "common/UniformBufferObject.glsl"

#define PURE_VERTEX_DEF
#include "common/Vertex.glsl"

layout(binding = 0) readonly uniform UniformBufferObjectStruct { UniformBufferObject Camera; };
layout(binding = 1) readonly buffer MaterialArray { Material[] Materials; };
layout(binding = 2) readonly buffer NodeProxyArray { NodeProxy[] NodeProxies; };

layout(location = 0) in vec3 InPosition;
layout(location = 1) in vec3 InNormal;
layout(location = 2) in vec3 InTangent;
layout(location = 3) in vec2 InTexCoord;
layout(location = 4) in uint InMaterialIndex;
layout(location = 1) in uint TexcoordXY;
layout(location = 2) in uint NormalXY;
layout(location = 3) in uint NormalZTangentX;
layout(location = 4) in uint TangentYZ;
layout(location = 5) in uint TangentWMatIdx;

layout(location = 0) out vec3 FragColor;
layout(location = 1) out vec3 FragNormal;
Expand All @@ -23,14 +27,16 @@ out gl_PerVertex
{
vec4 gl_Position;
};

void main()
{
Material m = Materials[InMaterialIndex];
NodeProxy proxy = NodeProxies[gl_InstanceIndex];
gl_Position = Camera.Projection * Camera.ModelView * proxy.worldTS * vec4(InPosition, 1.0);

Vertex v = UncompressVertex(InPosition, TexcoordXY, NormalXY, NormalZTangentX, TangentYZ, TangentWMatIdx);
Material m = Materials[proxy.matId[v.MaterialIndex]];
FragColor = m.Diffuse.xyz;
FragNormal = (proxy.worldTS * vec4(InNormal, 0.0)).xyz;
FragTexCoord = InTexCoord;
FragMaterialIndex = InMaterialIndex;
FragNormal = (proxy.worldTS * vec4(v.Normal, 0.0)).xyz;
FragTexCoord = v.TexCoord;
FragMaterialIndex = proxy.matId[v.MaterialIndex];
}
35 changes: 1 addition & 34 deletions assets/shaders/Graphics.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,11 @@
#extension GL_ARB_separate_shader_objects : enable
#extension GL_EXT_nonuniform_qualifier : require
#extension GL_GOOGLE_include_directive : require
#include "common/Material.glsl"
#include "common/UniformBufferObject.glsl"
#include "common/ColorFunc.glsl"

layout(binding = 0) readonly uniform UniformBufferObjectStruct { UniformBufferObject Camera; };
layout(binding = 1) readonly buffer MaterialArray { Material[] Materials; };
//layout(binding = 2) uniform sampler2D[] TextureSamplers;
layout(set = 1, binding = 0) uniform sampler2D TextureSamplers[];

layout(location = 0) in vec3 FragColor;
layout(location = 1) in vec3 FragNormal;
layout(location = 2) in vec2 FragTexCoord;
layout(location = 3) in flat uint FragMaterialIndex;

layout(location = 0) out vec4 OutColor;

void main()
{
const vec3 normal = normalize(FragNormal);
const int textureId = Materials[FragMaterialIndex].DiffuseTextureId;

const float t = 0.5*(normal.y + 1);
vec3 skyColor = mix(vec3(1.0), vec3(0.5, 0.7, 1.0) * 20, t);

const vec3 lightVector = normalize(vec3(5, 4, 3));
const float d = max(dot(lightVector, normal) * 20.0, 0.5);

vec3 albedo = FragColor;
if (textureId >= 0)
{
vec3 albedoraw = texture(TextureSamplers[textureId], FragTexCoord).rgb;
albedo *= albedoraw * albedoraw;
}

vec3 c = albedo * d + albedo * skyColor;
OutColor = vec4(1);
OutColor.rgb = LinearToST2084UE(c.rgb * Camera.PaperWhiteNit / 230.0);

OutColor.rgb = vec3(0);
OutColor = vec4(0);
OutColor.a = 0.25;
}
15 changes: 0 additions & 15 deletions assets/shaders/Graphics.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ layout(binding = 0) readonly uniform UniformBufferObjectStruct { UniformBufferOb
layout(binding = 1) readonly buffer MaterialArray { Material[] Materials; };

layout(location = 0) in vec3 InPosition;
layout(location = 1) in vec3 InNormal;
layout(location = 2) in vec3 InTangent;
layout(location = 3) in vec2 InTexCoord;
layout(location = 4) in uint InMaterialIndex;

layout(location = 0) out vec3 FragColor;
layout(location = 1) out vec3 FragNormal;
layout(location = 2) out vec2 FragTexCoord;
layout(location = 3) out flat uint FragMaterialIndex;

layout(push_constant) uniform PushConsts {
mat4 worldMatrix;
Expand All @@ -29,11 +20,5 @@ out gl_PerVertex

void main()
{
Material m = Materials[InMaterialIndex];

gl_Position = Camera.Projection * Camera.ModelView * pushConsts.worldMatrix * vec4(InPosition, 1.0);
FragColor = m.Diffuse.xyz;
FragNormal = vec3(pushConsts.worldMatrix * vec4(InNormal, 0.0)); // technically not correct, should be ModelInverseTranspose
FragTexCoord = InTexCoord;
FragMaterialIndex = InMaterialIndex;
}
4 changes: 2 additions & 2 deletions assets/shaders/HybridDeferredShading.comp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bool TraceRay(ivec2 ipos, vec3 origin, vec3 direction, vec3 iblColor, inout vec3
const Vertex v0 = UnpackVertex(vertexOffset + Indices[indexOffset]);
const Vertex v1 = UnpackVertex(vertexOffset + Indices[indexOffset + 1]);
const Vertex v2 = UnpackVertex(vertexOffset + Indices[indexOffset + 2]);
const Material material = Materials[v0.MaterialIndex];
const Material material = Materials[node.matId[v0.MaterialIndex]];

const vec3 normal = normalize((to_world(PayloadData.BaryCoords, v0.Normal, v1.Normal, v2.Normal) * worldtoobject).xyz);
const vec2 texCoord = Mix(v0.TexCoord, v1.TexCoord, v2.TexCoord, PayloadData.BaryCoords);
Expand Down Expand Up @@ -175,7 +175,7 @@ void main() {
// visibility fetch hit point, if primary ray, start from here
Vertex v = get_material_data(ipos, vBuffer, origin.xyz, ray_dir);
NodeProxy node = NodeProxies[vBuffer.x - 1];
Material mat = Materials[v.MaterialIndex];
Material mat = Materials[node.matId[v.MaterialIndex]];
vec4 albedo = mat.Diffuse;
if (mat.DiffuseTextureId >= 0)
{
Expand Down
4 changes: 3 additions & 1 deletion assets/shaders/ModernDeferredShading.comp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ void main() {

Vertex v = get_material_data(ipos, vBuffer, origin.xyz, ray_dir);

Material mat = Materials[v.MaterialIndex];
NodeProxy node = NodeProxies[vBuffer.x - 1];
Material mat = Materials[node.matId[v.MaterialIndex]];

vec4 albedo = mat.Diffuse;
if (mat.DiffuseTextureId >= 0)
{
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/RayCast.comp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ layout(set = 1, binding = 0) uniform sampler2D TextureSamplers[];

#include "common/RTSimple.glsl"

layout(local_size_x = 8, local_size_y = 4, local_size_z = 1) in;
layout(local_size_x = 32, local_size_y = 1, local_size_z = 1) in;

void main() {
RayCastContext context = RayIO[gl_GlobalInvocationID.x];
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/common/RTCommon.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void ProcessHit(const int InstCustIndex, const vec3 RayDirection, const float Ra
const Vertex v0 = UnpackVertex(vertexOffset + Indices[indexOffset]);
const Vertex v1 = UnpackVertex(vertexOffset + Indices[indexOffset + 1]);
const Vertex v2 = UnpackVertex(vertexOffset + Indices[indexOffset + 2]);
const Material material = Materials[v0.MaterialIndex];
const Material material = Materials[node.matId[v0.MaterialIndex]]; // read real materialIdx from node, vertex store the index

// Compute the ray hit point properties.
const vec3 barycentrics = vec3(1.0 - TwoBaryCoords.x - TwoBaryCoords.y, TwoBaryCoords.x, TwoBaryCoords.y);
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/common/RTSimple.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void SimpleHit(const int InstCustIndex, const mat4x3 WorldToObject, const vec2 T
const Vertex v1 = UnpackVertex(vertexOffset + Indices[indexOffset + 1]);
const Vertex v2 = UnpackVertex(vertexOffset + Indices[indexOffset + 2]);

MaterialId = v0.MaterialIndex;
MaterialId = node.matId[v0.MaterialIndex];

// Compute the ray hit point properties.
const vec3 barycentrics = vec3(1.0 - TwoBaryCoords.x - TwoBaryCoords.y, TwoBaryCoords.x, TwoBaryCoords.y);
Expand Down
3 changes: 2 additions & 1 deletion assets/shaders/common/UniformBufferObject.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ struct ALIGN_16 NodeProxy
{
uint instanceId;
uint modelId;
uint matId;
uint reserved1;
uint reserved2;
mat4 worldTS;
mat4 combinedPrevTS;
uint matId[16];
};
60 changes: 48 additions & 12 deletions assets/shaders/common/Vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@


#ifndef vertex_inc

#define vertex_inc

struct Vertex
Expand All @@ -13,20 +10,59 @@ struct Vertex
uint MaterialIndex;
};

Vertex UnpackVertex(uint index)
struct GPUVertex
{
vec3 Position;
uint TexcoordXY;
uint NormalXY;
uint NormalZTangentX;
uint TangentYZ;
uint TangentWMatIdx;
};

Vertex UncompressVertex(in vec3 Position, in uint TexcoordXY, in uint NormalXY, in uint NormalZTangentX, in uint TangentYZ, in uint TangentWMatIdx)
{
const uint vertexSize = 13;
const uint offset = index * vertexSize;

Vertex v;

v.Position = vec3(Vertices[offset + 0], Vertices[offset + 1], Vertices[offset + 2]);
v.Normal = vec3(Vertices[offset + 3], Vertices[offset + 4], Vertices[offset + 5]);
v.Tangent = vec4(Vertices[offset + 6], Vertices[offset + 7], Vertices[offset + 8], Vertices[offset + 9]);
v.TexCoord = vec2(Vertices[offset + 10], Vertices[offset + 11]);
v.MaterialIndex = floatBitsToUint(Vertices[offset + 12]);
v.Position = Position;
v.Normal = vec3(
unpackHalf2x16(NormalXY).x,
unpackHalf2x16(NormalXY).y,
unpackHalf2x16(NormalZTangentX).x
);
v.Tangent = vec4(
unpackHalf2x16(NormalZTangentX).y,
unpackHalf2x16(TangentYZ).x,
unpackHalf2x16(TangentYZ).y,
float((TangentWMatIdx >> 16) & 0xFFFF) - 1.0f
);
v.TexCoord = vec2(
unpackHalf2x16(TexcoordXY).x,
unpackHalf2x16(TexcoordXY).y
);
v.MaterialIndex = TangentWMatIdx & 0xFFFF;

return v;
}

#ifndef PURE_VERTEX_DEF
Vertex UnpackVertex(uint index)
{
const uint vertexSize = 8;
const uint offset = index * vertexSize;

vec3 Position = vec3(Vertices[offset + 0], Vertices[offset + 1], Vertices[offset + 2]);
uint TexcoordXY = floatBitsToUint(Vertices[offset + 3]);
uint NormalXY = floatBitsToUint(Vertices[offset + 4]);
uint NormalZTangentX = floatBitsToUint(Vertices[offset + 5]);
uint TangentYZ = floatBitsToUint(Vertices[offset + 6]);
uint TangentWMatIdx = floatBitsToUint(Vertices[offset + 7]);

uint TangentW = (TangentWMatIdx >> 16) & 0xFFFF;
uint matIdx = TangentWMatIdx & 0xFFFF;

return UncompressVertex(Position, TexcoordXY, NormalXY, NormalZTangentX, TangentYZ, TangentWMatIdx);
}
#endif

#endif
21 changes: 21 additions & 0 deletions src/Application/gkNextRenderer/gkNextRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ bool NextRendererGameInstance::OnCursorPosition(double xpos, double ypos)
bool NextRendererGameInstance::OnMouseButton(int button, int action, int mods)
{
modelViewController_.OnMouseButton( button, action, mods);

#if !ANDROID
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
auto mousePos = GetEngine().GetMousePos();
glm::vec3 org;
glm::vec3 dir;
GetEngine().GetScreenToWorldRay(mousePos, org, dir);
GetEngine().RayCastGPU( org, dir, [this](Assets::RayCastResult result)
{
if (result.Hitted)
{
GetEngine().GetScene().GetRenderCamera().FocalDistance = result.T;
GetEngine().DrawAuxPoint( result.HitPoint, glm::vec4(0.2, 1, 0.2, 1), 2, 30 );
}
return true;
});
return true;
}
#endif

return true;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Assets/Material.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "Common/CoreMinimal.hpp"
#include "Utilities/Glm.hpp"

namespace Assets
Expand Down Expand Up @@ -66,4 +67,11 @@ namespace Assets
float NormalTextureScale;
float Reserverd2;
};

struct FMaterial final
{
std::string name_;
uint32_t globalId_;
Material gpuMaterial_;
};
}
Loading

0 comments on commit bd6f9a0

Please sign in to comment.