gpuguy
gpuguy

Reputation: 4585

CUDA: Why must Texture references be declared globally at file scope?

I am trying to understand how Texture memory works. I read in CUDA-By-Example book, that

Texture references must be declared globally at file scope,

I could not find reason why it is so. So any pointers to understand this in a little detail?

As pointed out by njuffa in comment below some hardware limitations caused this restriction to be imposed at HLL level, but what are those limitations and how this restriction comes ?

Upvotes: 0

Views: 210

Answers (1)

Drop
Drop

Reputation: 13003

Disclaimer: Here I speculate on possible reason, from my experience with graphics programming. It is not supposed to be considered as ground truth or even correct.

This is how it's been done in shaders for ages.

The graphics pipeline is a state machine and every state transition is global. Current snapshot reflects current state of the driver and hardware, and affects entire program. Traditionally, resources are referenced in shader languages at the file scope at the top. Be it texture buffer, sampler or any other resource. From application side you bind appropriate buffer to the appropriate texture slot and bind a sampler object. Then, usually, you can use the bound resources in any of the shader stages.

I guess, when CUDA was developed, nobody bothered to change high-level representation, but it is possible of course. For example, one could declare texture resources anywhere in any scope, but in the end, from hardware point of view, resource binding would stay global anyway, and would require to be unbound or rebound to another resource when leaving the scope. Those instructions of course can be inserted by the compiler. Implementation would be similar to that for destructors in C++.

A reason not to change API, might be to explicitly reflect the fact that state transitions are global and are usually very costly, so hiding this from API user wouldn't be wise (for performance reasons).

Upvotes: 1

Related Questions