Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Unable to set sampler states when get the shader's sampler parameter.

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2012
    Posts
    9

    Unable to set sampler states when get the shader's sampler parameter.

    I can set my texture with the code below but the part that sets the sampler parameters has no effect on the texture drawn (it is still linear lookup)

    Code :
    	// Get sampler
    	GLuint textureSamplerParam = glGetUniformLocation(m_effect, "AtlasTextureSampler");
     
    	// Bind our texture in Texture Unit 0
    	glActiveTexture(GL_TEXTURE0);
    	glBindTexture(GL_TEXTURE_2D, texture);
     
    	// Bind the texture to the sampler
    	glBindSampler(texture, textureSamplerParam);
     
    	// Set sampler parameters
    	glSamplerParameteri(textureSamplerParam, GL_TEXTURE_WRAP_S, GL_CLAMP);
    	glSamplerParameteri(textureSamplerParam, GL_TEXTURE_WRAP_T, GL_CLAMP);
    	glSamplerParameteri(textureSamplerParam, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    	glSamplerParameteri(textureSamplerParam, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);

    I'm currently automating my texture loading through the glload library, does that affect the texture at all?

    Code :
    	    std::string filename(LOCAL_FILE_DIR);
    	    filename.append("TextureAtlasTexture.png");
     
    		// 'glload' must be initialised for 'glimg::CreateTexture' to work
    		if (glload::LoadFunctions() == glload::LS_LOAD_FAILED) 
    		{ 
    			// Initialisation failed
    		}
     
    		std::auto_ptr<glimg::ImageSet> pImgSet(glimg::loaders::stb::LoadFromFile(filename));
     
    		m_texture = glimg::CreateTexture(pImgSet.get(), 0);

    I have tried creating it myself but that returns nothing but an array of 0's. The dimensions of the array match that of the image though.

    Code :
    		std::auto_ptr<glimg::ImageSet> pImgSet(glimg::loaders::stb::LoadFromFile(filename));	
     
    		glimg::SingleImage image = pImgSet->GetImage(0, 0, 0);
    		glimg::Dimensions dims = image.GetDimensions();
     
    		// Allocate a new texture
    		glGenTextures(1, &m_texture);
     
    		// Bind the new texture
    		glBindTexture(GL_TEXTURE_2D, m_texture);
     
    		// Bind the data to the currently bound texture
    		glTexImage2D(
    			GL_TEXTURE_2D, 
    			0, 
    			GL_RGBA8, 
    			dims.width, 
    			dims.height, 
    			0,
    			GL_RED, 
    			GL_UNSIGNED_BYTE, 
    			image.GetImageData()     // Returns nothing but an array of '0'
    			);
     
    		// Automatically generate mipmaps
    		glGenerateMipmap(GL_TEXTURE_2D);
     
    		// Unbind texture
    		glBindTexture(GL_TEXTURE_2D, 0);

  2. #2
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    6,008
    Code :
    glBindSampler(texture, textureSamplerParam);

    You don't bind samplers to texture objects. You bind them to the context. The first parameter to `glBindSampler` is the texture unit you want to bind the sampler to, not a texture object name.

    Also, you don't have to bind samplers in order to modify them. That's why `glSamplerParameter` takes a sampler object and not a context target. So there's no reason to constantly change your sampler object in your rendering function. You should set it up once, after you create it, then just bind it when it's time to render.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2012
    Posts
    9
    Thanks Alfonse, let me see if I've understood you correctly.

    My texture setting function now looks like

    Code :
    void QuadrangleForward::setTexture(GLuint texture)
    {
    	// Texture is set separately as it doesn't always need to be set each time 'apply' is called
     
    	// Get sampler
    	GLuint textureSamplerParam = glGetUniformLocation(m_effect, "AtlasTextureSampler");
     
    	// Set sampler texture
    	glActiveTexture(GL_TEXTURE0);  
    	glBindTexture(GL_TEXTURE_2D , texture);  
    	glUniform1i(textureSamplerParam, 0);  
    }

    The sampler states are set to the texture

    Code :
    		// Select texture object  
    		glBindTexture(GL_TEXTURE_2D , m_texture);  
     
    		// Set sampler states  
    		glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP);  
    		glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP);  
    		glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST);  
    		glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST_MIPMAP_NEAREST);  
     
    		// Switch to default texture object  
    		glBindTexture(GL_TEXTURE_2D , 0);

    Which leaves me with the next question. How can I change the sampler state for different draw calls using the following?

    Code :
    		glSamplerParameteri(textureSamplerParam, GL_TEXTURE_WRAP_S, GL_CLAMP);
    		glSamplerParameteri(textureSamplerParam, GL_TEXTURE_WRAP_T, GL_CLAMP);
    		glSamplerParameteri(textureSamplerParam, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    		glSamplerParameteri(textureSamplerParam, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);

    EDIT:

    I've got it.... Sampler Objects! Forget the last question

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •