Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Texture replaced when rendered to


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
6 replies to this topic

#1 TheResolute   Members   -  Reputation: 187

Like
0Likes
Like

Posted 02 May 2012 - 10:18 AM

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

Sponsor:

#2 TheResolute   Members   -  Reputation: 187

Like
0Likes
Like

Posted 03 May 2012 - 01:17 AM

Any help at all would be greatly appreciated

#3 kauna   Crossbones+   -  Reputation: 2748

Like
0Likes
Like

Posted 03 May 2012 - 03:57 AM

Can you show some code perhaps? Like part where you draw your light sprites?

Best regards!

#4 TheResolute   Members   -  Reputation: 187

Like
0Likes
Like

Posted 03 May 2012 - 07:59 AM

Here's the setup:
    // 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_ );

And here's the render:
    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 );
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, 03 May 2012 - 08:03 AM.


#5 DJTN   Members   -  Reputation: 207

Like
1Likes
Like

Posted 03 May 2012 - 02:39 PM

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.

#6 TheResolute   Members   -  Reputation: 187

Like
0Likes
Like

Posted 03 May 2012 - 02:54 PM

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.


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

#7 TheResolute   Members   -  Reputation: 187

Like
0Likes
Like

Posted 03 May 2012 - 03:41 PM


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

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

There you have it, just some silly little semantic error

Thank you all for trying to help





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