1
\$\begingroup\$

I need to be able to compile an ".hlsl, .fx" shader in my DirectX app. The issue is that, with the code below, I can only specify 6 textures by their default path directory.

I don't want this - I want to be able to set one path to the directory containing all of my textures, then compile my shader and see the shader effect on all those 50 textures.

I am using DirectX June 2010 and using Direct3D11 and Visual Studio 2015.

#define NUM_TEXTURES 7
DETAIL_TESSELLATION_TEXTURE_STRUCT DetailTessellationTextures[NUM_TEXTURES + 
1] =
{
//    DiffuseMap              NormalHeightMap                 DisplayName,    
fHeightScale fBaseTextureRepeat fDensityScale fMeaningfulDifference
{ L"Textures\\rocks.jpg",    L"Textures\\rocks_NM_height.dds",  L"Rocks",       
10.0f,       1.0f,              25.0f,        2.0f/255.0f },
{ L"Textures\\stones.bmp",   L"Textures\\stones_NM_height.dds", L"Stones",      
5.0f,        1.0f,              10.0f,        5.0f/255.0f },
{ L"Textures\\wall.jpg",     L"Textures\\wall_NM_height.dds",   L"Wall",        
8.0f,        1.0f,              7.0f,         7.0f/255.0f },
{ L"Textures\\wood.jpg",     L"Textures\\four_NM_height.dds",   L"Four 
shapes", 30.0f,       1.0f,              35.0f,        2.0f/255.0f },
{ L"Textures\\concrete.bmp", L"Textures\\bump_NM_height.dds",   L"Bump",        
10.0f,       4.0f,              50.0f,        2.0f/255.0f },
{ L"Textures\\concrete.bmp", L"Textures\\dent_NM_height.dds",   L"Dent",        
10.0f,       4.0f,              50.0f,        2.0f/255.0f },
{ L"Textures\\wood.jpg",     L"Textures\\saint_NM_height.dds",  L"Saints" ,     
20.0f,       1.0f,              40.0f,        2.0f/255.0f },
{ L"",                       L"",                               L"Custom" ,     
5.0f,        1.0f,              10.0f,        2.0f/255.0f },
};
DWORD                               g_dwNumTextures = 0;
ID3D11ShaderResourceView*           
g_pDetailTessellationBaseTextureRV[NUM_TEXTURES+1];
ID3D11ShaderResourceView*           
g_pDetailTessellationHeightTextureRV[NUM_TEXTURES+1];
ID3D11ShaderResourceView*           
g_pDetailTessellationDensityTextureRV[NUM_TEXTURES+1];
ID3D11ShaderResourceView*           g_pLightTextureRV = NULL;
WCHAR                               
g_pszCustomTextureDiffuseFilename[MAX_PATH] = L"";
WCHAR                               
g_pszCustomTextureNormalHeightFilename[MAX_PATH] = L"";
DWORD                               g_bRecreateCustomTextureDensityMap  = 
false;
\$\endgroup\$
1
1
\$\begingroup\$

Normally your textures are loaded by the program and not by the shader directly. You then set the parameters of the GPU in some fashion such as:

m_texturePtr = m_effect->GetVariableByName("shaderTexture")->AsShaderResource();

which I grabbed from this tutorial as an example: http://rastertek.com/dx10tut48.html

The Graphics Card isn't made to randomly read from an unlimited numbers of textures at a time. Normally you will batch all the objects who require the same textures into the same call to the graphics library so that you don't have to keep swapping textures a lot.

If you have an object or collection of objects that use many different textures but need to be drawn in a single batch, then it's conventional to pre-combine those textures into a texture atlas or array texture. This is a single logical texture as far as the shader is concerned, and texture coordinates are used to pick out the particular texture you wanted from the larger collection.


In your case, simply make it so that before you render a model, you send the necessary texture to the graphics library first. The below is my code (C# monogame; using DX11 shaders)

public static void BatchDraw(Effect effect, bool clear)
        {   
            // walk through each texture type
            for (int j=0; j < (int)TextureType.MAX_TEXTURE; j++)
            {
                // derive the batch
                var currBatch = ObjectBatcher[j];
                
                // create a quad that is used for centering and sizing the texture
                var originalQuad = TextureSizes[j];

                // set the texture of the effect so that when we call draw, the correct type of texture is drawn
                //effect.Texture = TextureArray[j];
                effect.Parameters["colorTexture"].SetValue(TextureArray[j]);
                
                // for each of the "models", place it in 3-space and send the data to the graphics device
                var len = currBatch.PsuedoCount;
                for(int i=0; i<len; i++)
                {
                    AdjustModel(
                        // The offset of the texture to adjust the other vertices
                        originalQuad,
                        // The vertices that will be filled and then drawn
                        quadVertices, 
                        // The quad will be centered on this obj's position and spun by this obj's rotation
                        currBatch[i]);
                    
                    foreach (var pass in effect.CurrentTechnique.Passes)
                    { 
                        pass.Apply();
                    
                        // TODO: Look into the offset. Possibly a way to reduce my own computation?
                        myGD.DrawUserPrimitives(
                            // We’ll be rendering two triangles
                            PrimitiveType.TriangleList,
                            // The array of verts that we want to render
                            quadVertices,
                            // The offset, which is 0 since we want to start 
                            // at the beginning of the floorVerts array
                            0,
                            // The number of triangles to draw
                            2);
                    }
                    
                }

               
            }
        }

NOTE: I currently pass in each quad (my "models"), but instead I should pass in an array that describes all of those batched models at a time.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .