init
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2020-2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#extension GL_EXT_fragment_shading_rate : require
|
||||
|
||||
layout (binding = 1) uniform sampler2D samplerEnvMap;
|
||||
layout (binding = 2) uniform sampler2D samplerSphere;
|
||||
|
||||
layout (location = 0) in vec2 inUV;
|
||||
layout (location = 1) in vec3 inPos;
|
||||
layout (location = 2) in vec3 inNormal;
|
||||
layout (location = 3) in vec3 inViewVec;
|
||||
layout (location = 4) in vec3 inLightVec;
|
||||
|
||||
layout (binding = 0) uniform UBO {
|
||||
mat4 projection;
|
||||
mat4 modelview;
|
||||
mat4 skybox_modelview;
|
||||
int color_shading_rates;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec4 outColor;
|
||||
|
||||
layout(push_constant) uniform Push_Constants {
|
||||
vec4 offset;
|
||||
int object_type;
|
||||
} push_constants;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 color;
|
||||
|
||||
switch (push_constants.object_type) {
|
||||
case 0: // Skysphere
|
||||
{
|
||||
color = texture(samplerEnvMap, vec2(inUV.s, 1.0 - inUV.t));
|
||||
}
|
||||
break;
|
||||
|
||||
case 1: // Phong shading
|
||||
{
|
||||
vec3 ambient = texture(samplerSphere, inUV).rgb;
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 L = normalize(inLightVec);
|
||||
vec3 V = normalize(inViewVec);
|
||||
vec3 R = reflect(-L, N);
|
||||
vec3 diffuse = vec3(max(dot(N, L), 0.0));
|
||||
vec3 specular = vec3(pow(max(dot(R, V), 0.0), 8.0));
|
||||
color = vec4(ambient + diffuse + specular, 1.0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (ubo.color_shading_rates == 1) {
|
||||
// Visualize fragment shading rates
|
||||
|
||||
int v = 1;
|
||||
int h = 1;
|
||||
|
||||
if ((gl_ShadingRateEXT & gl_ShadingRateFlag2VerticalPixelsEXT) == gl_ShadingRateFlag2VerticalPixelsEXT) {
|
||||
v = 2;
|
||||
}
|
||||
if ((gl_ShadingRateEXT & gl_ShadingRateFlag4VerticalPixelsEXT) == gl_ShadingRateFlag4VerticalPixelsEXT) {
|
||||
v = 4;
|
||||
}
|
||||
if ((gl_ShadingRateEXT & gl_ShadingRateFlag2HorizontalPixelsEXT) == gl_ShadingRateFlag2HorizontalPixelsEXT) {
|
||||
h = 2;
|
||||
}
|
||||
if ((gl_ShadingRateEXT & gl_ShadingRateFlag4HorizontalPixelsEXT) == gl_ShadingRateFlag4HorizontalPixelsEXT) {
|
||||
h = 4;
|
||||
}
|
||||
|
||||
if (v == 1 && h == 1) {
|
||||
outColor = vec4(color.rrr * 1.0, 1.0);
|
||||
} else {
|
||||
outColor = vec4(color.rrr * 1.0 - ((v+h) * 0.05), 1.0);
|
||||
}
|
||||
} else {
|
||||
outColor = vec4(color.rgb, 1.0);
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,59 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2020-2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
layout (location = 1) in vec3 inNormal;
|
||||
layout (location = 2) in vec2 inUV;
|
||||
|
||||
layout (binding = 0) uniform UBO {
|
||||
mat4 projection;
|
||||
mat4 modelview;
|
||||
mat4 skybox_modelview;
|
||||
int color_shading_rates;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec2 outUV;
|
||||
layout (location = 1) out vec3 outPos;
|
||||
layout (location = 2) out vec3 outNormal;
|
||||
layout (location = 3) out vec3 outViewVec;
|
||||
layout (location = 4) out vec3 outLightVec;
|
||||
|
||||
layout(push_constant) uniform Push_Constants {
|
||||
vec4 offset;
|
||||
int object_type;
|
||||
} push_constants;
|
||||
|
||||
void main()
|
||||
{
|
||||
switch(push_constants.object_type) {
|
||||
case 0: // Skysphere
|
||||
outPos = vec3(mat3(ubo.skybox_modelview) * inPos);
|
||||
gl_Position = vec4(ubo.projection * vec4(outPos, 1.0));
|
||||
break;
|
||||
case 1: // Object
|
||||
vec3 localPos = inPos + push_constants.offset.xyz;
|
||||
outPos = vec3(ubo.modelview * vec4(localPos, 1.0));
|
||||
gl_Position = ubo.projection * ubo.modelview * vec4(localPos, 1.0);
|
||||
break;
|
||||
}
|
||||
outUV = inUV;
|
||||
outNormal = mat3(ubo.modelview) * inNormal;
|
||||
vec3 lightPos = mat3(ubo.modelview) * vec3(0.0, -10.0, -10.0);
|
||||
outLightVec = lightPos.xyz - outPos.xyz;
|
||||
outViewVec = -outPos.xyz;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,114 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
[[vk::binding(1, 0)]]
|
||||
Texture2D textureEnvMap : register(t1);
|
||||
[[vk::binding(1, 0)]]
|
||||
SamplerState samplerEnvMap : register(s1);
|
||||
[[vk::binding(2, 0)]]
|
||||
Texture2D textureSphere : register(t2);
|
||||
[[vk::binding(2, 0)]]
|
||||
SamplerState samplerSphere : register(s2);
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 modelview;
|
||||
float4x4 skybox_modelview;
|
||||
int color_shading_rates;
|
||||
};
|
||||
[[vk::binding(0, 0)]]
|
||||
ConstantBuffer<UBO> ubo : register(b0);
|
||||
|
||||
struct PushConstants
|
||||
{
|
||||
float4 offset;
|
||||
int object_type;
|
||||
};
|
||||
[[vk::push_constant]] PushConstants push_constants;
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float2 UV : TEXCOORD0;
|
||||
[[vk::location(2)]] float3 ViewVec : VECTOR0;
|
||||
[[vk::location(3)]] float3 LightVec : VECTOR1;
|
||||
};
|
||||
|
||||
float4 main(VSOutput input, uint shadingRate : SV_ShadingRate) : SV_TARGET
|
||||
{
|
||||
float4 color;
|
||||
|
||||
switch (push_constants.object_type) {
|
||||
case 0: // Skysphere
|
||||
{
|
||||
color = textureEnvMap.Sample(samplerEnvMap, float2(input.UV.x, 1.0 - input.UV.y));
|
||||
}
|
||||
break;
|
||||
|
||||
case 1: // Phong shading
|
||||
{
|
||||
float3 ambient = textureSphere.Sample(samplerSphere, input.UV).rgb;
|
||||
float3 N = normalize(input.Normal);
|
||||
float3 L = normalize(input.LightVec);
|
||||
float3 V = normalize(input.ViewVec);
|
||||
float3 R = reflect(-L, N);
|
||||
float3 diffuse = float3(max(dot(N, L), (0.0).xxx));
|
||||
float3 specular = float3(pow(max(dot(R, V), (0.0).xxx), 8.0));
|
||||
color = float4(ambient + diffuse + specular, 1.0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (ubo.color_shading_rates == 1) {
|
||||
// Visualize fragment shading rates
|
||||
|
||||
const uint SHADING_RATE_1X1 = 0;
|
||||
const uint SHADING_RATE_1X2 = 0x1;
|
||||
const uint SHADING_RATE_2X1 = 0x4;
|
||||
const uint SHADING_RATE_2X2 = 0x5;
|
||||
const uint SHADING_RATE_2X4 = 0x6;
|
||||
const uint SHADING_RATE_4X2 = 0x9;
|
||||
const uint SHADING_RATE_4X4 = 0xa;
|
||||
|
||||
int v = 1;
|
||||
int h = 1;
|
||||
|
||||
if ((shadingRate == SHADING_RATE_1X2) || (shadingRate == SHADING_RATE_2X2) || (shadingRate == SHADING_RATE_4X2)) {
|
||||
v = 2;
|
||||
}
|
||||
if ((shadingRate == SHADING_RATE_2X4) || (shadingRate == SHADING_RATE_4X4)) {
|
||||
v = 4;
|
||||
}
|
||||
if ((shadingRate == SHADING_RATE_2X1) || (shadingRate == SHADING_RATE_2X2) || (shadingRate == SHADING_RATE_2X4)) {
|
||||
h = 2;
|
||||
}
|
||||
if ((shadingRate == SHADING_RATE_4X2) || (shadingRate == SHADING_RATE_4X4)) {
|
||||
h = 4;
|
||||
}
|
||||
|
||||
if (v == 1 && h == 1) {
|
||||
return float4(color.rrr * 1.0, 1.0);
|
||||
} else {
|
||||
return float4(color.rrr * 1.0 - ((v+h) * 0.05), 1.0);
|
||||
}
|
||||
} else {
|
||||
return float4(color.rgb, 1.0);
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,73 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]] float3 Pos : POSITION0;
|
||||
[[vk::location(1)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 modelview;
|
||||
float4x4 skybox_modelview;
|
||||
int color_shading_rates;
|
||||
};
|
||||
[[vk::binding(0, 0)]]
|
||||
ConstantBuffer<UBO> ubo : register(b0);
|
||||
|
||||
struct PushConstants
|
||||
{
|
||||
float4 offset;
|
||||
int object_type;
|
||||
};
|
||||
[[vk::push_constant]] PushConstants push_constants;
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float2 UV : TEXCOORD0;
|
||||
[[vk::location(2)]] float3 ViewVec : VECTOR0;
|
||||
[[vk::location(3)]] float3 LightVec : VECTOR1;
|
||||
};
|
||||
|
||||
VSOutput main(VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
float3 outPos;
|
||||
|
||||
switch(push_constants.object_type) {
|
||||
case 0: // Skysphere
|
||||
outPos = mul((float3x3)ubo.skybox_modelview, input.Pos);
|
||||
output.Pos = float4(mul(ubo.projection, float4(outPos, 1.0)));
|
||||
break;
|
||||
case 1: // Object
|
||||
float3 localPos = input.Pos + push_constants.offset.xyz;
|
||||
outPos = mul((float3x3)ubo.modelview, localPos);
|
||||
output.Pos = mul(ubo.projection, mul(ubo.modelview, float4(localPos, 1.0)));
|
||||
break;
|
||||
}
|
||||
output.UV = input.UV;
|
||||
output.Normal = mul((float3x3)ubo.modelview, input.Normal);
|
||||
float3 lightPos = mul((float3x3)ubo.modelview, float3(0.0, -10.0, -10.0));
|
||||
output.LightVec = lightPos.xyz - outPos.xyz;
|
||||
output.ViewVec = -outPos.xyz;
|
||||
return output;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user