forked from google/neuroglancer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shader.ts
179 lines (163 loc) · 5.82 KB
/
shader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @license
* Copyright 2016 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {HashTableBase, NUM_ALTERNATIVES} from 'neuroglancer/gpu_hash/hash_table';
import {DataType} from 'neuroglancer/util/data_type';
import {RefCounted} from 'neuroglancer/util/disposable';
import {GL} from 'neuroglancer/webgl/context';
import {ShaderBuilder, ShaderProgram} from 'neuroglancer/webgl/shader';
import {glsl_equalUint64, glsl_uint64} from 'neuroglancer/webgl/shader_lib';
import {computeTextureFormat, OneDimensionalTextureAccessHelper, setOneDimensionalTextureData, TextureFormat} from 'neuroglancer/webgl/texture_access';
// MumurHash, excluding the final mixing steps.
export const glsl_hashCombine = [
glsl_uint64, `
highp uint hashCombine(highp uint state, highp uint value) {
value *= 0xcc9e2d51u;
value = (value << 15u) | (value >> 17u);
value *= 0x1b873593u;
state ^= value;
state = (state << 13u) | (state >> 19u);
state = (state * 5u) + 0xe6546b64u;
return state;
}
highp uint hashCombine(highp uint state, uint64_t x) {
state = hashCombine(state, x.value[0]);
return hashCombine(state, x.value[1]);
}
`
];
const textureFormat = computeTextureFormat(new TextureFormat(), DataType.UINT64, 1);
export class GPUHashTable<HashTable extends HashTableBase> extends RefCounted {
generation = -1;
texture: WebGLTexture|null = null;
constructor(public gl: GL, public hashTable: HashTable) {
super();
// createTexture should never actually return null.
this.texture = gl.createTexture();
}
copyToGPU() {
let {hashTable} = this;
let {generation} = hashTable;
if (this.generation === generation) {
return;
}
const {gl, texture} = this;
this.generation = generation;
gl.activeTexture(WebGL2RenderingContext.TEXTURE0 + gl.tempTextureUnit);
gl.bindTexture(WebGL2RenderingContext.TEXTURE_2D, texture);
hashTable.tableWithMungedEmptyKey(table => {
setOneDimensionalTextureData(this.gl, textureFormat, table);
});
gl.bindTexture(WebGL2RenderingContext.TEXTURE_2D, null);
}
disposed() {
let {gl} = this;
gl.deleteTexture(this.texture);
this.texture = null;
this.gl = <any>undefined;
this.hashTable = <any>undefined;
super.disposed();
}
static get<HashTable extends HashTableBase>(gl: GL, hashTable: HashTable) {
return gl.memoize.get(hashTable, () => new this(gl, hashTable));
}
}
export class HashSetShaderManager {
textureUnitSymbol = Symbol.for(`gpuhashtable:${this.prefix}`);
private accessHelper = new OneDimensionalTextureAccessHelper(`gpuhashtable_${this.prefix}`);
samplerName = this.prefix + '_sampler';
hashSeedsName = this.prefix + '_seeds';
hashKeyMask = this.prefix + '_keyMask';
readTable = this.prefix + '_readTable';
constructor(public prefix: string, public numAlternatives = NUM_ALTERNATIVES) {}
defineShader(builder: ShaderBuilder) {
let {hashSeedsName, samplerName, numAlternatives, hashKeyMask} = this;
builder.addUniform('highp uint', hashSeedsName, numAlternatives);
builder.addUniform('highp uint', hashKeyMask);
builder.addTextureSampler('usampler2D', samplerName, this.textureUnitSymbol);
builder.addFragmentCode(glsl_hashCombine);
builder.addFragmentCode(glsl_uint64);
builder.addFragmentCode(glsl_equalUint64);
this.accessHelper.defineShader(builder);
builder.addFragmentCode(
this.accessHelper.getAccessor(this.readTable, this.samplerName, DataType.UINT64, 1));
let s = '';
s += `
bool ${this.hasFunctionName}(uint64_t x) {
`;
for (let alt = 0; alt < numAlternatives; ++alt) {
s += `
{
uint h = hashCombine(${hashSeedsName}[${alt}], x) & ${hashKeyMask};
uint64_t key = ${this.readTable}(h);
if (equals(key, x)) {
return true;
}
}
`;
}
s += `
return false;
}
`;
builder.addFragmentCode(s);
}
get hasFunctionName() {
return `${this.prefix}_has`;
}
enable<HashTable extends HashTableBase>(
gl: GL, shader: ShaderProgram, hashTable: GPUHashTable<HashTable>) {
hashTable.copyToGPU();
const textureUnit = shader.textureUnit(this.textureUnitSymbol);
gl.activeTexture(WebGL2RenderingContext.TEXTURE0 + textureUnit);
gl.bindTexture(WebGL2RenderingContext.TEXTURE_2D, hashTable.texture);
gl.uniform1ui(shader.uniform(this.hashKeyMask), hashTable.hashTable.tableSize - 1);
gl.uniform1uiv(shader.uniform(this.hashSeedsName), hashTable.hashTable.hashSeeds);
}
disable(gl: GL, shader: ShaderProgram) {
const textureUnit = shader.textureUnit(this.textureUnitSymbol);
gl.activeTexture(WebGL2RenderingContext.TEXTURE0 + textureUnit);
gl.bindTexture(WebGL2RenderingContext.TEXTURE_2D, null);
}
}
export class HashMapShaderManager extends HashSetShaderManager {
defineShader(builder: ShaderBuilder) {
super.defineShader(builder);
let {numAlternatives, hashSeedsName, hashKeyMask} = this;
let s = `
bool ${this.getFunctionName}(uint64_t x, out uint64_t value) {
`;
for (let alt = 0; alt < numAlternatives; ++alt) {
s += `
{
uint h = hashCombine(${hashSeedsName}[${alt}], x) & ${hashKeyMask};
uint64_t key = ${this.readTable}(h * 2u);
if (equals(key, x)) {
value = ${this.readTable}(h * 2u + 1u);
return true;
}
}
`;
}
s += `
return false;
}
`;
builder.addFragmentCode(s);
}
get getFunctionName() {
return `${this.prefix}_get`;
}
}