Skip to content

Commit 089bff1

Browse files
committed
add bidn group for r3 transformation unfiforms
1 parent 814e77d commit 089bff1

File tree

9 files changed

+106
-14
lines changed

9 files changed

+106
-14
lines changed

src/ts/dynamicObject/HeightFieldPipeline.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export class HeightFieldPipeline extends AbstractPipeline {
2121
depthStencilFormat: GPUTextureFormat;
2222
depthCompare: GPUCompareFunction;
2323
depthWriteEnabled: boolean;
24-
bindGroup0Layout: GPUBindGroupLayout;
24+
project: Project;
2525

2626
constructor(project: Project, label: string) {
2727
super(label);
28-
this.bindGroup0Layout = project.renderer.bindGroup0Layout;
28+
this.project = project;
2929
this.shaderCode = staticShaderCode;
3030
this.preProzessedShaderCoder = resolveIncludes(this.shaderCode);
3131
this.vertexConstants = {};
@@ -47,9 +47,10 @@ export class HeightFieldPipeline extends AbstractPipeline {
4747
}
4848

4949
createPipelineLayout(): GPUPipelineLayout | "auto" {
50+
const renderer = this.project.renderer;
5051
return gpuDevice.createPipelineLayout({
5152
label: "Height field pipeline layout",
52-
bindGroupLayouts: [this.bindGroup0Layout],
53+
bindGroupLayouts: [renderer.bindGroup0Layout, renderer.bindGroupR3Layout],
5354
});
5455
}
5556

src/ts/dynamicObject/HeightFieldR3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class HeightFieldR3 extends R3Object {
77
xVerts: number;
88
yVerts: number;
99
constructor(project: Project, xVerts: number = 1000, yVerts: number = 1000) {
10-
super();
10+
super(project);
1111
this.xVerts = xVerts;
1212
this.yVerts = yVerts;
1313
this.pipeline = new HeightFieldPipeline(project, "Height Field Material");

src/ts/project/R3Object.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1+
import { gpuDevice } from "../GPUX";
12
import TransformationStack from "../transformation/TransformationStack";
3+
import { Project } from "./Project";
24

35
export default abstract class R3Object {
4-
index: number = 0;
56
transformationStack;
7+
defaultBindGroup: GPUBindGroup;
8+
vertexUniformBuffer: GPUBuffer;
9+
fragmentUniformBuffer: GPUBuffer;
10+
isSelected: boolean = false;
11+
isActive: boolean = false;
612

7-
constructor() {
13+
constructor(project: Project) {
814
this.transformationStack = new TransformationStack();
15+
const { vertexUniformBuffer, fragmentUniformBuffer, bindGroup } = project.renderer.getNew3RData();
16+
this.vertexUniformBuffer = vertexUniformBuffer;
17+
this.fragmentUniformBuffer = fragmentUniformBuffer;
18+
this.defaultBindGroup = bindGroup;
919
}
1020

1121
getWorldLocation() {
1222
return this.transformationStack.getTransformationMatrix().getTranslation();
1323
}
24+
vertexUniformData = new Float32Array(16);
25+
fragmentUniformData = new Uint32Array(1);
26+
updateUniforms() {
27+
this.transformationStack.getTransformationMatrix().pushInFloat32ArrayColumnMajor(this.vertexUniformData);
28+
const selectedMask = this.isSelected ? 1 : 0;
29+
const activeMask = this.isActive ? 2 : 0;
30+
this.fragmentUniformData[0] = selectedMask | activeMask;
31+
gpuDevice.queue.writeBuffer(this.vertexUniformBuffer, 0, this.vertexUniformData);
32+
gpuDevice.queue.writeBuffer(this.fragmentUniformBuffer, 0, this.fragmentUniformData);
33+
}
1434
}

src/ts/projection/PerspectiveProjection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Projection from "./Projection";
33

44
export default class PerspectiveProjection implements Projection {
55
vFOV = 90;
6-
near = 0.1;
6+
near = 0.01;
77
far = 1000;
88
getProjectionMatrix(width: number, height: number): mat4 {
99
const AR = width / height;

src/ts/rendering/Renderer.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default class Renderer {
2121
private viewUBO: GPUBuffer;
2222
private bindGroup0: GPUBindGroup;
2323
bindGroup0Layout: GPUBindGroupLayout;
24+
bindGroupR3Layout: GPUBindGroupLayout;
2425

2526
constructor(project: Project) {
2627
this.project = project;
@@ -42,6 +43,26 @@ export default class Renderer {
4243
},
4344
]
4445
});
46+
47+
this.bindGroupR3Layout = gpuDevice.createBindGroupLayout({
48+
label: "Default R3 bind group layout",
49+
entries: [
50+
{
51+
binding: 0,
52+
visibility: GPUShaderStage.VERTEX,
53+
buffer: {
54+
type: "uniform",
55+
}
56+
},
57+
{
58+
binding: 1,
59+
visibility: GPUShaderStage.FRAGMENT,
60+
buffer: {
61+
type: "uniform",
62+
}
63+
},
64+
]
65+
});
4566
this.bindGroup0 = gpuDevice.createBindGroup({
4667
label: "Global default bind group 0",
4768
entries: [
@@ -135,6 +156,9 @@ export default class Renderer {
135156
renderPassEncoder.setViewport(0, 0, viewport.getCurrentTexture().width, viewport.getCurrentTexture().height, 0, 1);
136157
for (const hf of this.project.scene.heightFieldObjects) {
137158
renderPassEncoder.setPipeline(hf.pipeline.gpuPipeline);
159+
hf.transformationStack.rotate.x += 0.01;
160+
hf.updateUniforms();
161+
renderPassEncoder.setBindGroup(1, hf.defaultBindGroup);
138162
renderPassEncoder.draw(hf.getVerts());
139163
}
140164
if (this.project.scene.gridPipeline) {
@@ -188,6 +212,42 @@ export default class Renderer {
188212
this.viewportCache.delete(viewport);
189213
}
190214

215+
getNew3RData(): { vertexUniformBuffer: GPUBuffer, fragmentUniformBuffer: GPUBuffer, bindGroup: GPUBindGroup } {
216+
const vertexUniformBuffer = gpuDevice.createBuffer({
217+
label: "R3 default vertex uniforms buffer",
218+
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.UNIFORM,
219+
size: 16 * 4
220+
});
221+
const fragmentUniformBuffer = gpuDevice.createBuffer({
222+
label: "R3 default fragment uniforms buffer",
223+
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.UNIFORM,
224+
size: 1 * 4
225+
});
226+
const bindGroup = gpuDevice.createBindGroup({
227+
label: "R3 Bind Group",
228+
entries: [
229+
{
230+
binding: 0,
231+
resource:
232+
{
233+
label: "R3 bind group vertex uniform resource",
234+
buffer: vertexUniformBuffer
235+
}
236+
},
237+
{
238+
binding: 1,
239+
resource:
240+
{
241+
label: "R3 bind group fragment uniform resource",
242+
buffer: fragmentUniformBuffer
243+
}
244+
}
245+
],
246+
layout: this.bindGroupR3Layout,
247+
});
248+
return { vertexUniformBuffer, fragmentUniformBuffer, bindGroup };
249+
}
250+
191251
static getDepthStencilFormat(): GPUTextureFormat {
192252
return "depth24plus";
193253
}

src/ts/rendering/Shader.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import sRGB_EOTF from "../../wgsl/utility/sRGB_EOTF.wgsl?raw";
33
import sRGB_OETF from "../../wgsl/utility/sRGB_OETF.wgsl?raw";
44
import fragmentOutput from "../../wgsl/utility/fragmentOutput.wgsl?raw";
55
import frame from "../../wgsl/utility/frame.wgsl?raw";
6+
import r3 from "../../wgsl/utility/r3.wgsl?raw";
67

78
const snippets: { [key: string]: string } = {
89
"utility/fragmentOutput": fragmentOutput,
910
"utility/linearRGBToLinearDisplayP3": linearRGBToLinearDisplayP3,
1011
"utility/sRGB_EOTF": sRGB_EOTF,
1112
"utility/sRGB_OETF": sRGB_OETF,
1213
"utility/frame": frame,
14+
"utility/r3": r3,
1315
};
1416

1517
export function resolveIncludes(code: string): string {

src/wgsl/debug/grid.wgsl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn vertex_main(@builtin(vertex_index) index: u32, @builtin(instance_index) insta
2020

2121
let far = getFar();
2222
let largePass = isLargePass(instance);
23-
let ls_pos = (vec2f(f32(x), f32(y)) * 2.0 - 1.0) * select(smallSize, far * 4.0, largePass);
23+
let ls_pos = (vec2f(f32(x), f32(y)) * 2.0 - 1.0) * select(smallSize + 0.125, far * 4.0, largePass);
2424

2525
let cameraGridOffset = floor(getCameraPosition().xy);
2626
let ws_pos = ls_pos + cameraGridOffset;
@@ -36,12 +36,11 @@ fn getGrid(coord: vec2f, lineThickness: f32) -> vec4f {
3636
return vec4(clamp(1.0 - val, vec2f(0), vec2f(1)), w);
3737
}
3838

39-
const falloffEnd = array<f32, 4>(0.25, 0.6, 0.8, 1.0);
4039
fn getAlpha(g_2D: vec4f, dist: f32, endFactor: f32, gridMode: i32, far: f32) -> f32 {
41-
var converage = max(g_2D.x / (g_2D.z * 8 + 1), g_2D.y / (g_2D.w * 8 + 1));
40+
var converage = max(g_2D.x / (g_2D.z * 80 + 1), g_2D.y / (g_2D.w * 80 + 1));
4241
let isX = g_2D.x >= g_2D.y;
4342

44-
let falloff = smoothstep(falloffEnd[gridMode] * far * endFactor, 0, dist);
43+
let falloff = smoothstep(far * endFactor, 0, dist);
4544
return converage * falloff;
4645
}
4746

@@ -52,7 +51,7 @@ fn fragment_main(vertexData: VertexOut) -> @location(0) vec4f {
5251
let abs_ls_pos = abs(vertexData.ls_position);
5352
let isInsideSmall = abs_ls_pos.x <= smallSize && abs_ls_pos.y <= smallSize;
5453

55-
if(largePass && isInsideSmall) {
54+
if((largePass && isInsideSmall) || (!largePass && !isInsideSmall)) {
5655
discard;
5756
}
5857
let lt = lineThickness * sqrt(getDPR());
@@ -63,7 +62,7 @@ fn fragment_main(vertexData: VertexOut) -> @location(0) vec4f {
6362
let g1000_2D = getGrid(ws_pos / 1000.0, lt);
6463

6564
let camPos = getCameraPosition();
66-
let dist = -vertexData.cs_position.z;
65+
let dist = length(vertexData.cs_position);
6766
let endFactor = smoothstep(0.0, 50.0, abs(camPos.z)) * 0.6 + 0.4;
6867
let far = view.projectionData[1];
6968
let a1 = getAlpha(g1_2D, dist, endFactor, 0, far);

src/wgsl/heightField.wgsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct VertexOut {
99
}
1010

1111
#include <utility/frame>
12+
#include <utility/r3>
1213

1314
const xVerts = 1000u;
1415
const yVerts = 1000u;
@@ -61,8 +62,9 @@ fn vertex_main(input: VertexIn) -> VertexOut {
6162
let domP1 = pos(mapToDomain(xNorm + epsilon, yNorm));
6263
let domP2 = pos(mapToDomain(xNorm, yNorm + epsilon));
6364
let ls_pos = vec3f(mapToLocal(xNorm, yNorm), domP0.z);
65+
let ws_pos = (vertexUniform.transformation * vec4f(ls_pos, 1)).xyz;
6466

65-
output.position = view.projectionMat * view.viewMat * vec4(ls_pos, 1);
67+
output.position = view.projectionMat * view.viewMat * vec4f(ws_pos, 1);
6668
output.ls_pos = ls_pos;
6769
output.normal = normalize(cross(domP2 - domP0, domP1 - domP0));
6870
return output;

src/wgsl/utility/r3.wgsl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct R3VertexUniform {
2+
transformation: mat4x4f,
3+
}
4+
struct R3FragmentUniform {
5+
state: u32
6+
}
7+
@group(1) @binding(0) var<uniform> vertexUniform: R3VertexUniform;
8+
@group(1) @binding(1) var<uniform> fragmentUniform: R3VertexUniform;

0 commit comments

Comments
 (0)