Skip to content

Commit 6cd9901

Browse files
authored
fix: naming of assets and textures (#34)
1 parent c797780 commit 6cd9901

File tree

8 files changed

+221
-202
lines changed

8 files changed

+221
-202
lines changed

src/index.ts

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import canvasSizeObserver from 'WebGPU/canvasSizeObserver'
22
import getDevice from 'WebGPU/getDevice'
33
import initPrograms from 'WebGPU/programs/initPrograms'
44
import runCreator from 'run'
5-
import { createTextureFromSource } from 'WebGPU/getTexture'
65
import {
76
init_state,
87
add_asset,
@@ -17,6 +16,7 @@ import initMouseController from 'WebGPU/pointer'
1716
import IconsPng from '../msdf/output/icons.png'
1817
import IconsJson from '../msdf/output/icons.json'
1918
import getDefaultPoints from 'utils/getDefaultPoints'
19+
import * as Textures from 'textures'
2020

2121
export type SerializedInputAsset = {
2222
id?: number // not needed while loading project but useful for undo/redo to maintain selection
@@ -39,11 +39,6 @@ export interface CreatorAPI {
3939
destroy: VoidFunction
4040
}
4141

42-
export interface TextureSource {
43-
url: string
44-
texture?: GPUTexture
45-
}
46-
4742
export default async function initCreator(
4843
canvas: HTMLCanvasElement,
4944
onAssetsUpdate: (assets: SerializedOutputAsset[]) => void,
@@ -57,8 +52,11 @@ export default async function initCreator(
5752
onProcessingUpdate(loadingTextures > 0 || isMouseEventProcessing)
5853
}
5954

60-
/* setup WebGPU stuff */
6155
const device = await getDevice()
56+
Textures.init(device, (texLoadings) => {
57+
loadingTextures = texLoadings
58+
updateProcessing()
59+
})
6260

6361
init_state(canvas.clientWidth, canvas.clientHeight)
6462
const context = canvas.getContext('webgpu')
@@ -83,37 +81,7 @@ export default async function initCreator(
8381
updateProcessing()
8482
})
8583

86-
const textures: TextureSource[] = []
87-
88-
function addTexture(url: string, callback?: (width: number, height: number) => void): number {
89-
loadingTextures++
90-
updateProcessing()
91-
92-
const textureId = textures.length
93-
textures.push({ url })
94-
95-
const img = new Image()
96-
img.src = url
97-
98-
img.onload = () => {
99-
textures[textureId].texture = createTextureFromSource(device, img, { flipY: true })
100-
callback?.(img.width, img.height)
101-
102-
loadingTextures--
103-
updateProcessing()
104-
}
105-
106-
img.onerror = () => {
107-
console.error(`Failed to load image from ${url}`)
108-
109-
loadingTextures--
110-
updateProcessing()
111-
}
112-
113-
return textureId
114-
}
115-
116-
connect_on_asset_update_callback((serializedData: AssetZig[]) => {
84+
connect_on_asset_update_callback((serializedData: ZigAssetOutput[]) => {
11785
const serializedAssetsTextureUrl = [...serializedData].map<SerializedOutputAsset>((asset) => ({
11886
id: asset.id,
11987
textureId: asset.texture_id,
@@ -123,21 +91,21 @@ export default async function initCreator(
12391
u: point.u,
12492
v: point.v,
12593
})),
126-
url: textures[asset.texture_id].url,
94+
url: Textures.getUrl(asset.texture_id),
12795
}))
12896
onAssetsUpdate(serializedAssetsTextureUrl)
12997
})
13098

13199
connect_on_asset_selection_callback(onAssetSelect)
132100

133101
const addImage: CreatorAPI['addImage'] = (url) => {
134-
const textureId = addTexture(url, (width, height) => {
102+
const textureId = Textures.add(url, (width, height) => {
135103
const points = getDefaultPoints(width, height, canvas.clientWidth, canvas.clientHeight)
136104
add_asset(0 /* no id yet, needs to be generated */, points, textureId)
137105
})
138106
}
139107

140-
addTexture(IconsPng, (width, height) => {
108+
Textures.add(IconsPng, (width, height) => {
141109
import_icons(
142110
IconsJson.chars.flatMap((char) => [
143111
char.id,
@@ -151,29 +119,29 @@ export default async function initCreator(
151119
)
152120
})
153121

154-
const stopCreator = runCreator(canvas, context, device, presentationFormat, textures, () => {
122+
const stopCreator = runCreator(canvas, context, device, presentationFormat, () => {
155123
isMouseEventProcessing = false
156124
updateProcessing()
157125
})
158126

159127
const resetAssets: CreatorAPI['resetAssets'] = async (assets, withSnapshot = false) => {
160128
const results = await Promise.allSettled(
161-
assets.map<Promise<AssetZig>>(
129+
assets.map<Promise<ZigAssetInput>>(
162130
(asset) =>
163131
new Promise((resolve, reject) => {
164132
if (asset.points) {
165133
return resolve({
166134
points: asset.points,
167-
texture_id: asset.textureId || addTexture(asset.url),
135+
texture_id: asset.textureId || Textures.add(asset.url),
168136
id: asset.id || 0,
169137
})
170138
}
171139

172-
const textureId = addTexture(asset.url, (width, height) => {
173-
// we wait to add image once points are known because otherwise
174-
// if we add img first with "Default" point value and update
175-
// it later once texture is loaded, we will get history snapshot with
176-
// that "default" points
140+
const textureId = Textures.add(asset.url, (width, height) => {
141+
// we wait to add image once points are known. Other option was to add image first
142+
// with "default" points and then update it once texture is loaded.
143+
// However, that would cause issues with undo/redo since we would have history
144+
// snapshot with "default" points and then update it to the real points.
177145
return resolve({
178146
points: getDefaultPoints(width, height, canvas.clientWidth, canvas.clientHeight),
179147
texture_id: textureId, // if there is no points, then for sure there is no asset.textureId
@@ -184,11 +152,11 @@ export default async function initCreator(
184152
)
185153
)
186154

187-
const zigAssets = results
155+
const serializedAssets = results
188156
.filter((result) => result.status === 'fulfilled')
189157
.map((result) => result.value)
190158

191-
reset_assets(zigAssets, withSnapshot)
159+
reset_assets(serializedAssets, withSnapshot)
192160
}
193161

194162
return {

src/loadingTexture.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default function getLoadingTexture(device: GPUDevice): GPUTexture {
2-
const kTextureWidth = 5
3-
const kTextureHeight = 7
2+
const textureWidth = 5
3+
const textureHeight = 7
44
const _ = [255, 0, 0, 255] // red
55
const y = [255, 255, 0, 255] // yellow
66
const b = [0, 0, 255, 255] // blue
@@ -17,15 +17,15 @@ export default function getLoadingTexture(device: GPUDevice): GPUTexture {
1717

1818
const texture = device.createTexture({
1919
label: 'yellow F on red',
20-
size: [kTextureWidth, kTextureHeight],
20+
size: [textureWidth, textureHeight],
2121
format: 'rgba8unorm',
2222
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
2323
})
2424
device.queue.writeTexture(
2525
{ texture },
2626
textureData,
27-
{ bytesPerRow: kTextureWidth * 4 },
28-
{ width: kTextureWidth, height: kTextureHeight }
27+
{ bytesPerRow: textureWidth * 4 },
28+
{ width: textureWidth, height: textureHeight }
2929
)
3030

3131
return texture

src/logic/texture.zig renamed to src/logic/asset.zig

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,23 @@ const SHADER_TRIANGLE_INDICIES = [_]usize{
77
2, 3, 0,
88
};
99

10-
pub const AssetZig = struct {
11-
points: [4]PointUV,
12-
texture_id: u32,
13-
id: u32,
14-
};
15-
16-
pub const TEXTURE_VERTEX_BUFFER_SIZE: usize = 6 * 6; // 6 vertices, each with 6 attributes (x, y, z, w, u, v)
17-
pub const TEXTURE_PICK_VERTEX_BUFFER_SIZE: usize = 7 * 6; // 6 vertices, each with 7 attributes (x, y, z, w, u, v, id)
18-
19-
pub const Texture = struct {
10+
pub const Asset = struct {
2011
id: u32,
2112
points: [4]PointUV,
2213
texture_id: u32,
2314

24-
pub fn new(id: u32, points: [4]PointUV, texture_id: u32) Texture {
25-
return Texture{
15+
pub const VERTEX_BUFFER_SIZE: usize = 6 * 6; // 6 vertices, each with 6 attributes (x, y, z, w, u, v)
16+
pub const PICK_VERTEX_BUFFER_SIZE: usize = 7 * 6; // 6 vertices, each with 7 attributes (x, y, z, w, u, v, id)
17+
18+
pub fn new(id: u32, points: [4]PointUV, texture_id: u32) Asset {
19+
return Asset{
2620
.id = id,
2721
.points = points,
2822
.texture_id = texture_id,
2923
};
3024
}
3125

32-
pub fn get_vertex_data(self: Texture, buffer: *[TEXTURE_VERTEX_BUFFER_SIZE]f32) void {
26+
pub fn get_vertex_data(self: Asset, buffer: *[VERTEX_BUFFER_SIZE]f32) void {
3327
var i: usize = 0;
3428

3529
for (SHADER_TRIANGLE_INDICIES) |index| {
@@ -44,7 +38,7 @@ pub const Texture = struct {
4438
}
4539
}
4640

47-
pub fn get_vertex_pick_data(self: Texture, buffer: *[TEXTURE_PICK_VERTEX_BUFFER_SIZE]f32) void {
41+
pub fn get_vertex_pick_data(self: Asset, buffer: *[PICK_VERTEX_BUFFER_SIZE]f32) void {
4842
var i: usize = 0;
4943

5044
for (SHADER_TRIANGLE_INDICIES) |index| {
@@ -60,17 +54,23 @@ pub const Texture = struct {
6054
}
6155
}
6256

63-
pub fn update_coords(self: *Texture, new_points: [4]Types.PointUV) void {
57+
pub fn update_coords(self: *Asset, new_points: [4]Types.PointUV) void {
6458
for (&self.points, 0..) |*item, i| {
6559
item.* = new_points[i];
6660
}
6761
}
6862

69-
pub fn serialize(self: Texture) AssetZig {
70-
return AssetZig{
63+
pub fn serialize(self: Asset) SerializedAsset {
64+
return SerializedAsset{
7165
.points = self.points,
7266
.texture_id = self.texture_id,
7367
.id = self.id,
7468
};
7569
}
7670
};
71+
72+
pub const SerializedAsset = struct {
73+
points: [4]PointUV,
74+
texture_id: u32,
75+
id: u32,
76+
};

src/logic/index.d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ interface PointUV {
1111
}
1212

1313
type ZigF32Array = { typedArray: Float32Array }
14-
type AssetZig = {
14+
type ZigAssetInput = {
15+
id: number
16+
points: PointUV[]
17+
texture_id: number
18+
}
19+
type ZigAssetOutput = {
1520
id: number
1621
points: PointUV[]
1722
texture_id: number
@@ -21,7 +26,7 @@ declare module '*.zig' {
2126
export const init_state: (width: number, height: number) => void
2227
export const add_asset: (maybe_asset_id: number, points: PointUV[], texture_id: number) => void
2328
export const remove_asset: () => void
24-
export const reset_assets: (assets: AssetZig[], with_snapshot: boolean) => void
29+
export const reset_assets: (assets: ZigAssetInput[], with_snapshot: boolean) => void
2530

2631
export const on_update_pick: (id: number) => void
2732
export const on_pointer_down: (x: number, y: number) => void
@@ -36,7 +41,7 @@ declare module '*.zig' {
3641
pick_texture: (vertexData: ZigF32Array, texture_id: number) => void
3742
pick_triangle: (vertexData: ZigF32Array) => void
3843
}) => void
39-
export const connect_on_asset_update_callback: (cb: (data: AssetZig[]) => void) => void
44+
export const connect_on_asset_update_callback: (cb: (data: ZigAssetOutput[]) => void) => void
4045
export const connect_on_asset_selection_callback: (cb: (data: number) => void) => void
4146

4247
export const canvas_render: VoidFunction

0 commit comments

Comments
 (0)