Skip to content

Commit

Permalink
fix: simplify handling PrimitiveFingerPrint
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki Shimada committed Dec 14, 2024
1 parent 8074b92 commit a412aa2
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 25 deletions.
28 changes: 11 additions & 17 deletions src/foundation/materials/core/Material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class Material extends RnObject {

// Ids
private _shaderProgramUidMap: Map<PrimitiveFingerPrint, CGAPIResourceHandle> = new Map();
private _primitiveFingerPrintBackUp: PrimitiveFingerPrint = '';
__materialUid: MaterialUID = -1;
private __materialTid: MaterialTID;
__materialSid: MaterialSID = -1; // material serial Id in the material type
Expand Down Expand Up @@ -280,7 +279,7 @@ export class Material extends RnObject {
* called from WebGLStrategyDataTexture and WebGLStrategyUniform only
* @param isUniformOnlyMode
*/
_setUniformLocationsOfMaterialNodes(isUniformOnlyMode: boolean, primitive?: Primitive) {
_setUniformLocationsOfMaterialNodes(isUniformOnlyMode: boolean, primitive: Primitive) {
const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository();

let array: ShaderSemanticsInfo[] = [];
Expand All @@ -289,15 +288,12 @@ export class Material extends RnObject {
array = array.concat(semanticsInfoArray);
}

const shaderProgramUid = this._shaderProgramUidMap.get(
primitive != null ? primitive._getFingerPrint() : this._primitiveFingerPrintBackUp
);
const shaderProgramUid = this._shaderProgramUidMap.get(primitive._getFingerPrint());
webglResourceRepository.setupUniformLocations(shaderProgramUid!, array, isUniformOnlyMode);
}

getShaderProgramUid(primitive?: Primitive): CGAPIResourceHandle {
const primitiveFingerPrint =
primitive !== undefined ? primitive._getFingerPrint() : this._primitiveFingerPrintBackUp;
getShaderProgramUid(primitive: Primitive): CGAPIResourceHandle {
const primitiveFingerPrint = primitive._getFingerPrint();
return this._shaderProgramUidMap.get(primitiveFingerPrint) ?? -1;
}

Expand Down Expand Up @@ -338,7 +334,6 @@ export class Material extends RnObject {
isWebGL2
);
this._shaderProgramUidMap.set(primitive._getFingerPrint(), programUid);
this._primitiveFingerPrintBackUp = primitive._getFingerPrint();

Material.__stateVersion++;

Expand All @@ -360,7 +355,6 @@ export class Material extends RnObject {
);

this._shaderProgramUidMap.set(primitive._getFingerPrint(), programUid);
this._primitiveFingerPrintBackUp = primitive._getFingerPrint();
Material.__stateVersion++;
}

Expand All @@ -375,15 +369,17 @@ export class Material extends RnObject {
*/
_createProgramByUpdatedSources(
updatedShaderSources: ShaderSources,
primitive: Primitive,
onError?: (message: string) => void
): [CGAPIResourceHandle, boolean] {
const [programUid, newOne] = _createProgramAsSingleOperationByUpdatedSources(
this,
primitive,
this._materialContent,
updatedShaderSources,
onError
);
this._shaderProgramUidMap.set(this._primitiveFingerPrintBackUp, programUid);
this._shaderProgramUidMap.set(primitive._getFingerPrint(), programUid);

if (programUid > 0) {
// this.__updatedShaderSources = updatedShaderSources;
Expand All @@ -397,11 +393,10 @@ export class Material extends RnObject {
* @internal
* called WebGLStrategyDataTexture and WebGLStrategyUniform only
*/
_setupBasicUniformsLocations(primitive?: Primitive) {
_setupBasicUniformsLocations(primitive: Primitive) {
const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository();

const primitiveFingerPrint =
primitive != null ? primitive._getFingerPrint() : this._primitiveFingerPrintBackUp;
const primitiveFingerPrint = primitive._getFingerPrint();
const shaderProgramUid = this._shaderProgramUidMap.get(primitiveFingerPrint);
webglResourceRepository.setupBasicUniformLocations(shaderProgramUid!);
}
Expand All @@ -413,11 +408,10 @@ export class Material extends RnObject {
_setupAdditionalUniformLocations(
shaderSemantics: ShaderSemanticsInfo[],
isUniformOnlyMode: boolean,
primitive?: Primitive
primitive: Primitive
) {
const webglResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository();
const primitiveFingerPrint =
primitive != null ? primitive._getFingerPrint() : this._primitiveFingerPrintBackUp;
const primitiveFingerPrint = primitive._getFingerPrint();
const shaderProgramUid = this._shaderProgramUidMap.get(primitiveFingerPrint);
webglResourceRepository.setupUniformLocations(
shaderProgramUid!,
Expand Down
7 changes: 7 additions & 0 deletions src/foundation/materials/core/ShaderHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class ShaderHandler {
/**
* Create a shader program Or Get a shader program from cache
* @param material
* @param primitive
* @param vertexShader
* @param pixelShader
* @param attributeNames
Expand All @@ -33,6 +34,7 @@ export class ShaderHandler {
*/
static _createShaderProgramWithCache(
material: Material,
primitive: Primitive,
vertexShader: string,
pixelShader: string,
attributeNames: AttributeNames,
Expand All @@ -49,6 +51,7 @@ export class ShaderHandler {
const cgApiResourceRepository = CGAPIResourceRepository.getCgApiResourceRepository();
shaderProgramUid = cgApiResourceRepository.createShaderProgram({
material,
primitive,
vertexShaderStr: vertexShader,
fragmentShaderStr: pixelShader,
attributeNames: attributeNames,
Expand All @@ -62,6 +65,7 @@ export class ShaderHandler {

export function _createProgramAsSingleOperationByUpdatedSources(
material: Material,
primitive: Primitive,
materialNode: AbstractMaterialContent,
updatedShaderSources: ShaderSources,
onError?: (message: string) => void
Expand All @@ -70,6 +74,7 @@ export function _createProgramAsSingleOperationByUpdatedSources(

const [shaderProgramUid, newOne] = ShaderHandler._createShaderProgramWithCache(
material,
primitive,
updatedShaderSources.vertex,
updatedShaderSources.pixel,
attributeNames,
Expand Down Expand Up @@ -182,6 +187,7 @@ export function _createProgramAsSingleOperationWebGL(

const [shaderProgramUid, newOne] = ShaderHandler._createShaderProgramWithCache(
material,
primitive,
vertexShader,
pixelShader,
attributeNames,
Expand Down Expand Up @@ -307,6 +313,7 @@ export function _createProgramAsSingleOperationWebGpu(

const [programUid, newOne] = ShaderHandler._createShaderProgramWithCache(
material,
primitive,
preprocessedVertex.code,
preprocessedPixel.code,
[],
Expand Down
2 changes: 2 additions & 0 deletions src/foundation/renderer/CGAPIResourceRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,15 @@ export interface ICGAPIResourceRepository {
*/
createShaderProgram({
material,
primitive,
vertexShaderStr,
fragmentShaderStr,
attributeNames,
attributeSemantics,
onError,
}: {
material: Material;
primitive: Primitive;
vertexShaderStr: string;
fragmentShaderStr: string;
attributeNames: AttributeNames;
Expand Down
2 changes: 2 additions & 0 deletions src/webgl/WebGLExtendedTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ShaderSemanticsName } from '../foundation/definitions/ShaderSemantics';
import { ShaderSemanticsInfo } from '../foundation/definitions/ShaderSemanticsInfo';
import { Primitive } from '../foundation/geometry/Primitive';
import { Material } from '../foundation/materials/core/Material';

export interface RnWebGLProgram extends WebGLProgram {
Expand All @@ -10,6 +11,7 @@ export interface RnWebGLProgram extends WebGLProgram {
_shaderSemanticsInfoMap: Map<ShaderSemanticsName, ShaderSemanticsInfo>;
__SPECTOR_rebuildProgram: unknown;
_material: WeakRef<Material>;
_primitive: WeakRef<Primitive>;
}

export interface RnWebGLTexture extends WebGLTexture {
Expand Down
4 changes: 4 additions & 0 deletions src/webgl/WebGLResourceRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,15 @@ export class WebGLResourceRepository
*/
createShaderProgram({
material,
primitive,
vertexShaderStr,
fragmentShaderStr,
attributeNames,
attributeSemantics,
onError,
}: {
material: Material;
primitive: Primitive;
vertexShaderStr: string;
fragmentShaderStr: string;
attributeNames: AttributeNames;
Expand Down Expand Up @@ -416,6 +418,7 @@ export class WebGLResourceRepository
shaderProgram._fragmentShaderStr = fragmentShaderStr;
shaderProgram._shaderSemanticsInfoMap = new Map();
shaderProgram._material = new WeakRef(material);
shaderProgram._primitive = new WeakRef(primitive);

gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
Expand Down Expand Up @@ -2882,6 +2885,7 @@ vec4 fetchVec4FromVec4Block(int vec4Idx) {

const programUid = renderingStrategy._reSetupShaderForMaterialBySpector(
material,
this._primitive.deref()!,
{
vertex: modifiedVertexSourceCode,
pixel: modifiedPixelSourceCode,
Expand Down
1 change: 1 addition & 0 deletions src/webgl/WebGLStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface WebGLStrategy {
setupShaderForMaterial(material: Material, primitive: Primitive): CGAPIResourceHandle;
_reSetupShaderForMaterialBySpector(
material: Material,
primitive: Primitive,
updatedShaderSources: ShaderSources,
onError: (message: string) => void
): CGAPIResourceHandle;
Expand Down
11 changes: 7 additions & 4 deletions src/webgl/WebGLStrategyDataTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,30 +190,33 @@ export class WebGLStrategyDataTexture implements CGAPIStrategy, WebGLStrategy {
*/
public _reSetupShaderForMaterialBySpector(
material: Material,
primitive: Primitive,
updatedShaderSources: ShaderSources,
onError: (message: string) => void
): CGAPIResourceHandle {
const [programUid, newOne] = material._createProgramByUpdatedSources(
updatedShaderSources,
primitive,
onError
);
if (programUid === CGAPIResourceRepository.InvalidCGAPIResourceUid) {
return programUid;
}

if (newOne) {
material._setupBasicUniformsLocations();
material._setupBasicUniformsLocations(primitive);

material._setUniformLocationsOfMaterialNodes(false);
material._setUniformLocationsOfMaterialNodes(false, primitive);

material._setupAdditionalUniformLocations(
WebGLStrategyCommonMethod.getPointSpriteShaderSemanticsInfoArray(),
false
false,
primitive
);
}

WebGLStrategyDataTexture.__globalDataRepository._setUniformLocationsForDataTextureModeOnly(
material.getShaderProgramUid()
material.getShaderProgramUid(primitive)
);

return programUid;
Expand Down
11 changes: 7 additions & 4 deletions src/webgl/WebGLStrategyUniform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,34 +211,37 @@ bool get_isBillboard(float instanceId) {
*/
public _reSetupShaderForMaterialBySpector(
material: Material,
primitive: Primitive,
updatedShaderSources: ShaderSources,
onError: (message: string) => void
): CGAPIResourceHandle {
const [programUid, newOne] = material._createProgramByUpdatedSources(
updatedShaderSources,
primitive,
onError
);
if (programUid === CGAPIResourceRepository.InvalidCGAPIResourceUid) {
return programUid;
}

if (newOne) {
material._setupBasicUniformsLocations();
material._setupBasicUniformsLocations(primitive);

material._setUniformLocationsOfMaterialNodes(true);
material._setUniformLocationsOfMaterialNodes(true, primitive);

const shaderSemanticsInfos = WebGLStrategyUniform.componentMatrices;
const shaderSemanticsInfosPointSprite =
WebGLStrategyCommonMethod.getPointSpriteShaderSemanticsInfoArray();

material._setupAdditionalUniformLocations(
shaderSemanticsInfos.concat(shaderSemanticsInfosPointSprite),
true
true,
primitive
);
}

WebGLStrategyUniform.__globalDataRepository._setUniformLocationsForUniformModeOnly(
material.getShaderProgramUid()
material.getShaderProgramUid(primitive)
);

return programUid;
Expand Down
2 changes: 2 additions & 0 deletions src/webgpu/WebGpuResourceRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,13 +773,15 @@ export class WebGpuResourceRepository
*/
createShaderProgram({
material,
primitive,
vertexShaderStr,
fragmentShaderStr,
attributeNames,
attributeSemantics,
onError,
}: {
material: Material;
primitive: Primitive;
vertexShaderStr: string;
fragmentShaderStr: string;
attributeNames: AttributeNames;
Expand Down

0 comments on commit a412aa2

Please sign in to comment.