@@ -515,36 +515,55 @@ class Shader::Setup::MemberData : public IECore::RefCounted
515515 struct TextureValue : public Value
516516 {
517517
518- TextureValue ( GLuint uniformIndex, GLuint textureUnit, ConstTexturePtr texture )
519- : m_uniformIndex( uniformIndex ), m_textureUnit( textureUnit ), m_texture( texture )
518+ TextureValue ( GLuint uniformIndex, GLuint textureUnit, GLenum targetType, ConstTexturePtr texture )
519+ : m_uniformIndex( uniformIndex ), m_textureUnit( textureUnit ), m_targetType( targetType ), m_texture( texture )
520520 {
521521 }
522522
523523 void bind () override
524524 {
525525 glActiveTexture ( GL_TEXTURE0 + m_textureUnit );
526- glGetIntegerv ( GL_TEXTURE_BINDING_2D, &m_previousTexture );
526+
527+ if ( m_targetType == GL_TEXTURE_1D )
528+ {
529+ glGetIntegerv ( GL_TEXTURE_BINDING_1D, &m_previousTexture );
530+ }
531+ else if ( m_targetType == GL_TEXTURE_2D )
532+ {
533+ glGetIntegerv ( GL_TEXTURE_BINDING_2D, &m_previousTexture );
534+ }
535+ else if ( m_targetType == GL_TEXTURE_3D )
536+ {
537+ glGetIntegerv ( GL_TEXTURE_BINDING_3D, &m_previousTexture );
538+ }
539+ else
540+ {
541+ throw IECore::Exception ( " Cannot bind texture in Shader::Setup with target type " + std::to_string ( m_targetType ) );
542+ }
543+
527544 if ( m_texture )
528545 {
529- m_texture->bind ( );
546+ glBindTexture ( m_targetType, m_texture->m_texture );
530547 }
531548 else
532549 {
533- glBindTexture ( GL_TEXTURE_2D , 0 );
550+ glBindTexture ( m_targetType , 0 );
534551 }
552+
535553 glUniform1i ( m_uniformIndex, m_textureUnit );
536554 }
537555
538556 void unbind () override
539557 {
540558 glActiveTexture ( GL_TEXTURE0 + m_textureUnit );
541- glBindTexture ( GL_TEXTURE_2D , m_previousTexture );
559+ glBindTexture ( m_targetType , m_previousTexture );
542560 }
543561
544562 private :
545563
546564 GLuint m_uniformIndex;
547565 GLuint m_textureUnit;
566+ GLenum m_targetType;
548567 ConstTexturePtr m_texture;
549568 GLint m_previousTexture;
550569
@@ -668,14 +687,38 @@ const Shader *Shader::Setup::shader() const
668687void Shader::Setup::addUniformParameter ( const std::string &name, ConstTexturePtr value )
669688{
670689 const Parameter *p = m_memberData->shader ->uniformParameter ( name );
671- if ( !p || ( p->type != GL_SAMPLER_2D && p->type != GL_INT_SAMPLER_2D && p->type != GL_UNSIGNED_INT_SAMPLER_2D ) )
690+
691+ GLenum targetType;
692+ if ( !p )
693+ {
694+ // No target uniform to bind to
695+ return ;
696+ }
697+ else if ( p->type == GL_SAMPLER_2D || p->type == GL_INT_SAMPLER_2D || p->type == GL_UNSIGNED_INT_SAMPLER_2D )
698+ {
699+ // This is the kind of texture that is currently officially supported by texture.
700+ targetType = GL_TEXTURE_2D;
701+ }
702+ else if ( p->type == GL_SAMPLER_3D || p->type == GL_INT_SAMPLER_3D || p->type == GL_UNSIGNED_INT_SAMPLER_3D )
703+ {
704+ // We haven't yet officially added support for 3D textures because we are currently maintaining ABI,
705+ // but we support 3D textures in Setup.
706+ // \todo : Add targetType to Texture, and check here that the targetType of the Texture matches
707+ // the corresponding uniform, rather than just overriding to force a match because we don't know what
708+ // target the texture is intended to have
709+ targetType = GL_TEXTURE_3D;
710+ }
711+ else if ( p->type == GL_SAMPLER_1D || p->type == GL_INT_SAMPLER_1D || p->type == GL_UNSIGNED_INT_SAMPLER_1D )
712+ {
713+ targetType = GL_TEXTURE_1D;
714+ }
715+ else
672716 {
673- // We can only support 2D samplers, because `Texture::bind()`
674- // unconditionally uses the GL_TEXTURE_2D target.
717+ // Target uniform not of supported type
675718 return ;
676719 }
677720
678- m_memberData->values .push_back ( new MemberData::TextureValue ( p->location , p->textureUnit , value ) );
721+ m_memberData->values .push_back ( new MemberData::TextureValue ( p->location , p->textureUnit , targetType, value ) );
679722}
680723
681724template <typename Container>
0 commit comments