Skip to content

Commit

Permalink
auto mipmap generation
Browse files Browse the repository at this point in the history
  • Loading branch information
znah committed Sep 20, 2023
1 parent 8ff4347 commit 59baea9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
12 changes: 5 additions & 7 deletions demo/DotCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ class DotCamera {

frame(glsl, {time, canvasSize}) {
let tex;
if (this.video.videoWidth == 0) {
tex = glsl({time,
FP:`step(0.0, sin(length(XY)*20.0-time*3.0+atan(XY.x,XY.y)*3.))*0.25`},
{size:[512, 512], tag:'tmp'});
if (this.video.videoWidth) {
tex = glsl({}, {data:this.video, tag:'video'});
} else {
tex = glsl({},
{size:[this.video.videoWidth, this.video.videoHeight], data:this.video, tag:'video'});
tex = glsl({time, FP:`step(0.0, sin(length(XY)*20.0-time*3.0+atan(XY.x,XY.y)*3.))*0.25`},
{size:[512, 512], tag:'tmp'});
}
const lum = glsl({tex:tex.edge.linear, MipGen:1,
const lum = glsl({tex:tex.edge.linear,
VP:`vec2 r = vec2(ViewSize)/vec2(tex_size()); r /= max(r.x, r.y); VPos.xy = XY/r;`,
FP:`dot(tex(1.0-UV).rgb, vec3(0.21,0.72,0.07))`}, {scale:1/2, format:'r8', tag:'lum'});
const merged = glsl({T:lum.edge.miplinear, FP:`
Expand Down
4 changes: 2 additions & 2 deletions demo/Physarum.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Physarum {
const par = (s, v, ...arg)=>{U[s]||=v;controllers.push(gui.add(U,s,...arg))};
const presets = {
worms: {senseFlip: false, senseAng:15, senseDist:10,moveAng:7,moveDist:1},
mesh: {senseFlip: false, senseAng:42, senseDist:19,moveAng:7,moveDist:2},
mesh:{senseFlip:false, senseAng:45, senseDist:15, moveAng:17,moveDist:1},
flocks: {senseFlip: true, senseAng:40, senseDist:36,moveAng:7,moveDist:1},
swirl:{senseFlip: true, senseAng:1.28, senseDist: 19, moveAng: 7, moveDist: 2},
swirl:{senseFlip: true, senseAng:1.28, senseDist: 19, moveAng: 7, moveDist: 2},
};
this.preset = 'worms';
gui.add(this, 'preset', Object.keys(presets)).onChange(name=>{
Expand Down
6 changes: 3 additions & 3 deletions demo/Shadowmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ class Shadowmap {
const target = shadowPass ?
glsl({}, {size:[1024, 1024], format:'depth', tag:'shadowmap'}) : null;
params = {...params, shadowPass, DepthTest:1};
// sphere
glsl({...params, Grid:[3], Mesh:[32,32], Clear:[.5,.5,.8,1], VP:`
Normal = uv2sphere(UV);
emitVertex(Normal*0.3-vec3(0,0,0.3));`, FP:`
emitFragment(vec3(0.8, 0.2, 0.2));`}, target);

// spirals
glsl({...params, Mesh:[10, 256], VP:`
vec3 surf(vec2 uv) {
float s = uv.y*TAU*8.0;
Expand All @@ -68,15 +69,14 @@ class Shadowmap {
void vertex() {
emitVertex(SURF(surf, UV, Normal, 1e-3));
}`, FP:`emitFragment(vec3(0.3, 0.7, 0.2));`}, target);

// snow
glsl({...params, Grid:[16, 16, 16], VP:`
PointSize = 0.005;
Normal = vec3(0,0,1);
vec3 p = fract(hash(ID)-time*vec3(0.01,0.01,0.1));
emitVertex((p-0.5)*2.0);`, FP:`
if (length(XY)>1.0) discard;
emitFragment(vec3(0.9, 0.9, 0.8));`}, target);

// floor
glsl({...params, Face:'front', VP:`
Normal = vec3(0,0,1);
Expand Down
42 changes: 21 additions & 21 deletions swissgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ function compileProgram(gl, vs, fs) {
gl.uniform1i(loc, unit);
program.setters[name] = tex=>{
gl.activeTexture(gl.TEXTURE0+unit);
gl.bindTexture(target, tex?tex.handle:null);
if (tex) gl.bindSampler(unit, tex._sampler);
tex ? tex.bindSampler(unit) : gl.bindTexture(target, null);
}
} else {
const fname = Type2Setter[info.type];
Expand Down Expand Up @@ -377,8 +376,8 @@ function linkShader(gl, uniforms, Inc, VP, FP) {

class TextureSampler {
fork(updates) {
const {gl, handle, layern, filter, wrap} = {...this, ...updates};
return updateObject(new TextureSampler(), {gl, handle, layern, filter, wrap});
const {gl, handle, gltarget, layern, filter, wrap} = {...this, ...updates};
return updateObject(new TextureSampler(), {gl, handle, gltarget, layern, filter, wrap});
}
get linear() {return this.fork({filter:'linear'})}
get nearest() {return this.fork({filter:'nearest'})}
Expand Down Expand Up @@ -406,6 +405,16 @@ class TextureSampler {
}
return gl._samplers[id];
}
bindSampler(unit) {
// assume unit is already active
const {gl, gltarget, handle} = this;
gl.bindTexture(gltarget, handle);
if (this.filter == 'miplinear' && !handle.hasMipmap) {
gl.generateMipmap(gltarget)
handle.hasMipmap = true;
}
gl.bindSampler(unit, this._sampler);
}
}

class TextureTarget extends TextureSampler {
Expand Down Expand Up @@ -444,12 +453,6 @@ class TextureTarget extends TextureSampler {
this.size = size;
if (this.depth) {this.depth.update(size, data);}
}
genMipmaps() {
const {gl, handle, gltarget} = this;
gl.bindTexture(gltarget, handle);
gl.generateMipmap(gltarget);
gl.bindTexture(gltarget, null);
}
attach(gl) {
if (!this.layern) {
const attachment = this.format == 'depth' ? gl.DEPTH_ATTACHMENT : gl.COLOR_ATTACHMENT0;
Expand All @@ -466,7 +469,7 @@ class TextureTarget extends TextureSampler {
gl.drawBuffers(drawBuffers);
}
}
bind(gl) {
bind(gl, readonly=false) {
if (this.fbo) {
gl.bindFramebuffer(gl.FRAMEBUFFER, this.fbo);
} else {
Expand All @@ -475,6 +478,7 @@ class TextureTarget extends TextureSampler {
this.attach(gl)
if (this.depth) this.depth.attach(gl);
}
if (!readonly) {this.handle.hasMipmap = false;}
return this.size;
}
_getBox(box) {
Expand All @@ -490,7 +494,7 @@ class TextureTarget extends TextureSampler {
}
_readPixels(box, targetBuf) {
const {glformat, type} = this.formatInfo;
this.bind(this.gl);
this.bind(this.gl, /*readonly*/true);
this.gl.readPixels(...box, glformat, type, targetBuf);
}
readSync(...optBox) {
Expand Down Expand Up @@ -618,7 +622,10 @@ function isReadyTarget(target) {
return !target /*cavnas*/ || Array.isArray(target) || !!target.bind;
}

function getTargetSize(gl, {size, scale=1}) {
function getTargetSize(gl, {size, scale=1, data}) {
if (!size && (data && data.videoWidth && data.videoHeight)) {
size = [data.videoWidth, data.videoHeight];
}
size = size || [gl.canvas.width, gl.canvas.height];
return [Math.ceil(size[0]*scale), Math.ceil(size[1]*scale)];
}
Expand Down Expand Up @@ -661,18 +668,11 @@ function bindTarget(gl, target) {
}

const OptNames = new Set([
'Inc', 'VP', 'FP', 'MipGen',
'Inc', 'VP', 'FP',
'Clear', 'Blend', 'View', 'Grid', 'Mesh', 'Aspect', 'DepthTest', 'AlphaCoverage', 'Face'
]);

function drawQuads(self, params, target) {
if (params.MipGen && target) {
const p = {...params};
delete p.MipGen;
const r = drawQuads(self, p, target);
(Array.isArray(r)?r[0]:r).genMipmaps()
return r;
}
const options={}, uniforms={}
for (const p in params) {
(OptNames.has(p)?options:uniforms)[p] = params[p];
Expand Down

0 comments on commit 59baea9

Please sign in to comment.