Skip to content

Commit

Permalink
all renderer compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
gameknife committed Dec 28, 2024
1 parent 14a8fd4 commit f640ae5
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 82 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
26 changes: 16 additions & 10 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()
{
NodeProxy proxy = NodeProxies[gl_InstanceIndex];
Material m = Materials[proxy.matId[0]];
gl_Position = Camera.Projection * Camera.ModelView * proxy.worldTS * vec4(InPosition, 1.0);
// FragColor = m.Diffuse.xyz;
// FragNormal = (proxy.worldTS * vec4(InNormal, 0.0)).xyz;
// FragTexCoord = InTexCoord;
// FragMaterialIndex = proxy.matId[InMaterialIndex];

Vertex v = UncompressVertex(InPosition, TexcoordXY, NormalXY, NormalZTangentX, TangentYZ, TangentWMatIdx);
Material m = Materials[proxy.matId[v.MaterialIndex]];
FragColor = m.Diffuse.xyz;
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[0];

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;
}
43 changes: 23 additions & 20 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 @@ -23,22 +20,8 @@ struct GPUVertex
uint TangentWMatIdx;
};


Vertex UnpackVertex(uint index)
Vertex UncompressVertex(in vec3 Position, in uint TexcoordXY, in uint NormalXY, in uint NormalZTangentX, in uint TangentYZ, in uint TangentWMatIdx)
{
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;

Vertex v;

v.Position = Position;
Expand All @@ -51,15 +34,35 @@ Vertex UnpackVertex(uint index)
unpackHalf2x16(NormalZTangentX).y,
unpackHalf2x16(TangentYZ).x,
unpackHalf2x16(TangentYZ).y,
float(TangentW) - 1.0f
float((TangentWMatIdx >> 16) & 0xFFFF) - 1.0f
);
v.TexCoord = vec2(
unpackHalf2x16(TexcoordXY).x,
unpackHalf2x16(TexcoordXY).y
);
v.MaterialIndex = matIdx;
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
10 changes: 9 additions & 1 deletion src/Assets/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#include "Options.hpp"
#include "Texture.hpp"

#define FLATTEN_VERTICE 0
#define FLATTEN_VERTICE 1

using namespace glm;

Expand Down Expand Up @@ -230,6 +230,13 @@ namespace Assets
nodeMap[child]->SetParent(sceneNode);
}
}

bool LoadImageData(tinygltf::Image * image, const int image_idx, std::string * err,
std::string * warn, int req_width, int req_height,
const unsigned char * bytes, int size, void * user_data )
{
return true;
}

void Model::LoadGLTFScene(const std::string& filename, Assets::EnvironmentSetting& cameraInit, std::vector< std::shared_ptr<Assets::Node> >& nodes,
std::vector<Assets::Model>& models,
Expand All @@ -243,6 +250,7 @@ namespace Assets
std::string err;
std::string warn;

gltfLoader.SetImageLoader(LoadImageData, nullptr);
if(!gltfLoader.LoadBinaryFromFile(&model, &err, &warn, filename) )
{
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Vulkan/GraphicsPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GraphicsPipeline::GraphicsPipeline(
{
const auto& device = swapChain.Device();
const auto bindingDescription = Assets::GPUVertex::GetBindingDescription();
const auto attributeDescriptions = Assets::GPUVertex::GetAttributeDescriptions();
const auto attributeDescriptions = Assets::GPUVertex::GetFastAttributeDescriptions();

VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Expand Down

0 comments on commit f640ae5

Please sign in to comment.