-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Voxel shadow fixes for interleaved buffers #17376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
df7868e
719cc5b
8d17560
cd0ae8d
61f7e37
3d8897e
33b07ed
437c627
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,8 @@ import { MaterialFlags } from "./materialFlags"; | |
| import { Texture } from "./Textures/texture"; | ||
| import type { CubeTexture } from "./Textures/cubeTexture"; | ||
| import type { Color3 } from "core/Maths/math.color"; | ||
| import type { Geometry } from "../Meshes/geometry"; | ||
| import { Vector3 } from "core/Maths/math.vector"; | ||
|
|
||
| // For backwards compatibility, we export everything from the pure version of this file. | ||
| export * from "./materialHelper.functions.pure"; | ||
|
|
@@ -598,6 +600,98 @@ export function GetFogState(mesh: AbstractMesh, scene: Scene) { | |
| return scene.fogEnabled && mesh.applyFog && scene.fogMode !== Constants.FOGMODE_NONE; | ||
| } | ||
|
|
||
| /** | ||
| * Interface representing metadata for vertex pulling | ||
| */ | ||
| export interface IVertexPullingMetadata { | ||
| /** | ||
| * Offset in vertex buffer where data starts | ||
| */ | ||
| offset: number; | ||
|
|
||
| /** | ||
| * Stride between elements in the vertex buffer | ||
| */ | ||
| stride: number; | ||
|
|
||
| /** | ||
| * Type of the vertex buffer (e.g., float, int) | ||
| */ | ||
| type: number; // VertexBuffer type constant | ||
| } | ||
|
|
||
| // Store vertex pulling metadata per geometry | ||
| const _VertexPullingMetadataCache = new WeakMap<Geometry, Map<string, IVertexPullingMetadata>>(); | ||
|
|
||
| /** | ||
| * Prepares vertex pulling uniforms for the given attributes and mesh | ||
| * @param geometry The geometry containing the vertex buffers | ||
| * @returns A map of attribute names to their metadata, or null if unavailable | ||
| */ | ||
| export function PrepareVertexPullingUniforms(geometry: Geometry): Nullable<Map<string, IVertexPullingMetadata>> { | ||
| if (!geometry) { | ||
| return null; | ||
| } | ||
| const vertexBuffers = geometry.getVertexBuffers(); | ||
| if (!vertexBuffers) { | ||
| return null; | ||
| } | ||
|
|
||
| // Check cache first | ||
| let metadata = _VertexPullingMetadataCache.get(geometry); | ||
| if (!metadata) { | ||
| metadata = new Map<string, IVertexPullingMetadata>(); | ||
| _VertexPullingMetadataCache.set(geometry, metadata); | ||
| } else { | ||
| // Return cached metadata if it exists and hasn't changed | ||
| let needsUpdate = false; | ||
| for (const vb in vertexBuffers) { | ||
| if (!metadata.has(vb)) { | ||
| needsUpdate = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!needsUpdate) { | ||
| return metadata; | ||
| } | ||
| } | ||
|
|
||
| // Build or update metadata | ||
| for (const vb in vertexBuffers) { | ||
| const vertexBuffer = vertexBuffers[vb]; | ||
| if (vertexBuffer) { | ||
| const offset = vertexBuffer.byteOffset; | ||
| const stride = vertexBuffer.byteStride; | ||
| const type = vertexBuffer.type; | ||
|
|
||
| metadata.set(vb, { | ||
| offset: offset, | ||
| stride: stride, | ||
| type: type, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return metadata; | ||
| } | ||
|
|
||
| /** | ||
| * Bind vertex pulling uniforms to the effect | ||
| * @param effect The effect to bind the uniforms to | ||
| * @param metadata The vertex pulling metadata | ||
| */ | ||
| export function BindVertexPullingUniforms(effect: Effect, metadata: Map<string, IVertexPullingMetadata>): void { | ||
| if (!metadata || !effect) { | ||
| return; | ||
| } | ||
|
|
||
| for (const [attribute, data] of metadata.entries()) { | ||
| const uniformName = `vp_${attribute}_info`; | ||
| // Pack into vec3: (offset, stride, type) | ||
| effect.setVector3(uniformName, new Vector3(data.offset, data.stride, data.type)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper used to prepare the list of defines associated with misc. values for shader compilation | ||
| * @param mesh defines the current mesh | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,8 +30,11 @@ import { | |
| BindMorphTargetParameters, | ||
| BindSceneUniformBuffer, | ||
| PrepareDefinesAndAttributesForMorphTargets, | ||
| PrepareVertexPullingUniforms, | ||
| BindVertexPullingUniforms, | ||
| PushAttributesForInstances, | ||
| } from "./materialHelper.functions"; | ||
| import type { IVertexPullingMetadata } from "./materialHelper.functions"; | ||
| import type { IColor3Like, IColor4Like, IVector2Like, IVector3Like, IVector4Like } from "core/Maths/math.like"; | ||
| import type { InternalTexture } from "./Textures/internalTexture"; | ||
|
|
||
|
|
@@ -147,6 +150,7 @@ export class ShaderMaterial extends PushMaterial { | |
| private _cachedWorldViewMatrix = new Matrix(); | ||
| private _cachedWorldViewProjectionMatrix = new Matrix(); | ||
| private _multiview = false; | ||
| private _vertexPullingMetadata: Map<string, IVertexPullingMetadata> | null = null; | ||
|
|
||
| /** | ||
| * @internal | ||
|
|
@@ -909,12 +913,18 @@ export class ShaderMaterial extends PushMaterial { | |
| defines.push("#define USE_VERTEX_PULLING"); | ||
|
|
||
| const indexBuffer = renderingMesh.geometry?.getIndexBuffer(); | ||
| if (indexBuffer) { | ||
| if (indexBuffer && !(renderingMesh as Mesh).isUnIndexed) { | ||
| defines.push("#define VERTEX_PULLING_USE_INDEX_BUFFER"); | ||
| if (indexBuffer.is32Bits) { | ||
| defines.push("#define VERTEX_PULLING_INDEX_BUFFER_32BITS"); | ||
| } | ||
| } | ||
|
|
||
| // Add vertex buffer metadata defines for proper stride/offset handling | ||
| const geometry = renderingMesh.geometry; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you want to use the new Also, I think we should pass Another thing is that we should probably use uniforms and not defines to pass data, else we will basically generate a new shader for each mesh, as (at least)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, good idea. I've changed to uniforms now. |
||
| if (geometry) { | ||
| this._vertexPullingMetadata = PrepareVertexPullingUniforms(geometry); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should normally update the |
||
| } | ||
| } | ||
|
|
||
| const drawWrapper = storeEffectOnSubMeshes ? subMesh._getDrawWrapper(undefined, true) : this._drawWrapper; | ||
|
|
@@ -1085,6 +1095,10 @@ export class ShaderMaterial extends PushMaterial { | |
| // Clip plane | ||
| BindClipPlane(effect, this, scene); | ||
|
|
||
| if (this._vertexPullingMetadata) { | ||
| BindVertexPullingUniforms(effect, this._vertexPullingMetadata); | ||
| } | ||
|
|
||
| // Misc | ||
| if (this._useLogarithmicDepth) { | ||
| BindLogDepth(storeEffectOnSubMeshes ? subMesh.materialDefines : effect.defines, effect, scene); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If metadata and effect can be null/undefined, you should update their declaration in
BindVertexPullingUniformsaccordingly.