Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/game/assets/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ func (m *Manager) runReloads(ctx context.Context, jobs []*reloadJob) ([]Reloaded
j.fetch(ctx)
}
}()
// Waits on the engine goroutine instead of posting the apply back, because an automation tool handler can only answer synchronously (nsgo-dev/nsgo#2342).
select {
case <-done:
case <-ctx.Done():
Expand Down
1 change: 1 addition & 0 deletions packages/game/mcp/screenshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ func (b *Bridge) handleScreenshot(ctx context.Context, raw json.RawMessage) (any
}
res.Path = cleanRelPath
} else {
// png_base64 reaches an MCP client as text, not an image, because app tools cannot return image content (nsgo-dev/nsgo#2343).
res.PNGBase64 = base64.StdEncoding.EncodeToString(pngBytes)
}
return res, nil
Expand Down
2 changes: 1 addition & 1 deletion packages/game/render3d/aa.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ fn fs_main(in: FXAAVertexOut) -> @location(0) vec4<f32> {
let lumaCenterSmaller = lumaM < lumaLocalAvg;
let endLuma = select(lumaEnd2, lumaEnd1, isDir1);
let endSmaller = endLuma < 0.0;
// Spelled out instead of endSmaller != lumaCenterSmaller: NSGo v0.3.0's software VM compares bool operands as equal (cmpVal in internal/driver/gpu_software.go ignores irVal.b).
// Spelled out instead of endSmaller != lumaCenterSmaller: NSGo v0.3.0's software VM compares bool operands as equal (cmpVal in internal/driver/gpu_software.go ignores irVal.b) (nsgo-dev/nsgo#2335).
let correctVariation = (endSmaller && !lumaCenterSmaller) || (!endSmaller && lumaCenterSmaller);

var pixelOffset = 0.0;
Expand Down
2 changes: 1 addition & 1 deletion packages/game/render3d/clustered.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ func DeviceSupportsClusteredLighting(dev *gpu.Device) bool {
//
// It returns shade_pbr for the rig plus every local light binned into the cluster that holds the fragment, each shaded with pbr_brdf, point_light_attenuation and spot_cone_attenuation, so it matches [ShadeClusteredGridPoint]. frag_pos is the fragment's @builtin(position). Prepend [CameraWGSL], which supplies camera.view for the depth slice, and pair it with a [ClusteredLightSystem] in clustered mode; [ClusteredFallbackBindingsWGSL] declares the same helper for the fallback path.
//
// Depth slicing is a 6-step binary search over the uniform split-depth table rather than log2 and exp, because NSGo v0.3.0's software GPU VM has no log, log2, exp or exp2 builtin and silently evaluates them to zero (internal/driver/gpu_software.go, evalCall).
// Depth slicing is a 6-step binary search over the uniform split-depth table rather than log2 and exp, because NSGo v0.3.0's software GPU VM has no log, log2, exp or exp2 builtin and silently evaluates them to zero (internal/driver/gpu_software.go, evalCall) (nsgo-dev/nsgo#2334).
const ClusteredBindingsWGSL = LightWGSL + clusteredStorageWGSL

// ClusteredFallbackBindingsWGSL is [LightWGSL] plus the fallback uniform block at [LightGroup] binding 1 and shade_pbr_clustered with the same signature as in [ClusteredBindingsWGSL], using no storage buffers or compute, for WebGL 2 and OpenGL ES 3.0 targets. frag_pos is ignored: every uploaded light is evaluated for every fragment.
Expand Down
2 changes: 1 addition & 1 deletion packages/game/render3d/custom_material.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func computeCustomLayout(label string, params []ShaderParamDesc) (CustomUniformL
for i, p := range params {
if p.Kind.IsUniform() {
align, size := p.Kind.UniformLayout()
// WGSL packs a 4-byte member into the last 4 bytes of a preceding vec3, but NSGo's Metal backend declares the struct with float3 members, which take 16 bytes, so every member after that would be read from the wrong offset on macOS and iOS.
// WGSL packs a 4-byte member into the last 4 bytes of a preceding vec3, but NSGo's Metal backend declares the struct with float3 members, which take 16 bytes, so every member after that would be read from the wrong offset on macOS and iOS (nsgo-dev/nsgo#2340).
if prevVec3 != "" && align == 4 {
return CustomUniformLayout{}, &CustomMaterialError{
Material: label,
Expand Down
2 changes: 1 addition & 1 deletion packages/game/render3d/fog.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const FogWGSL = `struct Fog {
height_params: vec4<f32>,
}

// pow with base e instead of exp: NSGo v0.3.0's software GPU VM evaluates exp, exp2, log, log2 and atan as 0 (internal/driver/gpu_software.go evalCall has no case for them). Switch back to exp once NSGo handles it.
// pow with base e instead of exp: NSGo v0.3.0's software GPU VM evaluates exp, exp2, log, log2 and atan as 0 (internal/driver/gpu_software.go evalCall has no case for them) (nsgo-dev/nsgo#2334). Switch back to exp once NSGo handles it.
fn fog_exp(x: f32) -> f32 {
return pow(2.718281828, x);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/game/render3d/lightmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ func (m LightmapMaterial) Validate() error {
return nil
}

// LightmapWGSL is the WGSL shader module compiled by [NewLightmapRenderer] for a non-sRGB color target. It encodes the linear result with linear_to_srgb before writing, as [LitPBRWGSL] does, so lightmapped and PBR-lit draws in one pass match; for an sRGB target the renderer compiles a variant that leaves the encode to the hardware. It uses only pow and algebraic math (no exp/exp2/log2) so it executes identically across hardware GPU backends and NSGo's software GPU VM.
// LightmapWGSL is the WGSL shader module compiled by [NewLightmapRenderer] for a non-sRGB color target. It encodes the linear result with linear_to_srgb before writing, as [LitPBRWGSL] does, so lightmapped and PBR-lit draws in one pass match; for an sRGB target the renderer compiles a variant that leaves the encode to the hardware. It uses only pow and algebraic math (no exp/exp2/log2) so it executes identically across hardware GPU backends and NSGo's software GPU VM (nsgo-dev/nsgo#2334).
const LightmapWGSL = lightmapBodyWGSL + `
return vec4<f32>(linear_to_srgb(lit), albedo.a);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/game/render3d/local_shadows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (

var localShadowTestClear = core.RGB(0x0c1018)

// localShadowTestSubdividedQuad builds a grid of subdivisions x subdivisions triangles spanning center +/- uAxis*halfU +/- vAxis*halfV with outward normal. Subdividing large floors and walls ensures triangles never straddle w <= 0 of a point light's cube-face frustum in the software rasterizer.
// localShadowTestSubdividedQuad builds a grid of subdivisions x subdivisions triangles spanning center +/- uAxis*halfU +/- vAxis*halfV with outward normal. Subdividing large floors and walls ensures triangles never straddle w <= 0 of a point light's cube-face frustum in the software rasterizer (nsgo-dev/nsgo#2344).
func localShadowTestSubdividedQuad(center, uAxis, vAxis, normal math3d.Vec3, halfU, halfV float32, subdiv int) render3d.Mesh {
var m render3d.Mesh
n := normal.Norm()
Expand Down
2 changes: 1 addition & 1 deletion packages/game/render3d/material.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (d *MaterialDefaults) Layout() *gpu.BindGroupLayout {

// UnlitLayout returns the [MaterialGroup] bind group layout shared by every [GPUUnlitMaterial] created with d, for [NewUnlitPipeline].
//
// [MaterialDefaults.Valid] does not cover it: NSGo restores a bind group layout after a device loss only when a restored bind group uses it, so this layout comes back only while a retained [GPUUnlitMaterial] exists. Check its Valid method before building an unlit pipeline after a restore.
// [MaterialDefaults.Valid] does not cover it: NSGo restores a bind group layout after a device loss only when a restored bind group uses it, so this layout comes back only while a retained [GPUUnlitMaterial] exists (nsgo-dev/nsgo#2338). Check its Valid method before building an unlit pipeline after a restore.
func (d *MaterialDefaults) UnlitLayout() *gpu.BindGroupLayout {
if d == nil {
return nil
Expand Down
1 change: 1 addition & 0 deletions packages/game/render3d/post3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ func postDestFormat(dst *gpu.Texture, colorFmt, outFmt gpu.TextureFormat) (gpu.T
return outFmt, nil
}
f := dst.Desc().Format
// Checked here because gpu.RenderPass.SetPipeline does not compare a pipeline's target formats with the pass attachments (nsgo-dev/nsgo#2341).
if f != colorFmt && f != outFmt {
return gpu.FormatUndefined, fmt.Errorf("%w: destination format %s is neither ColorFormat %s nor OutputFormat %s", ErrInvalidPostProcess, f, colorFmt, outFmt)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/game/render3d/skin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
// SkinUniformSize is the byte size of the skin uniform block that [AppendJointMatrices] writes and [SkinWGSL] declares: MaxSkinJoints * 64 = 8192 bytes.
SkinUniformSize = MaxSkinJoints * 64

// The palette sits at each pipeline's first free group because gpu.PipelineDesc.Layouts refuses nil entries (nsgo-dev/nsgo#2337).

// SkinGroup is the bind group index of the joint palette ([GPUSkinPalette.BindGroup]) in [SkinWGSL] and the unlit [NewSkinnedMeshPipeline], which binds only [CameraGroup] and the palette. Lit and material pipelines keep group 1 for lights and materials, so they bind the palette at [LitPBRSkinGroup] or [MaterialSkinGroup] instead.
SkinGroup = 1

Expand Down
4 changes: 2 additions & 2 deletions packages/game/render3d/skybox.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
IBLUniformSize = 9*16 + 16
)

// SkyboxWGSL renders a cubemap as an infinitely distant background using a single fullscreen triangle driven by @builtin(vertex_index) (zero vertex buffers). It emits depth z = 0.99999994 (1 - 2^-24, the largest float32 below 1.0), so a pipeline with [gpu.CompareLessEqual] and DepthWrite disabled draws the skybox after opaque geometry without shading occluded pixels: only geometry whose depth is within 2^-24 of the far plane, below the precision of a 24-bit depth buffer, is painted over. Emitting exactly 1.0 would be the textbook choice, but NSGo's software rasterizer rejects fragments whose interpolated depth rounds above 1.0, which leaves clear-color holes in the sky.
// SkyboxWGSL renders a cubemap as an infinitely distant background using a single fullscreen triangle driven by @builtin(vertex_index) (zero vertex buffers). It emits depth z = 0.99999994 (1 - 2^-24, the largest float32 below 1.0), so a pipeline with [gpu.CompareLessEqual] and DepthWrite disabled draws the skybox after opaque geometry without shading occluded pixels: only geometry whose depth is within 2^-24 of the far plane, below the precision of a 24-bit depth buffer, is painted over. Emitting exactly 1.0 would be the textbook choice, but NSGo's software rasterizer rejects fragments whose interpolated depth rounds above 1.0, which leaves clear-color holes in the sky (nsgo-dev/nsgo#2344).
const SkyboxWGSL = `struct SkyboxParams {
inv_view_proj: mat4x4<f32>,
exposure: f32,
Expand Down Expand Up @@ -109,7 +109,7 @@ fn env_brdf_approx(f0: vec3<f32>, roughness: f32, n_dot_v: f32) -> vec3<f32> {
let c0 = vec4<f32>(-1.0, -0.0275, -0.572, 0.022);
let c1 = vec4<f32>(1.0, 0.0425, 1.04, -0.04);
let r = roughness * c0 + c1;
// pow(2.0, x) instead of exp2(x): NSGo v0.3.0's software GPU VM has no exp, exp2, log or log2 and evaluates them to zero.
// pow(2.0, x) instead of exp2(x): NSGo v0.3.0's software GPU VM has no exp, exp2, log or log2 and evaluates them to zero (nsgo-dev/nsgo#2334).
let a004 = min(r.x * r.x, pow(2.0, -9.28 * n_dot_v)) * r.x + r.y;
let ab = vec2<f32>(-1.04, 1.04) * a004 + r.zw;
return f0 * ab.x + vec3<f32>(ab.y);
Expand Down
6 changes: 3 additions & 3 deletions packages/game/render3d/tonemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func appendToneMapUniforms(dst []byte, opts ToneMapOptions, region toneMapRegion
//
// Entry points: vs_main (vertexless full-screen triangle via @builtin(vertex_index), drawn with pass.Draw(3, 0)) and fs_main (exposure, operator dispatch across None/Reinhard/ACES/Neutral/AgX, and exact sRGB or power-law gamma encoding).
//
// All math uses only operations supported across WebGPU, Metal, D3D11, OpenGL 3.3, WebGL 2, OpenGL ES 3.0, and NSGo's software GPU VM (no exp or log2 builtins).
// All math uses only operations supported across WebGPU, Metal, D3D11, OpenGL 3.3, WebGL 2, OpenGL ES 3.0, and NSGo's software GPU VM (no exp or log2 builtins) (nsgo-dev/nsgo#2334).
const ToneMapWGSL = `struct ToneMapParams {
exposure: f32,
inv_gamma: f32,
Expand Down Expand Up @@ -446,7 +446,7 @@ fn tonemap_neutral(color: vec3<f32>) -> vec3<f32> {
return clamp(mix(c, vec3<f32>(new_peak, new_peak, new_peak), vec3<f32>(g, g, g)), vec3<f32>(0.0, 0.0, 0.0), vec3<f32>(1.0, 1.0, 1.0));
}

// fast_log2_agx computes log2 over AgX's input range with power-of-two range reduction and an atanh series, because NSGo v0.3.0's software GPU has no case for the log2, exp2, log and exp builtins and silently returns 0 for them.
// fast_log2_agx computes log2 over AgX's input range with power-of-two range reduction and an atanh series, because NSGo v0.3.0's software GPU has no case for the log2, exp2, log and exp builtins and silently returns 0 for them (nsgo-dev/nsgo#2334).
fn fast_log2_agx(v: f32) -> f32 {
var m = clamp(v, 0.0001757761, 16.291573);
var e: f32 = 0.0;
Expand Down Expand Up @@ -529,7 +529,7 @@ fn tonemap_agx(c: vec3<f32>) -> vec3<f32> {
);
}

// sanitize_hdr maps NaN and non-positive values to 0 and clamps the rest to the largest finite half float, so overflowed RGBA16Float highlights (+Inf) and extreme exposures never reach the operators as Inf or NaN. It tests NaN with a strict comparison, which is false for NaN under IEEE 754, instead of relying on how max and min treat NaN; NSGo v0.3.0's software VM also treats NaN as equal to every value, so >= and <= would let it through there.
// sanitize_hdr maps NaN and non-positive values to 0 and clamps the rest to the largest finite half float, so overflowed RGBA16Float highlights (+Inf) and extreme exposures never reach the operators as Inf or NaN. It tests NaN with a strict comparison, which is false for NaN under IEEE 754, instead of relying on how max and min treat NaN; NSGo v0.3.0's software VM also treats NaN as equal to every value, so >= and <= would let it through there (nsgo-dev/nsgo#2335).
fn sanitize_hdr(x: f32) -> f32 {
if (x > 0.0) {
return min(x, 65504.0);
Expand Down
4 changes: 2 additions & 2 deletions packages/game/render3d/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (d RenderTextureDesc) effectiveSampleCount() int {
// Its underlying [*gpu.RenderTarget] belongs to the [*core.Scope] passed to [NewRenderTexture] and is released when that scope is disposed or earlier via [RenderTexture.Close]. Use it from the engine goroutine, like the [gpu.Device] it came from.
type RenderTexture struct {
cx *core.Scope
// owner is a child of cx that holds only the current target's cleanups. gpu registers a cleanup on the scope for every texture it creates and closing the texture early does not remove it, so Resize disposes the old target's child scope instead of letting cx collect one set of cleanups per resize. It is nil when cx is nil.
// owner is a child of cx that holds only the current target's cleanups. gpu registers a cleanup on the scope for every texture it creates and closing the texture early does not remove it, so Resize disposes the old target's child scope instead of letting cx collect one set of cleanups per resize (nsgo-dev/nsgo#2339). It is nil when cx is nil.
owner *core.Scope
target *gpu.RenderTarget
gpuTex *GPUTexture
Expand Down Expand Up @@ -877,7 +877,7 @@ func (r *ViewportRenderer) RenderFrame(frame *gpu.Frame, surface SurfaceDesc, vi
return r.renderWith(frame.Device(), surface, views, frame.BeginRenderPass, draw)
}

// RenderEncoder renders all active views in execution order (offscreen [RenderTexture] views first, then main-surface viewports) into enc (for example on [gpu.NewSoftwareRenderer]). dev is the device enc was begun on: a device restored after a loss is a new [*gpu.Device], and per-view camera bind groups created on any other device are dropped by the pass. The caller submits enc.Finish().
// RenderEncoder renders all active views in execution order (offscreen [RenderTexture] views first, then main-surface viewports) into enc (for example on [gpu.NewSoftwareRenderer]). dev is the device enc was begun on: a device restored after a loss is a new [*gpu.Device], and per-view camera bind groups created on any other device are dropped by the pass (nsgo-dev/nsgo#2345). The caller submits enc.Finish().
func (r *ViewportRenderer) RenderEncoder(dev *gpu.Device, enc *gpu.Encoder, surface SurfaceDesc, views []CameraView, draw func(ViewportContext)) error {
if enc == nil {
return gpu.Error{Code: gpu.ErrDeviceLost, Message: "render3d: nil encoder"}
Expand Down
Loading