Jump to content

  • Log In with Google      Sign In   
  • Create Account


For Loop Mechanics


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 27 November 2012 - 01:43 AM

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, 27 November 2012 - 01:47 AM.

I wonder as I wander...

http://www.davesgameoflife.com


Sponsor:

#2 Steve_Segreto   Crossbones+   -  Reputation: 1460

Like
0Likes
Like

Posted 27 November 2012 - 01:53 AM

If you think of begin as element 0, begin -1 doesn't make much sense does it?

#3 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 27 November 2012 - 01:55 AM

Steve,

My original intention was to get element rend(), however, the compiler complains because the variable is an iterator, not a reverse_iterator.

I wonder as I wander...

http://www.davesgameoflife.com


#4 Zipster   Crossbones+   -  Reputation: 545

Like
2Likes
Like

Posted 27 November 2012 - 02:36 AM

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).

#5 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 27 November 2012 - 03:47 AM

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;
}

I wonder as I wander...

http://www.davesgameoflife.com





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS