Exemplo n.º 1
1
/** Try to save the last texture sent to glTexImage2D in a file of the given
*   file name. This function should only be used for textures sent to
*   glTexImage2D with a compressed internal format as argument.<br>
*   \note The following format is used to save the compressed texture:<br>
*         <internal-format><width><height><size><data> <br>
*         The first four elements are integers and the last one is stored
*         on \c size bytes.
*   \see loadCompressedTexture
*/
void saveCompressedTexture(const std::string& compressed_tex)
{
    int internal_format, width, height, size, compressionSuccessful;
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, (GLint *)&internal_format);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint *)&width);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint *)&height);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, (GLint *)&compressionSuccessful);
    if (!compressionSuccessful)
        return;
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, (GLint *)&size);

    char *data = new char[size];
    glGetCompressedTexImage(GL_TEXTURE_2D, 0, (GLvoid*)data);
    std::ofstream ofs(compressed_tex.c_str(), std::ios::out | std::ios::binary);
    if (ofs.is_open())
    {
        ofs.write((char*)&internal_format, sizeof(int));
        ofs.write((char*)&width, sizeof(int));
        ofs.write((char*)&height, sizeof(int));
        ofs.write((char*)&size, sizeof(int));
        ofs.write(data, size);
        ofs.close();
    }
    delete[] data;
}
Exemplo n.º 2
0
bool Texture2D::GetData(unsigned level, void* dest) const
{
    if (!object_.name_ || !graphics_)
    {
        URHO3D_LOGERROR("No texture created, can not get data");
        return false;
    }

#ifndef GL_ES_VERSION_2_0
    if (!dest)
    {
        URHO3D_LOGERROR("Null destination for getting data");
        return false;
    }

    if (level >= levels_)
    {
        URHO3D_LOGERROR("Illegal mip level for getting data");
        return false;
    }

    if (graphics_->IsDeviceLost())
    {
        URHO3D_LOGWARNING("Getting texture data while device is lost");
        return false;
    }

    if (multiSample_ > 1 && !autoResolve_)
    {
        URHO3D_LOGERROR("Can not get data from multisampled texture without autoresolve");
        return false;
    }
    
    if (resolveDirty_)
        graphics_->ResolveToTexture(const_cast<Texture2D*>(this));

    graphics_->SetTextureForUpdate(const_cast<Texture2D*>(this));

    if (!IsCompressed())
        glGetTexImage(target_, level, GetExternalFormat(format_), GetDataType(format_), dest);
    else
        glGetCompressedTexImage(target_, level, dest);

    graphics_->SetTexture(0, 0);
    return true;
#else
    // Special case on GLES: if the texture is a rendertarget, can make it current and use glReadPixels()
    if (usage_ == TEXTURE_RENDERTARGET)
    {
        graphics_->SetRenderTarget(0, const_cast<Texture2D*>(this));
        // Ensure the FBO is current; this viewport is actually never rendered to
        graphics_->SetViewport(IntRect(0, 0, width_, height_));
        glReadPixels(0, 0, width_, height_, GetExternalFormat(format_), GetDataType(format_), dest);
        return true;
    }

    URHO3D_LOGERROR("Getting texture data not supported");
    return false;
#endif
}
Exemplo n.º 3
0
	void OGLTexture1D::Map1D(uint32_t array_index, uint32_t level, TextureMapAccess tma, uint32_t x_offset, uint32_t /*width*/, void*& data)
	{
		last_tma_ = tma;

		uint32_t const texel_size = NumFormatBytes(format_);

		uint8_t* p;
		OGLRenderEngine& re = *checked_cast<OGLRenderEngine*>(&Context::Instance().RenderFactoryInstance().RenderEngineInstance());
		switch (tma)
		{
		case TMA_Read_Only:
		case TMA_Read_Write:
			{
				GLint gl_internalFormat;
				GLenum gl_format;
				GLenum gl_type;
				OGLMapping::MappingFormat(gl_internalFormat, gl_format, gl_type, format_);

				re.BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
				re.BindBuffer(GL_PIXEL_PACK_BUFFER, pbos_[array_index * num_mip_maps_ + level]);

				re.BindTexture(0, target_type_, texture_);
				if (IsCompressedFormat(format_))
				{
					glGetCompressedTexImage(target_type_, level, nullptr);
					p = static_cast<uint8_t*>(glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY));
				}
				else
				{
					glGetTexImage(target_type_, level, gl_format, gl_type, nullptr);
					p = static_cast<uint8_t*>(glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY));
				}
			}
			break;

		case TMA_Write_Only:
			re.BindBuffer(GL_PIXEL_PACK_BUFFER, 0);
			re.BindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[array_index * num_mip_maps_ + level]);
			p = static_cast<uint8_t*>(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY));
			break;

		default:
			BOOST_ASSERT(false);
			p = nullptr;
			break;
		}

		if (IsCompressedFormat(format_))
		{
			uint32_t const block_size = NumFormatBytes(format_) * 4;
			data = p + (x_offset / 4 * block_size);
		}
		else
		{
			data = p + x_offset * texel_size;
		}
	}
