Sign in to follow this  
KingofNoobs

For Loop Mechanics

Recommended Posts

Hello,

Could anyone tell me if there is anything wrong with initializing "reverse iterators" for reverse list iteration by moving to end() -1 and begin() -1 and then running my for loop in reverse. The code follows:

AtlasBuffers::iterator begin;
AtlasBuffers::iterator end;
if(mFromTop)
{
begin = --mAtlasBuffers.end();
end = --mAtlasBuffers.begin();
}
else
{
begin = mAtlasBuffers.begin();
end = mAtlasBuffers.end();
}
for(; begin != end;)
{
const Atlas * tempAtlas = begin->first;
VIAggregator tempVIAggregator = begin->second;
std::vector<SimpleVertex> tempVertexAggregator = tempVIAggregator.first;
std::vector<uint32_t> tempIndexAggregator = tempVIAggregator.second;

// Only update VRAM buffers if they have changed
if(mVertexBufferChanged)
{
ID3D11Buffer * vertexBuffer = 0;
CreateVertexBuffer(&vertexBuffer, tempVertexAggregator);
unsigned int stride = sizeof(SimpleVertex);
unsigned int offset = 0;
mContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
ReleaseCOM(vertexBuffer);
// Index buffer only changed if vertex buffer changed
if(mIndexBufferChanged)
{
ID3D11Buffer * indexBuffer = 0;
CreateIndexBuffer(&indexBuffer, tempIndexAggregator);
mContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
ReleaseCOM(indexBuffer);
}

}

// Only reset texture if it has changed.
if(tempAtlas != mLastAtlasUsed)
{
mContext->PSSetShaderResources( 0, 1, (ID3D11ShaderResourceView*const*) &(mAtlasBuffers.begin()->first->GetTexture()) );
mLastAtlasUsed = tempAtlas;
}
mContext->DrawIndexed( tempIndexAggregator.size(), 0, 0 );
if(mFromTop)
{
++begin;
}
else
{
--begin;
}

} // for
mFromTop = !mFromTop;

Thanks in advance.

-Dave Ottley Edited by KingofNoobs

Share this post


Link to post
Share on other sites
Doing end()-1 isn't technically legal, so that would be a bad idea. I would refactor all your inner loop code into a separate method and then use completely separate loops for both forward iteration and reverse iteration (using reverse iterators).

Share this post


Link to post
Share on other sites
Zipster et al,

Reimplemented. Is this better:

auto DrawAtlas = [&] (AtlasBufferPair begin)
{
const Atlas * tempAtlas = begin.first;
VIAggregator tempVIAggregator = begin.second;
std::vector<SimpleVertex> tempVertexAggregator = tempVIAggregator.first;
std::vector<uint32_t> tempIndexAggregator = tempVIAggregator.second;

// Only update VRAM buffers if they have changed
if(mVertexBufferChanged)
{
ID3D11Buffer * vertexBuffer = 0;
CreateVertexBuffer(&vertexBuffer, tempVertexAggregator);
unsigned int stride = sizeof(SimpleVertex);
unsigned int offset = 0;
mContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
ReleaseCOM(vertexBuffer);
// Index buffer only changed if vertex buffer changed
if(mIndexBufferChanged)
{
ID3D11Buffer * indexBuffer = 0;
CreateIndexBuffer(&indexBuffer, tempIndexAggregator);
mContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
ReleaseCOM(indexBuffer);
}

}

// Only reset texture if it has changed.
if(tempAtlas != mLastAtlasUsed)
{
mContext->PSSetShaderResources( 0, 1, (ID3D11ShaderResourceView*const*) &(mAtlasBuffers.begin()->first->GetTexture()) );
mLastAtlasUsed = tempAtlas;
}
// One draw call per atlas
mContext->DrawIndexed( tempIndexAggregator.size(), 0, 0 );
};
if(mFromTop)
{
auto begin = mAtlasBuffers.rbegin();
auto end = mAtlasBuffers.rend();
for(auto begin = mAtlasBuffers.rbegin(); begin != mAtlasBuffers.rend(); ++begin)
{
DrawAtlas(*begin);
}
mFromTop = false;
}
else
{
for(auto begin = mAtlasBuffers.begin();begin != mAtlasBuffers.end(); ++begin)
{
DrawAtlas(*begin);
}
mFromTop = true;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this