Sign in to follow this  

Texture replaced when rendered to

This topic is 2049 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 there,

I have a 2D lighting engine that renders the game scene to the back buffer, renders the lighting map to a texture, and then renders the texture to the back buffer with a blend state
The issue I'm encountering is that every time I make a Draw call when rendering a light to be added to the light map, it completely replaces all data in the texture including its width and height
For example, let's say I make Draw call that will fill the entire light map texture with black, when I make another Draw call to render a light sprite to the texture, the entire texture is replaced by that single sprite and it also resizes as well
There is the possibly that this is not actually what is occurring, but all evidence from a fair amount of testing indicates this

What I'd like to do is render my lighting sprites to my light map texture just like I would render my object sprites to the back buffer, in that each successive Draw call only adds the new sprite on top of whatever else was there and only replaces certain pixels
I do not believe this is a blend state issue because I use the same one for rendering to the back buffer and the light map, and the back buffer rendering works just fine
Please provide an explanation as to what is going on and how to make this process function as I've described

Thanks

Share this post


Link to post
Share on other sites
Here's the setup:
[CODE]
// Lighting
ZeroMemory( &blendDesc, sizeof( blendDesc ) );
blendDesc.RenderTarget[0].BlendEnable = TRUE;
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_DEST_COLOR;
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
d3dDevice_->CreateBlendState( &blendDesc, &lightingBlendState_ );
float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };

// Light map and render target view
DXGI_SAMPLE_DESC sampleDesc = {
1,
0
};
D3D11_TEXTURE2D_DESC texDesc = {
1024,
768,
1,
1,
DXGI_FORMAT_R32G32B32A32_FLOAT,
sampleDesc,
D3D11_USAGE_DEFAULT,
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE,
NULL,
NULL
};
d3dDevice_->CreateTexture2D( &texDesc, NULL, ( ID3D11Texture2D** )&lightMap_ );
D3D11_RENDER_TARGET_VIEW_DESC renderDesc = {
DXGI_FORMAT_R32G32B32A32_FLOAT,
D3D11_RTV_DIMENSION_TEXTURE2D,
{ 0 },
};
d3dDevice_->CreateRenderTargetView( lightMap_, &renderDesc, &lightMapTarget_ );
D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesc = {
DXGI_FORMAT_R32G32B32A32_FLOAT,
D3D11_SRV_DIMENSION_TEXTURE2D,
0,
1
};
d3dDevice_->CreateShaderResourceView( lightMap_, &resourceDesc, &lightMapResource_ );
[/CODE]

And here's the render:
[CODE]
d3dContext_->OMSetRenderTargets( 1, &lightMapTarget_, NULL );

colorMap = dxiSprites_[kBlackSprite].GetShaderResourceView( );
d3dContext_->PSSetShaderResources( 0, 1, &colorMap );
spritePtr = 0;
HRESULT d3dResult = d3dContext_->Map( vertexBufferText_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapResource );
spritePtr = ( VertexPos* )mapResource.pData;
spritePtr[0].pos = XMFLOAT3( 1, -1, 0 );
spritePtr[1].pos = XMFLOAT3( -1, -1, 0 );
spritePtr[2].pos = XMFLOAT3( -1, 1, 0 );
spritePtr[3].pos = XMFLOAT3( -1, 1, 0 );
spritePtr[4].pos = XMFLOAT3( 1, 1, 0 );
spritePtr[5].pos = XMFLOAT3( 1, -1, 0 );
spritePtr[0].tex0 = XMFLOAT2( 1, 0 );
spritePtr[1].tex0 = XMFLOAT2( 0, 0 );
spritePtr[2].tex0 = XMFLOAT2( 0, 1 );
spritePtr[3].tex0 = XMFLOAT2( 0, 1 );
spritePtr[4].tex0 = XMFLOAT2( 1, 1 );
spritePtr[5].tex0 = XMFLOAT2( 1, 0 );
d3dContext_->Unmap( vertexBufferText_, 0 );
d3dContext_->Draw( 6, 0 );


stride = sizeof( VertexPos );
offset = 0;
d3dContext_->IASetInputLayout( inputLayout_ );
d3dContext_->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
d3dContext_->VSSetShader( solidColorVS_, 0, 0 );
d3dContext_->PSSetShader( solidColorPS_, 0, 0 );
d3dContext_->PSSetSamplers( 0, 1, &colorMapSampler_ );
for( int i = kNumberOfSprites; i < kNumberOfLights + kNumberOfSprites; i++ )
{
ID3D11Buffer* vertexBuffer = dxiSprites_[i].GetVertexBuffer( );
ID3D11ShaderResourceView* colorMap = dxiSprites_[i].GetShaderResourceView( );
d3dContext_->IASetVertexBuffers( 0, 1, &vertexBuffer, &stride, &offset );
d3dContext_->PSSetShaderResources( 0, 1, &colorMap );
if( i == kDarknessLight )
{
XMFLOAT2 pos = XMFLOAT2( 0.0f, 0.0f );
XMMATRIX world = dxiSprites_[i].CreateAWorldMatrix( pos, ( 360.0f - 0 ) * 0.0174532925f, XMFLOAT2( 1, 1 ) );
XMMATRIX mvp = XMMatrixMultiply( world, vpMatrix_ );
mvp = XMMatrixMultiply( mvp, XMMatrixTranslation( 512 / ( kWindowWidth / 2 ), 384 / ( kWindowHeight / 2 ), 0.0f ) );
mvp = XMMatrixTranspose( mvp );
d3dContext_->UpdateSubresource( mvpCB_, 0, 0, &mvp, 0, 0 );
d3dContext_->VSSetConstantBuffers( 0, 1, &mvpCB_ );
d3dContext_->Draw( 6, 0 );
}
for( int j = 0; j < drawData->spriteDataLength; j++ )
{
if( drawData->spriteData[j].index == kFlagshipSprite )
{
XMFLOAT2 pos = XMFLOAT2( 0.0f, 0.0f );
XMMATRIX world = dxiSprites_[i].CreateAWorldMatrix( pos, ( 360.0f - drawData->spriteData[j].rotation ) * 0.0174532925f, drawData->spriteData[j].scale );
XMMATRIX mvp = XMMatrixMultiply( world, vpMatrix_ );
mvp = XMMatrixMultiply( mvp, XMMatrixTranslation( drawData->spriteData[j].position.x / ( kWindowWidth / 2 ), drawData->spriteData[j].position.y / ( kWindowHeight / 2 ), 0.0f ) );
mvp = XMMatrixTranspose( mvp );
d3dContext_->UpdateSubresource( mvpCB_, 0, 0, &mvp, 0, 0 );
d3dContext_->VSSetConstantBuffers( 0, 1, &mvpCB_ );
d3dContext_->Draw( 6, 0 );
}
}
}

d3dContext_->OMSetRenderTargets( 1, &backBufferTarget_, NULL );
d3dContext_->OMSetBlendState( lightingBlendState_, blendfactor, 0xFFFFFFFF );
d3dContext_->PSSetShaderResources( 0, 1, &lightMapResource_ );
d3dContext_->Draw( 6, 0 );
[/CODE]
The first chunk of that fills the texture with darkness (black)
Then the second bit draws all the lighting sprites (usually white circles), and a darkness sprite
Darkness is drawn twice for debugging reasons, indicating that regardless of what's in the texture, it's replaced when a new Draw call is made
And the last piece draws the texture to the existing rendered frame with the correct blend state

Thanks in advance for any help Edited by TheResolute

Share this post


Link to post
Share on other sites
Off the top of my head:

You could be clearing the target before rendering the next iteration.
Your blending operations could be wrong.
If you're using shaders, the shader might not be updated with the new data on the next draw call.
Or there is an issue with your loop that draws.

Use PIX to debug so you can pin point where the issue is happening in code.

Share this post


Link to post
Share on other sites
[quote name='DJTN' timestamp='1336077544' post='4937205']
Off the top of my head:

You could be clearing the target before rendering the next iteration.
Your blending operations could be wrong.
If you're using shaders, the shader might not be updated with the new data on the next draw call.
Or there is an issue with your loop that draws.

Use PIX to debug so you can pin point where the issue is happening in code.
[/quote]

That PIX piece is an excellent suggestion
I'll have to figure that out and it might just be awesome so thank you

Share this post


Link to post
Share on other sites
[color=#000000][font=Arial,]
Turns out, and you'll notice this with those code samples, that I (in my infinite incompetence) do not set the correct vertex shader or buffer for drawing the entire texture2d to the back buffer[/font][/color][color=#000000][font=Arial,]
So, it used the last set vertex buffer (the one used to to draw the last thing rendered to the texture XD ) when it rendered the texture to the back buffer. This created the effect that I believed to be the texture2d being replaced by whatever was rendered to it[/font][/color][color=#000000][font=Arial,]
There you have it, just some silly little semantic error[/font][/color][color=#000000][font=Arial,]
Thank you all for trying to help[/font][/color]

Share this post


Link to post
Share on other sites

This topic is 2049 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.

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