Skip to content

Commit 6675439

Browse files
committed
Use try-finally to simplify code
1 parent 1566afa commit 6675439

1 file changed

Lines changed: 37 additions & 35 deletions

File tree

src/volumetric_clouds/main.clj

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -632,15 +632,16 @@ void main()
632632
(defmacro framebuffer-render
633633
[texture width height & body]
634634
`(let [fbo# (GL30/glGenFramebuffers)]
635-
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER fbo#)
636-
(GL11/glBindTexture GL11/GL_TEXTURE_2D ~texture)
637-
(GL32/glFramebufferTexture GL30/GL_FRAMEBUFFER GL30/GL_COLOR_ATTACHMENT0 ~texture 0)
638-
(GL20/glDrawBuffers (volumetric-clouds.main/make-int-buffer (int-array [GL30/GL_COLOR_ATTACHMENT0])))
639-
(GL11/glViewport 0 0 ~width ~height)
640-
(let [result# (do ~@body)]
641-
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER 0)
642-
(GL30/glDeleteFramebuffers fbo#)
643-
result#)))
635+
(try
636+
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER fbo#)
637+
(GL11/glBindTexture GL11/GL_TEXTURE_2D ~texture)
638+
(GL32/glFramebufferTexture GL30/GL_FRAMEBUFFER GL30/GL_COLOR_ATTACHMENT0 ~texture 0)
639+
(GL20/glDrawBuffers (volumetric-clouds.main/make-int-buffer (int-array [GL30/GL_COLOR_ATTACHMENT0])))
640+
(GL11/glViewport 0 0 ~width ~height)
641+
~@body
642+
(finally
643+
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER 0)
644+
(GL30/glDeleteFramebuffers fbo#)))))
644645

645646

646647
(defn setup-point-attribute
@@ -660,24 +661,25 @@ void main()
660661
(defmacro render-array
661662
[width height & body]
662663
`(let [texture# (volumetric-clouds.main/make-texture-2d ~width ~height)]
663-
(volumetric-clouds.main/framebuffer-render texture# ~width ~height ~@body)
664-
(let [result# (volumetric-clouds.main/read-texture-2d texture# ~width ~height)]
665-
(GL11/glDeleteTextures texture#)
666-
result#)))
664+
(try
665+
(volumetric-clouds.main/framebuffer-render texture# ~width ~height ~@body)
666+
(volumetric-clouds.main/read-texture-2d texture# ~width ~height)
667+
(finally
668+
(GL11/glDeleteTextures texture#)))))
667669

668670

669671
(defn render-pixel
670672
[vertex-sources fragment-sources]
671673
(let [program (make-program-with-shaders vertex-sources fragment-sources)
672674
vao (setup-quad-vao)]
673675
(setup-point-attribute program)
674-
(let [result
675-
(render-array 1 1
676-
(GL20/glUseProgram program)
677-
(GL11/glDrawElements GL11/GL_QUADS 4 GL11/GL_UNSIGNED_INT 0))]
678-
(teardown-vao vao)
679-
(GL20/glDeleteProgram program)
680-
result)))
676+
(try
677+
(render-array 1 1
678+
(GL20/glUseProgram program)
679+
(GL11/glDrawElements GL11/GL_QUADS 4 GL11/GL_UNSIGNED_INT 0))
680+
(finally
681+
(teardown-vao vao)
682+
(GL20/glDeleteProgram program)))))
681683

682684

683685
(render-pixel [vertex-test] [fragment-test])
@@ -932,13 +934,13 @@ void main()
932934
program (make-program-with-shaders [vertex-test] fragment-sources)
933935
vao (setup-quad-vao)]
934936
(setup-point-attribute program)
935-
(let [result
936-
(render-array width height
937-
(setup-fog-uniforms program width height)
938-
(GL11/glDrawElements GL11/GL_QUADS 4 GL11/GL_UNSIGNED_INT 0))]
939-
(teardown-vao vao)
940-
(GL20/glDeleteProgram program)
941-
result)))
937+
(try
938+
(render-array width height
939+
(setup-fog-uniforms program width height)
940+
(GL11/glDrawElements GL11/GL_QUADS 4 GL11/GL_UNSIGNED_INT 0))
941+
(finally
942+
(teardown-vao vao)
943+
(GL20/glDeleteProgram program)))))
942944

943945

944946
(defn rgba-array->bufimg [data width height]
@@ -992,14 +994,14 @@ float noise(vec3 idx)
992994
(let [fragment-sources (concat cloud-shaders [ray-box fragment-cloud])
993995
program (make-program-with-shaders [vertex-test] fragment-sources)
994996
vao (setup-quad-vao)]
995-
(setup-point-attribute program)
996-
(let [result
997-
(render-array width height
998-
(setup-noise-uniforms program width height)
999-
(GL11/glDrawElements GL11/GL_QUADS 4 GL11/GL_UNSIGNED_INT 0))]
1000-
(teardown-vao vao)
1001-
(GL20/glDeleteProgram program)
1002-
result)))
997+
(try
998+
(setup-point-attribute program)
999+
(render-array width height
1000+
(setup-noise-uniforms program width height)
1001+
(GL11/glDrawElements GL11/GL_QUADS 4 GL11/GL_UNSIGNED_INT 0))
1002+
(finally
1003+
(teardown-vao vao)
1004+
(GL20/glDeleteProgram program)))))
10031005

10041006

10051007
(bufimg/tensor->image (rgba-array->bufimg (render-noise 640 480 constant-scatter no-shadow (cloud-transfer "noise" 0.01) noise-shader) 640 480))

0 commit comments

Comments
 (0)