Skip to main content

A question about DX textures and materials

Submitted by WiffleCube on
Forum

In DirectX is CreateTextureFromFile smart enough to know
if it has already loaded a particular texture?

The reason I ask is that many examples load a mesh's textures by looping through
the materials, using the material's pTextureFilename field to load the textures-
so that may mean a lot of needlessly duplicated textures in memory if DirectX doesn't
recognise textures as being previously loaded (unless you keep track yourself).

Submitted by Leto on Mon, 18/07/05 - 8:29 PM Permalink

quote:In DirectX is CreateTextureFromFile smart enough to know
if it has already loaded a particular texture?

Short answer: It doesn't as far as I can tell, and I'd be surprised if it did. One way to check would be just to try and load a texture from the same file and see if the function returns the same pointer.

Longer answer: I can see that sort of functionality causing weird and confusing problems for us users. It would mean DirectX would have to maintain some sort of internal texture library - do you bother trying to work out if two different directory paths point to the same file? What if some external program overwrites the file or renames it? What about textures modified, or even created procedurally, at runtime? It's quite possible to sort through the issues, but the seemingly simple CreateTextureFromFile function suddenly has different semantics and side-effects from what is documented.

Submitted by WiffleCube on Tue, 19/07/05 - 10:29 AM Permalink

I suspected this might be the case- was curious how much
'housekeeping' DX does. This means that it's important to
maintain a list of previously loaded textures to compare
against (ex. by strcmp on filename) so that the textures
aren't duplicated in memory.

Forum

In DirectX is CreateTextureFromFile smart enough to know
if it has already loaded a particular texture?

The reason I ask is that many examples load a mesh's textures by looping through
the materials, using the material's pTextureFilename field to load the textures-
so that may mean a lot of needlessly duplicated textures in memory if DirectX doesn't
recognise textures as being previously loaded (unless you keep track yourself).


Submitted by Leto on Mon, 18/07/05 - 8:29 PM Permalink

quote:In DirectX is CreateTextureFromFile smart enough to know
if it has already loaded a particular texture?

Short answer: It doesn't as far as I can tell, and I'd be surprised if it did. One way to check would be just to try and load a texture from the same file and see if the function returns the same pointer.

Longer answer: I can see that sort of functionality causing weird and confusing problems for us users. It would mean DirectX would have to maintain some sort of internal texture library - do you bother trying to work out if two different directory paths point to the same file? What if some external program overwrites the file or renames it? What about textures modified, or even created procedurally, at runtime? It's quite possible to sort through the issues, but the seemingly simple CreateTextureFromFile function suddenly has different semantics and side-effects from what is documented.

Submitted by WiffleCube on Tue, 19/07/05 - 10:29 AM Permalink

I suspected this might be the case- was curious how much
'housekeeping' DX does. This means that it's important to
maintain a list of previously loaded textures to compare
against (ex. by strcmp on filename) so that the textures
aren't duplicated in memory.