From b29789d4683cd28757af2cbda00bdfe54645d549 Mon Sep 17 00:00:00 2001 From: Martin Stewart Date: Tue, 19 Sep 2023 23:08:47 +0200 Subject: [PATCH 1/4] Upgrade glsl parsing package and add test --- .ghci | 3 + stack.yaml | 2 + stack.yaml.lock | 11 ++ test/Test.hs | 7 +- test/scenario-webgl-extensions/elm.json | 26 +++++ .../src/Triangle.elm | 105 ++++++++++++++++++ 6 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 .ghci create mode 100644 test/scenario-webgl-extensions/elm.json create mode 100644 test/scenario-webgl-extensions/src/Triangle.elm diff --git a/.ghci b/.ghci new file mode 100644 index 000000000..1d4439685 --- /dev/null +++ b/.ghci @@ -0,0 +1,3 @@ +:set -fbyte-code +:set -fobject-code +:def rr const $ return $ unlines [":r","Test.target"] \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index 97d2812f5..9d85c20b9 100644 --- a/stack.yaml +++ b/stack.yaml @@ -15,6 +15,8 @@ extra-deps: - 'vendor/elm-format' # windows pinning for build issues - ansi-terminal-0.11 +- git: https://github.com/noteed/language-glsl.git + commit: 99c3aae76363b127679da640a6f00d61d2203830 flags: regex-posix: _regex-posix-clib: true diff --git a/stack.yaml.lock b/stack.yaml.lock index f83f42d73..16c94fad7 100644 --- a/stack.yaml.lock +++ b/stack.yaml.lock @@ -46,6 +46,17 @@ packages: size: 1461 original: hackage: ansi-terminal-0.11 +- completed: + name: language-glsl + version: 0.4.0.0 + git: https://github.com/noteed/language-glsl.git + pantry-tree: + size: 1066 + sha256: 155d01f395cc7df3c511df6ff35835b9d54f88a629a58098af7b54a4eeff8e63 + commit: 99c3aae76363b127679da640a6f00d61d2203830 + original: + git: https://github.com/noteed/language-glsl.git + commit: 99c3aae76363b127679da640a6f00d61d2203830 snapshots: - completed: sha256: 9313df78f49519315342f4c51ffc5da12659d3735f8ac3c54a1fb98ff874474e diff --git a/test/Test.hs b/test/Test.hs index 6ce8c82f8..56584265c 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -122,8 +122,8 @@ previewProject = do Dir.withCurrentDirectory p $ Lamdera.CLI.Deploy.run () () -target = - Test.Check.checkWithParams "/Users/mario/dev/projects/lamdera-dashboard" +--target = +-- Test.Check.checkWithParams "/Users/mario/dev/projects/lamdera-dashboard" -- Test.Check.checkWithParams "/Users/mario/lamdera/test/sheep-game" "sheep-game" -- target = buildTestHarnessToProductionJs @@ -277,6 +277,9 @@ liveReloadLive = do -- "src/Bytes/Encode.elm" -- "withDebug" +target = do + let project = "E:/repos/compiler/test/scenario-webgl-extensions/" + Lamdera.Compile.makeDev project [ "src/Triangle.elm" ] all = EasyTest.run allTests diff --git a/test/scenario-webgl-extensions/elm.json b/test/scenario-webgl-extensions/elm.json new file mode 100644 index 000000000..ed814642d --- /dev/null +++ b/test/scenario-webgl-extensions/elm.json @@ -0,0 +1,26 @@ +{ + "type": "application", + "source-directories": [ + "src" + ], + "elm-version": "0.19.1", + "dependencies": { + "direct": { + "elm/browser": "1.0.0", + "elm/core": "1.0.0", + "elm/html": "1.0.0", + "elm/json": "1.0.0", + "elm-explorations/linear-algebra": "1.0.0", + "elm-explorations/webgl": "1.1.3" + }, + "indirect": { + "elm/time": "1.0.0", + "elm/url": "1.0.0", + "elm/virtual-dom": "1.0.0" + } + }, + "test-dependencies": { + "direct": {}, + "indirect": {} + } +} diff --git a/test/scenario-webgl-extensions/src/Triangle.elm b/test/scenario-webgl-extensions/src/Triangle.elm new file mode 100644 index 000000000..ef9b45ce5 --- /dev/null +++ b/test/scenario-webgl-extensions/src/Triangle.elm @@ -0,0 +1,105 @@ +module Triangle exposing (main) + +{- + Rotating triangle, that is a "hello world" of the WebGL +-} + +import Browser +import Browser.Events exposing (onAnimationFrameDelta) +import Html exposing (Html) +import Html.Attributes exposing (width, height, style) +import WebGL exposing (Mesh, Shader) +import Math.Matrix4 as Mat4 exposing (Mat4) +import Math.Vector3 as Vec3 exposing (vec3, Vec3) +import Json.Decode exposing (Value) + + +main : Program Value Float Float +main = + Browser.element + { init = \_ -> ( 0, Cmd.none ) + , view = view + , subscriptions = (\_ -> onAnimationFrameDelta Basics.identity) + , update = (\elapsed currentTime -> ( elapsed + currentTime, Cmd.none )) + } + + +view : Float -> Html msg +view t = + WebGL.toHtml + [ width 400 + , height 400 + , style "display" "block" + ] + [ WebGL.entity + vertexShader + fragmentShader + mesh + { perspective = perspective (t / 1000) } + ] + + +perspective : Float -> Mat4 +perspective t = + Mat4.mul + (Mat4.makePerspective 45 1 0.01 100) + (Mat4.makeLookAt (vec3 (4 * cos t) 0 (4 * sin t)) (vec3 0 0 0) (vec3 0 1 0)) + + + +-- Mesh + + +type alias Vertex = + { position : Vec3 + , color : Vec3 + } + + +mesh : Mesh Vertex +mesh = + WebGL.triangles + [ ( Vertex (vec3 0 0 0) (vec3 1 0 0) + , Vertex (vec3 1 1 0) (vec3 0 1 0) + , Vertex (vec3 1 -1 0) (vec3 0 0 1) + ) + ] + + + +-- Shaders + + +type alias Uniforms = + { perspective : Mat4 } + + +vertexShader : Shader Vertex Uniforms { vcolor : Vec3 } +vertexShader = + [glsl| + + attribute vec3 position; + attribute vec3 color; + uniform mat4 perspective; + varying vec3 vcolor; + + void main () { + gl_Position = perspective * vec4(position, 1.0); + vcolor = color; + } + + |] + + +fragmentShader : Shader {} Uniforms { vcolor : Vec3 } +fragmentShader = + [glsl| + #extension EXT_frag_depth : enable + precision mediump float; + varying vec3 vcolor; + + void main () { + gl_FragColor = vec4(vcolor, 1.0); + } + + |] From ca556b428007335d401dcf10b65ab866a4c5890d Mon Sep 17 00:00:00 2001 From: Martin Stewart Date: Wed, 20 Sep 2023 10:40:35 +0200 Subject: [PATCH 2/4] wip --- test/Test.hs | 7 ++++--- test/Test/WebGL.hs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 test/Test/WebGL.hs diff --git a/test/Test.hs b/test/Test.hs index 56584265c..a684ee48b 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -19,6 +19,7 @@ import qualified Test.Check import qualified Test.Wire import qualified Test.Ext.ElmPages.Check import qualified Test.TypeHashes +import qualified Test.WebGL import qualified Lamdera.Evergreen.TestMigrationHarness import qualified Lamdera.Evergreen.TestMigrationGenerator @@ -277,9 +278,8 @@ liveReloadLive = do -- "src/Bytes/Encode.elm" -- "withDebug" -target = do - let project = "E:/repos/compiler/test/scenario-webgl-extensions/" - Lamdera.Compile.makeDev project [ "src/Triangle.elm" ] +target = tests [] +--target = Test.WebGL.suite all = EasyTest.run allTests @@ -302,4 +302,5 @@ allTests = , scope "Test.Check -> " $ Test.Check.suite , scope "Lamdera.Evergreen.TestMigrationHarness -> " $ Lamdera.Evergreen.TestMigrationHarness.suite , scope "Lamdera.Evergreen.TestMigrationGenerator -> " $ Lamdera.Evergreen.TestMigrationGenerator.suite + , scope "Test.WebGL -> " $ Test.WebGL.suite ] diff --git a/test/Test/WebGL.hs b/test/Test/WebGL.hs new file mode 100644 index 000000000..59e481b8d --- /dev/null +++ b/test/Test/WebGL.hs @@ -0,0 +1,36 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Test.WebGL where + +import qualified System.Directory as Dir +import System.FilePath (()) +import Data.Text as T + +import Lamdera +import EasyTest + +import qualified Init +import qualified Lamdera.CLI.Login +import qualified Lamdera.AppConfig +import qualified Lamdera.Update +import qualified Lamdera.Compile +import qualified Lamdera.Evergreen.Snapshot +import Test.Helpers +import Test.Check + +-- import qualified Lamdera.CLI.Check +-- import qualified Lamdera.CLI.Reset +-- import qualified Lamdera.CLI.Live +-- import qualified Lamdera.ReverseProxy +-- import Test.Wire +import qualified Ext.Common + + +suite :: Test () +suite = tests + [] +-- pending $ scope "make Elm app containing extension directive in shader" $ do +-- let project = "E:/repos/compiler/test/scenario-webgl-extensions/" +-- actual <- catchOutput $ Lamdera.Compile.makeDev project [ "src/Triangle.elm" ] +-- expectTextContains actual "1 module compiled successfully" + --] From 4755877f52def772e48fbcd9bf72863c178218d2 Mon Sep 17 00:00:00 2001 From: MartinSStewart Date: Wed, 20 Sep 2023 20:33:05 +0200 Subject: [PATCH 3/4] Improve tests --- test/Test.hs | 3 +-- test/Test/WebGL.hs | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/test/Test.hs b/test/Test.hs index a684ee48b..6bb720b5b 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -278,8 +278,7 @@ liveReloadLive = do -- "src/Bytes/Encode.elm" -- "withDebug" -target = tests [] ---target = Test.WebGL.suite +target = EasyTest.run Test.WebGL.suite all = EasyTest.run allTests diff --git a/test/Test/WebGL.hs b/test/Test/WebGL.hs index 59e481b8d..3fb8f4c77 100644 --- a/test/Test/WebGL.hs +++ b/test/Test/WebGL.hs @@ -28,9 +28,22 @@ import qualified Ext.Common suite :: Test () suite = tests - [] --- pending $ scope "make Elm app containing extension directive in shader" $ do --- let project = "E:/repos/compiler/test/scenario-webgl-extensions/" --- actual <- catchOutput $ Lamdera.Compile.makeDev project [ "src/Triangle.elm" ] --- expectTextContains actual "1 module compiled successfully" - --] + [ scope "make Elm app containing extension directive in shader" $ + let + elmStuffFolder = "/Users/martinstewart/Documents/GitHub/compiler/test/scenario-webgl-extensions/elm-stuff" + + setup = do + rmdir elmStuffFolder + + cleanup _ = do + rmdir elmStuffFolder + + test _ = do + let project = "/Users/martinstewart/Documents/GitHub/compiler/test/scenario-webgl-extensions/" + actual <- catchOutput $ Lamdera.Compile.makeDev project [ "src/Triangle.elm" ] + + expectTextContains actual + "Success! Compiled 1 module." + in + using setup cleanup test + ] From 3d61f8b0a19b62d4d676333df113482cf78ffbb3 Mon Sep 17 00:00:00 2001 From: MartinSStewart Date: Wed, 20 Sep 2023 21:49:47 +0200 Subject: [PATCH 4/4] Make paths relative --- test/Test/WebGL.hs | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/test/Test/WebGL.hs b/test/Test/WebGL.hs index 3fb8f4c77..83006c839 100644 --- a/test/Test/WebGL.hs +++ b/test/Test/WebGL.hs @@ -15,6 +15,7 @@ import qualified Lamdera.AppConfig import qualified Lamdera.Update import qualified Lamdera.Compile import qualified Lamdera.Evergreen.Snapshot +import qualified Lamdera.Relative import Test.Helpers import Test.Check @@ -28,22 +29,12 @@ import qualified Ext.Common suite :: Test () suite = tests - [ scope "make Elm app containing extension directive in shader" $ - let - elmStuffFolder = "/Users/martinstewart/Documents/GitHub/compiler/test/scenario-webgl-extensions/elm-stuff" + [ scope "make Elm app containing extension directive in shader" $ do + project <- io $ Lamdera.Relative.findDir "test/scenario-webgl-extensions" - setup = do - rmdir elmStuffFolder + _ <- io $ rmdir (project "elm-stuff") - cleanup _ = do - rmdir elmStuffFolder + actual <- catchOutput $ Lamdera.Compile.makeDev project [ "src/Triangle.elm" ] - test _ = do - let project = "/Users/martinstewart/Documents/GitHub/compiler/test/scenario-webgl-extensions/" - actual <- catchOutput $ Lamdera.Compile.makeDev project [ "src/Triangle.elm" ] - - expectTextContains actual - "Success! Compiled 1 module." - in - using setup cleanup test + expectTextContains actual "Success! Compiled 1 module." ]