Skip to content

Commit

Permalink
Generalizing LAYERS and supporting OpenGL ES 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jeapostrophe committed Dec 21, 2016
1 parent b5d6e09 commit ae1b9dd
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 59 deletions.
33 changes: 20 additions & 13 deletions mode-lambda/backend/gl.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
ffi/unsafe/cvector
(only-in ffi/vector
u32vector
list->s32vector)
list->s32vector
s32vector-ref)
mode-lambda/backend/gl/util
mode-lambda/backend/lib
mode-lambda/core
Expand All @@ -25,9 +26,9 @@
(define QUAD_VERTS 4)

(define LAYER-VALUES 12)
(define (layer-config->bytes layer-config)
(define (layer-config->bytes how-many-layers layer-config)
(define lc-bytes-per-value (ctype-sizeof _float))
(define lc-bs (make-bytes (* LAYER-VALUES LAYERS lc-bytes-per-value)))
(define lc-bs (make-bytes (* LAYER-VALUES how-many-layers lc-bytes-per-value)))
(for ([i (in-naturals)]
[lc (in-vector layer-config)])
(match-define
Expand Down Expand Up @@ -55,13 +56,18 @@
(define VERTEX_SPEC
(add-between VERTEX_SPEC_L ","))

(define (make-draw csd width.fx height.fx screen-mode)
(define (make-draw csd width.fx height.fx how-many-layers screen-mode)
(define width (fx->fl width.fx))
(define height (fx->fl height.fx))
(eprintf "You are using OpenGL ~v with gl-backend-version of ~v\n"
(gl-version)
(gl-backend-version))

(define max-draw-buffers (s32vector-ref (glGetIntegerv GL_MAX_DRAW_BUFFERS) 0))
(unless (<= how-many-layers max-draw-buffers)
(error 'mode-lambda "Too many layers for device: ~v vs ~v."
how-many-layers max-draw-buffers))

(define shot! (gl-screenshot!))

(match-define
Expand All @@ -77,8 +83,9 @@
(unless (equal? layer-config last-layer-config)
(set! last-layer-config layer-config)
(with-texture (GL_TEXTURE3 LayerConfigId)
(load-texture/float-bytes LAYER-VALUES LAYERS
(layer-config->bytes layer-config)))))))
(load-texture/float-bytes LAYER-VALUES how-many-layers
(layer-config->bytes how-many-layers
layer-config)))))))

(define render-layers!
(let ()
Expand Down Expand Up @@ -108,7 +115,7 @@
[(gl-es?)
(eprintf "GL: Skipping glBindFragDataLocation on OpenGL ES\n")]
[else
(for ([i (in-range LAYERS)])
(for ([i (in-range how-many-layers)])
(glBindFragDataLocation layer-program i (format "out_Color~a" i)))])

(define tmp-vao (glGen glGenVertexArrays))
Expand All @@ -127,7 +134,7 @@

(define layer-dfbos
(for/list ([i (in-range 2)])
(make-delayed-fbo LAYERS)))
(make-delayed-fbo how-many-layers)))

