0
\$\begingroup\$

When a texture (2D) is supplied to a shader as a 'uniform' input, it is first uploaded to OpenGL using glTexImage2D() and then using glUniform1i() it is associated to shader uniform.

eg code :

Texture data

glTexImage2D(): is used to transfer texture data to the server side

glGetUniformLocation(): is used to access shader uniform handle

glUniform1i(): associates the data pointed by texture unit to the shader 'uniform'

but when we pass matrix (eg matrix4x4) to a shader as a 'uniform' input, when don't use any specific function to upload it to OpenGL.

(we just used to glUniform..() to associate the data with the shader input which we also used in the case of texture data)

Matrix data

glGetUniformLocation(): to access shader uniform handle

glUniformMatrix4fv(): to associate matrix data to the shader uniform input.

Where does the matrix data live in each step in the process of passing it to a shader as a uniform input?

  • Does matrix data always live on client side/ CPU accessible memory and fetched every frame by server side?

  • If it is uploaded to OpenGL:

    • which step/function call uploads the data?

    • where does the data live in OpenGL memory?

    • how its memory location is pointed?

\$\endgroup\$
1
\$\begingroup\$

The matrix data is "uploaded" using glUniformMatrix4fv.

The data lives wherever the OpenGL driver wants it to be. The data is written to the shader's uniform data storage. That can be in GPU memory only, held in CPU memory until a draw call is issued at which point the GPU fetches the data from CPU memory, the data could be put in the command queue for the GPU to store before running the shader, or any other method.

Shaders most likely have a block of memory for its uniforms that the GPU loads into it's shader cores local memory/registers when the shader is ran, or the GPU driver could inline the values right into the shader code, or a mix. It's really up to the driver and GPU architecture.

Conceptually, the glUniformMatrix4fv() data (and the other uniforms) are written to the shader. That data/settings stays with the shader.

Texture samplers are different. You don't write a pointer to the texture in the shader. What you set in the shader is the index of which hardware sampler is being used for the shader's named/abstracted samplers.

And configure the hardware sampler(s) to use a specific texture as a separate operation, not associated with the shader object: You change the shader and the textures stay configured in the sampler units as it was, but that shader can be set to use sampler unit 3 instead of unit 0.

The client/server language in OpenGL is a bit confusing and in practice isn't really how it is implemented anymore (and drivers themselves sometimes get it wrong) but you can change "client" for "application" and "server" for "OS+GPU+driver" side and it's a bit closer to current implementations.

In practice, often, both the application (client) and the driver (server) keeps copies of data to speed things up (eg: for table look ups and merging data updates to send in bulks).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.