Skip to content

Commit 198c48a

Browse files
Merge pull request #1282 from danieldresser-ie/shaderSetupRaw
Allow Manually Created Textures in IECoreGL::Shader::Setup
2 parents 2dcd943 + 1b3976a commit 198c48a

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

include/IECoreGL/Shader.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ class IECOREGL_API Shader : public IECore::RunTimeTyped
158158

159159
const Shader *shader() const;
160160

161+
/// \todo : This function currently has a special hidden feature to compensate for not being
162+
/// able to change ABI at the moment. While Texture itself currently only supports 2D textures,
163+
/// if you pass a Texture into this function, it will just be treated as a container for a GL
164+
/// texture ID, and the target type for the texture will be deduced from the type of the
165+
/// corresponding sampler uniform. Once we can add targetType to Texture, we can clean all
166+
/// this up.
161167
void addUniformParameter( const std::string &name, ConstTexturePtr value );
162168
void addUniformParameter( const std::string &name, IECore::ConstDataPtr value );
163169
/// Binds the specified value to the named vertex attribute. The divisor will be passed to

include/IECoreGL/Texture.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class IECOREGL_API Texture : public Bindable
9696
/// Derived classes must set this in their constructor.
9797
GLuint m_texture;
9898

99+
// \todo : Needed by hack in Shader::Setup::MemberData::TextureValue until we are able
100+
// to break ABI and add targetType support to Texture
101+
friend class Shader;
102+
99103
};
100104

101105
IE_CORE_DECLAREPTR( Texture );

src/IECoreGL/Shader.cpp

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
668687
void 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

681724
template<typename Container>

0 commit comments

Comments
 (0)