Exemplo n.º 4
0
static bool
check_texture(GLuint texture, unsigned level,
	      const struct texture_format *format, const unsigned char *data)
{
	int i, j, k;
	bool pass = true;
	unsigned char *tex_data;
	float passrate;

	tex_data = malloc(TEX_SIZE * TEX_SIZE * format->bytes);

	glBindTexture(GL_TEXTURE_2D, texture);
	if (format->block_width != 1 || format->block_height != 1) {
		/* Compressed */
		glGetCompressedTexImage(GL_TEXTURE_2D, level, tex_data);
	} else {
		glGetTexImage(GL_TEXTURE_2D, level, format->format,
			      format->data_type, tex_data);
	}

	passrate = 0;
	for (j = 0; j < TEX_SIZE; ++j) {
	        for (i = 0; i < TEX_SIZE; ++i) {
			if (memcmp(tex_data + ((j * TEX_SIZE) + i) * format->bytes,
				   data + ((j * TEX_SIZE) + i) * format->bytes,
				   format->bytes) == 0) {
				passrate += 1;
			} else {
				fprintf(stdout, "texel mismatch at (%d, %d); expected 0x",
					i, j);
				for (k = format->bytes - 1; k >= 0; --k)
					fprintf(stdout, "%02x", data[((j * TEX_SIZE) + i) * format->bytes + k]);

				fprintf(stdout, ", received 0x");
				for (k = format->bytes - 1; k >= 0; --k)
					fprintf(stdout, "%02x", tex_data[((j * TEX_SIZE) + i) * format->bytes + k]);
				fprintf(stdout, ".\n");

				pass = false;
			}
		}
	}
	passrate /= TEX_SIZE * TEX_SIZE;
	printf("%0.1f%% of pixels match\n", passrate * 100);

	free(tex_data);

	return pass;
}
Exemplo n.º 5
0
bool Texture2DArray::GetData(unsigned layer, unsigned level, void* dest) const
{
#ifndef GL_ES_VERSION_2_0
    if (!object_.name_ || !graphics_)
    {
        URHO3D_LOGERROR("Texture array not created, can not get data");
        return false;
    }

    if (!dest)
    {
        URHO3D_LOGERROR("Null destination for getting data");
        return false;
    }

    if (layer != 0)
    {
        URHO3D_LOGERROR("Only the full download of the array is supported, set layer=0");
        return false;
    }

    if (level >= levels_)
    {
        URHO3D_LOGERROR("Illegal mip level for getting data");
        return false;
    }

    if (graphics_->IsDeviceLost())
    {
        URHO3D_LOGWARNING("Getting texture data while device is lost");
        return false;
    }

    graphics_->SetTextureForUpdate(const_cast<Texture2DArray*>(this));

    if (!IsCompressed())
        glGetTexImage(target_, level, GetExternalFormat(format_), GetDataType(format_), dest);
    else
        glGetCompressedTexImage(target_, level, dest);

    graphics_->SetTexture(0, 0);
    return true;
#else
    URHO3D_LOGERROR("Getting texture data not supported");
    return false;
#endif
}
Exemplo n.º 6
0
bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
{
    if (!object_ || !graphics_)
    {
        URHO3D_LOGERROR("No texture created, can not get data");
        return false;
    }

    if (!dest)
    {
        URHO3D_LOGERROR("Null destination for getting data");
        return false;
    }

    if (level >= levels_)
    {
        URHO3D_LOGERROR("Illegal mip level for getting data");
        return false;
    }

    if (graphics_->IsDeviceLost())
    {
        URHO3D_LOGWARNING("Getting texture data while device is lost");
        return false;
    }
    if (multiSample_ > 1 && !autoResolve_)
    {
        URHO3D_LOGERROR("Can not get data from multisampled texture without autoresolve");
        return false;
    }

    if (resolveDirty_)
        graphics_->ResolveToTexture(const_cast<TextureCube*>(this));

    graphics_->SetTextureForUpdate(const_cast<TextureCube*>(this));

    if (!IsCompressed())
        glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, GetExternalFormat(format_), GetDataType(format_), dest);
    else
        glGetCompressedTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, dest);

    graphics_->SetTexture(0, nullptr);
    return true;
}
Exemplo n.º 7
0
static bool
check_texture(GLuint texture, unsigned level,
	      const struct texture_format *format, const unsigned char *data)
{
	int i, j, k;
	bool pass = true;
	unsigned char *tex_data;

	tex_data = malloc(TEX_SIZE * TEX_SIZE * format->bytes);

	glBindTexture(GL_TEXTURE_2D, texture);
	if (format->block_width != 1 || format->block_height != 1) {
		/* Compressed */
		glGetCompressedTexImage(GL_TEXTURE_2D, level, tex_data);
	} else {
		glGetTexImage(GL_TEXTURE_2D, level, format->format,
			      format->data_type, tex_data);
	}

	for (j = 0; j < TEX_SIZE; ++j) {
	        for (i = 0; i < TEX_SIZE; ++i) {
			int pos = ((j * TEX_SIZE) + i) * format->bytes;
			if (!pixels_equal(tex_data + pos, data + pos, format)) {
				fprintf(stdout, "texel mismatch at (%d, %d); expected 0x",
					i, j);
				for (k = format->bytes - 1; k >= 0; --k)
					fprintf(stdout, "%02x", data[pos + k]);

				fprintf(stdout, ", received 0x");
				for (k = format->bytes - 1; k >= 0; --k)
					fprintf(stdout, "%02x", tex_data[pos + k]);
				fprintf(stdout, ".\n");

				pass = false;
				i = j = TEX_SIZE; /* exit loops upon error */
			}
		}
	}

	free(tex_data);

	return pass;
}
Exemplo n.º 8
0
	void GLTextureBuffer::download(const PixelData &data)
	{
		if(data.getWidth() != getWidth() || data.getHeight() != getHeight() || data.getDepth() != getDepth())
			BS_EXCEPT(InvalidParametersException, "only download of entire buffer is supported by GL");

		glBindTexture( mTarget, mTextureID );
		if(PixelUtil::isCompressed(data.getFormat()))
		{
			if(data.getFormat() != mFormat || !data.isConsecutive())
				BS_EXCEPT(InvalidParametersException, 
				"Compressed images must be consecutive, in the source format");

			// Data must be consecutive and at beginning of buffer as PixelStorei not allowed
			// for compressed formate
			glGetCompressedTexImage(mFaceTarget, mLevel, data.getData());
		} 
		else
		{
			if(data.getWidth() != data.getRowPitch())
				glPixelStorei(GL_PACK_ROW_LENGTH, data.getRowPitch());

			if(data.getHeight()*data.getWidth() != data.getSlicePitch())
				glPixelStorei(GL_PACK_IMAGE_HEIGHT, (data.getSlicePitch()/data.getWidth()));

			if(data.getLeft() > 0 || data.getTop() > 0 || data.getFront() > 0)
				glPixelStorei(GL_PACK_SKIP_PIXELS, data.getLeft() + data.getRowPitch() * data.getTop() + data.getSlicePitch() * data.getFront());

			if((data.getWidth()*PixelUtil::getNumElemBytes(data.getFormat())) & 3)
				glPixelStorei(GL_PACK_ALIGNMENT, 1);

			// We can only get the entire texture
			glGetTexImage(mFaceTarget, mLevel, GLPixelUtil::getGLOriginFormat(data.getFormat()), 
				GLPixelUtil::getGLOriginDataType(data.getFormat()), data.getData());

			// Restore defaults
			glPixelStorei(GL_PACK_ROW_LENGTH, 0);
			glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
			glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
			glPixelStorei(GL_PACK_ALIGNMENT, 4);
		}

		BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_Texture);
	}
