Skip to content

Commit

Permalink
Merge branch 'gfx-ray-query' into v3.8.5-pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
star-e committed Oct 15, 2024
2 parents f833b8a + c7c47ab commit 86b6365
Show file tree
Hide file tree
Showing 95 changed files with 4,811 additions and 21 deletions.
9 changes: 9 additions & 0 deletions cocos/asset/assets/effect-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ export declare namespace EffectAsset {
count: number;
stageFlags: ShaderStageFlags;
}

export interface IIAccelerationStructureInfp{
set: number;
binding: number;
name: string;
count: number;
stageFlags: ShaderStageFlags;
}
export interface IAttributeInfo {
name: string;
format: Format;
Expand Down Expand Up @@ -174,6 +182,7 @@ export declare namespace EffectAsset {
images: IImageInfo[];
subpassInputs: IInputAttachmentInfo[];
descriptors: IDescriptorInfo[];
accelerationStructures: IIAccelerationStructureInfp[];
}
export interface IPreCompileInfo {
[name: string]: boolean[] | number[] | string[];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#pragma extension([GL_EXT_ray_query, __VERSION__ >= 460, enable])

#pragma builtin(global)
layout(set = 0, binding = 8) uniform accelerationStructureEXT cc_topLevelAS;

34 changes: 34 additions & 0 deletions editor/assets/chunks/builtin/uniforms/cc-scene-shading-data.chunk
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma extension([GL_EXT_scalar_block_layout, __VERSION__ >= 450, enable])
#pragma extension([GL_EXT_buffer_reference2, __VERSION__ >= 450, require])
#pragma extension([GL_EXT_shader_explicit_arithmetic_types,__VERSION__>=450,enable])

layout(buffer_reference, scalar) readonly buffer IndicesBuffer32 { ivec3 i[]; };
layout(buffer_reference, scalar) readonly buffer IndicesBuffer16 { i16vec3 i[]; };

struct VertexAttributes {
vec3 position;
vec3 normal;
vec2 texCoord;
vec4 tangent;
};

layout(buffer_reference, scalar) readonly buffer VertexAttributesBuffer { VertexAttributes v[];};

struct GeomDesc{
uint64_t indexAddress;
uint64_t vertexAddress;
};

struct MeshDesc
{
uint16_t subMeshGeometryOffset;
uint16_t subMeshMaterialOffset;
uint16_t subMeshCount;
uint16_t padding;
};

#pragma builtin(global)
layout(set = 0, binding = 9) readonly buffer cc_scene_geometry_desc {GeomDesc geomDescs[];};

#pragma builtin(global)
layout(set = 0, binding = 10) readonly buffer cc_scene_instance_desc {MeshDesc instanceDescs[];};
67 changes: 67 additions & 0 deletions editor/assets/chunks/legacy/shading-standard-additive.chunk
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,70 @@ vec4 CCStandardShadingAdditive (StandardSurface s, vec4 shadowPos) {
return vec4(finalColor, 0.0);
}
#endif

#if __VERSION__ >= 460

#pragma extension([GL_EXT_ray_query, __VERSION__ >= 460, enable])

vec4 CCStandardShadingAdditiveShadowRay(in StandardSurface s, in vec3 V,in accelerationStructureEXT as) {
vec3 position;
HIGHP_VALUE_FROM_STRUCT_DEFINED(position, s.position);
// Calculate diffuse & specular
vec3 diffuse = s.albedo.rgb * (1.0 - s.metallic);
vec3 specular = mix(vec3(0.04), s.albedo.rgb, s.metallic);
vec3 diffuseContrib = diffuse / PI;

vec3 N = normalize(s.normal);
float NV = max(abs(dot(N, V)), 0.0);
specular = BRDFApprox(specular, s.roughness, NV);
vec3 finalColor = vec3(0.0);

int numLights = CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_FORWARD ? LIGHTS_PER_PASS : int(cc_lightDir[0].w);

for (int i = 0; i < LIGHTS_PER_PASS; i++) {
if (i >= numLights) break;
vec3 SLU = cc_lightPos[i].xyz - position;
vec3 SL = normalize(SLU);
vec3 SH = normalize(SL + V);
float SNL = max(dot(N, SL), 0.0);
float SNH = max(dot(N, SH), 0.0);

float distSqr = dot(SLU, SLU);
float litRadius = cc_lightSizeRangeAngle[i].x;
float litRadiusSqr = litRadius * litRadius;
float illum = litRadiusSqr / max(litRadiusSqr, distSqr);
float attRadiusSqrInv = 1.0 / max(cc_lightSizeRangeAngle[i].y, 0.01);
attRadiusSqrInv *= attRadiusSqrInv;
float att = GetDistAtt(distSqr, attRadiusSqrInv);
vec3 lspec = specular * CalcSpecular(s.roughness, SNH, SH, N);

if (cc_lightPos[i].w > 0.0) {
float cosInner = max(dot(-cc_lightDir[i].xyz, SL), 0.01);
float cosOuter = cc_lightSizeRangeAngle[i].z;
float litAngleScale = 1.0 / max(0.001, cosInner - cosOuter);
float litAngleOffset = -cosOuter * litAngleScale;
att *= GetAngleAtt(SL, -cc_lightDir[i].xyz, litAngleScale, litAngleOffset);
}

vec3 lightColor = cc_lightColor[i].rgb;

float shadow = 1.0;
rayQueryEXT rayQuery;
rayQueryInitializeEXT(rayQuery,as,gl_RayFlagsTerminateOnFirstHitEXT, 0xFF, position, 0.1f, SL, length(SLU));

while(rayQueryProceedEXT(rayQuery));

if(rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT)
{
float t = rayQueryGetIntersectionTEXT(rayQuery, true);
shadow -= (length(SLU) - t) / length(SLU);
}

lightColor *= shadow;
finalColor += SNL * lightColor * cc_lightColor[i].w * illum * att * (diffuseContrib + lspec);
}

return vec4(finalColor, 0.0);
}

#endif
99 changes: 99 additions & 0 deletions editor/assets/chunks/legacy/shading-standard-base.chunk
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,102 @@ vec4 CCStandardShadingBase (StandardSurface s, vec4 shadowPos) {

return vec4(finalColor, s.albedo.a);
}

#if __VERSION__ >=460

#pragma extension([GL_EXT_ray_query, __VERSION__ >= 460, enable])

vec4 CCStandardShadingBaseShadowRay (in StandardSurface s,in vec3 V,in accelerationStructureEXT as) {
// Calculate diffuse & specular

vec3 diffuse = s.albedo.rgb * (1.0 - s.metallic);
vec3 specular = mix(vec3(0.08 * s.specularIntensity), s.albedo.rgb, s.metallic);

vec3 position;
HIGHP_VALUE_FROM_STRUCT_DEFINED(position, s.position);

vec3 N = normalize(s.normal);
vec3 L = normalize(-cc_mainLitDir.xyz);
float NL = max(dot(N, L), 0.0);

float shadow = 1.0;

rayQueryEXT rayQuery;
rayQueryInitializeEXT(rayQuery,as,gl_RayFlagsTerminateOnFirstHitEXT, 0xFF, position, 0.1f, normalize(vec3(L.x,L.y,L.z)), 10000.0);
while(rayQueryProceedEXT(rayQuery));

if(rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT)
{
shadow = 0.0;
}

#if CC_USE_LIGHTMAP && !CC_FORWARD_ADD
vec3 lightmap = s.lightmap.rgb;
#if CC_USE_HDR
// convert from standard camera exposure parameters to current exposure value
// baked in LDR scene still regarded as exposured with standard camera parameters
lightmap.rgb *= cc_exposure.w * cc_exposure.x;
#endif
vec3 finalColor = diffuse * lightmap.rgb * shadow;
#else
float NV = max(abs(dot(N, V)), 0.0);
specular = BRDFApprox(specular, s.roughness, NV);

vec3 H = normalize(L + V);
float NH = max(dot(N, H), 0.0);
vec3 finalColor = NL * cc_mainLitColor.rgb * cc_mainLitColor.w;
vec3 diffuseContrib = diffuse / PI;

// Cook-Torrance Microfacet Specular BRDF
vec3 specularContrib = specular * CalcSpecular(s.roughness, NH, H, N);
vec3 dirlightContrib = (diffuseContrib + specularContrib);

dirlightContrib *= shadow;
finalColor *= dirlightContrib;
#endif

float fAmb = 0.5 - N.y * 0.5;
vec3 ambDiff = mix(cc_ambientSky.rgb, cc_ambientGround.rgb, fAmb);


#if CC_USE_IBL
#if CC_USE_DIFFUSEMAP && (!CC_LIGHT_PROBE_ENABLED || !CC_USE_LIGHT_PROBE)
// Diffuse reflection irradiance
vec4 diffuseMap = texture(cc_diffuseMap, N);
#if CC_USE_DIFFUSEMAP == IBL_RGBE
ambDiff = unpackRGBE(diffuseMap);
#else
ambDiff = SRGBToLinear(diffuseMap.rgb);
#endif
#endif

vec3 R = normalize(reflect(-V, N));

vec3 rotationDir = RotationVecFromAxisY(R.xyz, cc_surfaceTransform.z, cc_surfaceTransform.w);
#if USE_REFLECTION_DENOISE && !CC_IBL_CONVOLUTED
vec3 env = GetEnvReflectionWithMipFiltering(rotationDir, s.roughness, cc_ambientGround.w, 0.6);
#else
vec4 envmap = fragTextureLod(cc_environment, rotationDir, s.roughness * (cc_ambientGround.w - 1.0));

#if CC_USE_IBL == IBL_RGBE
vec3 env = unpackRGBE(envmap);
#else
vec3 env = SRGBToLinear(envmap.rgb);
#endif
#endif

finalColor += env * cc_ambientSky.w * specular * s.occlusion;
#endif

#if CC_LIGHT_PROBE_ENABLED && CC_USE_LIGHT_PROBE
ambDiff = SHEvaluate(N);
finalColor += ambDiff.rgb * diffuse * s.occlusion;
#else
finalColor += ambDiff.rgb * cc_ambientSky.w * diffuse * s.occlusion;
#endif

finalColor += s.emissive;

return vec4(finalColor, s.albedo.a);
}
#endif
Loading

0 comments on commit 86b6365

Please sign in to comment.