Samplers in struct uniform

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

Author: Fantashit

1 thought on “Samplers in struct uniform

  1. 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.

Comments are closed.