Exemplo n.º 9
0
bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
{
    #ifndef GL_ES_VERSION_2_0
    if (!object_ || !graphics_)
    {
        LOGERROR("No texture created, can not get data");
        return false;
    }
    
    if (!dest)
    {
        LOGERROR("Null destination for getting data");
        return false;
    }
    
    if (level >= levels_)
    {
        LOGERROR("Illegal mip level for getting data");
        return false;
    }
    
    if (graphics_->IsDeviceLost())
    {
        LOGWARNING("Getting texture data while device is lost");
        return false;
    }
    
    graphics_->SetTextureForUpdate(const_cast<TextureCube*>(this));
    
    if (!IsCompressed())
        glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, GetExternalFormat(format_), GetDataType(format_), dest);
    else
        glGetCompressedTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, dest);
    
    graphics_->SetTexture(0, 0);
    return true;
    #else
    LOGERROR("Getting texture data not supported");
    return false;
    #endif
}
Exemplo n.º 10
0
static enum piglit_result
call_CompressedTexSubImage_when_texture_is_referenced(void *data)
{
	void *compressed;
	GLuint tex;
	GLint size;

	tex = piglit_rgbw_texture(GL_COMPRESSED_RGBA_BPTC_UNORM, 16, 16,
				  GL_FALSE, GL_FALSE, GL_UNSIGNED_NORMALIZED);

	glGetTextureHandleARB(tex);
	if (!piglit_check_gl_error(GL_NO_ERROR))
		return PIGLIT_FAIL;

	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
				 GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &size);

	compressed = malloc(size);
	glGetCompressedTexImage(GL_TEXTURE_2D, 0, compressed);

	/* The ARB_bindless_texture spec says:
	 *
	 * "The contents of the images in a texture object may still be
	 *  updated via commands such as TexSubImage*, CopyTexSubImage*, and
	 *  CompressedTexSubImage*, and by rendering to a framebuffer object,
	 *  even if the texture object is referenced by one or more texture
	 *  handles."
	 */
	glCompressedTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 16, 16,
				  GL_COMPRESSED_RGBA_BPTC_UNORM, size,
				  compressed);
	free(compressed);
	if (!piglit_check_gl_error(GL_NO_ERROR))
		return PIGLIT_FAIL;

	return PIGLIT_PASS;
}
Exemplo n.º 11
0
	void GLtexture::saveToFile(const String& filename) {
		glBindTexture(GL_TEXTURE_2D, getObject());

		int width, height;
		int internal_format;

		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format);

		TexFormat texformat = trInternalFormat(internal_format);
		Image result;
		result.initImage(texformat, width, height);
		void* data = Malloc(result.getDataSize(0));

		int level = 0;
		while (1) {
			int imagesize;
			glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
			glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_HEIGHT, &height);
			if (!width) {
				break;
			}
			if (texformat.isCompressed()) {
				glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &imagesize);
				glGetCompressedTexImage(GL_TEXTURE_2D, level, data);
				result.setData(level, data, imagesize);
			} else {
				glGetTexImage(GL_TEXTURE_2D, level, GL_BGRA, GL_UNSIGNED_BYTE, data);
				result.setData(level, data, imagesize);
			}
			level++;
		}
		Free(data);
		result.saveFile_dds(filename);
	}
