question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Samplers in struct uniform

See original GitHub issue
Description of the problem

I have a shader that is using a struct with a sampler2D field. I’ve had to make a change in three.js to make it work. Perhaps the change should be included in three.js?

I’m getting this crash:

Uncaught TypeError: Cannot read property 'allocTextureUnit' of undefined
    at SingleUniform.setValueT1 [as setValue] (three.js:4505)
    at StructuredUniform.setValue (three.js:4697)
    at Function.WebGLUniforms.upload (three.js:4826)
    at setProgram (three.js:22170)
    at WebGLRenderer.renderBufferDirect (three.js:20964)
    at renderObject (three.js:21722)
    at renderObjects (three.js:21695)
    at WebGLRenderer.render (three.js:21463)
    at render (app.js:2758)
    at Object.execute (app.js:2760)

This is from build/three.js (0.86 from npm) (removed some whitespace):

	StructuredUniform.prototype.setValue = function ( gl, value ) {

		// Note: Don't need an extra 'renderer' parameter, since samplers
		// are not allowed in structured uniforms.

		var seq = this.seq;
		for ( var i = 0, n = seq.length; i !== n; ++ i ) {
			var u = seq[ i ];
			u.setValue( gl, value[ u.id ] );
		}
	};

If I do add renderer as parameter and pass it along to u.setValue, everything works as expected:

	StructuredUniform.prototype.setValue = function ( gl, value, renderer ) {

		var seq = this.seq;

		for ( var i = 0, n = seq.length; i !== n; ++ i ) {
			var u = seq[ i ];
			u.setValue( gl, value[ u.id ], renderer );
		}
	};

Now, about the comment. Is it true that samplers in structs are not allowed? Because it really does work after this change. Is there a difference between browsers, OSs or GPUs regarding this? My understanding after browsing around a bit, is that samplers in structs are allowed as long as the struct is a uniform. So I think three.js should allow this too.

Three.js version
  • Dev
  • r86
Browser
  • All of them
  • Chrome
  • Firefox
  • Internet Explorer
OS
  • All of them
  • Windows
  • macOS
  • Linux
  • Android
  • iOS
Hardware Requirements (graphics card, VR Device, …)

Radeon Pro 560 4096 MB

EDIT: spelling & grammar

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
markusjohnssoncommented, Aug 5, 2017

No, there is a bug in Edge which causes it to not support samplers in (uniform) structs. However, all other browsers support it. But threejs does not allow it, in any browser. The simple change I made (seen above) makes it work with threejs though.

1reaction
markusjohnssoncommented, Jul 21, 2017

I did some further investigation. It seams like samplers in structs are allowed in Chrome on Windows too, but not in Edge. So I guess I’ll have to rewrite my shaders anyway.

UPDATE: I’ve opened a bug with Edge: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12874198/

I’m planning on rewriting my shader with an array of samplers instead and keeping the array index in the struct.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Samplers inside structs - OpenGL - Khronos Forums
Well, it's not explicitly stated, but it is implied by the fact that samplers must be uniforms (or function parameters) and uniforms must...
Read more >
GLSL sampler2D in struct - opengl - Stack Overflow
The uniform is actually material.tex in this example. Structs as uniforms are basically just a namespace sort of thing. It definitely is ...
Read more >
Images and samplers inside structs · Issue #164 - GitHub
I have a simple shader that's not compiling and throwing this assert: "Attempting to use arrays of separate images. This is not possible...
Read more >
Struct UniformSampler | Perception | 0.6.0-preview.1
Struct UniformSampler. Returns uniformly distributed random values within a designated range. Inherited Members. ValueType.Equals(Object).
Read more >
glium::uniforms::Sampler - Rust - Docs.rs
API documentation for the Rust `Sampler` struct in crate `glium`.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found