Raylib-Go-Wasm icon indicating copy to clipboard operation
Raylib-Go-Wasm copied to clipboard

BeginShaderMode call doesn't run shaders

Open deoxyimran opened this issue 1 month ago • 1 comments

I successfully managed to compile and load custom shaders but can't seem to run any with BeginShaderMode (getting Panic: invalid value when passing uniforms). Are the shader functions not fully implemented/buggy?

some code snippets:

// Loads appropriate assets for a given level
func (a *AssetManager) LoadAssets(level int) {
	switch level {
	case 1:
		a.Shaders["environment"] = rl.LoadShader("assets/shaders/environment.vs", "assets/shaders/environment.fs")
		// if a.ShaderLocs["environment"] == nil {
		// 	a.ShaderLocs["environment"] = make([]int32, rl.MaxShaderLocations)
		// }
		// a.ShaderLocs["environment"][rl.ShaderLocVectorView] = rl.GetShaderLocation(a.Shaders["environment"], "viewPoss")
		a.Models["battleground1"] = rl.LoadModel("assets/models/battleground1.glb")
	default:
	}
	a.assetsLoaded = true
}

func (m *Map) Render() {
	switch m.level {
	case 1:
		rl.BeginShaderMode(m.assetManager.Shaders["environment"])
		rl.DrawModelEx(
			m.assetManager.Models["battleground1"],
			rl.Vector3{0, 0, 0},
			rl.Vector3{0, 1, 0},
			90,
			rl.Vector3{1, 1, 1},
			rl.White,
		)
		rl.EndShaderMode()
	default:
	}
}

deoxyimran avatar Nov 30 '25 08:11 deoxyimran

Yeah, I dont think shaders are implemented properly. Let me try to reproduce this.

BrownNPC avatar Nov 30 '25 11:11 BrownNPC

I've figured out the issue. But the problem is I do not know how I can make it possible to pass uniforms without making it extremely slow and unusable.

I had this issue previously here

It's basically not safe to share memory between 2 wasm modules. so we have to copy our Go data to JS, then convert that JS data to C. Some primitives can be passed as-is to C, like ints and floats. But things like strings and arrays need to be converted.

Passing a float32 array to C would require us to first use malloc then copy the golang float32 array's bytes into that C array (very slow operation), passing that C array to SetShaderValue, deallocating the C array.

I think I might be hitting a wall with how these bindings are implemented. I might have to look for other ways to get this done. Shared memory would be ideal. But if not, then I will have to implement heavy caching to minimize memory allocations. it would be a ton of work.

BrownNPC avatar Dec 04 '25 23:12 BrownNPC