Ejemplo n.º 1
0
KIHX_API void kiRenderToBuffer( void* buffer, int width, int height, int bpp )
{
	if ( buffer == nullptr )
	{
		return;
	}
	
	__UNDONE( temporal testing code );

	// Create color and depth stencil buffers.
	ColorFormat colorFormat = kih::GetSuitableColorFormatFromBpp( bpp );
	static std::shared_ptr<Texture> renderTarget = Texture::Create( width, height, colorFormat, buffer );
	static std::shared_ptr<Texture> depthStencil = Texture::Create( width, height, ColorFormat::D32F, NULL );
	static std::shared_ptr<UnorderedAccessView<OutputMergerInputStream>> omUAV = std::make_shared<UnorderedAccessView<OutputMergerInputStream>>( width * height * 2 );

	// Create rendering contexts as many of Thread::HardwareConcurrency.
	const int Concurrency = 8;	// HACK
	static std::array<std::shared_ptr<RenderingContext>, Concurrency> contexts;
	if ( contexts[0] == nullptr )
	{
		int index = 0;
		LoopUnroll<Concurrency>::Work( 
			[&index]() 
			{ 
				auto context = RenderingDevice::GetInstance()->CreateRenderingContext();
				context->SetViewport( 0, 0, static_cast<unsigned short>( renderTarget->Width() ), static_cast<unsigned short>( renderTarget->Height() ) );
				context->SetFixedPipelineMode( false );
				
				context->SetRenderTarget( renderTarget, 0 );
				context->SetDepthStencil( depthStencil );

				contexts[index++] = context;
			} 
		);
	}
	
	auto context = contexts[0];

	context->Clear( 0, 0, 0, 255 );
	context->SetUnorderedAccessView( nullptr );

	// Draw the world.
	const Matrix4& matWorld = RenderingDevice::GetInstance()->GetWorldMatrix();

	// Grid arragement
	if ( grid_scene.Bool() )
	{
		// threading or not
		if ( concurrency.Int() <= 1 )
		{
			DrawGridScene( context, matWorld );
		}
		else
		{
			// Copy array contexts to vector one.
			StlVector<std::shared_ptr<RenderingContext>> concurrencyContexts;
			concurrencyContexts.resize( concurrency.Int() - 1 );		// the other thread is for resolve.	
			for ( size_t i = 0; i < concurrencyContexts.size(); ++i )
			{
				contexts[i]->SetUnorderedAccessView( omUAV );
				concurrencyContexts[i] = contexts[i];
			}

			DrawGridSceneInParallel( concurrencyContexts, matWorld );
		}
	}
	else
	{
		context->GetSharedConstantBuffer().SetMatrix4( ConstantBuffer::WorldMatrix, matWorld );
		context->GetSharedConstantBuffer().SetVector4( ConstantBuffer::DiffuseColor, Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
		context->Draw( GetMeshes()[0] );
	}
}