chorama 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/.configs/tsconfig.lib.json +43 -0
  2. package/.configs/tsconfig.website.json +34 -0
  3. package/.github/workflows/static.yml +88 -0
  4. package/.vscode/launch.json +29 -0
  5. package/.vscode/tasks.json +19 -0
  6. package/README.md +127 -0
  7. package/assets/images/disappointed.jpg +0 -0
  8. package/assets/images/skybox/grimmnight_back.png +0 -0
  9. package/assets/images/skybox/grimmnight_bottom.png +0 -0
  10. package/assets/images/skybox/grimmnight_front.png +0 -0
  11. package/assets/images/skybox/grimmnight_left.png +0 -0
  12. package/assets/images/skybox/grimmnight_right.png +0 -0
  13. package/assets/images/skybox/grimmnight_top.png +0 -0
  14. package/assets/images/skybox/miramar_back.png +0 -0
  15. package/assets/images/skybox/miramar_bottom.png +0 -0
  16. package/assets/images/skybox/miramar_front.png +0 -0
  17. package/assets/images/skybox/miramar_left.png +0 -0
  18. package/assets/images/skybox/miramar_right.png +0 -0
  19. package/assets/images/skybox/miramar_top.png +0 -0
  20. package/assets/images/uv.jpg +0 -0
  21. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_BaseColor.png +0 -0
  22. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_Normal.png +0 -0
  23. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_OcclusionRoughMetal.png +0 -0
  24. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_BaseColor.png +0 -0
  25. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_Normal.png +0 -0
  26. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_OcclusionRoughMetal.png +0 -0
  27. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_BaseColor.png +0 -0
  28. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_Normal.png +0 -0
  29. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_OcclusionRoughMetal.png +0 -0
  30. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_BaseColor.png +0 -0
  31. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_Normal.png +0 -0
  32. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_OcclusionRoughMetal.png +0 -0
  33. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_BaseColor.png +0 -0
  34. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_Normal.png +0 -0
  35. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_OcclusionRoughMetal.png +0 -0
  36. package/assets/models/gltf/flight_helmet/index.bin +0 -0
  37. package/assets/models/gltf/flight_helmet/index.gltf +705 -0
  38. package/assets/models/gltf/object.gltf +23 -0
  39. package/assets/models/gltf/pirate_girl/index.bin +0 -0
  40. package/assets/models/gltf/pirate_girl/index.gltf +2082 -0
  41. package/assets/models/obj/pirate_girl/pirate_girl.obj +18459 -0
  42. package/assets/models/obj/pirate_girl/pirate_girl.png +0 -0
  43. package/astro.config.mjs +45 -0
  44. package/content/guide/api-map.md +89 -0
  45. package/content/guide/camera-and-controls.md +98 -0
  46. package/content/guide/first-scene.md +176 -0
  47. package/content/guide/index.md +72 -0
  48. package/content/guide/installation.md +179 -0
  49. package/content/guide/materials-and-lighting.md +138 -0
  50. package/content/guide/plugins-and-render-pipeline.md +124 -0
  51. package/content/guide/render-targets-and-views.md +147 -0
  52. package/content/guide/scene-graph-and-transforms.md +113 -0
  53. package/content/guide/textures-and-assets.md +120 -0
  54. package/content/guide/troubleshooting.md +49 -0
  55. package/env.d.ts +19 -0
  56. package/examples/addons/rendergraph_gui.js +580 -0
  57. package/examples/camera/orthographic.js +120 -0
  58. package/examples/camera/perspective.js +138 -0
  59. package/examples/lights/directional.js +397 -0
  60. package/examples/lights/multiple_spot_lights.js +304 -0
  61. package/examples/lights/point.js +337 -0
  62. package/examples/lights/spot.js +366 -0
  63. package/examples/loader/gltf_material.js +111 -0
  64. package/examples/loader/gltfloader.js +78 -0
  65. package/examples/loader/objloader.js +95 -0
  66. package/examples/material/cullface.js +111 -0
  67. package/examples/material/materials.js +126 -0
  68. package/examples/material/standard/basic.js +164 -0
  69. package/examples/mesh/circle.js +117 -0
  70. package/examples/mesh/cuboid.js +151 -0
  71. package/examples/mesh/cylinder.js +139 -0
  72. package/examples/mesh/geometries.js +108 -0
  73. package/examples/mesh/meshTopology.js +103 -0
  74. package/examples/mesh/plane.js +117 -0
  75. package/examples/mesh/skinning.js +136 -0
  76. package/examples/mesh/uvsphere.js +113 -0
  77. package/examples/other/rotatingCube.js +93 -0
  78. package/examples/other/rotatingSphere.js +96 -0
  79. package/examples/rendertarget/basic_canvas.js +130 -0
  80. package/examples/rendertarget/depth_texture.js +130 -0
  81. package/examples/rendertarget/image_target.js +140 -0
  82. package/examples/rendertarget/multiple_views.js +158 -0
  83. package/examples/rendertarget/render_masks.js +173 -0
  84. package/examples/rendertarget/split_screen.js +123 -0
  85. package/examples/rendertarget/split_view.js +137 -0
  86. package/examples/skybox/skybox.js +111 -0
  87. package/examples/texture/arrays.js +156 -0
  88. package/examples/texture/textureWrap.js +118 -0
  89. package/examples/transform/propagation.js +92 -0
  90. package/package.json +55 -0
  91. package/rollup.config.js +66 -0
  92. package/scripts/stage-chorama.mjs +29 -0
  93. package/src/caches/cache.js +420 -0
  94. package/src/caches/index.js +2 -0
  95. package/src/caches/uniformbuffers.js +104 -0
  96. package/src/cameracontrols/index.js +258 -0
  97. package/src/constants/index.js +3 -0
  98. package/src/constants/mesh.js +197 -0
  99. package/src/constants/others.js +218 -0
  100. package/src/constants/texture.js +183 -0
  101. package/src/core/constants.js +14 -0
  102. package/src/core/extensions.js +42 -0
  103. package/src/core/index.js +7 -0
  104. package/src/core/layouts/index.js +4 -0
  105. package/src/core/layouts/meshvertex.js +60 -0
  106. package/src/core/layouts/uniform.js +21 -0
  107. package/src/core/layouts/uniformbuffer.js +15 -0
  108. package/src/core/layouts/vertexbuffer.js +43 -0
  109. package/src/core/limits.js +247 -0
  110. package/src/core/resources/blendparams.js +89 -0
  111. package/src/core/resources/framebuffer.js +127 -0
  112. package/src/core/resources/gpubuffer.js +32 -0
  113. package/src/core/resources/gpumesh.js +43 -0
  114. package/src/core/resources/gputexture.js +73 -0
  115. package/src/core/resources/index.js +5 -0
  116. package/src/core/shader.js +62 -0
  117. package/src/core/webgl/bindgroup.js +89 -0
  118. package/src/core/webgl/descriptors.js +104 -0
  119. package/src/core/webgl/index.js +5 -0
  120. package/src/core/webgl/renderpassencoder.js +96 -0
  121. package/src/core/webgl/renderpipeline.js +54 -0
  122. package/src/core/webgl/utils.js +371 -0
  123. package/src/core/webgl/webglrenderdevice.js +235 -0
  124. package/src/function.js +358 -0
  125. package/src/index.js +15 -0
  126. package/src/loader/gltf.js +2172 -0
  127. package/src/loader/index.js +3 -0
  128. package/src/loader/loader.js +174 -0
  129. package/src/loader/obj.js +188 -0
  130. package/src/loader/texture.js +85 -0
  131. package/src/loader/utils.js +16 -0
  132. package/src/material/basic.js +75 -0
  133. package/src/material/depth.js +73 -0
  134. package/src/material/index.js +8 -0
  135. package/src/material/lambert.js +73 -0
  136. package/src/material/material.js +106 -0
  137. package/src/material/normal.js +30 -0
  138. package/src/material/phong.js +86 -0
  139. package/src/material/raw.js +52 -0
  140. package/src/material/standard.js +221 -0
  141. package/src/math/index.js +3 -0
  142. package/src/math/transform.js +38 -0
  143. package/src/mesh/attribute/attribute.js +79 -0
  144. package/src/mesh/attribute/index.js +1 -0
  145. package/src/mesh/attributedata/index.js +1 -0
  146. package/src/mesh/attributedata/separate.js +180 -0
  147. package/src/mesh/builders/base.js +41 -0
  148. package/src/mesh/builders/circle.js +63 -0
  149. package/src/mesh/builders/cuboid.js +135 -0
  150. package/src/mesh/builders/cylinder.js +131 -0
  151. package/src/mesh/builders/index.js +7 -0
  152. package/src/mesh/builders/plane.js +73 -0
  153. package/src/mesh/builders/utils.js +20 -0
  154. package/src/mesh/builders/uvsphere.js +80 -0
  155. package/src/mesh/builders/wireframe.js +62 -0
  156. package/src/mesh/index.js +4 -0
  157. package/src/mesh/mesh.js +149 -0
  158. package/src/objects/bone.js +17 -0
  159. package/src/objects/camera/camera.js +56 -0
  160. package/src/objects/camera/index.js +2 -0
  161. package/src/objects/camera/projection.js +203 -0
  162. package/src/objects/debug/index.js +1 -0
  163. package/src/objects/debug/skeleton.js +28 -0
  164. package/src/objects/index.js +7 -0
  165. package/src/objects/light/ambient.js +20 -0
  166. package/src/objects/light/directional.js +29 -0
  167. package/src/objects/light/index.js +5 -0
  168. package/src/objects/light/point.js +32 -0
  169. package/src/objects/light/shadow/index.js +1 -0
  170. package/src/objects/light/shadow/shadow.js +67 -0
  171. package/src/objects/light/spot.js +56 -0
  172. package/src/objects/mesh.js +141 -0
  173. package/src/objects/object3d.js +167 -0
  174. package/src/objects/skybox.js +38 -0
  175. package/src/plugins/camera/camera.js +19 -0
  176. package/src/plugins/camera/index.js +2 -0
  177. package/src/plugins/camera/nodes/cameraview.js +46 -0
  178. package/src/plugins/camera/nodes/index.js +2 -0
  179. package/src/plugins/camera/nodes/opaquepass.js +79 -0
  180. package/src/plugins/index.js +6 -0
  181. package/src/plugins/light/index.js +2 -0
  182. package/src/plugins/light/light.js +23 -0
  183. package/src/plugins/light/nodes/index.js +1 -0
  184. package/src/plugins/light/nodes/light.js +127 -0
  185. package/src/plugins/meshmaterial/index.js +3 -0
  186. package/src/plugins/meshmaterial/meshmaterial.js +381 -0
  187. package/src/plugins/meshmaterial/nodes/index.js +1 -0
  188. package/src/plugins/meshmaterial/nodes/meshmaterial.js +50 -0
  189. package/src/plugins/meshmaterial/resources/index.js +1 -0
  190. package/src/plugins/meshmaterial/resources/meshmaterialpipelines.js +50 -0
  191. package/src/plugins/shadow/index.js +3 -0
  192. package/src/plugins/shadow/nodes/index.js +3 -0
  193. package/src/plugins/shadow/nodes/shadow.js +272 -0
  194. package/src/plugins/shadow/nodes/shadowOccluder.js +112 -0
  195. package/src/plugins/shadow/nodes/shadowOpaquePass.js +73 -0
  196. package/src/plugins/shadow/resources/ShadowMap.js +99 -0
  197. package/src/plugins/shadow/resources/index.js +2 -0
  198. package/src/plugins/shadow/resources/shadowpipelines.js +25 -0
  199. package/src/plugins/shadow/shadow.js +31 -0
  200. package/src/plugins/skeletonhelper/index.js +1 -0
  201. package/src/plugins/skeletonhelper/skeletonhelper.js +160 -0
  202. package/src/plugins/skybox/index.js +3 -0
  203. package/src/plugins/skybox/nodes/index.js +1 -0
  204. package/src/plugins/skybox/nodes/skybox.js +143 -0
  205. package/src/plugins/skybox/resources/index.js +2 -0
  206. package/src/plugins/skybox/resources/skyboxmesh.js +14 -0
  207. package/src/plugins/skybox/resources/skyboxpipeline.js +6 -0
  208. package/src/plugins/skybox/skybox.js +137 -0
  209. package/src/renderer/core/index.js +179 -0
  210. package/src/renderer/graph/index.js +3 -0
  211. package/src/renderer/graph/nodes.js +34 -0
  212. package/src/renderer/graph/rendergraph.js +182 -0
  213. package/src/renderer/index.js +5 -0
  214. package/src/renderer/plugin.js +36 -0
  215. package/src/renderer/renderer.js +179 -0
  216. package/src/renderer/views.js +28 -0
  217. package/src/rendertarget/canvastarget.js +30 -0
  218. package/src/rendertarget/image.js +132 -0
  219. package/src/rendertarget/index.js +3 -0
  220. package/src/rendertarget/rendertarget.js +89 -0
  221. package/src/shader/basicFragment.glsl +30 -0
  222. package/src/shader/basicVertex.glsl +87 -0
  223. package/src/shader/common/color.glsl +7 -0
  224. package/src/shader/common/common.glsl +25 -0
  225. package/src/shader/common/index.js +4 -0
  226. package/src/shader/common/light.glsl +437 -0
  227. package/src/shader/common/math.glsl +12 -0
  228. package/src/shader/debug/index.js +2 -0
  229. package/src/shader/debug/skeletonFragment.glsl +8 -0
  230. package/src/shader/debug/skeletonVertex.glsl +27 -0
  231. package/src/shader/depthFragment.glsl +37 -0
  232. package/src/shader/index.js +11 -0
  233. package/src/shader/lambertFragment.glsl +126 -0
  234. package/src/shader/normalFragment.glsl +25 -0
  235. package/src/shader/phongFragment.glsl +140 -0
  236. package/src/shader/skyboxFragment.glsl +16 -0
  237. package/src/shader/skyboxVertex.glsl +20 -0
  238. package/src/shader/standardFragment.glsl +274 -0
  239. package/src/texture/index.js +2 -0
  240. package/src/texture/sampler.js +111 -0
  241. package/src/texture/texture.js +234 -0
  242. package/src/utils/index.js +115 -0
  243. package/tsconfig.json +11 -0
  244. package/website/config/index.ts +1 -0
  245. package/website/config/navigation.ts +53 -0
  246. package/website/content.config.ts +92 -0
  247. package/website/layouts/DocLayout.astro +501 -0
  248. package/website/layouts/Example.astro +91 -0
  249. package/website/pages/examples/[...slug].astro +77 -0
  250. package/website/pages/examples/index.astro +98 -0
  251. package/website/pages/examples/samples/[...slug].astro +17 -0
  252. package/website/pages/guide/[slug].astro +30 -0
  253. package/website/pages/guide/index.astro +21 -0
  254. package/website/pages/index.astro +9 -0
  255. package/website/plugins/remark-link-base.js +23 -0
  256. package/website/utils/url.ts +30 -0