Exemplo n.º 12
0
static bool
test_format(int width, int height, GLfloat *image, GLenum requested_format)
{
	GLubyte *compressed_image;
	GLenum format2;
	int x, y, w, h;
	GLuint tex;
	bool pass = true;
	GLuint expected_size;
	GLint is_compressed;
	GLint compressed_size;
	GLint format;

	glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
	glPixelStorei(GL_UNPACK_ROW_LENGTH, width);

	/* Setup initial texture */
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, requested_format, width, height, 0,
		     GL_RGBA, GL_FLOAT, image);

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = check_rendering(width, height) && pass;

	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED,
				 &is_compressed);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT,
				 &format);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0,
				 GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
				 &compressed_size);

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	if (!is_compressed) {
		printf("Image was not compressed\n");
		pass = false;
	}

	if (format != requested_format) {
		printf("Internal Format mismatch. Found: 0x%04x Expected: 0x%04x\n",
		       format, requested_format);
		pass = false;
	}

	expected_size = piglit_compressed_image_size(requested_format, width,
			height);

	if (compressed_size != expected_size) {
		printf("Compressed image size mismatch. Found: %u Expected: %u\n",
		       compressed_size, expected_size);
		pass = false;
	}

	/* Use GL_TEXTURE_COMPRESSED_IMAGE_SIZE even if it wasn't what we
	 * expected to avoid corruption due to under-allocated buffer.
	 */
	compressed_image = malloc(compressed_size);

	/* Read back the compressed image data */
	glGetCompressedTexImage(GL_TEXTURE_2D, 0, compressed_image);

	/* Try texsubimage on 4-texel boundary - should work */
	x = 20;
	y = 12;
	w = 16;
	h = 8;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
	glTexSubImage2D(GL_TEXTURE_2D, 0,
			x, y, w, h,
			GL_RGBA, GL_FLOAT, image);

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = check_rendering(width, height) && pass;

	/* Try texsubimage on non 4-texel boundary - should not work */
	x = 10;
	y = 11;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
	glTexSubImage2D(GL_TEXTURE_2D, 0,
			x, y, w, h,
			GL_RGBA, GL_FLOAT, image);

	pass = piglit_check_gl_error(GL_INVALID_OPERATION) && pass;

	/* Try compressed subimage on 4-texel boundary - should work */
	x = 12;
	y = 8;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
	glCompressedTexSubImage2D(GL_TEXTURE_2D, 0,
				  x, y, w, h,
				  format,
				  piglit_compressed_image_size(format, w, h),
				  compressed_image +
				  piglit_compressed_pixel_offset(format, width, x, y));

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = check_rendering(width, height) && pass;

	/* Try compressed subimage on non 4-texel boundary - should not work */
	x = 14;
	y = 9;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
	glCompressedTexSubImage2D(GL_TEXTURE_2D, 0,
				  x, y, w, h,
				  format,
				  piglit_compressed_image_size(format, w, h),
				  compressed_image +
				  piglit_compressed_pixel_offset(format, width, 0, 0));

	pass = piglit_check_gl_error(GL_INVALID_OPERATION) && pass;

	/* Try compressed subimage with size not a multiple of 4 -
	 * should not work
	 */
	x = 8;
	y = 8;
	w = 14;
	h = 10;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
	glCompressedTexSubImage2D(GL_TEXTURE_2D, 0,
				  x, y, w, h,
				  format,
				  piglit_compressed_image_size(format, 4, 4),
				  compressed_image +
				  piglit_compressed_pixel_offset(format, width, x, y));
	/* Note, we can get either of these errors depending on the order
	 * in which glCompressedTexSubImage parameters are checked.
	 * INVALID_OPERATION for the bad size or INVALID_VALUE for the
	 * wrong compressed image size.
	 */
	pass = check_gl_error2(GL_INVALID_OPERATION, GL_INVALID_VALUE) && pass;

	/* Try compressed subimage with invalid offset - should not work */
	x = -3;
	y = 8;
	w = 4;
	h = 4;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
	glCompressedTexSubImage2D(GL_TEXTURE_2D, 0,
				  x, y, w, h,
				  format,
				  piglit_compressed_image_size(format, w, h),
				  compressed_image +
				  piglit_compressed_pixel_offset(format, width, 0, 0));

	pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass;

	/* Try compressed subimage with too large of image - should not work */
	x = 16;
	y = 8;
	w = width * 2;
	h = height * 2;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
	glCompressedTexSubImage2D(GL_TEXTURE_2D, 0,
				  x, y, w, h,
				  format,
				  piglit_compressed_image_size(format, w, h),
				  compressed_image +
				  piglit_compressed_pixel_offset(format, width, x, y));

	pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass;

	/* Try compressed subimage with different format - should not work */
	if (format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT)
		format2 = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
	else
		format2 = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
	x = 4;
	y = 4;
	w = 4;
	h = 4;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
	glCompressedTexSubImage2D(GL_TEXTURE_2D, 0,
				  x, y, w, h,
				  format2,
				  piglit_compressed_image_size(format2, w, h),
				  compressed_image +
				  piglit_compressed_pixel_offset(format2, width, x, y));

	pass = piglit_check_gl_error(GL_INVALID_OPERATION) && pass;

	/* Try zero-sized subimage - should not be an error */
	x = 4;
	y = 4;
	w = 0;
	h = 0;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
	glCompressedTexSubImage2D(GL_TEXTURE_2D, 0,
				  x, y, w, h,
				  format,
				  piglit_compressed_image_size(format, w, h),
				  compressed_image +
				  piglit_compressed_pixel_offset(format, width, x, y));

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;


	/* Try CompressedTexSubImage into level 1 (which is missing) */
	x = 0;
	y = 0;
	w = 4;
	h = 4;
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
	glCompressedTexSubImage2D(GL_TEXTURE_2D, 1,
				  x, y, w, h,
				  format,
				  piglit_compressed_image_size(format, w, h),
				  compressed_image +
				  piglit_compressed_pixel_offset(format, width, x, y));

	pass = piglit_check_gl_error(GL_INVALID_OPERATION) && pass;

	/* Try CompressedTexImage of size zero - should not be an erorr */
	w = 0;
	h = 0;
	glCompressedTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0,
			       piglit_compressed_image_size(format, w, h),
			       compressed_image);

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;

	/* Try CompressedTexImage with size which is a not a multiple of the
         * block size - should not be an erorr
         */
	w = width - 1;
	h = height - 1;
	glCompressedTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0,
			       piglit_compressed_image_size(format, w, h),
			       compressed_image);

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	pass = check_rendering(width, height) && pass;

	glDeleteTextures(1, &tex);

	free(compressed_image);

	return pass;
}
Exemplo n.º 13
0
void DxtEncoder::encode(
    izanagi::graph::CGraphicsDevice* device,
    izanagi::graph::CTexture* texture)
{
    izanagi::sys::CTimer timer;

    device->SaveRenderState();

    device->SetRenderState(
        izanagi::graph::E_GRAPH_RS_ZENABLE,
        IZ_FALSE);

    // For rendering full screen quad without vertex buffer.
    device->SetRenderState(
        izanagi::graph::E_GRAPH_RS_CULLMODE,
        izanagi::graph::E_GRAPH_CULL_NONE);

    auto texHandle = m_tex->GetTexHandle();

    // Set FBO.
    CALL_GL_API(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo));
    CALL_GL_API(glFramebufferTexture2D(
        GL_FRAMEBUFFER,
        GL_COLOR_ATTACHMENT0,
        GL_TEXTURE_2D,
        texHandle,
        0));

    GLenum attachedColor[] = {
        GL_COLOR_ATTACHMENT0,
    };

    CALL_GL_API(::glDrawBuffers(1, attachedColor));

    CALL_GL_API(glDisable(GL_FRAMEBUFFER_SRGB));

    device->SetShaderProgram(m_shd);

    {
        CALL_GL_API(::glActiveTexture(GL_TEXTURE0));

        GLuint handle = texture->GetTexHandle();

        CALL_GL_API(::glBindTexture(GL_TEXTURE_2D, handle));
    }

    auto hImage = m_shd->GetHandleByName("image");
    auto hMode = m_shd->GetHandleByName("mode");

    CALL_GL_API(glUniform1i(hImage, 0));
    CALL_GL_API(glUniform1i(hMode, 3));

    device->SetViewport(
        izanagi::graph::SViewport(0, 0, m_width/4, m_height/4));

    // NOTE
    // 頂点バッファを使わず全画面に描画する頂点シェーダ.
    CALL_GL_API(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));

    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

#if 1
    // Copy to PBO.
    {
        timer.Begin();

        // NOTE
        // PBO は STREAM_COPY で作成されている.

        CALL_GL_API(glReadBuffer(GL_COLOR_ATTACHMENT0));
        CALL_GL_API(glBindBuffer(GL_PIXEL_PACK_BUFFER, m_pbo));

        // Copy from GL_COLOR_ATTACHMENT0 to PBO.
        CALL_GL_API(glReadPixels(
            0, 0,
            m_width / 4, m_height / 4,
            GL_RGBA_INTEGER,
            GL_UNSIGNED_INT,
            0));

        CALL_GL_API(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));

        auto time = timer.End();
        IZ_PRINTF("ReadToPBO [%f]\n", time);
    }

    CALL_GL_API(glBindFramebuffer(GL_FRAMEBUFFER, 0));

    // Copy to texture memory.
    {
        timer.Begin();

        auto texdxt = m_texDxt->GetTexHandle();

        CALL_GL_API(glBindTexture(GL_TEXTURE_2D, texdxt));
        CALL_GL_API(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, m_pbo));

        // Copy from PBO to texture(DXT5) memory.
        CALL_GL_API(glCompressedTexSubImage2D(
            GL_TEXTURE_2D,
            0,
            0, 0,
            m_width, m_height,
            GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
            m_width * m_height,
            0));

        CALL_GL_API(glBindTexture(GL_TEXTURE_2D, 0));
        CALL_GL_API(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));

        auto time = timer.End();
        IZ_PRINTF("CopyToDXT [%f]\n", time);
    }

    // Readback to memory.
    {
        timer.Begin();

        auto texdxt = m_texDxt->GetTexHandle();

        CALL_GL_API(glBindTexture(GL_TEXTURE_2D, texdxt));

        CALL_GL_API(glGetCompressedTexImage(
            GL_TEXTURE_2D,
            0,
            m_pixels));

        CALL_GL_API(glBindTexture(GL_TEXTURE_2D, 0));

        auto time = timer.End();
        IZ_PRINTF("ReadToMem [%f]\n", time);
    }
