Skip to content

Commit 9879495

Browse files
committed
cleanup
1 parent 979bc01 commit 9879495

File tree

3 files changed

+126
-148
lines changed

3 files changed

+126
-148
lines changed

src/mesh/frontend.ts

Lines changed: 9 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ import {
5757
registerRedrawWhenSegmentationDisplayState3DChanged,
5858
SegmentationLayerSharedObject,
5959
} from "#src/segmentation_display_state/frontend.js";
60-
import { PreprocessedSegmentPropertyMap } from "#src/segmentation_display_state/property_map.js";
60+
import { addSegmentPropertiesToShaderBuilder, setSegmentPropertyUniforms, shaderCodeWithPropertyPreprocessing } from "#src/segmentation_display_state/property_map.js";
6161
import type { WatchableValueInterface } from "#src/trackable_value.js";
6262
import { makeCachedDerivedWatchableValue } from "#src/trackable_value.js";
63-
import { DataType } from "#src/util/data_type.js";
6463
import type { Borrowed, RefCounted } from "#src/util/disposable.js";
6564
import { vec4 } from "#src/util/geom.js";
6665
import {
@@ -391,12 +390,11 @@ export class MeshShaderManager {
391390
makeGetter(layer: RefCounted & { gl: GL; displayState: MeshDisplayState }) {
392391
const parameters = layer.registerDisposer(
393392
makeCachedDerivedWatchableValue(
394-
(a, b, c) => {
395-
console.log("update CDWV");
393+
(silhouetteRendering, shaderBuilderState, segmentProperties) => {
396394
return {
397-
silhouetteRenderingEnabled: a > 0,
398-
shaderBuilderState: b,
399-
segmentProperties: c,
395+
silhouetteRenderingEnabled: silhouetteRendering > 0,
396+
shaderBuilderState,
397+
segmentProperties,
400398
};
401399
},
402400
[
@@ -407,33 +405,6 @@ export class MeshShaderManager {
407405
),
408406
);
409407

410-
parameters.changed.add(() => {
411-
console.log("parameters changed");
412-
});
413-
414-
layer.displayState.segmentationGroupState.value.segmentPropertyMap.changed.add(
415-
() => {
416-
console.log("segment property map changed");
417-
},
418-
);
419-
420-
function getShaderOutputType(ioType: DataType): string {
421-
switch (ioType) {
422-
case DataType.UINT8:
423-
case DataType.UINT16:
424-
case DataType.UINT32:
425-
return "uint";
426-
case DataType.INT8:
427-
case DataType.INT16:
428-
case DataType.INT32:
429-
return "int";
430-
case DataType.FLOAT32:
431-
return "float";
432-
case DataType.UINT64:
433-
return "uvec2";
434-
}
435-
}
436-
437408
return parameterizedEmitterDependentShaderGetter(layer, layer.gl, {
438409
memoizeKey: `mesh/MeshShaderManager/${this.fragmentRelativeVertices}/${this.vertexPositionFormat}`,
439410
parameters,
@@ -457,44 +428,8 @@ export class MeshShaderManager {
457428
builder.addUniform("highp uint", "uPickID");
458429
builder.addUniform("highp uvec2", "uID");
459430

460-
// TODO define segment property uniforms here
461-
// console.log("segmentProperties", segmentProperties);
462-
463-
const shaderCodeWithPropertyPreprocessing = (code: string) => {
464-
if (!segmentProperties) return code;
465-
const { numericalProperties, tags } = segmentProperties;
466-
numericalProperties;
467-
if (tags) {
468-
for (const [i, tag] of tags.tags.entries()) {
469-
code = code.replaceAll(`tag("${tag}")`, `uTag${i} == 1u`);
470-
}
471-
}
472-
return code;
473-
};
474-
475431
if (segmentProperties) {
476-
const { numericalProperties, tags } = segmentProperties;
477-
for (const property of numericalProperties) {
478-
console.log("numeric", property);
479-
// TODO maybe just number it instead of using id
480-
builder.addUniform(
481-
`highp ${getShaderOutputType(property.dataType)}`,
482-
property.id.replaceAll(" ", "_"),
483-
);
484-
console.log("added numeric uniform", property.id);
485-
}
486-
if (tags) {
487-
console.log("tags", tags);
488-
489-
for (const [i, tag] of tags.tags.entries()) {
490-
console.log("tag", tag, tag.replaceAll(" ", "_"));
491-
builder.addUniform("highp uint", `uTag${i}`);
492-
builder.addVertexCode(
493-
`\n#define TAG_${tag.replaceAll(" ", "_").replaceAll("-", "_").toUpperCase()} uTag${i}\n`,
494-
);
495-
// gl.uniform1ui(shader.uniform(`tag${tagIndices.charCodeAt(i)}`), 1);
496-
}
497-
}
432+
addSegmentPropertiesToShaderBuilder(segmentProperties, builder);
498433
}
499434

500435
if (silhouetteRenderingEnabled) {
@@ -551,88 +486,14 @@ vColor *= pow(1.0 - absCosAngle, uSilhouettePower);
551486

552487
builder.addVertexCode(
553488
"\n#define main userMain\n" +
554-
shaderCodeWithLineDirective(shaderCodeWithPropertyPreprocessing(shaderBuilderState.parseResult.code)) +
489+
shaderCodeWithLineDirective(shaderCodeWithPropertyPreprocessing(segmentProperties, shaderBuilderState.parseResult.code)) +
555490
"\n#undef main\n",
556491
);
557492
},
558493
});
559494
}
560495
}
561496

562-
const addPropertyUniforms = (
563-
gl: GL,
564-
shader: ShaderProgram,
565-
segmentPropertyMap: PreprocessedSegmentPropertyMap,
566-
id: bigint,
567-
) => {
568-
const index = segmentPropertyMap.getSegmentInlineIndex(id);
569-
// const {tags} = segmentPropertyMap;
570-
// console.log("index", index, objectId, key);
571-
// console.log(
572-
// "segmentPropertyMap.numerical",
573-
// segmentPropertyMap.numericalProperties,
574-
// );
575-
// console.log("segmentPropertyMap.numerical", segmentPropertyMap.tags);
576-
577-
const getShaderUniformValueSetter = (gl: GL, ioType: DataType) => {
578-
switch (ioType) {
579-
case DataType.UINT8:
580-
case DataType.UINT16:
581-
case DataType.UINT32:
582-
return gl.uniform1ui;
583-
case DataType.INT8:
584-
case DataType.INT16:
585-
case DataType.INT32:
586-
return gl.uniform1i;
587-
case DataType.FLOAT32:
588-
return gl.uniform1f;
589-
case DataType.UINT64:
590-
throw new Error("nope");
591-
// return gl.uniform
592-
};
593-
};
594-
595-
if (index !== -1) {
596-
const { labels, tags: tagsProperty, numericalProperties } = segmentPropertyMap;
597-
labels;
598-
599-
for (const property of numericalProperties) {
600-
const value = property.values[index];
601-
const uniformSetter = getShaderUniformValueSetter(gl, property.dataType);
602-
uniformSetter.call(gl, shader.uniform(property.id.replaceAll(" ", "_")), value);
603-
}
604-
605-
// let label = "";
606-
// if (labels !== undefined) {
607-
// label = labels.values[index];
608-
// }
609-
if (tagsProperty !== undefined) {
610-
const { values, tags } = tagsProperty;
611-
const tagIndices = values[index];
612-
for (let i = 0; i < tags.length; ++i) {
613-
gl.uniform1ui(shader.uniform(`uTag${i}`), 0);
614-
}
615-
for (let i = 0, length = tagIndices.length; i < length; ++i) {
616-
const tagIdx = tagIndices.charCodeAt(i);
617-
console.log("enable tag", tagIdx);
618-
gl.uniform1ui(shader.uniform(`uTag${tagIdx}`), 1);
619-
}
620-
}
621-
// if (label.length === 0) return undefined;
622-
623-
// if (tags) {
624-
// console.log('tags', tags);
625-
// // for (const tag of tags) {
626-
// }
627-
getShaderUniformValueSetter;
628-
// for (const [name, channel] of Object.entries(value)) {
629-
// const setter = getShaderUniformValueSetter(gl, channel.dataType);
630-
// if (setter) {
631-
// setter.call(gl, shader.uniforms[name], channel.value);
632-
// }
633-
// }
634-
}
635-
};
636497
export interface MeshDisplayState extends SegmentationDisplayState3D {
637498
silhouetteRendering: WatchableValueInterface<number>;
638499

@@ -783,7 +644,7 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
783644
} = displayState.segmentationGroupState.value;
784645
if (segmentPropertyMap) {
785646
console.log('key/objectId', key, objectId);
786-
addPropertyUniforms(gl, shader, segmentPropertyMap, objectId);
647+
setSegmentPropertyUniforms(gl, shader, segmentPropertyMap, objectId);
787648
}
788649

789650
for (const fragmentId of manifestChunk.fragmentIds) {
@@ -1174,7 +1035,7 @@ export class MultiscaleMeshLayer extends PerspectiveViewRenderLayer<ThreeDimensi
11741035
} = displayState.segmentationGroupState.value;
11751036
if (segmentPropertyMap) {
11761037
console.log('key/objectId', key, objectId);
1177-
addPropertyUniforms(gl, shader, segmentPropertyMap, objectId);
1038+
setSegmentPropertyUniforms(gl, shader, segmentPropertyMap, objectId);
11781039
}
11791040

11801041
getMultiscaleChunksToDraw(

src/segmentation_display_state/property_map.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import {
4040
} from "#src/util/lerp.js";
4141
import { getObjectId } from "#src/util/object_id.js";
4242
import { defaultStringCompare } from "#src/util/string.js";
43+
import { GL } from "#src/webgl/context.js";
44+
import { ShaderBuilder, ShaderProgram } from "#src/webgl/shader.js";
4345

4446
export type InlineSegmentProperty =
4547
| InlineSegmentStringProperty
@@ -1338,3 +1340,116 @@ export function queryIncludesColumn(
13381340
includeColumns.includes(fieldId)
13391341
);
13401342
}
1343+
1344+
function getShaderOutputType(ioType: DataType): string {
1345+
switch (ioType) {
1346+
case DataType.UINT8:
1347+
case DataType.UINT16:
1348+
case DataType.UINT32:
1349+
return "uint";
1350+
case DataType.INT8:
1351+
case DataType.INT16:
1352+
case DataType.INT32:
1353+
return "int";
1354+
case DataType.FLOAT32:
1355+
return "float";
1356+
case DataType.UINT64:
1357+
return "uvec2";
1358+
}
1359+
}
1360+
1361+
const getShaderUniformValueSetter = (gl: GL, ioType: DataType) => {
1362+
switch (ioType) {
1363+
case DataType.UINT8:
1364+
case DataType.UINT16:
1365+
case DataType.UINT32:
1366+
return gl.uniform1ui;
1367+
case DataType.INT8:
1368+
case DataType.INT16:
1369+
case DataType.INT32:
1370+
return gl.uniform1i;
1371+
case DataType.FLOAT32:
1372+
return gl.uniform1f;
1373+
case DataType.UINT64:
1374+
throw new Error("nope");
1375+
// return gl.uniform
1376+
}
1377+
};
1378+
1379+
export function shaderCodeWithPropertyPreprocessing(
1380+
segmentProperties: PreprocessedSegmentPropertyMap | undefined,
1381+
code: string,
1382+
) {
1383+
if (!segmentProperties) return code;
1384+
const { numericalProperties, tags } = segmentProperties;
1385+
numericalProperties;
1386+
if (tags) {
1387+
for (const [i, tag] of tags.tags.entries()) {
1388+
code = code.replaceAll(`tag("${tag}")`, `uSegmentTagProperty${i} == 1u`);
1389+
}
1390+
}
1391+
for (const [i, property] of numericalProperties.entries()) {
1392+
code = code.replaceAll(`prop("${property.id}")`, `uSegmentNumericalProperty${i}`);
1393+
}
1394+
return code;
1395+
}
1396+
1397+
export function addSegmentPropertiesToShaderBuilder(
1398+
segmentProperties: PreprocessedSegmentPropertyMap,
1399+
builder: ShaderBuilder,
1400+
) {
1401+
if (segmentProperties) {
1402+
const { numericalProperties, tags } = segmentProperties;
1403+
for (const [i, property] of numericalProperties.entries()) {
1404+
builder.addUniform(
1405+
`highp ${getShaderOutputType(property.dataType)}`,
1406+
`uSegmentNumericalProperty${i}`,
1407+
);
1408+
}
1409+
if (tags) {
1410+
for (const [i, tag] of tags.tags.entries()) {
1411+
tag;
1412+
builder.addUniform("highp uint", `uSegmentTagProperty${i}`);
1413+
// builder.addVertexCode(
1414+
// `\n#define TAG_${tag.replaceAll(" ", "_").replaceAll("-", "_").toUpperCase()} uSegmentTagProperty${i}\n`,
1415+
// );
1416+
}
1417+
}
1418+
}
1419+
}
1420+
1421+
export const setSegmentPropertyUniforms = (
1422+
gl: GL,
1423+
shader: ShaderProgram,
1424+
segmentPropertyMap: PreprocessedSegmentPropertyMap,
1425+
id: bigint,
1426+
) => {
1427+
const index = segmentPropertyMap.getSegmentInlineIndex(id);
1428+
1429+
if (index !== -1) {
1430+
const {
1431+
labels,
1432+
tags: tagsProperty,
1433+
numericalProperties,
1434+
} = segmentPropertyMap;
1435+
labels; // TODO, support labels
1436+
1437+
for (const [i, property] of numericalProperties.entries()) {
1438+
const value = property.values[index];
1439+
const uniformSetter = getShaderUniformValueSetter(gl, property.dataType);
1440+
uniformSetter.call(gl, shader.uniform(`uSegmentNumericalProperty${i}`), value);
1441+
}
1442+
1443+
if (tagsProperty !== undefined) {
1444+
const { values, tags } = tagsProperty;
1445+
const tagIndices = values[index];
1446+
for (let i = 0; i < tags.length; ++i) {
1447+
gl.uniform1ui(shader.uniform(`uSegmentTagProperty${i}`), 0);
1448+
}
1449+
for (let i = 0, length = tagIndices.length; i < length; ++i) {
1450+
const tagIdx = tagIndices.charCodeAt(i);
1451+
gl.uniform1ui(shader.uniform(`uSegmentTagProperty${tagIdx}`), 1);
1452+
}
1453+
}
1454+
}
1455+
};

src/sliceview/volume/segmentation_renderlayer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export class SegmentationRenderLayer extends SliceViewVolumeRenderLayer<ShaderPa
222222
}
223223

224224
defineShader(builder: ShaderBuilder, parameters: ShaderParameters) {
225+
console.log('seg defineShader', parameters);
225226
this.hashTableManager.defineShader(builder);
226227
let getUint64Code = `
227228
uint64_t getUint64DataValue() {
@@ -338,6 +339,7 @@ uint64_t getMappedObjectId(uint64_t value) {
338339
shader: ShaderProgram,
339340
parameters: ShaderParameters,
340341
) {
342+
console.log("initializing segmentation shader", parameters);
341343
const { gl } = this;
342344
const { displayState, segmentationGroupState } = this;
343345
const { segmentSelectionState } = this.displayState;

0 commit comments

Comments
 (0)