Advertisement

Sample a texture by its index

Started by December 23, 2020 10:49 AM
10 comments, last by who_say_pawno 4 years, 1 month ago

The question is probably very banal, but I did not find an answer to it. I use DirectX 9 (I can't do it higher due to hardware specifics). I have a model not in my format that I need to render. The format stores the positions of the vertices and each vertex has a material index, the material index can be used to get the texture name. By the name of the texture, I load the desired file and get IDirect3DTexture9*. The question is, how do I get the texture index for a particular vertex in a pixel shader?

If I understand correctly, I have to create an array of textures in the pixel shader:

sampler MyTextures[16];

float4 main(PSInput input) { return tex2D(MyTextures[input.MaterialIndex], input.UV); }

Set texture to each cell before rendering the model (call IDirect3DDevice9::SetTexture), and then extract the index from the input data and get the color of the pixel. But how can I put an index in the incoming data if there is no suitable slot in the vertex declaration for this?

I mean there is no "D3DDECLUSAGE_MATERIALINDEX" here:

https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3ddeclusage

What should I do?

I'll have two number 9s, a number 9 large, a number 6 with extra dip, a number 7, two number 45s, one with cheese, and a large soda.

(REMOVED)

Advertisement

@ddlox And how would this issue be solved on a different version of DirectX?

I'll have two number 9s, a number 9 large, a number 6 with extra dip, a number 7, two number 45s, one with cheese, and a large soda.

(REMOVED)

@ddlox I'm just confused about one thing. I'm just trying to render a model of the RenderWare engine (DFF file), I looked at the sources as they create a vertex declaration, and did not see D3DDECLUSAGE_BLENDINDICES there. But how do they then use the texture index to select the desired sampler?

I'm just too lazy to reverse the pixel shader from the game…

You can see for yourself:

https://github.com/GTAmodding/rw37/blob/31261c08ff02f79e0cdb6d3db5276e7f6ddfb5bd/src/src/pipe/p2/d3d9/nodeD3D9SubmitNoLight.c#L237

I'll have two number 9s, a number 9 large, a number 6 with extra dip, a number 7, two number 45s, one with cheese, and a large soda.

My guess is that pixel function does not switch samplers across triangle in the manner of a material, rather only in parallel if so.

Material attribute is the same for all three verticies and does not interpolate to a different value across triangle, thus you can fetch it on any pixel and convert it to int number as index, if you happen to batch more samplers in a single draw call.

Advertisement

@JohnnyCode I didn't understand what you were suggesting to do. Maybe because of my poor english, you misunderstood the question.

I'll have two number 9s, a number 9 large, a number 6 with extra dip, a number 7, two number 45s, one with cheese, and a large soda.

who_say_pawno said:
I'm just trying to render a model of the RenderWare engine (DFF file),

u didn't mention RW in your first post, u should have said so

who_say_pawno said:
I'm just too lazy to reverse the pixel shader from the game…

use your debugger, sorry but i don't encourage laziness, nope -

dig in!

ddlox said:

who_say_pawno said:
I'm just trying to render a model of the RenderWare engine (DFF file),

u didn't mention RW in your first post, u should have said so

who_say_pawno said:
I'm just too lazy to reverse the pixel shader from the game…

use your debugger, sorry but i don't encourage laziness, nope -

dig in!

I thought it didn't matter where I got the models from. (no piracy, just want to get a nice picture)

For some reason it seemed to me that there are HLSL decompilers, and I wanted to use it to get the source code of the shader from the game, but I did not find any working decompiler

Waiting for someone's help...

I'll have two number 9s, a number 9 large, a number 6 with extra dip, a number 7, two number 45s, one with cheese, and a large soda.

That's not really how it works in D3D9, you don't actually have any ability to index into an “array” of textures like that unless the index is a compile-time constant. The standard way to do this would be to split things up so that you have a separate draw call for each material, and in between you bind just the texture(s) that the material needs.

This topic is closed to new replies.

Advertisement