The OpenGL documentation says very little about these two functions. When it would make sense to use glTexParameterIiv instead of glTexParameteriv or even glTexParameterfv?
If the values for GL_TEXTURE_BORDER_COLOR are specified with glTexParameterIiv or glTexParameterIuiv, the values are stored unmodified with an internal data type of integer. If specified with glTexParameteriv, they are converted to floating point with the following equation: f=(2c+1)/(2b−1). If specified with glTexParameterfv, they are stored unmodified as floating-point values.
You sort of answered your own question with the snippet you pasted. Traditional textures are fixed-point (unsigned normalized, where values like 255 are converted to 1.0 through normalization), but GL 3.0 introduced integral (signed / unsigned integer) texture types (where integer values stay integers).
If you had an integer texture and wanted to assign a border color (for use with the GL_CLAMP_TO_BORDER wrap mode), you would use one variant of those two functions (depending on whether you want signed or unsigned).
You cannot filter integer textures, but you can still have texture coordinate wrap behavior. Since said textures are integer and glTexParameteriv (...) normalizes the color values it is passed, an extra function had to be created to keep the color data integer.
You will find this same sort of thing with glVertexAttribIPointer (...) and so forth; adding support for integer data (as opposed to simply converting integer data to floating-point) to the GL pipeline required a lot of new commands.
Related
Is there a fast way to clear an OpenGL buffer with a double-precision data type or set a default value with an API call to avoid using a compute shader?
For half- and single-precision types, glClearBufferData/glClearNamedBufferData can be used, but it appears like there is no internal format enum for 64 bit types, which makes the switch from single- to double-precision data in scientific computing applications more cumbersome. Or am I missing an extension?
I am looking for a solution that works with OpenGL 4.6, Nvidia-specific extensions are fine.
At the end of the day, a "double" is just a way of interpreting 64-bits of data. Your goal is to get the right 64-bits into your buffer.
As far as buffer clearing is concerned, the image format and pixel transfer parameters are just an explanation of how to interpret the data you pass. If the internal format of the clearing operation is GL_RG32UI, then each "pixel" in the buffer is 64-bits of data.
Given that, all you need to do is to get the clearing function to take a block of 64-bits and copy it exactly as you provide it. To do this, you have to use the right pixel transfer parameters.
See, pixel transfer operations can perform data conversion, taking the data pointer you pass and converting it to match the internal format. You don't want that; you want a direct copy. So your pixel transfer parameters need to exactly match the internal format. Which is quite easy.
A format of GL_RG_INTEGER represents a two-component pixel that stores integer data, in red-green order. And a type of GL_UNSIGNED_INT means that each component is a 32-bit unsigned integer. This exactly matches the internal format of GL_RG32UI, so the copying algorithm won't mess with the bytes of your data.
So, given some 64-bit double value in C or C++, clearing a buffer to that double ought to be as simple as:
void clear_buffer_to_double(GLuint buffer, double dbl)
{
glClearNamedBufferData(buffer, GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT, &dbl);
}
glTexImage2D takes internalFormat (which specifies number of bits and data type/encoding), format (without number of bits and encoding) and type.
Is it possible, for example, to let OpenGL convert passed pixel data containing 32 bit integers from format GL_RGB_INTEGER and type GL_INT to internal format GL_RGB32F?
The wiki article https://www.khronos.org/opengl/wiki/Pixel_Transfer#Format_conversion suggests to me it's possible by stating:
Pixels specified by the user must be converted between the user-specified format (with format and type) and the internal representation controlled by the image format of the image.
But I wasn't able to read from floating point sampler in shader.
The _INTEGER pixel transfer formats are only to be used for transferring data to integer image formats. You are filling in a floating-point texture, so that doesn't qualify. You should have gotten an OpenGL Error.
Indeed, the very article you linked to spells this out:
Also, if "_INTEGER" is specified but the image format is not integral, then the transfer fails.
GL_RGBA32F is not an integral image format.
If you remove the _INTEGER part, then the pixel transfer will "work". OpenGL will assume that the integer data are normalized values, and therefore you will get floating-point values on the range [-1, 1]. That is, if you pass a 32-bit integer value of 1, the corresponding floating-point value will be 1/(2^31-1), which is a very small number (and thus, almost certainly just 0.0).
If you want OpenGL to cast the integer as if by a C cast (float)1... well, there's actually no way to do that. You'll just have to convert the data yourself.
I'm trying to understand glVertexAttribPointer, and I noticed that it accepts many more types than those that have an equivalent in GLSL. So to write down everything I know so far, I made this diagram matching all the types (except for the packed ones, which don't matter, and GL_FIXED, which I don't understand.
Blue nodes represent the type in GLSL, while yellow nodes represent the symbolic constants passed to glVertexAttribPointer.
Red nodes represent some kind of conversion between types.
Each yellow node is only directly connected to one blue node, what seems to be its main representation in GLSL, but some can be converted to some other form.
So I guess my question is: Are the relationships in this diagram correct, and how does GL_FIXED fit into it?
No.
You cannot feed a VS int, uint, or double input variable (or vectors of these) from a call to glVertexAttribPointer. This function only feeds float types. If you use non-normalized integers with this function, then they will be cast to floats as if by a standard cast operation (255 becomes 255.0f). GL_FIXED is just another floating-point representation, where a 32-bit integer is treated as a 16.16 fixed-point value. Naturally, this will be converted to a floating-point number when fed to the VS.
To feed the VS integers, you must use glVertexAttribIPointer. To feed doubles, you must use glVertexAttribLPointer.
bool types cannot be input variables of any kind.
I am using GL_RGB10_A2UI internal format in glCopyTexImage1D() API but getting GL_INVALID_OPERATION error. Does OpenGL 3.3 support GL_RGB10_A2UI in glCopyTexImage1D() ?
GL_RGB10_A2UI is an integral image format; it contains integers, not normalized floating-point values that are stored as integers. Therefore, unless your framebuffer also contains unsigned integer values, this copy operation will fail with the expected error.
Of course, the only way for your framebuffer to have unsigned integers (rather than unsigned normalized integers, which is the usual case) would be to use an FBO. In which case, you could just be rendering directly to this texture, and you wouldn't need to copy from it.
I'm guessing you probably meant to use GL_RGB10_A2, which represent unsigned normalized values.
i would like to implement 'drawing modes' (in my own graphics library).
That is drawing with AND, OR, etc
However i am storing colors using floats, each channel between 0 and 1.0
Do i have to first convert each color channel to 0-255 before i can use the AND, OR, etc drawing modes? and then convert back to float (0.0-1.0) ?
Or is there another way of doing it?
thanks
I believe the question is not clear enough. AND, OR, etc. are boolean operators. Many languages support bitwise versions as well. So, you first need to define what exactly is the meaning of AND-ing or OR-ing two color values. What is Red AND Green? Is it Black?
If the answer to the above question is positive, then you probably want to apply these operators in the bitwise sense on the (integer) RGB representation of your colors. In this case, you do need to:
1. Convert the floats to (8-bit or other resolution) integers
2. Pack the 3 channels (or 4 with Alpha) into one word (probably 32-bit integer)
3. Apply the bitwise operator
4. Unpack the channels and convert back to floats.
Note that in conversion of floats to int you first need to multiply your float value by MAX_COLOR (255 in your example), and then cast. Otherwise, you end up with all channels being 0. Opposite when you convert back to floats, first cast then divide by MAX_COLOR to normalize your values.