changeset 210:f3912838cd69

fix: always use rgba
author Sam <sam@basx.dev>
date Tue, 09 May 2023 18:19:17 +0700
parents 461c18ee759b
children 744285b47a4d
files examples/E01_hello_triangle.nim examples/E02_squares.nim examples/E03_hello_cube.nim examples/E04_input.nim examples/E10_pong.nim src/semicongine/core/vector.nim src/semicongine/engine.nim src/semicongine/mesh.nim src/semicongine/renderer.nim
diffstat 9 files changed, 53 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/examples/E01_hello_triangle.nim	Tue May 09 01:37:46 2023 +0700
+++ b/examples/E01_hello_triangle.nim	Tue May 09 18:19:17 2023 +0700
@@ -4,9 +4,9 @@
 const
   vertexInput = @[
     attr[Vec3f]("position"),
-    attr[Vec3f]("color"),
+    attr[Vec4f]("color"),
   ]
-  vertexOutput = @[attr[Vec3f]("outcolor")]
+  vertexOutput = @[attr[Vec4f]("outcolor")]
   fragOutput = @[attr[Vec4f]("color")]
   vertexCode = compileGlslShader(
     stage=VK_SHADER_STAGE_VERTEX_BIT,
@@ -21,7 +21,7 @@
     stage=VK_SHADER_STAGE_FRAGMENT_BIT,
     inputs=vertexOutput,
     outputs=fragOutput,
-    main="color = vec4(outcolor, 1);"
+    main="color = outcolor;"
   )
 
 var
@@ -29,7 +29,7 @@
     "triangle",
     newMesh(
       [newVec3f(-0.5, 0.5), newVec3f(0, -0.5), newVec3f(0.5, 0.5)],
-      [X, Y, Z],
+      [newVec4f(1, 0, 0, 1), newVec4f(0, 1, 0, 1), newVec4f(0, 0, 1, 1)],
     )
   ))
   myengine = initEngine("Hello triangle")
--- a/examples/E02_squares.nim	Tue May 09 01:37:46 2023 +0700
+++ b/examples/E02_squares.nim	Tue May 09 18:19:17 2023 +0700
@@ -13,7 +13,7 @@
     HEIGHT = 2'f32 / ROWS
   var
     vertices: array[COLUMNS * ROWS * 4, Vec3f]
-    colors: array[COLUMNS * ROWS * 4, Vec3f]
+    colors: array[COLUMNS * ROWS * 4, Vec4f]
     iValues: array[COLUMNS * ROWS * 4, uint32]
     indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
 
@@ -22,7 +22,7 @@
       let
         y: float32 = (row * 2 / COLUMNS) - 1
         x: float32 = (col * 2 / ROWS) - 1
