Skip to content

Commit 53ef3b7

Browse files
committed
always textures
1 parent 1aee965 commit 53ef3b7

File tree

6 files changed

+102
-244
lines changed

6 files changed

+102
-244
lines changed

src/gpu_hash/shader.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ export class HashSetShaderManager {
119119
this.readTable = prefix + "_readTable";
120120
}
121121

122-
defineShader(builder: ShaderBuilder) {
122+
defineShader(builder: ShaderBuilder, fragment = true) {
123+
const addCode = fragment ? builder.addFragmentCode.bind(builder) : builder.addVertexCode.bind(builder);
123124
const { hashSeedsName, samplerName, numAlternatives, hashKeyMask } = this;
124125
builder.addUniform("highp uint", hashSeedsName, numAlternatives);
125126
builder.addUniform("highp uint", hashKeyMask);
@@ -135,16 +136,17 @@ export class HashSetShaderManager {
135136
// const adder = vertex ? builder.addVertexCode.bind(builder) : builder.addFragmentCode.bind(builder);
136137
// adder(glsl_hashCombine);
137138

138-
builder.addFragmentCode(glsl_hashCombine);
139-
builder.addFragmentCode(glsl_uint64);
140-
builder.addFragmentCode(glsl_equalUint64);
141-
this.accessHelper.defineShader(builder);
139+
addCode(glsl_hashCombine);
140+
addCode(glsl_uint64);
141+
addCode(glsl_equalUint64);
142+
console.log("what does this next thing do?");
143+
this.accessHelper.defineShader(builder); // TODO does thsi do anything?
142144
// TODO, can we use DataType.Float32 here for float segment properties?
143-
builder.addFragmentCode(
145+
addCode(
144146
this.accessHelper.getAccessor(
145147
this.readTable,
146148
this.samplerName,
147-
DataType.UINT64,
149+
DataType.UINT64, // TODO maybe play with this?
148150
1,
149151
),
150152
);
@@ -167,7 +169,7 @@ bool ${this.hasFunctionName}(uint64_t x) {
167169
return false;
168170
}
169171
`;
170-
builder.addFragmentCode(s);
172+
addCode(s);
171173
}
172174

173175
get hasFunctionName() {
@@ -201,8 +203,9 @@ bool ${this.hasFunctionName}(uint64_t x) {
201203
}
202204

203205
export class HashMapShaderManager extends HashSetShaderManager {
204-
defineShader(builder: ShaderBuilder) {
205-
super.defineShader(builder);
206+
defineShader(builder: ShaderBuilder, fragment = true) {
207+
const addCode = fragment ? builder.addFragmentCode.bind(builder) : builder.addVertexCode.bind(builder);
208+
super.defineShader(builder, fragment);
206209
const { numAlternatives, hashSeedsName, hashKeyMask } = this;
207210
let s = `
208211
bool ${this.getFunctionName}(uint64_t x, out uint64_t value) {
@@ -223,7 +226,7 @@ bool ${this.getFunctionName}(uint64_t x, out uint64_t value) {
223226
return false;
224227
}
225228
`;
226-
builder.addFragmentCode(s);
229+
addCode(s);
227230
}
228231

229232
get getFunctionName() {

src/layer/segmentation/index.ts

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import {
7070
getPreprocessedSegmentPropertyMap,
7171
getShaderOutputType,
7272
SegmentationColorUserShader,
73+
SegmentationColorUserShaderManager,
7374
} from "#src/segmentation_display_state/property_map.js";
7475
import { LocalSegmentationGraphSource } from "#src/segmentation_graph/local.js";
7576
import { VisibleSegmentEquivalencePolicy } from "#src/segmentation_graph/segment_id.js";
@@ -432,6 +433,8 @@ vec4 segmentColor(vec4 color) {
432433
class SegmentationUserLayerDisplayState implements SegmentationDisplayState {
433434
private getSegmentColorShader;
434435

436+
segmentationColorUserShader: SegmentationColorUserShaderManager;
437+
435438
constructor(public layer: SegmentationUserLayer) {
436439
// Even though `SegmentationUserLayer` assigns this to its `displayState` property, redundantly
437440
// assign it here first in order to allow it to be accessed by `segmentationGroupState`.
@@ -484,8 +487,6 @@ class SegmentationUserLayerDisplayState implements SegmentationDisplayState {
484487
),
485488
);
486489

487-
this.getSegmentColorShader = this.makeSegmentColorShaderGetter();
488-
489490
this.selectSegment = layer.selectSegment;
490491
this.filterBySegmentLabel = layer.filterBySegmentLabel;
491492

@@ -544,29 +545,18 @@ class SegmentationUserLayerDisplayState implements SegmentationDisplayState {
544545
),
545546
);
546547

547-
// this.segmentPropertyMap.changed.add(() => {
548-
// console.log(
549-
// "segmentPropertyMap changed",
550-
// this.segmentPropertyMap.value,
551-
// );
552-
// });
553-
554-
// this.segmentationGroupState.changed.add(() => {
555-
// console.log(
556-
// "segmentationGroupState changed",
557-
// this.segmentationGroupState.value,
558-
// );
559-
// });
560-
561-
// this.segmentationGroupState.value.segmentPropertyMap.changed.add(() => {
562-
// console.log(
563-
// "segmentationGroupState.value.segmentPropertyMap changed",
564-
// this.segmentationGroupState.value,
565-
// );
566-
// });
567-
568-
this.segmentationColorUserShader = new SegmentationColorUserShader(this);
569-
}
548+
// so 2d/3d share GL context
549+
// the offscreen canvas is a separate context that doesn't share textures
550+
this.segmentationColorUserShader = new SegmentationColorUserShaderManager(this, this.layer.manager.chunkManager.chunkQueueManager.gl);
551+
552+
this.offscreenGL = initializeWebGL(new OffscreenCanvas(1, 1));
553+
this.offscreenSegmentationColorUserShader = new SegmentationColorUserShaderManager(this, this.offscreenGL);
554+
this.getSegmentColorShader = this.makeSegmentColorShaderGetter();
555+
556+
};
557+
558+
offscreenGL;
559+
offscreenSegmentationColorUserShader;
570560

571561
segmentSelectionState = new SegmentSelectionState();
572562
selectedAlpha = trackableAlphaValue(0.5);
@@ -597,58 +587,35 @@ class SegmentationUserLayerDisplayState implements SegmentationDisplayState {
597587
useTempSegmentStatedColors2d: SharedWatchableValue<boolean>;
598588
hasVolume = new TrackableBoolean(false, false);
599589

600-
segmentationColorUserShader;
601-
602590
filterBySegmentLabel: (id: bigint) => void;
603591

604592
moveToSegment = (id: bigint) => {
605593
this.layer.moveToSegment(id);
606594
};
607595

608596
makeSegmentColorShaderGetter = () => {
609-
const gl = initializeWebGL(new OffscreenCanvas(1, 1));
610597
const parameters = this.layer.registerDisposer(
611598
new AggregateWatchableValue(() => ({
612599
shaderBuilderState: this.segmentColorShaderControlState.builderState,
613600
segmentProperties: this.segmentationGroupState.value.segmentPropertyMap,
614601
})),
615602
);
616-
return parameterizedEmitterDependentShaderGetter(this.layer, gl, {
603+
return parameterizedEmitterDependentShaderGetter(this.layer, this.offscreenGL, {
617604
memoizeKey: `segmentColorShaderTODO`,
618605
parameters,
619606
encodeParameters: (p) => {
620607
return `${p.shaderBuilderState.parseResult.code}/${p.segmentProperties?.numericalProperties.length}`;
621608
},
622609
shaderError: this.layer.displayState.shaderError, // TODO can I reuse this?
623610
defineShader: (builder, { }) => {
611+
this.offscreenSegmentationColorUserShader.defineShader(builder, /*fragment=*/ false);
624612
builder.addAttribute("highp vec4", "aVertexPosition");
625613
builder.addUniform("highp vec4", "uColor");
626614
builder.addUniform("highp uvec2", "uID");
627615
builder.addVarying("highp vec4", "vColor");
628-
629-
let vertexMain = ``;
630-
631-
// TODO this is basically a copy from mesh/frontend.ts
632-
// TODO we error when segment properties is undefined
633-
// for (const [i, property] of extractUsedSegmentProperties(
634-
// segmentProperties,
635-
// shaderBuilderState.parseResult.code,
636-
// )) {
637-
// builder.addVertexCode(
638-
// `highp ${getShaderOutputType(property.dataType)} segmentNumericalProperty${i};`,
639-
// );
640-
// builder.addUniform(
641-
// `highp ${getShaderOutputType(property.dataType)}`,
642-
// `uSegmentNumericalProperty${i}`,
643-
// );
644-
// vertexMain += `
645-
// segmentNumericalProperty${i} = uSegmentNumericalProperty${i};
646-
// `;
647-
// }
648-
this.segmentationColorUserShader.defineShader(builder);
649-
vertexMain += `
616+
let vertexMain = `
650617
gl_Position = aVertexPosition;
651-
loadSegmentProperties();
618+
loadSegmentProperties(uint64_t(uID));
652619
vColor = segmentColor(uColor);
653620
`;
654621
builder.addVertexMain(vertexMain);
@@ -673,8 +640,12 @@ vColor = segmentColor(uColor);
673640
positionBuffer.bindToVertexAttrib(shader.attribute("aVertexPosition"), 2);
674641
gl.uniform4fv(shader.uniform("uColor"), color);
675642

676-
this.segmentationColorUserShader.setSegmentPropertyUniforms(id, gl, shader);
677-
643+
this.offscreenSegmentationColorUserShader.enable(gl, shader);
644+
gl.uniform2ui(
645+
shader.uniform(`uID`),
646+
Number(id & 0xffffffffn),
647+
Number(id >> 32n),
648+
);
678649
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
679650
const data = new Uint8Array(4);
680651
// TODO can I read straight to float?

src/mesh/frontend.ts

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ import {
5454
registerRedrawWhenSegmentationDisplayState3DChanged,
5555
SegmentationLayerSharedObject,
5656
} from "#src/segmentation_display_state/frontend.js";
57-
import {
58-
extractUsedSegmentProperties,
59-
getShaderOutputType,
60-
SegmentationColorUserShader,
61-
} from "#src/segmentation_display_state/property_map.js";
6257
import type { WatchableValueInterface } from "#src/trackable_value.js";
6358
import {
6459
AggregateWatchableValue,
@@ -80,9 +75,9 @@ import type { GL } from "#src/webgl/context.js";
8075
import type { WatchableShaderError } from "#src/webgl/dynamic_shader.js";
8176
import {
8277
parameterizedEmitterDependentShaderGetter,
83-
shaderCodeWithLineDirective,
8478
} from "#src/webgl/dynamic_shader.js";
8579
import type { ShaderBuilder, ShaderProgram } from "#src/webgl/shader.js";
80+
import { glsl_uint64 } from "#src/webgl/shader_lib.js";
8681
import type { ShaderControlState } from "#src/webgl/shader_ui_controls.js";
8782
import {
8883
addControlsToBuilder,
@@ -291,6 +286,14 @@ export class MeshShaderManager {
291286
gl.uniform1ui(shader.uniform("uPickID"), pickID);
292287
}
293288

289+
setID(gl: GL, shader: ShaderProgram, id: bigint) {
290+
gl.uniform2ui(
291+
shader.uniform(`uID`),
292+
Number(id & 0xffffffffn),
293+
Number(id >> 32n),
294+
);
295+
}
296+
294297
beginModel(
295298
gl: GL,
296299
shader: ShaderProgram,
@@ -366,7 +369,7 @@ export class MeshShaderManager {
366369
gl.disableVertexAttribArray(shader.attribute("aVertexNormal"));
367370
}
368371

369-
makeGetter(layer: RefCounted & { gl: GL; displayState: MeshDisplayState, segmentationColorUserShader: SegmentationColorUserShader }) {
372+
makeGetter(layer: RefCounted & { gl: GL; displayState: MeshDisplayState }) {
370373
const parameters = layer.registerDisposer(
371374
new AggregateWatchableValue((refCounted) => ({
372375
shaderBuilderState:
@@ -395,13 +398,15 @@ export class MeshShaderManager {
395398
) => {
396399
addControlsToBuilder(shaderBuilderState, builder);
397400
this.vertexPositionHandler.defineShader(builder);
401+
layer.displayState.segmentationColorUserShader.defineShader(builder, /*fragment=*/ false);
398402
builder.addAttribute("highp vec2", "aVertexNormal");
399403
builder.addVarying("highp vec4", "vColor");
400404
builder.addUniform("highp vec4", "uLightDirection");
401405
builder.addUniform("highp vec4", "uColor");
402406
builder.addUniform("highp mat3", "uNormalMatrix");
403407
builder.addUniform("highp mat4", "uModelViewProjection");
404408
builder.addUniform("highp uint", "uPickID");
409+
builder.addUniform("highp uvec2", "uID");
405410

406411
if (silhouetteRenderingEnabled) {
407412
builder.addUniform("highp float", "uSilhouettePower");
@@ -411,6 +416,7 @@ export class MeshShaderManager {
411416
builder.addUniform("highp vec3", "uFragmentShape");
412417
}
413418
builder.addVertexCode(glsl_decodeNormalOctahedronSnorm8);
419+
builder.addVertexCode(glsl_uint64);
414420
let vertexMain = "";
415421
if (this.fragmentRelativeVertices) {
416422
vertexMain += `
@@ -423,18 +429,14 @@ highp vec3 vertexPosition = getVertexPosition();
423429
highp vec3 normalMultiplier = vec3(1.0, 1.0, 1.0);
424430
`;
425431
}
426-
427-
layer.segmentationColorUserShader.defineShader(builder);
428-
429-
430432
vertexMain += `
431433
gl_Position = uModelViewProjection * vec4(vertexPosition, 1.0);
432434
vec3 origNormal = decodeNormalOctahedronSnorm8(aVertexNormal);
433435
vec3 normal = normalize(uNormalMatrix * (normalMultiplier * origNormal));
434436
float absCosAngle = abs(dot(normal, uLightDirection.xyz));
435437
float lightingFactor = absCosAngle + uLightDirection.w;
436438
vColor = uColor;
437-
loadSegmentProperties();
439+
loadSegmentProperties(uint64_t(uID));
438440
vColor = segmentColor(vColor);
439441
vColor = vec4(lightingFactor * vColor.rgb, vColor.a);
440442
`;
@@ -461,7 +463,6 @@ export interface MeshDisplayState extends SegmentationDisplayState3D {
461463
export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRenderLayerAttachmentState> {
462464
protected meshShaderManager;
463465
private getShader;
464-
public segmentationColorUserShader;
465466
backend: SegmentationLayerSharedObject;
466467

467468
constructor(
@@ -474,7 +475,6 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
474475
/*fragmentRelativeVertices=*/ false,
475476
VertexPositionFormat.float32,
476477
);
477-
this.segmentationColorUserShader = new SegmentationColorUserShader(displayState);
478478
this.getShader = this.meshShaderManager.makeGetter(this);
479479

480480
registerRedrawWhenSegmentationDisplayState3DChanged(displayState, this);
@@ -487,7 +487,7 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
487487
),
488488
);
489489
this.registerDisposer(
490-
displayState.segmentColorShaderControlState.parseResult.changed.add(
490+
displayState.segmentationColorUserShader.changed.add(
491491
this.redrawNeeded.dispatch,
492492
),
493493
);
@@ -585,6 +585,7 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
585585
if (renderContext.emitPickID) {
586586
meshShaderManager.setPickID(gl, shader, pickIndex!);
587587
}
588+
meshShaderManager.setID(gl, shader, objectId);
588589
totalChunks += manifestChunk.fragmentIds.length;
589590

590591
for (const fragmentId of manifestChunk.fragmentIds) {
@@ -820,7 +821,6 @@ function hasFragmentChunk(
820821
export class MultiscaleMeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRenderLayerAttachmentState> {
821822
protected meshShaderManager: MeshShaderManager;
822823
private getShader;
823-
public segmentationColorUserShader;
824824
backend: SegmentationLayerSharedObject;
825825

826826
constructor(
@@ -834,9 +834,6 @@ export class MultiscaleMeshLayer extends PerspectiveViewRenderLayer<ThreeDimensi
834834
source.format.vertexPositionFormat,
835835
);
836836

837-
this.segmentationColorUserShader = new SegmentationColorUserShader(displayState);
838-
839-
840837
this.getShader = this.meshShaderManager.makeGetter(this);
841838

842839
registerRedrawWhenSegmentationDisplayState3DChanged(displayState, this);
@@ -970,36 +967,9 @@ export class MultiscaleMeshLayer extends PerspectiveViewRenderLayer<ThreeDimensi
970967
if (renderContext.emitPickID) {
971968
meshShaderManager.setPickID(gl, shader, pickIndex!);
972969
}
970+
meshShaderManager.setID(gl, shader, objectId);
973971

974-
this.segmentationColorUserShader.setSegmentPropertyUniforms(objectId, gl, shader);
975-
// const {
976-
// segmentPropertyMap: { value: segmentPropertyMap },
977-
// } = displayState.segmentationGroupState.value;
978-
// if (segmentPropertyMap) {
979-
// const { code } =
980-
// displayState.segmentColorShaderControlState.parseResult.value;
981-
// const index = segmentPropertyMap.getSegmentInlineIndex(objectId);
982-
983-
// // setSegmentPropertyUniforms()
984-
985-
// if (index !== -1) {
986-
// for (const [i, property] of extractUsedSegmentProperties(
987-
// segmentPropertyMap,
988-
// code,
989-
// )) {
990-
// const value = property.values[index];
991-
// const uniformSetter = getShaderUniformValueSetter(
992-
// gl,
993-
// property.dataType,
994-
// );
995-
// uniformSetter.call(
996-
// gl,
997-
// shader.uniform(`uSegmentNumericalProperty${i}`),
998-
// value,
999-
// );
1000-
// }
1001-
// }
1002-
// }
972+
displayState.segmentationColorUserShader.enable(gl, shader);
1003973

1004974
getMultiscaleChunksToDraw(
1005975
manifest,

0 commit comments

Comments
 (0)