Jump to content
  • Advertisement
Sign in to follow this  
wh1sp3rik

Constant struct arrays

This topic is 2133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

trying to make an array of struct arrays :) Unfortunately, i am not sure if it's possible .... it should be somehow.

 

This is how it's usually declared:

		const D3D11_INPUT_ELEMENT_DESC ILayout0[] = // position only
		{ 
			{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,    0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
		};
		const D3D11_INPUT_ELEMENT_DESC ILayout1[] = // normal
		{ 
			{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
			{"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
			{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,      0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
			{"TANGENT",  0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0},
			{"BINORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0},
		};

but i would like to make a one array from that:

		const D3D11_INPUT_ELEMENT_DESC LayoutArray[][] =
		{
			{
				{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
			},
			{
				{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
				{"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
				{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,      0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
				{"TANGENT",  0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0},
				{"BINORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0},
			},
		};

but unfortunately .. error ( second bracket is underlined )

error C2087: 'LayoutArray' : missing subscript
error C2078: too many initializers

Is this possible to declare it this way somehow ?

 

About D3D11_INPUT_ELEMENT_DESC struct. It's a DirectX11 struct and it declared

typedef struct D3D11_INPUT_ELEMENT_DESC
    {
    LPCSTR SemanticName;
    UINT SemanticIndex;
    DXGI_FORMAT Format;
    UINT InputSlot;
    UINT AlignedByteOffset;
    D3D11_INPUT_CLASSIFICATION InputSlotClass;
    UINT InstanceDataStepRate;
    } 	D3D11_INPUT_ELEMENT_DESC;

Thank you for answers ;-)

Share this post


Link to post
Share on other sites
Advertisement
When defining a multi-dimensional array you can only leave off the first array dimension.
const D3D11_INPUT_ELEMENT_DESC LayoutArray[][5] =
		{
			{
				{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
			},
			{
				{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
				{"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
				{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,      0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
				{"TANGENT",  0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0},
				{"BINORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0},
			},
		};
Note that this will define a multi-dimensional array with extents [2][5]. That first array will have four extra zero-initialized entries hanging around.

Share this post


Link to post
Share on other sites

By the way, if those are inside functions or methods, the construction of those arrays will be executed on each call.

Use static const, not just const.

 

 

L. Spiro

Share this post


Link to post
Share on other sites

By the way, if those are inside functions or methods, the construction of those arrays will be executed on each call.

Use static const, not just const.

 

 

L. Spiro

 

I'd have to look at the disassembly to check that... seems like a simple and obvious optimisation the compiler could make to me? I can believe it on some platforms though ;)

 

It used to be illegal to initialise non-static local arrays in C, are you thinking of that? When I learnt C the compiler we used insisted on that...

 

EDIT: I seem to remember you moaning about the Nintendo DS compiler in the past though being a bit crap. In general, for maximum efficiency I suppose it is a good idea to make all local const arrays (and local consts in general by extension I guess, maybe only if they are expensive to initialise), static.

 

EDIT2: Static locals in C++ usually have a (teeny weeny) cost as well in C++ since the compiler needs to know if they have already been constructed every time the function is called.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

It used to be illegal to initialise non-static local arrays in C, are you thinking of that? When I learnt C the compiler we used insisted on that...

No. I don’t use C that much and am not so familiar with those kinds of quirks.


EDIT2: Static locals in C++ usually have a (teeny weeny) cost as well in C++ since the compiler needs to know if they have already been constructed every time the function is called.

That much is true, but obviously less overhead than creating the whole array each time.
Where performance is concerned I make them static members of the class so they are created at start-up and create no run-time overhead.


L. Spiro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!