-        color = Vec3f([(x + 1) / 2, (y + 1) / 2, 0'f32])
+        color = newVec4f((x + 1) / 2, (y + 1) / 2, 0, 1)
         squareIndex = row * COLUMNS + col
         vertIndex = squareIndex * 4
       vertices[vertIndex + 0] = newVec3f(x, y)
@@ -44,10 +44,10 @@
   const
     vertexInput = @[
       attr[Vec3f]("position"),
-      attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite),
+      attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
       attr[uint32]("index"),
     ]
-    vertexOutput = @[attr[Vec3f]("outcolor")]
+    vertexOutput = @[attr[Vec4f]("outcolor")]
     uniforms = @[attr[float32]("time")]
     fragOutput = @[attr[Vec4f]("color")]
     vertexCode = compileGlslShader(
@@ -60,7 +60,7 @@
 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
 float v = min(1, max(0, pow(pos_weight - t, 2)));
 v = pow(1 - v, 3000);
-outcolor = vec3(color.r, color.g, v * 0.5);
+outcolor = vec4(color.r, color.g, v * 0.5, 1);
 gl_Position = vec4(position, 1.0);
 """
     )
@@ -69,7 +69,7 @@
       inputs=vertexOutput,
       uniforms=uniforms,
       outputs=fragOutput,
-      main="color = vec4(outcolor, 1);"
+      main="color = outcolor;"
     )
   var squaremesh = newMesh(
     positions=vertices,
--- a/examples/E03_hello_cube.nim	Tue May 09 01:37:46 2023 +0700
+++ b/examples/E03_hello_cube.nim	Tue May 09 18:19:17 2023 +0700
@@ -30,13 +30,16 @@
     TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top
     BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom
   ]
+  R = newVec4f(1, 0, 0, 1)
+  G = newVec4f(0, 1, 0, 1)
+  B = newVec4f(0, 0, 1, 1)
   cube_color = @[
-    Rf32, Rf32, Rf32, Rf32,
-    Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32,
-    Gf32, Gf32, Gf32, Gf32,
-    Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32,
-    Bf32, Bf32, Bf32, Bf32,
-    Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32,
+    R, R, R, R,
+    R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, R * 0.5'f32,
+    G, G, G, G,
+    G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, G * 0.5'f32,
+    B, B, B, B,
+    B * 0.5'f32, B * 0.5'f32, B * 0.5'f32, B * 0.5'f32,
   ]
 var
   tris: seq[array[3, uint16]]
@@ -51,9 +54,9 @@
   const
     vertexInput = @[
       attr[Vec3f]("position"),
-      attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite),
+      attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
     ]
-    vertexOutput = @[attr[Vec3f]("outcolor")]
+    vertexOutput = @[attr[Vec4f]("outcolor")]
     uniforms = @[
       attr[Mat4]("projection"),
       attr[Mat4]("view"),
@@ -72,7 +75,7 @@
       inputs=vertexOutput,
       uniforms=uniforms,
       outputs=fragOutput,
-      main="color = vec4(outcolor, 1);"
+      main="color = outcolor;"
     )
   myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
   var cube = newScene("scene", newEntity("cube", newMesh(positions=cube_pos, indices=tris, colors=cube_color)))
--- a/examples/E04_input.nim	Tue May 09 01:37:46 2023 +0700
+++ b/examples/E04_input.nim	Tue May 09 18:19:17 2023 +0700
@@ -24,9 +24,9 @@
   )
   keyDimension = 50'f32
   keyGap = 10'f32
-  backgroundColor = newVec3f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471)
-  baseColor = newVec3f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118'f32)
-  activeColor = newVec3f(0.6509803921568628'f32, 0.22745098039215686, 0.3137254901960784'f32)
+  backgroundColor = newVec4f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471, 1)
+  baseColor = newVec4f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118, 1)
+  activeColor = newVec4f(0.6509803921568628, 0.22745098039215686, 0.3137254901960784, 1)
   arrow_colors = @[
     baseColor * 0.9'f32,
     baseColor * 0.9'f32,
@@ -58,7 +58,7 @@
 var
   scene: Scene
   keyvertexpos: seq[Vec3f]
-  keyvertexcolor: seq[Vec3f]
+  keyvertexcolor: seq[Vec4f]
   keymeshindices: seq[array[3, uint16]]
   rowpos = newVec2f(0, 0)
   i = 0'u16
@@ -149,10 +149,10 @@
   const
     vertexInput = @[
       attr[Vec3f]("position"),
-      attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite),
+      attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
       attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
     ]
-    vertexOutput = @[attr[Vec3f]("outcolor")]
+    vertexOutput = @[attr[Vec4f]("outcolor")]
     uniforms = @[attr[Mat4]("projection")]
     fragOutput = @[attr[Vec4f]("color")]
     vertexCode = compileGlslShader(
@@ -167,7 +167,7 @@
       inputs=vertexOutput,
       uniforms=uniforms,
       outputs=fragOutput,
-      main="color = vec4(outcolor, 1);"
+      main="color = outcolor;"
     )
 
   # set up rendering
--- a/examples/E10_pong.nim	Tue May 09 01:37:46 2023 +0700
+++ b/examples/E10_pong.nim	Tue May 09 18:19:17 2023 +0700
@@ -3,10 +3,10 @@
 import semicongine
 
 let
-  barcolor = hexToColor("5A3F00").gamma(2.2).colorToHex()
+  barcolor = hexToColorAlpha("5A3F00").gamma(2.2).colorToHex()
   barSize = 0.1'f
   barWidth = 0.01'f
-  ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex()
+  ballcolor = hexToColorAlpha("B17F08").gamma(2.2).colorToHex()
   ballSize = 0.01'f
   backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2)
   ballSpeed = 60'f
@@ -33,10 +33,10 @@
   const
     vertexInput = @[
       attr[Vec3f]("position"),
-      attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite),
+      attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
       attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
     ]
-    vertexOutput = @[attr[Vec3f]("outcolor")]
+    vertexOutput = @[attr[Vec4f]("outcolor")]
     uniforms = @[attr[Mat4]("projection")]
     fragOutput = @[attr[Vec4f]("color")]
     vertexCode = compileGlslShader(
@@ -51,7 +51,7 @@
       inputs=vertexOutput,
       uniforms=uniforms,
       outputs=fragOutput,
-      main="color = vec4(outcolor, 1);"
+      main="color = outcolor;"
     )
 
   # set up rendering
--- a/src/semicongine/core/vector.nim	Tue May 09 01:37:46 2023 +0700
+++ b/src/semicongine/core/vector.nim	Tue May 09 18:19:17 2023 +0700
@@ -27,8 +27,7 @@
 # define some often used constants
 func ConstOne2[T: SomeNumber](): auto {.compiletime.} = TVec2[T]([T(1), T(1)])
 func ConstOne3[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(1), T(1), T(1)])
-func ConstOne4[T: SomeNumber](): auto {.compiletime.} = TVec4[T]([T(1), T(1), T(
-    1), T(1)])
+func ConstOne4[T: SomeNumber](): auto {.compiletime.} = TVec4[T]([T(1), T(1), T(1), T(1)])
 func ConstX[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(1), T(0), T(0)])
 func ConstY[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(0), T(1), T(0)])
 func ConstZ[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(0), T(0), T(1)])
@@ -49,8 +48,7 @@
 macro generateAllConsts() =
   result = newStmtList()
   for component in ["X", "Y", "Z", "R", "G", "B", "One2", "One3", "One4"]:
-    for theType in ["int", "int8", "int16", "int32", "int64", "float",
-        "float32", "float64"]:
+    for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]:
       var typename = theType[0 .. 0]
       if theType[^2].isDigit:
         typename = typename & theType[^2]
--- a/src/semicongine/engine.nim	Tue May 09 01:37:46 2023 +0700
+++ b/src/semicongine/engine.nim	Tue May 09 18:19:17 2023 +0700
@@ -1,6 +1,5 @@
 import std/sequtils
 import std/options
-import std/strutils
 import std/logging
 import std/os
 
--- a/src/semicongine/mesh.nim	Tue May 09 01:37:46 2023 +0700
+++ b/src/semicongine/mesh.nim	Tue May 09 18:19:17 2023 +0700
@@ -40,7 +40,7 @@
 func newMesh*(
   positions: openArray[Vec3f],
   indices: openArray[array[3, uint32|int32|uint16|int16|int]],
-  colors: openArray[Vec3f]=[],
+  colors: openArray[Vec4f]=[],
   uvs: openArray[Vec2f]=[],
   instanceCount=1'u32,
   autoResize=true
@@ -55,11 +55,11 @@
   result.data["position"] = DataList(thetype: Vec3F32)
   setValues(result.data["position"], positions.toSeq)
   if colors.len > 0:
-    result.data["color"] = DataList(thetype: Vec3F32)
+    result.data["color"] = DataList(thetype: Vec4F32)
     setValues(result.data["color"], colors.toSeq)
   if uvs.len > 0:
     result.data["uv"] = DataList(thetype: Vec2F32)
-    setValues(result.data["uv"], colors.toSeq)
+    setValues(result.data["uv"], uvs.toSeq)
 
   for i in indices:
     assert uint32(i[0]) < result.vertexCount
@@ -84,7 +84,7 @@
 
 func newMesh*(
   positions: openArray[Vec3f],
-  colors: openArray[Vec3f]=[],
+  colors: openArray[Vec4f]=[],
   uvs: openArray[Vec2f]=[],
   instanceCount=1'u32,
 ): auto =
@@ -96,6 +96,9 @@
 func dataSize*(mesh: Mesh, attribute: string): uint32 =
   mesh.data[attribute].size
 
+func dataType*(mesh: Mesh, attribute: string): DataType =
+  mesh.data[attribute].theType
+
 func indexDataSize*(mesh: Mesh): uint32 =
   case mesh.indexType
     of None: 0
@@ -165,13 +168,13 @@
 proc clearDataChanged*(mesh: var Mesh) =
   mesh.changedAttributes = @[]
 
-func rect*(width=1'f32, height=1'f32, color="ffffff"): Mesh =
+func rect*(width=1'f32, height=1'f32, color="ffffffff"): Mesh =
   result = new Mesh
   result.vertexCount = 4
   result.indicesCount = 6
   result.instanceCount = 1
   result.data["position"] = DataList(thetype: Vec3F32)
-  result.data["color"] = DataList(thetype: Vec3F32)
+  result.data["color"] = DataList(thetype: Vec4F32)
   result.data["uv"] = DataList(thetype: Vec2F32)
   result.indexType = Small
   result.smallIndices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]]
@@ -179,40 +182,40 @@
   let
     half_w = width / 2
     half_h = height / 2
-    c = hexToColor(color)
+    c = hexToColorAlpha(color)
     v = [newVec3f(-half_w, -half_h), newVec3f( half_w, -half_h), newVec3f( half_w,  half_h), newVec3f(-half_w,  half_h)]
 
   setValues(result.data["position"], v.toSeq)
   setValues(result.data["color"], @[c, c, c, c])
   setValues(result.data["uv"], @[newVec2f(0, 0), newVec2f(1, 0), newVec2f(1, 1), newVec2f(0, 1)])
 
-func tri*(width=1'f32, height=1'f32, color="ffffff"): Mesh =
+func tri*(width=1'f32, height=1'f32, color="ffffffff"): Mesh =
   result = new Mesh
   result.vertexCount = 3
   result.instanceCount = 1
   result.data["position"] = DataList(thetype: Vec3F32)
-  result.data["color"] = DataList(thetype: Vec3F32)
+  result.data["color"] = DataList(thetype: Vec4F32)
   let
     half_w = width / 2
     half_h = height / 2
-    colorVec = hexToColor(color)
+    colorVec = hexToColorAlpha(color)
   setValues(result.data["position"], @[
     newVec3f(0, -half_h), newVec3f( half_w, half_h), newVec3f(-half_w,  half_h),
   ])
   setValues(result.data["color"], @[colorVec, colorVec, colorVec])
 
-func circle*(width=1'f32, height=1'f32, nSegments=12'u16, color="ffffff"): Mesh =
+func circle*(width=1'f32, height=1'f32, nSegments=12'u16, color="ffffffff"): Mesh =
   assert nSegments >= 3
   result = new Mesh
   result.vertexCount = nSegments + 2
   result.instanceCount = 1
   result.indexType = Small
   result.data["position"] = DataList(thetype: Vec3F32)
-  result.data["color"] = DataList(thetype: Vec3F32)
+  result.data["color"] = DataList(thetype: Vec4F32)
   let
     half_w = width / 2
     half_h = height / 2
-    c = hexToColor(color)
+    c = hexToColorAlpha(color)
     step = (2'f32 * PI) / float32(nSegments)
   var
     pos = @[newVec3f(0, 0), newVec3f(0, half_h)]
--- a/src/semicongine/renderer.nim	Tue May 09 01:37:46 2023 +0700
+++ b/src/semicongine/renderer.nim	Tue May 09 18:19:17 2023 +0700
@@ -73,6 +73,7 @@
     for inputAttr in inputs:
       if not mesh.hasDataFor(inputAttr.name):
         mesh.initData(inputAttr)
+      assert mesh.dataType(inputAttr.name) == inputAttr.thetype, &"mesh attribute {inputAttr.name} has type {mesh.dataType(inputAttr.name)} but shader expects {inputAttr.thetype}"
   
   # create index buffer if necessary
   var indicesBufferSize = 0'u64