#endif

    // 元に戻す.
    device->SetViewport(
        izanagi::graph::SViewport(0, 0, m_width, m_height));

    device->LoadRenderState();
}
Exemplo n.º 14
0
	void GLTextureBuffer::download(const PixelData &data)
	{
		if (data.getWidth() != getWidth() || data.getHeight() != getHeight() || data.getDepth() != getDepth())
		{
			LOGERR("Only download of entire buffer is supported by OpenGL.");
			return;
		}

		glBindTexture(mTarget, mTextureID);
		BS_CHECK_GL_ERROR();

		if(PixelUtil::isCompressed(data.getFormat()))
		{
			// Block-compressed data cannot be smaller than 4x4, and must be a multiple of 4
			const UINT32 actualWidth = Math::divideAndRoundUp(std::max(mWidth, 4U), 4U) * 4U;
			const UINT32 actualHeight = Math::divideAndRoundUp(std::max(mHeight, 4U), 4U) * 4U;

			const UINT32 expectedRowPitch = actualWidth;
			const UINT32 expectedSlicePitch = actualWidth * actualHeight;

			const bool isConsecutive = data.getRowPitch() == expectedRowPitch && data.getSlicePitch() == expectedSlicePitch;
			if (data.getFormat() != mFormat || !isConsecutive)
			{
				LOGERR("Compressed images must be consecutive, in the source format");
				return;
			}

			// Data must be consecutive and at beginning of buffer as PixelStorei not allowed
			// for compressed formate
			glGetCompressedTexImage(mFaceTarget, mLevel, data.getData());
			BS_CHECK_GL_ERROR();
		} 
		else
		{
			if (data.getWidth() != data.getRowPitch())
			{
				glPixelStorei(GL_PACK_ROW_LENGTH, data.getRowPitch());
				BS_CHECK_GL_ERROR();
			}

			if (data.getHeight()*data.getWidth() != data.getSlicePitch())
			{
				glPixelStorei(GL_PACK_IMAGE_HEIGHT, (data.getSlicePitch() / data.getWidth()));
				BS_CHECK_GL_ERROR();
			}

			if (data.getLeft() > 0 || data.getTop() > 0 || data.getFront() > 0)
			{
				glPixelStorei(
					GL_PACK_SKIP_PIXELS, 
					data.getLeft() + data.getRowPitch() * data.getTop() + data.getSlicePitch() * data.getFront());
				BS_CHECK_GL_ERROR();
			}

			if ((data.getWidth()*PixelUtil::getNumElemBytes(data.getFormat())) & 3)
			{
				glPixelStorei(GL_PACK_ALIGNMENT, 1);
				BS_CHECK_GL_ERROR();
			}

			// We can only get the entire texture
			glGetTexImage(mFaceTarget, mLevel, GLPixelUtil::getGLOriginFormat(data.getFormat()), 
				GLPixelUtil::getGLOriginDataType(data.getFormat()), data.getData());
			BS_CHECK_GL_ERROR();

			// Restore defaults
			glPixelStorei(GL_PACK_ROW_LENGTH, 0);
			BS_CHECK_GL_ERROR();

			glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
			BS_CHECK_GL_ERROR();

			glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
			BS_CHECK_GL_ERROR();

			glPixelStorei(GL_PACK_ALIGNMENT, 4);
			BS_CHECK_GL_ERROR();
		}

		BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_Texture);
	}
Exemplo n.º 15
0
void CMapGenerator::GenerateSMT(CVirtualArchive* archive)
{
	CVirtualFile* fileSMT = archive->AddFile("maps/generated.smt");

	const int tileSize = 32;

	//--- Make TileFileHeader ---
	TileFileHeader smtHeader;
	strcpy(smtHeader.magic, "spring tilefile");
	smtHeader.version = 1;
	smtHeader.numTiles = 1; //32 * 32 * (generator->GetMapSize().x * 32) * (generator->GetMapSize().y * 32);
	smtHeader.tileSize = tileSize;
	smtHeader.compressionType = 1;

	const int bpp = 3;
	int tilePos = 0;
	unsigned char tileData[tileSize * tileSize * bpp];
	for(int x = 0; x < tileSize; x++)
	{
		for(int y = 0; y < tileSize; y++)
		{
			tileData[tilePos] = 0;
			tileData[tilePos + 1] = 0xFF;
			tileData[tilePos + 2] = 0;
			tilePos += bpp;
		}
	}
	glClearErrors();
	GLuint tileTex;
	glGenTextures(1, &tileTex);
	glBindTexture(GL_TEXTURE_2D, tileTex);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, tileSize, tileSize, 0, GL_RGB, GL_UNSIGNED_BYTE, tileData);
	glGenerateMipmapEXT(GL_TEXTURE_2D);
	char tileDataDXT[SMALL_TILE_SIZE];

	int dxtImageOffset = 0;
	int dxtImageSize = 512;
	for(int x = 0; x < 4; x++)
	{
		glGetCompressedTexImage(GL_TEXTURE_2D, x, tileDataDXT + dxtImageOffset);
		dxtImageOffset += dxtImageSize;
		dxtImageSize /= 4;
	}

	glDeleteTextures(1, &tileTex);

	GLenum errorcode = glGetError();
	if(errorcode != GL_NO_ERROR)
	{
		throw content_error("Error generating map - texture generation not supported");
	}

	size_t totalSize = sizeof(TileFileHeader);
	fileSMT->buffer.resize(totalSize);

	int writePosition = 0;
	memcpy(&(fileSMT->buffer[writePosition]), &smtHeader, sizeof(smtHeader));
	writePosition += sizeof(smtHeader);

	fileSMT->buffer.resize(fileSMT->buffer.size() + smtHeader.numTiles * SMALL_TILE_SIZE);
	for(int x = 0; x < smtHeader.numTiles; x++)
	{
		memcpy(&(fileSMT->buffer[writePosition]), tileDataDXT, SMALL_TILE_SIZE);
		writePosition += SMALL_TILE_SIZE;
	}
}
Exemplo n.º 16
0
PIGLIT_GL_TEST_CONFIG_END


static bool
test_getsubimage(GLenum target,
		 GLsizei width, GLsizei height, GLsizei numSlices,
		 GLenum intFormat)
{
	const GLint bufSize = width * height * 4 * sizeof(GLubyte);
	GLubyte *texData;
	GLubyte *refData = malloc(6 * bufSize); /* 6 for cubemaps */
	GLubyte *testData = malloc(6 * bufSize); /* 6 for cubemaps */
	GLuint tex;
	int i, slice, compressedSize, compSize;
	int blockWidth, blockHeight, blockSize;
	bool pass = true;
	const int level = 0;
	int x0, y0, x1, y1, w0, h0, w1, h1;

	printf("Testing %s %s %d x %d\n",
	       piglit_get_gl_enum_name(target),
	       piglit_get_gl_enum_name(intFormat),
	       width, height);

	/* For all S3TC formats */
	blockWidth = blockHeight = 4;
	if (intFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ||
	    intFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
		blockSize = 8;
	}
	else {
		assert(intFormat == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
		       intFormat == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT);
		blockSize = 16;
	}

	/* Size must be multiple of block dims */
	assert(width % blockWidth == 0);
	assert(height % blockHeight == 0);

	compressedSize = (width / blockWidth) * (height / blockHeight)
		* blockSize;
	if (0) {
		printf("byte per block row: %d\n",
		       (width / blockWidth) * blockSize);
		printf("compressed image size = %d\n", compressedSize);
	}

	/* initial texture data */
	texData = malloc(compressedSize);
	for (i = 0; i < compressedSize; i++) {
		texData[i] = (i+10) & 0xff;
	}

	glGenTextures(1, &tex);
	glBindTexture(target, tex);

	/* Define texture image */
	if (target == GL_TEXTURE_CUBE_MAP) {
		for (slice = 0; slice < 6; slice++) {
			glCompressedTexImage2D(
				GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice,
				level, intFormat, width, height, 0,
				compressedSize, texData);
		}
		glGetTexLevelParameteriv(GL_TEXTURE_CUBE_MAP_POSITIVE_X, level,
					 GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
					 &compSize);
		assert(numSlices == 6);
	}
	else if (target == GL_TEXTURE_CUBE_MAP_ARRAY) {
		assert(numSlices % 6 == 0);
		glCompressedTexImage3D(target, level, intFormat,
				       width, height, numSlices, 0,
				       compressedSize * numSlices, NULL);
		for (slice = 0; slice < numSlices; slice++) {
			glCompressedTexSubImage3D(target, level,
						  0, 0, slice,
						  width, height, 1,
						  intFormat,
						  compressedSize, texData);
		}
		glGetTexLevelParameteriv(target, level,
					 GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
					 &compSize);
		compSize /= numSlices;
	}
	else if (target == GL_TEXTURE_2D_ARRAY) {
		glCompressedTexImage3D(target, level, intFormat,
				       width, height, numSlices, 0,
				       compressedSize * numSlices, NULL);
		for (slice = 0; slice < numSlices; slice++) {
			glCompressedTexSubImage3D(target, level,
						  0, 0, slice,
						  width, height, 1,
						  intFormat,
						  compressedSize, texData);
		}
		glGetTexLevelParameteriv(target, level,
					 GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
					 &compSize);
		compSize /= numSlices;
	}
	else {
		assert(target == GL_TEXTURE_2D);
		glCompressedTexImage2D(target, level, intFormat,
				       width, height, 0,
				       compressedSize, texData);
		glGetTexLevelParameteriv(target, level,
					 GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
					 &compSize);
		assert(numSlices == 1);
	}

	assert(compSize == compressedSize);

	/* Should be no GL errors */
	if (!piglit_check_gl_error(GL_NO_ERROR)) {
		pass = false;
	}

	refData = calloc(1, numSlices * compressedSize);
	testData = calloc(1, numSlices * compressedSize);

	/* compute pos/size of sub-regions */
	x0 = 0;
	y0 = 0;
	x1 = width / 4;  /* quarter width */
	y1 = height / 2;  /* half height */

	/* Position must be multiple of block dims */
	assert(x1 % blockWidth == 0);
	assert(y1 % blockHeight == 0);

	w0 = x1 - x0;
	w1 = width - x1;
	h0 = y1 - y0;
	h1 = height - y1;

	/* Sizes must be multiple of block dims */
	assert(w0 % blockWidth == 0);
	assert(w1 % blockWidth == 0);
	assert(h0 % blockHeight == 0);
	assert(h1 % blockHeight == 0);

	glPixelStorei(GL_PACK_ALIGNMENT, 1);
	glPixelStorei(GL_PACK_ROW_LENGTH, width);
	glPixelStorei(GL_PACK_IMAGE_HEIGHT, height);
	glPixelStorei(GL_PACK_COMPRESSED_BLOCK_WIDTH, blockWidth);
	glPixelStorei(GL_PACK_COMPRESSED_BLOCK_HEIGHT, blockHeight);
	glPixelStorei(GL_PACK_COMPRESSED_BLOCK_SIZE, blockSize);

	/* Should be no GL errors */
	if (!piglit_check_gl_error(GL_NO_ERROR)) {
		pass = false;
	}

	/*
	 * Get whole compressed image (the reference)
	 */
	if (target == GL_TEXTURE_CUBE_MAP) {
		for (slice = 0; slice < 6; slice++) {
			glGetCompressedTexImage(
				GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice,
				level,
				refData + slice * compressedSize);
		}
	}
	else {
		glGetCompressedTexImage(target, level, refData);
	}

	if (!piglit_check_gl_error(GL_NO_ERROR)) {
		pass = false;
	}

	/*
	 * Now get four sub-regions which should be equivalent
	 * to the whole reference image.
	 */

	/* lower-left */
	glPixelStorei(GL_PACK_SKIP_PIXELS, x0);
	glPixelStorei(GL_PACK_SKIP_ROWS, y0);
	glGetCompressedTextureSubImage(tex, level,
				       x0, y0, 0, w0, h0, numSlices,
				       numSlices * compressedSize, testData);

	/* lower-right */
	glPixelStorei(GL_PACK_SKIP_PIXELS, x1);
	glPixelStorei(GL_PACK_SKIP_ROWS, y0);
	glGetCompressedTextureSubImage(tex, level,
				       x1, y0, 0, w1, h0, numSlices,
				       numSlices * compressedSize, testData);

	/* upper-left */
	glPixelStorei(GL_PACK_SKIP_PIXELS, x0);
	glPixelStorei(GL_PACK_SKIP_ROWS, y1);
	glGetCompressedTextureSubImage(tex, level,
				       x0, y1, 0, w0, h1, numSlices,
				       numSlices * compressedSize, testData);

	/* upper-right */
	glPixelStorei(GL_PACK_SKIP_PIXELS, x1);
	glPixelStorei(GL_PACK_SKIP_ROWS, y1);
	glGetCompressedTextureSubImage(tex, level,
				       x1, y1, 0, w1, h1, numSlices,
				       numSlices * compressedSize, testData);

	/* defaults */
	glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
	glPixelStorei(GL_PACK_SKIP_ROWS, 0);

	/* Should be no GL errors */
	if (!piglit_check_gl_error(GL_NO_ERROR)) {
		pass = false;
	}

	/* now compare the images */
	for (slice = 0; slice < numSlices; slice++) {
		int sliceStart = slice * compressedSize;
		if (memcmp(refData + sliceStart,
			   testData + sliceStart,
			   compressedSize)) {
			int i;
			for (i = 0; i < compressedSize; i++) {
				if (refData[sliceStart + i] !=
				    testData[sliceStart + i]) {
					printf("fail in slice/face %d at offset %d\n",
					       slice, i);
					printf("expected %d, found %d\n",
					       refData[sliceStart + i],
					       testData[sliceStart + i]);
					break;
				}
			}
			printf("Failure for %s %s\n",
			       piglit_get_gl_enum_name(target),
			       piglit_get_gl_enum_name(intFormat));
			pass = false;
		}
	}

	free(texData);
	free(refData);
	free(testData);

	glDeleteTextures(1, &tex);

	return pass;
}
Exemplo n.º 17
0
// NOTE: Always saves in TGA format
void DGTexture::saveToFile(const char* fileName){
    if (_isLoaded) {
        FILE* fh;
        char fullFileName[DGMaxFileLength - 4];
        
        strncpy(fullFileName, fileName, DGMaxFileLength - 4);
        
        if (_compressionLevel) {
            TEXMainHeader header;
            TEXSubHeader subheader;
            GLint internalformat, size;
             
            // TODO: Must check that the texture is RGB
            // NOTE: Let's try to support alpha channel for these textures
             
            glBindTexture(GL_TEXTURE_2D, _ident);
            glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internalformat);
            glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &size);
            _bitmap = (GLubyte*)malloc(size * sizeof(GLubyte)); 
            glGetCompressedTexImage(GL_TEXTURE_2D, 0, _bitmap);
            header.compressionLevel = 1;
             
            strcpy(header.name, this->name()); // This is the object name
            header.width = (short)_width;
            header.height = (short)_height;
            header.numTextures = 0;
             
            subheader.cubePosition = 0;
            subheader.depth = (short)_depth;
            subheader.size = (int)size;
            subheader.format = (int)internalformat;
            
            strncat(fullFileName, ".tex", 4);
            
            fh = fopen(fullFileName, "wb");
            fwrite(&TEXIdent, 1, 8, fh);
            fwrite(&header, 1, sizeof(header), fh);
            fwrite(&subheader, 1, sizeof(subheader), fh);
            fwrite(_bitmap, 1, sizeof(GLubyte) * size, fh);
             
            free(_bitmap);
            fclose(fh);
        }
        else {
            unsigned char cGarbage = 0, type, mode;
            short int iGarbage = 0;
            int i;
            GLint size;
            
            strncat(fullFileName, ".tga", 4);
            
            fh = fopen(fullFileName, "wb");
            if (fh == NULL)
                return;
            
            glBindTexture(GL_TEXTURE_2D, _ident);
            
            // We do this in case the texture wasn't loaded
            glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &_width);
            glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &_height);
            //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_DEPTH, &texture_data->depth);
            _depth = 24; // Hardcoded to 24 bits
            
            mode = _depth / 8;
            if ((_depth == 24) || (_depth == 32))
                type = 2;
            else
                type = 3;
            
            fwrite(&cGarbage, sizeof(unsigned char), 1, fh);
            fwrite(&cGarbage, sizeof(unsigned char), 1, fh);
            
            fwrite(&type, sizeof(unsigned char), 1, fh);
            
            fwrite(&iGarbage, sizeof(short int), 1, fh);
            fwrite(&iGarbage, sizeof(short int), 1, fh);
            fwrite(&cGarbage, sizeof(unsigned char), 1, fh);
            
            
            fwrite(&iGarbage, sizeof(short int), 1, fh);
            fwrite(&iGarbage, sizeof(short int), 1, fh);
            
            fwrite(&_width, sizeof(short int), 1, fh);
            fwrite(&_height, sizeof(short int), 1, fh);
            fwrite(&_depth, sizeof(unsigned char), 1, fh);
            
            fwrite(&cGarbage, sizeof(unsigned char), 1, fh);
            
            _depth = mode;
            
            size = _width * _height * _depth;
            _bitmap = (GLubyte*)malloc(size * sizeof(GLubyte)); 
            glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, _bitmap);
            
            if (mode >= 3) {
                unsigned char aux;

                for (i = 0; i < _width * _height * mode ; i+= mode) {
                    aux = _bitmap[i];
                    _bitmap[i] = _bitmap[i + 2];
                    _bitmap[i + 2] = aux;
                }
            }
            
            fwrite(_bitmap, 1, sizeof(GLubyte) * size, fh);
            fclose(fh);
            
            free(_bitmap);
        }
    }
}
Exemplo n.º 18
0
void Texture::getCompressedImage(const GLint lod, GLvoid * image) const
{
    bind();

    glGetCompressedTexImage(m_target, lod, image);
}
Exemplo n.º 19
0
void gl4es_glGetCompressedTextureImage(GLuint texture, GLenum target, GLint level, GLvoid *img) {
    text(glGetCompressedTexImage(target, level, img));
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL13_nglGetCompressedTexImage(JNIEnv *__env, jclass clazz, jint target, jint lod, jlong imgAddress, jlong __functionAddress) {
	GLvoid *img = (GLvoid *)(intptr_t)imgAddress;
	glGetCompressedTexImagePROC glGetCompressedTexImage = (glGetCompressedTexImagePROC)(intptr_t)__functionAddress;
	UNUSED_PARAMS(__env, clazz)
	glGetCompressedTexImage(target, lod, img);
}
Exemplo n.º 21
0
//----------------------------------------------------------------------------//
void OpenGL1Texture::blitToMemory(void* targetData)
{
    if (OpenGLInfo::getSingleton().isUsingOpenglEs())
    {
        /* OpenGL ES 3.1 or below doesn't support
        "glGetTexImage"/"glGetCompressedTexImage", so we need to emulate it
        using "glReadPixels". */

        GLint old_pack;
        glGetIntegerv(GL_PACK_ALIGNMENT, &old_pack);
        GLuint texture_framebuffer(0);
        GLint framebuffer_old(0), pack_alignment_old(0);
        glGenFramebuffers(1, &texture_framebuffer);
        GLenum framebuffer_target(0), framebuffer_param(0);
        if (OpenGLInfo::getSingleton()
            .isSeperateReadAndDrawFramebufferSupported())
        {
            framebuffer_param = GL_READ_FRAMEBUFFER_BINDING;
            framebuffer_target = GL_READ_FRAMEBUFFER;
        }
        else
        {
            framebuffer_param = GL_FRAMEBUFFER_BINDING;
            framebuffer_target = GL_FRAMEBUFFER;
        }
        glGetIntegerv(framebuffer_param, &framebuffer_old);
        glBindFramebuffer(framebuffer_target, texture_framebuffer);
        glFramebufferTexture2D(framebuffer_target, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, d_ogltexture, 0);
        GLint read_buffer_old(0), pixel_pack_buffer_old(0);
        if (OpenGLInfo::getSingleton().isReadBufferSupported())
        {
            glGetIntegerv(GL_READ_BUFFER, &read_buffer_old);
            glReadBuffer(GL_COLOR_ATTACHMENT0);
            glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &pixel_pack_buffer_old);
            glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
        }
        glGetIntegerv(GL_PACK_ALIGNMENT, &pack_alignment_old);
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glReadPixels(0, 0, static_cast<GLsizei>(d_dataSize.d_width),
            static_cast<GLsizei>(d_dataSize.d_height), GL_RGBA, GL_UNSIGNED_BYTE, targetData);
        glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment_old);
        if (OpenGLInfo::getSingleton().isReadBufferSupported())
        {
            glBindBuffer(GL_PIXEL_PACK_BUFFER, pixel_pack_buffer_old);
            glReadBuffer(read_buffer_old);
        }
        glBindFramebuffer(framebuffer_target, framebuffer_old);
        glDeleteFramebuffers(1, &texture_framebuffer);

    }
    else // Desktop OpenGL
    {
        // save existing config
        GLuint old_tex;
        glGetIntegerv(GL_TEXTURE_BINDING_2D, reinterpret_cast<GLint*>(&old_tex));

        glBindTexture(GL_TEXTURE_2D, d_ogltexture);

        if (d_isCompressed)
        {
            glGetCompressedTexImage(GL_TEXTURE_2D, 0, targetData);
        }
        else
        {
            GLint old_pack;
            glGetIntegerv(GL_PACK_ALIGNMENT, &old_pack);

            glPixelStorei(GL_PACK_ALIGNMENT, 1);
            glGetTexImage(GL_TEXTURE_2D, 0, d_format, d_subpixelFormat, targetData);

            glPixelStorei(GL_PACK_ALIGNMENT, old_pack);
        }

        // restore previous config.
        glBindTexture(GL_TEXTURE_2D, old_tex);
    }
}
Exemplo n.º 22
0
void gl4es_glGetCompressedMultiTexImage(GLenum texunit, GLenum target, GLint level, GLvoid *img) {
    text(glGetCompressedTexImage(target, level, img));
}
Exemplo n.º 23
0
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL13_nglGetCompressedTexImageBO(JNIEnv *env, jclass clazz, jint target, jint lod, jlong img_buffer_offset, jlong function_pointer) {
	GLvoid *img_address = (GLvoid *)(intptr_t)offsetToPointer(img_buffer_offset);
	glGetCompressedTexImagePROC glGetCompressedTexImage = (glGetCompressedTexImagePROC)((intptr_t)function_pointer);
	glGetCompressedTexImage(target, lod, img_address);
}
Exemplo n.º 24
0
enum piglit_result
piglit_display(void)
{
	GLuint tex, tex_src;
	bool pass;
	int level;
	unsigned bw, bh, bs;

	piglit_get_compressed_block_size(format->token, &bw, &bh, &bs);
	glClearColor(0.5, 0.5, 0.5, 0.5);
	glClear(GL_COLOR_BUFFER_BIT);

	tex_src = piglit_rgbw_texture(format->token, SIZE, SIZE,
				      GL_TRUE, GL_FALSE,
				      GL_UNSIGNED_NORMALIZED);
	glGenTextures(1, &tex);

	for (level = 0; (SIZE >> level) > 0; level++) {
		int w, h;
		int expected_size, size;
		void *compressed;

		w = SIZE >> level;
		h = SIZE >> level;
		expected_size = piglit_compressed_image_size(format->token, w, h);

		glBindTexture(GL_TEXTURE_2D, tex_src);
		glGetTexLevelParameteriv(GL_TEXTURE_2D, level,
					 GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
					 &size);

		if (size != expected_size) {
			fprintf(stderr, "Format %s level %d (%dx%d) size %d "
				"doesn't match expected size %d\n",
				format->name, level, w, h, size, expected_size);
			piglit_report_result(PIGLIT_FAIL);
		}

		compressed = malloc(size);

		glGetCompressedTexImage(GL_TEXTURE_2D, level, compressed);

		glBindTexture(GL_TEXTURE_2D, tex);
		glCompressedTexImage2D(GL_TEXTURE_2D, level, format->token,
				       w, h, 0, size, compressed);
		if (!piglit_check_gl_error(GL_NO_ERROR))
			piglit_report_result(PIGLIT_FAIL);

		free(compressed);
	}

	glDeleteTextures(1, &tex_src);
	glBindTexture(GL_TEXTURE_2D, tex);

	display_mipmaps(10, 10);
	pass = check_resulting_mipmaps(10, 10);

	piglit_present_results();

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}