(define (make-sprite-draw!)
(define layer-vao (glGen glGenVertexArrays))
Expand Down Expand Up @@ -187,9 +194,9 @@
[with-feature (GL_BLEND)]
[with-program (layer-program)])
(set-uniform-scale-info! layer-program the-scale-info)
(glDrawBuffers LAYERS
(glDrawBuffers how-many-layers
(list->s32vector
(for/list ([i (in-range LAYERS)])
(for/list ([i (in-range how-many-layers)])
(GL_COLOR_ATTACHMENTi i))))
(glClearColor 0.0 0.0 0.0 0.0)
(glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
Expand All @@ -216,9 +223,9 @@
(glUniform1i (glGetUniformLocation combine-program "LayerConfigTex")
(gl-texture-index GL_TEXTURE0))
(glUniform1iv (glGetUniformLocation combine-program "LayerTargets")
LAYERS
how-many-layers
(list->s32vector
(for/list ([i (in-range LAYERS)])
(for/list ([i (in-range how-many-layers)])
(fx+ 1 i))))))

(define combine-dfbos
Expand Down Expand Up @@ -357,7 +364,7 @@
(define-make-delayed-render
stage-draw/dc
make-draw
(csd width height)
(csd width height how-many-layers)
((gl-filter-mode))
(layer-config static-st dynamic-st))

Expand Down
4 changes: 2 additions & 2 deletions mode-lambda/backend/gl/combine.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@glsl-include["lib.glsl"]

uniform sampler2D LayerTargets[@LAYERS];
uniform sampler2D LayerTargets[@how-many-layers];
uniform sampler2D LayerConfigTex;
uniform float XScale;
uniform float YScale;
Expand Down Expand Up @@ -41,7 +41,7 @@ void main() {
float ay = height - iTexCoord.y * height;

vec4 fin_Color = vec4(0.0, 0.0, 0.0, 1.0);
@in[compiletimelayer (in-range LAYERS)]{
@in[compiletimelayer (in-range @how-many-layers)]{
{
int layer = @compiletimelayer;
@glsl-include["layer.glsl"]
Expand Down
2 changes: 1 addition & 1 deletion mode-lambda/backend/gl/combine.vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@glsl-include["lib.glsl"]
@glsl-include["quad.glsl"]

uniform sampler2D LayerTargets[@LAYERS];
uniform sampler2D LayerTargets[@how-many-layers];
uniform sampler2D LayerConfigTex;
uniform vec2 LogicalSize;

Expand Down
13 changes: 9 additions & 4 deletions mode-lambda/backend/gl/ngl.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ in vec2 TexCoord;
in float Palette;
in float Layer;

@in[i (in-range LAYERS)]{
@in[i (in-range how-many-layers)]{
layout (location = @i) out vec4 out_Color@i;
}

void main(void)
{
vec4 PixelColor;
vec4 SpriteColor = texture(SpriteAtlasTex, TexCoord);

SpriteColor = texelFetch(SpriteAtlasTex,
ivec2( round(TexCoord.x), round(TexCoord.y) ),
0);

vec4 PixelColor;

if ( Palette == 0.0 ) {
PixelColor = SpriteColor;
} else {
Expand All @@ -29,14 +34,14 @@ void main(void)
}

vec4 fin_Color;

fin_Color.a = PixelColor.a * Color.a;
fin_Color.rgb = PixelColor.rgb + Color.rgb;

vec4 blank_Color = vec4(0.0,0.0,0.0,0.0);

int iLayer = int(floor(Layer));
@in[i (in-range LAYERS)]{
@in[i (in-range how-many-layers)]{
out_Color@i = (iLayer == @i) ? fin_Color : blank_Color;
}
}
2 changes: 1 addition & 1 deletion mode-lambda/backend/gl/ngl.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void main(void)
TexCoord =
vec2(tx + ((horiz + 1.0)/+2.0) * w,
ty + (( vert + 1.0)/+2.0) * h)
/ float(atlasSize.x);
; // / float(atlasSize.x); // for use with texture (rather than texelFetch)
Palette = float(pal);
Layer = float(layer);
}
6 changes: 3 additions & 3 deletions mode-lambda/backend/gl/util.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

;; XXX maybe 410
(define gl-backend-version (make-parameter '3.3))
(define valid-gl-backends '(3.3 es3.2))
(define valid-gl-backends '(3.3 es3.1))
(define (gl-es?)
(eq? 'es3.2 (gl-backend-version)))
(eq? 'es3.1 (gl-backend-version)))

(define-syntax-rule (glsl-include p) (include-template p))

Expand Down Expand Up @@ -139,7 +139,7 @@
(string-append (format "#version ~a\n"
(match (gl-backend-version)
['3.3 "330 core"]
['es3.2 "320 es"]))
['es3.1 "310 es"]))
VertexShader))
(s32vector))
(glCompileShader VertexShaderId)
Expand Down
8 changes: 4 additions & 4 deletions mode-lambda/backend/software.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
(when (fl<=3 0.0 λ3 1.0)
(draw-triangle! t x y λ1 λ2 λ3)))))

(define (stage-render csd width height)
(define (stage-render csd width height layers)
(define width.0 (fx->fl width))
(define hwidth.0 (fl* 0.5 width.0))
(define height.0 (fx->fl height))
Expand All @@ -135,7 +135,7 @@
pal-size pal-bs pal->idx)
csd)
(define root-bs-v
(build-vector LAYERS (λ (i) (make-bytes (* 4 width height)))))
(build-vector layers (λ (i) (make-bytes (* 4 width height)))))
(define combined-bs
(make-bytes (* 4 width height)))
(define tri-hash (make-2d-hash width height))
Expand Down Expand Up @@ -441,8 +441,8 @@
(make-parameter #f))

(define gui-mode 'draw)
(define (stage-draw/dc csd width height)
(define render (stage-render csd width height))
(define (stage-draw/dc csd width height how-many-layers)
(define render (stage-render csd width height how-many-layers))
(λ (layer-config static-st dynamic-st)
(define sprite-tree (cons static-st dynamic-st))
(define bs (render layer-config sprite-tree))
Expand Down
9 changes: 3 additions & 6 deletions mode-lambda/core.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
(struct compiled-sprite-db
(atlas-size atlas-bs spr->idx idx->w*h*tx*ty pal-size pal-bs pal->idx))

(define LAYERS 8)
(define layer/c
(and/c byte? (between/c 0 (sub1 LAYERS))))
(define layer/c byte?)

(define-cstruct _layer-data
([cx _float]
Expand All @@ -36,8 +34,6 @@

(define default-layer
(make-layer-data 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 1.0 #f #f))
(define default-layer-config
(make-vector LAYERS #f))

;; Mode7 Docs:
;; - http://www.coranac.com/tonc/text/mode7.htm
Expand Down Expand Up @@ -85,7 +81,7 @@
(printf "sprite-data size: ~v bytes\n" (ctype-sizeof _sprite-data)))

(define layer-vector/c
(apply vector/c (make-list LAYERS (or/c false/c layer-data?))))
(vectorof (or/c false/c layer-data?)))

(define-syntax-rule (backend/c (addl-input ...) output)
(-> layer-vector/c tree/c tree/c addl-input ... output))
Expand All @@ -97,6 +93,7 @@

(define (stage-backend/c output/c)
(-> compiled-sprite-db?
exact-nonnegative-integer?
exact-nonnegative-integer?
exact-nonnegative-integer?
output/c))
Expand Down
2 changes: 1 addition & 1 deletion mode-lambda/examples/one.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
(define csd-p (build-path here "csd"))
(save-csd! original-csd csd-p #:debug? #t)
(define csd (load-csd csd-p))
(define render (stage-draw/dc csd W H))
(define render (stage-draw/dc csd W H 8))
(vector ns csd render (make-text-renderer the-font csd)))

(define (go renderi mode spin?)
Expand Down
4 changes: 0 additions & 4 deletions mode-lambda/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@

(provide
(contract-out
[LAYERS
exact-nonnegative-integer?]
[PALETTE-DEPTH
exact-nonnegative-integer?]
[default-layer-config
layer-vector/c]
[compiled-sprite-db?
(-> any/c
boolean?)]
Expand Down
13 changes: 9 additions & 4 deletions mode-lambda/scribblings/backend-gl.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pretty fast, but kind of complicated.
@defthing[stage-draw/dc
(->i ([cdb compiled-sprite-db?]
[render-width exact-nonnegative-integer?]
[render-height exact-nonnegative-integer?])
(->i ([layer-config layer-vector/c]
[render-height exact-nonnegative-integer?]
[layers exact-nonnegative-integer?])
(->i ([layer-config (vectorof layer-data?)]
[static-st any/c]
[dynamic-st any/c])
(->i ([draw-width exact-nonnegative-integer?]
Expand All @@ -25,13 +26,17 @@ pretty fast, but kind of complicated.
any)))]{

Prepares a function that accepts rendering states and returns a
function that draws that rendering state.}
function that draws that rendering state. @racket[layers] must be less
than or equal to @litchar{GL_MAX_DRAW_BUFFERS}, which tends to be
either 4 on embedded devices, 8 on older devices, and 16 on new
devices. (A future version of this backend will use a different
implementation technique and limit it to be a @racket[byte?].)}

@defthing[gui-mode symbol?]{

A symbol to be sent to @racket[make-gui] of @racketmodname[lux/chaos/gui].}

@defthing[gl-backend-version (parameter/c (one-of '3.3 'es3.2))]{
@defthing[gl-backend-version (parameter/c (one-of '3.3 'es3.1))]{

Sets the desired version of OpenGL to use.}

Expand Down
5 changes: 3 additions & 2 deletions mode-lambda/scribblings/backend-software.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ very slow, and a little complicated, but easier to test than
@defthing[stage-draw/dc
(->i ([cdb compiled-sprite-db?]
[render-width exact-nonnegative-integer?]
[render-height exact-nonnegative-integer?])
(->i ([layer-config layer-vector/c]
[render-height exact-nonnegative-integer?]
[layers exact-nonnegative-integer?])
(->i ([layer-config (vectorof layer-data?)]
[static-st any/c]
[dynamic-st any/c])
(->i ([draw-width exact-nonnegative-integer?]
Expand Down
18 changes: 4 additions & 14 deletions mode-lambda/scribblings/mode-lambda.scrbl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#lang scribble/manual
@require[(only-in mode-lambda/core
LAYERS PALETTE-DEPTH)
PALETTE-DEPTH)
@for-label[racket/base
racket/contract/base]]

Expand All @@ -26,9 +26,9 @@ drawing surface. @ML follows an ahead-of-time, staged rendering regime
whereby all color data must be "compiled", using
@racket[compile-sprite-db], before any drawing can occur.

@ML draws sprites on to one of @racket[#,LAYERS] layers. The larger
layers are on-top, with transparent pixels seeping through. @ML makes
no guarantees about the layering and transparency behavior of sprites
@ML draws sprites on to one of the layers. The larger layers are
on-top, with transparent pixels seeping through. @ML makes no
guarantees about the layering and transparency behavior of sprites
within a single layer. Each layer may have an independent center
point, width, scaling coefficients, rotation, wrapping behavior, and
Mode-7 coefficients.
Expand Down Expand Up @@ -213,21 +213,11 @@ Compiles the database @racket[db].}
Use this module to construct scenes for one of the backends. Connect
it to @racketmodname[mode-lambda/static] with @racket[load-csd].

@defthing[LAYERS exact-nonnegative-integer?]{

The number of layers supported by
@racketmodname[mode-lambda]. Currently @racket[#,LAYERS].}

@defthing[PALETTE-DEPTH exact-nonnegative-integer?]{

The number of colors in one palette supported by
@racketmodname[mode-lambda]. Currently @racket[#,PALETTE-DEPTH].}

@defthing[default-layer-config layer-vector/c]{

The default configuration of layers. You almost certainly don't want
to use it.}

@defproc[(compiled-sprite-db? [x any/c])
boolean?]{

Expand Down

0 comments on commit ae1b9dd

Please sign in to comment.