Skip to content

Commit

Permalink
feat(webgl): add renderExt to TextureFormatDecl, add FBO tex fmt checks
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 20, 2019
1 parent 2aa31ce commit 180e89c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 11 additions & 0 deletions packages/webgl/src/api/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export interface TextureFormatDecl {
* Format is renderable
*/
render?: boolean;
/**
* Format is renderable via extension
*/
renderExt?: boolean;
/**
* Format is filterable (other than GL_NEAREST)
*/
Expand Down Expand Up @@ -223,6 +227,7 @@ export const TEX_FORMATS: IObjectOf<TextureFormatDecl> = {
[TextureFormat.R11F_G11F_B10F]: {
format: TextureFormat.RGB,
filter: true,
renderExt: true,
num: 3,
types: [
TextureType.FLOAT,
Expand All @@ -236,6 +241,7 @@ export const TEX_FORMATS: IObjectOf<TextureFormatDecl> = {
[TextureFormat.R16F]: {
format: TextureFormat.RED,
filter: true,
renderExt: true,
num: 1,
types: [TextureType.FLOAT, 4, TextureType.HALF_FLOAT, 2]
},
Expand All @@ -253,6 +259,7 @@ export const TEX_FORMATS: IObjectOf<TextureFormatDecl> = {
},
[TextureFormat.R32F]: {
format: TextureFormat.RED,
renderExt: true,
num: 1,
types: [TextureType.FLOAT, 4]
},
Expand Down Expand Up @@ -296,6 +303,7 @@ export const TEX_FORMATS: IObjectOf<TextureFormatDecl> = {
[TextureFormat.RG16F]: {
format: TextureFormat.RG,
filter: true,
renderExt: true,
num: 2,
types: [TextureType.FLOAT, 8, TextureType.HALF_FLOAT, 4]
},
Expand All @@ -313,6 +321,7 @@ export const TEX_FORMATS: IObjectOf<TextureFormatDecl> = {
},
[TextureFormat.RG32F]: {
format: TextureFormat.RG,
renderExt: true,
num: 2,
types: [TextureType.FLOAT, 8]
},
Expand Down Expand Up @@ -500,6 +509,7 @@ export const TEX_FORMATS: IObjectOf<TextureFormatDecl> = {
[TextureFormat.RGBA16F]: {
format: TextureFormat.RGBA,
filter: true,
renderExt: true,
num: 4,
types: [TextureType.FLOAT, 16, TextureType.HALF_FLOAT, 8]
},
Expand All @@ -517,6 +527,7 @@ export const TEX_FORMATS: IObjectOf<TextureFormatDecl> = {
},
[TextureFormat.RGBA32F]: {
format: TextureFormat.RGBA,
renderExt: true,
num: 4,
types: [TextureType.FLOAT, 16]
},
Expand Down
21 changes: 15 additions & 6 deletions packages/webgl/src/fbo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "@thi.ng/api";
import { FboOpts, IFbo } from "./api/buffers";
import { ITexture } from "./api/texture";
import { ITexture, TEX_FORMATS } from "./api/texture";
import { isGL2Context } from "./checks";
import { error } from "./error";
import { RBO } from "./rbo";
Expand Down Expand Up @@ -32,10 +32,11 @@ export class FBO implements IFbo {
constructor(gl: WebGLRenderingContext, opts?: Partial<FboOpts>) {
this.gl = gl;
this.fbo = gl.createFramebuffer() || error("error creating FBO");
this.ext = (!isGL2Context(gl) && opts && opts!.tex && opts!.tex!.length > 1)
? gl.getExtension("WEBGL_draw_buffers") ||
error("missing WEBGL_draw_buffers ext")
: undefined;
this.ext =
!isGL2Context(gl) && opts && opts!.tex && opts!.tex!.length > 1
? gl.getExtension("WEBGL_draw_buffers") ||
error("missing WEBGL_draw_buffers ext")
: undefined;
this.maxAttachments = gl.getParameter(GL_MAX_COLOR_ATTACHMENTS_WEBGL);
opts && this.configure(opts);
}
Expand Down Expand Up @@ -67,12 +68,20 @@ export class FBO implements IFbo {
);
const attachments: number[] = [];
for (let i = 0; i < opts.tex.length; i++) {
const tex = opts.tex[i];
assert(
!!(
TEX_FORMATS[tex.format].render ||
TEX_FORMATS[tex.format].renderExt
),
`texture #${i} has non-renderable format`
);
const attach = GL_COLOR_ATTACHMENT0_WEBGL + i;
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
attach,
gl.TEXTURE_2D,
opts.tex[i].tex,
tex.tex,
0
);
attachments[i] = attach;
Expand Down

0 comments on commit 180e89c

Please sign in to comment.