@@ -0,0 +1,420 @@
1
+ /**@import { WebGLRenderPipelineDescriptor } from '../core/index.js' */
2
+ /**@import { WebGLAtttributeParams } from '../function.js' */
3
+
4
+ import { CanvasTarget, ImageRenderTarget, RenderTarget } from "../rendertarget/index.js"
5
+ import { Texture } from "../texture/index.js"
6
+ import { Attribute, Mesh } from "../mesh/index.js"
7
+ import { FrameBuffer, GPUMesh, GPUTexture, MeshVertexLayout, WebGLRenderDevice, WebGLRenderPipeline } from "../core/index.js"
8
+ import { UniformBuffers } from "./uniformbuffers.js"
9
+ import { BufferType, BufferUsage } from "../constants/others.js"
10
+ import { getFramebufferAttachment, mapToIndicesType, mapVertexFormatToWebGL } from "../function.js"
11
+ import { assert } from "../utils/index.js"
12
+ import { getVertexFormatComponentNumber, getVertexFormatComponentSize } from "../constants/mesh.js"
13
+ import { TextureType } from "../constants/texture.js"
14
+
15
+ export class Caches {
16
+ uniformBuffers = new UniformBuffers()
17
+ /**
18
+ * @type {Map<Mesh, GPUMesh>}
19
+ */
20
+ meshes = new Map()
21
+ /**
22
+ * @type {Map<Texture, GPUTexture>}
23
+ */
24
+ textures = new Map()
25
+ /**
26
+ * @type {WebGLRenderPipeline[]}
27
+ */
28
+ renderpipelines = []
29
+
30
+ /**
31
+ * @type {Map<RenderTarget, FrameBuffer>}
32
+ */
33
+ renderTargets = new Map()
34
+
35
+ /**
36
+ * @type {MeshVertexLayout[]}
37
+ */
38
+ meshLayouts = []
39
+
40
+ /**
41
+ * @param {WebGLRenderDevice} device
42
+ * @param {RenderTarget} target
43
+ * @returns {FrameBuffer}
44
+ */
45
+ getFrameBuffer(device, target) {
46
+ const current = this.renderTargets.get(target)
47
+ if (current) {
48
+ if (!renderTargetChanged(device, this, target, current)) {
49
+ return current
50
+ }
51
+ if (target instanceof CanvasTarget) {
52
+ current.width = target.width
53
+ current.height = target.height
54
+ return current
55
+ }
56
+ }
57
+
58
+ // Flush out any change detection that happened when the target was created
59
+ target.changed()
60
+
61
+ if (target instanceof ImageRenderTarget) {
62
+ const framebuffer = device.context.createFramebuffer()
63
+ const colorAttachments = []
64
+ const drawBuffers = target.color.map((texture, offset) => {
65
+ if (texture) {
66
+ return WebGL2RenderingContext.COLOR_ATTACHMENT0 + offset
67
+ }
68
+ return WebGL2RenderingContext.NONE
69
+ })
70
+ let depthBuffer
71
+
72
+ device.context.bindFramebuffer(WebGL2RenderingContext.FRAMEBUFFER, framebuffer)
73
+
74
+ for (let i = 0; i < target.color.length; i++) {
75
+ const color = /**@type {Texture}*/ (target.color[i])
76
+ const texture = this.getTexture(device, color)
77
+ bindTextureToAttachment(device, texture, i, target.layer)
78
+ colorAttachments[i] = texture
79
+ }
80
+
81
+ if (target.depthTexture) {
82
+ const texture = this.getTexture(device, target.depthTexture)
83
+
84
+ bindTextureToAttachment(device, texture, 0, target.layer)
85
+ depthBuffer = texture
86
+ }
87
+
88
+ const newTarget = new FrameBuffer(
89
+ framebuffer,
90
+ colorAttachments,
91
+ drawBuffers,
92
+ depthBuffer,
93
+ target.width,
94
+ target.height
95
+ )
96
+
97
+ // TODO: Dispose off the previous frame buffers
98
+ this.renderTargets.set(target, newTarget)
99
+ return newTarget
100
+ } else if (target instanceof CanvasTarget) {
101
+ const frameBuffer = new FrameBuffer(
102
+ null,
103
+ [],
104
+ [WebGL2RenderingContext.BACK],
105
+ undefined,
106
+ target.width,
107
+ target.height
108
+ )
109
+
110
+ this.renderTargets.set(target, frameBuffer)
111
+ return frameBuffer
112
+ } else {
113
+ throw 'Unsupported render target'
114
+ }
115
+ }
116
+
117
+ /**
118
+ * @param {WebGLRenderDevice} device
119
+ * @param {Mesh} mesh
120
+ * @param {ReadonlyMap<string, Attribute>} attributes
121
+ */
122
+ getMesh(device, mesh, attributes) {
123
+ const gpuMesh = this.meshes.get(mesh)
124
+ if (gpuMesh && !mesh.changed) {
125
+ return gpuMesh
126
+ }
127
+
128
+ const [layout, layoutId] = this.getLayout(mesh, attributes)
129
+ const vao = device.context.createVertexArray()
130
+ const newMesh = new GPUMesh(vao, 0, layoutId)
131
+
132
+ // Flush out any change detection that happened when the mesh was creates
133
+ mesh.changed
134
+ // TODO: Stop leaking memory, delete old gpu buffers
135
+ device.context.bindVertexArray(vao)
136
+ updateVAO(device, layout, mesh, newMesh)
137
+ this.meshes.set(mesh, newMesh)
138
+
139
+ return newMesh
140
+ }
141
+
142
+ /**
143
+ * @param {Mesh} mesh
144
+ * @param {ReadonlyMap<string, Attribute>} attributes
145
+ * @returns {[MeshVertexLayout, number]}
146
+ */
147
+ getLayout(mesh, attributes) {
148
+ for (let i = 0; i < this.meshLayouts.length; i++) {
149
+ const layout = /**@type {MeshVertexLayout} */(this.meshLayouts[i])
150
+ if (layout.compatibleWithMesh(mesh)) {
151
+ return [layout, i]
152
+ }
153
+ }
154
+ const layout = MeshVertexLayout.fromMesh(mesh, attributes)
155
+ const newId = this.meshLayouts.length
156
+
157
+ this.meshLayouts.push(layout)
158
+
159
+ return [layout, newId]
160
+ }
161
+
162
+ /**
163
+ * @param {WebGLRenderDevice} device
164
+ * @param {Texture} texture
165
+ * @returns {GPUTexture}
166
+ */
167
+ getTexture(device, texture) {
168
+ const gpuTexture = this.textures.get(texture)
169
+
170
+ if (gpuTexture) {
171
+ if (texture.changed) {
172
+ if (
173
+ texture.type === gpuTexture.type &&
174
+ texture.format === gpuTexture.actualFormat &&
175
+ texture.width === gpuTexture.width &&
176
+ texture.height === gpuTexture.height &&
177
+ texture.depth === gpuTexture.depth
178
+ ) {
179
+ // non-structural change, no need to create new gpu texture
180
+
181
+ if (texture.data) {
182
+ device.writeTexture({
183
+ texture: gpuTexture,
184
+ data: texture.data
185
+ })
186
+ }
187
+ return gpuTexture
188
+ }
189
+ } else {
190
+ return gpuTexture
191
+ }
192
+ }
193
+
194
+ // TODO: Stop leaking memory, delete old gpu textures
195
+ const newTex = device.createTexture({
196
+ type: texture.type,
197
+ format: texture.format,
198
+ width: texture.width,
199
+ height: texture.height,
200
+ depth: texture.depth,
201
+
202
+ })
203
+
204
+ if (texture.data) {
205
+ device.writeTexture({
206
+ texture: newTex,
207
+ data: texture.data
208
+ })
209
+ }
210
+
211
+ if (texture.generateMipmaps) {
212
+ device.context.generateMipmap(texture.type)
213
+ }
214
+
215
+ // Flush out any change detection that happened when the image was creates
216
+ texture.changed
217
+ this.textures.set(texture, newTex)
218
+ return newTex
219
+ }
220
+
221
+ /**
222
+ * @param {WebGLRenderDevice} device
223
+ * @param {WebGLRenderPipelineDescriptor} descriptor
224
+ * @returns {[WebGLRenderPipeline, number]}
225
+ */
226
+ createRenderPipeline(device, descriptor) {
227
+ const id = this.renderpipelines.length
228
+ const pipeline = device.createRenderPipeline(descriptor)
229
+
230
+ for (const [name, uboLayout] of pipeline.uniformBlocks) {
231
+ const ubo = this.uniformBuffers.getorSet(device.context, name, uboLayout)
232
+ const index = device.context.getUniformBlockIndex(pipeline.program, name)
233
+
234
+ device.context.uniformBlockBinding(pipeline.program, index, ubo.point)
235
+ }
236
+ this.renderpipelines[id] = pipeline
237
+ return [pipeline, id]
238
+ }
239
+
240
+ /**
241
+ * @param {number} id
242
+ * @returns {WebGLRenderPipeline | undefined}
243
+ */
244
+ getRenderPipeline(id) {
245
+ return this.renderpipelines[id]
246
+ }
247
+
248
+ /**
249
+ * @param {number} id
250
+ */
251
+ getMeshVertexLayout(id) {
252
+ return this.meshLayouts[id]
253
+ }
254
+ }
255
+
256
+ /**
257
+ * @param {WebGLRenderDevice} device
258
+ * @param {MeshVertexLayout} layout
259
+ * @param {Mesh} mesh
260
+ * @param {GPUMesh} gpuMesh
261
+ */
262
+ function updateVAO(device, layout, mesh, gpuMesh) {
263
+ const { indices, attributes } = mesh
264
+ let attrCount
265
+
266
+ // TODO: Delete the old buffers if present, probably leaking memory here
267
+ if (indices !== undefined) {
268
+ const buffer = device.createBuffer({
269
+ type: BufferType.ElementArray,
270
+ usage: BufferUsage.Static,
271
+ size: indices.byteLength
272
+ })
273
+ device.writeBuffer(buffer, indices)
274
+ gpuMesh.indexType = mapToIndicesType(indices)
275
+ gpuMesh.indexBuffer = buffer
276
+ }
277
+
278
+ for (const vertexLayout of layout.layouts) {
279
+ const attribute = vertexLayout.attributes[0]
280
+
281
+ assert(attribute, "The mesh vertex layout is incorrectly set up for the provided mesh.")
282
+
283
+ const data = attributes.get(attribute.name)
284
+
285
+ assert(data, `The provided mesh does not have the vertex attribute ${attribute.name}`)
286
+
287
+ // This only works for separate buffers for each vertex attribute.
288
+ const buffer = device.createBuffer({
289
+ type: BufferType.Array,
290
+ size: data.byteLength,
291
+ usage: BufferUsage.Static
292
+ })
293
+ const params = mapVertexFormatToWebGL(attribute.format)
294
+ const count = data.byteLength / (getVertexFormatComponentSize(attribute.format) * getVertexFormatComponentNumber(attribute.format))
295
+
296
+ device.writeBuffer(buffer, data)
297
+ setVertexAttribute(device.context, attribute.id, params)
298
+ gpuMesh.attributeBuffers.push(buffer)
299
+
300
+ if (attrCount) {
301
+ if (count < attrCount) {
302
+ attrCount = count
303
+ }
304
+ } else {
305
+ attrCount = count
306
+ }
307
+ }
308
+
309
+ if (indices) {
310
+ gpuMesh.count = indices.length
311
+ } else if (attrCount !== undefined) {
312
+ gpuMesh.count = attrCount
313
+ } else {
314
+ gpuMesh.count = 0
315
+ }
316
+ }
317
+
318
+ /**
319
+ * @param {WebGL2RenderingContext} context
320
+ * @param {number} index
321
+ * @param {WebGLAtttributeParams} params
322
+ * @param {number} [stride = 0]
323
+ * @param {number} [offset = 0]
324
+ */
325
+ function setVertexAttribute(context, index, params, stride = 0, offset = 0) {
326
+ const { type, size, normalized } = params
327
+ context.enableVertexAttribArray(index)
328
+ switch (type) {
329
+ case WebGL2RenderingContext.FLOAT:
330
+ context.vertexAttribPointer(index, size, type, normalized, stride, offset);
331
+ break;
332
+ case WebGL2RenderingContext.BYTE:
333
+ case WebGL2RenderingContext.UNSIGNED_BYTE:
334
+ case WebGL2RenderingContext.SHORT:
335
+ case WebGL2RenderingContext.UNSIGNED_SHORT:
336
+ case WebGL2RenderingContext.INT:
337
+ case WebGL2RenderingContext.UNSIGNED_INT:
338
+ if (normalized) {
339
+ context.vertexAttribPointer(index, size, type, normalized, stride, offset);
340
+ } else {
341
+ context.vertexAttribIPointer(index, size, type, stride, offset);
342
+ }
343
+ break;
344
+ default:
345
+ throw new Error(`Unsupported GlDataType: ${type.toString()}`);
346
+ }
347
+ }
348
+
349
+ /**
350
+ * @param {WebGLRenderDevice} device
351
+ * @param {GPUTexture} gpuTexture
352
+ * @param {number} offset
353
+ * @param {number} layer
354
+ */
355
+ function bindTextureToAttachment(device, gpuTexture, offset, layer) {
356
+ const attachment = getFramebufferAttachment(gpuTexture.actualFormat) + offset
357
+ switch (gpuTexture.type) {
358
+ case TextureType.Texture2D:
359
+ device.context.framebufferTexture2D(
360
+ WebGL2RenderingContext.FRAMEBUFFER,
361
+ attachment,
362
+ WebGL2RenderingContext.TEXTURE_2D,
363
+ gpuTexture.inner,
364
+ 0
365
+ )
366
+ break;
367
+ case TextureType.Texture2DArray:
368
+ device.context.framebufferTextureLayer(
369
+ WebGL2RenderingContext.FRAMEBUFFER,
370
+ attachment,
371
+ gpuTexture.inner,
372
+ 0,
373
+ layer
374
+ )
375
+ break;
376
+ default:
377
+ break;
378
+ }
379
+ }
380
+
381
+ /**
382
+ * @param {WebGLRenderDevice} device
383
+ * @param {Caches} caches
384
+ * @param {RenderTarget} target
385
+ * @param {FrameBuffer} frameBuffer
386
+ */
387
+ function renderTargetChanged(device, caches, target, frameBuffer) {
388
+ if (target instanceof ImageRenderTarget) {
389
+ const colorLength = Math.max(target.color.length, frameBuffer.colorAttachments.length)
390
+ for (let i = 0; i < colorLength; i++) {
391
+ const texture = target.color[i]
392
+ const attachment = frameBuffer.colorAttachments[i]
393
+ if (texture) {
394
+ const current = caches.getTexture(device, texture)
395
+
396
+ if (current !== attachment) {
397
+ return true
398
+ }
399
+ } else {
400
+ if (attachment) {
401
+ return true
402
+ }
403
+ }
404
+ }
405
+
406
+ if (target.depthTexture) {
407
+ const current = caches.getTexture(device, target.depthTexture)
408
+
409
+ if (current !== frameBuffer.depthBuffer) {
410
+ return true
411
+ }
412
+ } else {
413
+ if (frameBuffer.depthBuffer && !target.depth) {
414
+ return true
415
+ }
416
+ }
417
+ }
418
+
419
+ return target.changed()
420
+ }
@@ -0,0 +1,2 @@
1
+ export * from './cache.js'
2
+ export * from './uniformbuffers.js'
@@ -0,0 +1,104 @@
1
+ import { UniformBufferLayout } from "../core/layouts/uniformbuffer.js"
2
+
3
+
4
+ export class UniformBufferPointAllocator {
5
+ number = 0
6
+
7
+ reserve() {
8
+ const id = this.number
9
+ this.number++
10
+
11
+ return id
12
+ }
13
+ }
14
+
15
+ export class UniformBuffers {
16
+ /**
17
+ * @type {Map<string,UniformBuffer>}
18
+ */
19
+ list = new Map()
20
+
21
+ allocator = new UniformBufferPointAllocator()
22
+
23
+ /**
24
+ * @param {WebGL2RenderingContext} gl
25
+ * @param {string} name
26
+ * @param {UniformBufferLayout} layout
27
+ * @returns {UniformBuffer}
28
+ */
29
+ set(gl, name, layout) {
30
+ const index = this.allocator.reserve()
31
+ return this.setAtPoint(gl, name, index, layout)
32
+ }
33
+
34
+ /**
35
+ * @param {WebGL2RenderingContext} gl
36
+ * @param {string} name
37
+ * @param {number} index
38
+ * @param {UniformBufferLayout} layout
39
+ */
40
+ setAtPoint(gl, name, index, layout) {
41
+ const ubo = new UniformBuffer(gl, index, layout.size)
42
+ this.list.set(name, ubo)
43
+
44
+ return ubo
45
+ }
46
+
47
+ /**
48
+ * @param {string} name
49
+ */
50
+ get(name) {
51
+ return this.list.get(name)
52
+ }
53
+
54
+ /**
55
+ * @param {WebGL2RenderingContext} gl
56
+ * @param {string} name
57
+ * @param {UniformBufferLayout} layout
58
+ * @returns {UniformBuffer}
59
+ */
60
+ getorSet(gl, name, layout) {
61
+ const ubo = this.get(name)
62
+
63
+ if (ubo) {
64
+ if (ubo.size >= layout.size) {
65
+ return ubo
66
+ }
67
+
68
+ // TODO: Delete the old buffer, we are leaking gpu memory here
69
+ return this.setAtPoint(gl, name, ubo.point, layout)
70
+ }
71
+
72
+ return this.set(gl, name, layout)
73
+ }
74
+ }
75
+ /**
76
+ * @param {WebGL2RenderingContext} gl
77
+ */
78
+ export class UniformBuffer {
79
+ /**
80
+ * @param {WebGL2RenderingContext} gl
81
+ * @param {number} point
82
+ * @param {number} bufSize
83
+ */
84
+ constructor(gl, point, bufSize) {
85
+ this.point = point;
86
+ this.buffer = gl.createBuffer();
87
+ this.size = bufSize
88
+
89
+ gl.bindBuffer(gl.UNIFORM_BUFFER, this.buffer)
90
+ gl.bufferData(gl.UNIFORM_BUFFER, bufSize, gl.DYNAMIC_DRAW)
91
+ gl.bindBuffer(gl.UNIFORM_BUFFER, null)
92
+ gl.bindBufferBase(gl.UNIFORM_BUFFER, point, this.buffer)
93
+ }
94
+ /**
95
+ * @param {WebGL2RenderingContext} gl
96
+ * @param {ArrayBuffer} data
97
+ */
98
+ update(gl, data) {
99
+ gl.bindBuffer(gl.UNIFORM_BUFFER, this.buffer);
100
+ gl.bufferSubData(gl.UNIFORM_BUFFER, 0, data);
101
+ gl.bindBuffer(gl.UNIFORM_BUFFER, null);
102
+ return this;
103
+ }
104
+ }