Skip to content

Commit

Permalink
feat: sampler mipmapping (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett authored Oct 14, 2023
1 parent 7c50eae commit f2aea2c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/RenderTarget.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Sampler } from './Sampler'
import { Texture } from './Texture'
import { ARRAY_TYPE } from './_utils'

Expand All @@ -8,7 +9,11 @@ export class RenderTarget {
/**
* A {@link Texture} array to write color attachments to.
*/
readonly textures: Texture[] = []
readonly textures: Texture[]
/**
* A {@link Sampler} used for sampling texture attachments.
*/
public sampler: Sampler = new Sampler({ generateMipmaps: false })
/**
* Used internally to flag for update on resize. Default is `true`.
*/
Expand All @@ -28,7 +33,7 @@ export class RenderTarget {
*/
readonly count: number = 1,
) {
this.textures = ARRAY_TYPE.from({ length: count }, () => new Texture())
this.textures = ARRAY_TYPE.from({ length: count }, () => new Texture(undefined, this.sampler))
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export interface SamplerOptions {
* Flags this sampler for update. Default is `true`.
*/
needsUpdate: boolean
/**
* Whether to generate mipmaps for increased perceived quality (WebGL only). Default is `true`.
*
* **Note**: this is not implemented in WebGPU. See https://github.com/gpuweb/gpuweb/issues/386.
*/
generateMipmaps: boolean
}

/**
Expand All @@ -47,7 +53,8 @@ export class Sampler implements SamplerOptions {
public wrapS: Wrapping = 'clamp'
public wrapT: Wrapping = 'clamp'
public anisotropy: number = 1
public needsUpdate = true
public generateMipmaps: boolean = true
public needsUpdate: boolean = true

constructor(options?: Partial<SamplerOptions>) {
if (options) Object.assign(this, options)
Expand Down
17 changes: 15 additions & 2 deletions src/WebGLRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ const GL_FILTERS: Record<Filter, number> = {
linear: GL_LINEAR,
} as const

const GL_LINEAR_MIPMAP_NEAREST = 0x2701
const GL_NEAREST_MIPMAP_LINEAR = 0x2702

const GL_MIPMAP_FILTERS: Record<Filter, number> = {
nearest: GL_LINEAR_MIPMAP_NEAREST,
linear: GL_NEAREST_MIPMAP_LINEAR,
} as const

const GL_REPEAT = 0x2901
const GL_CLAMP_TO_EDGE = 0x812f
const GL_MIRRORED_REPEAT = 0x8370
Expand Down Expand Up @@ -266,8 +274,9 @@ export class WebGLRenderer {
this.gl.samplerParameteri(target, anisotropyExt.TEXTURE_MAX_ANISOTROPY_EXT, sampler.anisotropy)
}

const MIN_FILTERS = sampler.generateMipmaps ? GL_MIPMAP_FILTERS : GL_FILTERS
this.gl.samplerParameteri(target, GL_TEXTURE_MAG_FILTER, GL_FILTERS[sampler.magFilter])
this.gl.samplerParameteri(target, GL_TEXTURE_MIN_FILTER, GL_FILTERS[sampler.minFilter])
this.gl.samplerParameteri(target, GL_TEXTURE_MIN_FILTER, MIN_FILTERS[sampler.minFilter])

this.gl.samplerParameteri(target, GL_TEXTURE_WRAP_S, GL_WRAPPINGS[sampler.wrapS])
this.gl.samplerParameteri(target, GL_TEXTURE_WRAP_T, GL_WRAPPINGS[sampler.wrapT])
Expand Down Expand Up @@ -305,7 +314,11 @@ export class WebGLRenderer {
if (!(texture.image instanceof HTMLVideoElement)) texture.needsUpdate = false
}

this._updateSampler(texture.sampler)
if (texture.needsUpdate || texture.sampler.needsUpdate) {
if (texture.sampler.generateMipmaps) this.gl.generateMipmap(GL_TEXTURE_2D)

this._updateSampler(texture.sampler)
}

return target
}
Expand Down

0 comments on commit f2aea2c

Please sign in to comment.