Пример #1
0
    namespace graphics
    {
        DepthBuffer g_sceneDepthBuffer(1.0f);  // D32_FLOAT_S8_UINT
        ColorBuffer g_sceneColorBuffer(Color(0.2f, 0.4f, 0.6f));  // R11G11B10_FLOAT
        ColorBuffer g_overlayBuffer;    // R8G8B8A8_UNORM
        ColorBuffer g_imguiBuffer;      // R8G8B8A8_UNORM


        void InitializeRenderingBuffers(uint32_t bufferWidth, uint32_t bufferHeight)
        {
            GraphicsContext& initContext = GraphicsContext::Begin();

            g_sceneColorBuffer.Create(L"Main Color Buffer", bufferWidth, bufferHeight, 1, DXGI_FORMAT_R11G11B10_FLOAT);
            //g_sceneColorBuffer.Create(L"Main Color Buffer", bufferWidth, bufferHeight, 1, DXGI_FORMAT_R8G8B8A8_UNORM);

            g_sceneDepthBuffer.Create(L"Scene Depth Buffer", bufferWidth, bufferHeight, DXGI_FORMAT_D32_FLOAT);

            g_overlayBuffer.Create(L"UI Overlay", graphics::g_displayWidth, graphics::g_displayHeight, 1, DXGI_FORMAT_R8G8B8A8_UNORM);
            g_imguiBuffer.Create(L"imgui Overlay", graphics::g_displayWidth, graphics::g_displayHeight, 1, DXGI_FORMAT_R8G8B8A8_UNORM);
        }
        
        void DestroyRenderingBuffers()
        {
            g_sceneDepthBuffer.Destroy();
            g_sceneColorBuffer.Destroy();
            g_overlayBuffer.Destroy();
            g_imguiBuffer.Destroy();
        }
    }
Пример #2
0
 void DestroyRenderingBuffers()
 {
     g_sceneDepthBuffer.Destroy();
     g_sceneColorBuffer.Destroy();
     g_overlayBuffer.Destroy();
     g_imguiBuffer.Destroy();
 }
Пример #3
0
ColorBuffer *ImageFilter::copyColorBuffer(ColorBuffer *pColorBuffer)
{
   if (pColorBuffer == NULL)
   {
      return NULL;
   }

   // get the input color buffer and texture properties in order
   // to allocate a ColorBuffer object
   GLenum textureTarget = pColorBuffer->getTextureTarget();
   GLenum textureFormat = pColorBuffer->getTextureFormat();
   GLenum dataType = GL_FLOAT;

   int width = pColorBuffer->getWidth();
   int height = pColorBuffer->getHeight();
   unsigned int alpha = pColorBuffer->getAlpha();
   unsigned int numChannels = ImageUtilities::getNumColorChannels(textureFormat);

   GLint internalFormat = GL_FLOAT_R32_NV;
   if (numChannels == 2)
   {
      internalFormat = GL_FLOAT_RG32_NV;
   }
   else if (numChannels == 3)
   {
      internalFormat = GL_FLOAT_RGB32_NV;
   }
   else if (numChannels == 4)
   {
      internalFormat = GL_FLOAT_RGBA32_NV;
   }

   ColorBuffer* pNewColorBuffer = NULL;
   if (glewGetExtension("GL_EXT_framebuffer_object"))
   {
      // allocate color buffer
      pNewColorBuffer = new ColorBuffer(textureTarget, internalFormat, width, height, 
                                            textureFormat, dataType, alpha);
   }
#ifdef WIN_API
   else if (wglewGetExtension("WGL_ARB_pixel_format") && wglewGetExtension("WGL_ARB_pbuffer") &&
           wglewGetExtension("WGL_ARB_render_texture") && glewGetExtension("GL_NV_float_buffer"))
   {
      // allocate color buffer
      pNewColorBuffer = new ColorBuffer(width, height, textureFormat, dataType, alpha);
   }
#endif

   if (pNewColorBuffer != NULL)
   {
      // clear color buffer by setting values to zero
      pNewColorBuffer->clear();
   }

   return pNewColorBuffer;
}
Пример #4
0
        void InitializeRenderingBuffers(uint32_t bufferWidth, uint32_t bufferHeight)
        {
            GraphicsContext& initContext = GraphicsContext::Begin();

            g_sceneColorBuffer.Create(L"Main Color Buffer", bufferWidth, bufferHeight, 1, DXGI_FORMAT_R11G11B10_FLOAT);
            //g_sceneColorBuffer.Create(L"Main Color Buffer", bufferWidth, bufferHeight, 1, DXGI_FORMAT_R8G8B8A8_UNORM);

            g_sceneDepthBuffer.Create(L"Scene Depth Buffer", bufferWidth, bufferHeight, DXGI_FORMAT_D32_FLOAT);

            g_overlayBuffer.Create(L"UI Overlay", graphics::g_displayWidth, graphics::g_displayHeight, 1, DXGI_FORMAT_R8G8B8A8_UNORM);
            g_imguiBuffer.Create(L"imgui Overlay", graphics::g_displayWidth, graphics::g_displayHeight, 1, DXGI_FORMAT_R8G8B8A8_UNORM);
        }
Пример #5
0
void MaterialResource::SetSRVInternal(ColorBuffer& buffer, bool immediate)
{
	// Validate type
	if (m_type != ShaderResourceType::Unsupported)
	{
		assert_msg(IsSRVType(m_type),
			"MaterialResource is bound to a UAV, but an SRV is being assigned.");
	}

	m_cpuHandle = buffer.GetSRV();

	_ReadWriteBarrier();

	DispatchToRenderThread(m_cpuHandle, immediate);
}
void ParticleEffects::Render( CommandContext& Context, const Camera& Camera, ColorBuffer& ColorTarget, DepthBuffer& DepthTarget, ColorBuffer& LinearDepth)
{
	if (!Enable || !s_InitComplete || ParticleEffectsActive.size() == 0)
		return;

	ScopedTimer _prof(L"Particle Render", Context);

	uint32_t Width = (uint32_t)ColorTarget.GetWidth();
	uint32_t Height = (uint32_t)ColorTarget.GetHeight();
	uint32_t BinsPerRow = 4 * DivideByMultiple(Width, 4 * BIN_SIZE_X);

	s_ChangesPerView.gViewProj = Camera.GetViewProjMatrix();  
	s_ChangesPerView.gInvView = Invert(Camera.GetViewMatrix());
	float HCot = Camera.GetProjMatrix().GetX().GetX();
	float VCot = Camera.GetProjMatrix().GetY().GetY();
	s_ChangesPerView.gVertCotangent = VCot;
	s_ChangesPerView.gAspectRatio = HCot / VCot;
	s_ChangesPerView.gRcpFarZ = 1.0f / Camera.GetFarClip();
	s_ChangesPerView.gInvertZ = Camera.GetNearClip() / (Camera.GetFarClip() - Camera.GetNearClip());
	s_ChangesPerView.gBufferWidth = (float)ColorTarget.GetWidth();
	s_ChangesPerView.gBufferHeight = (float)ColorTarget.GetHeight();
	s_ChangesPerView.gRcpBufferWidth = 1.0f / ColorTarget.GetWidth();
	s_ChangesPerView.gRcpBufferHeight = 1.0f / ColorTarget.GetHeight();
	s_ChangesPerView.gBinsPerRow = BinsPerRow;
	s_ChangesPerView.gTileRowPitch = BinsPerRow * TILES_PER_BIN_X;
	s_ChangesPerView.gTilesPerRow = DivideByMultiple(Width, TILE_SIZE);
	s_ChangesPerView.gTilesPerCol = DivideByMultiple(Height, TILE_SIZE);


	if (EnableTiledRendering)
	{
		ComputeContext& CompContext = Context.GetComputeContext();
		CompContext.ClearUAV(BinCounters[0]);
		CompContext.ClearUAV(BinCounters[1]);
		CompContext.SetRootSignature(RootSig);
		CompContext.SetDynamicConstantBufferView(1, sizeof(CBChangesPerView), &s_ChangesPerView);	
		RenderTiles(CompContext, ColorTarget, LinearDepth);
	}
	else
	{
		GraphicsContext& GrContext = Context.GetGraphicsContext();
		GrContext.SetRootSignature(RootSig);
		GrContext.SetDynamicConstantBufferView(1, sizeof(CBChangesPerView), &s_ChangesPerView);	
		RenderSprites(GrContext, ColorTarget, DepthTarget, LinearDepth);
	}

}
void ParticleEffects::Render( CommandContext& Context, const Camera& Camera, ColorBuffer& ColorTarget, DepthBuffer& DepthTarget, ColorBuffer& LinearDepth)
{
	if (!Enable || !s_InitComplete || ParticleEffectsActive.size() == 0)
		return;

	uint32_t Width = (uint32_t)ColorTarget.GetWidth();
	uint32_t Height = (uint32_t)ColorTarget.GetHeight();

	ASSERT(
		Width == DepthTarget.GetWidth() &&
		Height == DepthTarget.GetHeight() &&
		Width == LinearDepth.GetWidth() &&
		Height == LinearDepth.GetHeight(),
		"There is a mismatch in buffer dimensions for rendering particles"
	);

	ScopedTimer _prof(L"Particle Render", Context);

	uint32_t BinsPerRow = 4 * DivideByMultiple(Width, 4 * BIN_SIZE_X);

	s_ChangesPerView.gViewProj = Camera.GetViewProjMatrix();  
	s_ChangesPerView.gInvView = Invert(Camera.GetViewMatrix());
	float HCot = Camera.GetProjMatrix().GetX().GetX();
	float VCot = Camera.GetProjMatrix().GetY().GetY();
	s_ChangesPerView.gVertCotangent = VCot;
	s_ChangesPerView.gAspectRatio = HCot / VCot;
	s_ChangesPerView.gRcpFarZ = 1.0f / Camera.GetFarClip();
	s_ChangesPerView.gInvertZ = Camera.GetNearClip() / (Camera.GetFarClip() - Camera.GetNearClip());
	s_ChangesPerView.gBufferWidth = (float)Width;
	s_ChangesPerView.gBufferHeight = (float)Height;
	s_ChangesPerView.gRcpBufferWidth = 1.0f / Width;
	s_ChangesPerView.gRcpBufferHeight = 1.0f / Height;
	s_ChangesPerView.gBinsPerRow = BinsPerRow;
	s_ChangesPerView.gTileRowPitch = BinsPerRow * TILES_PER_BIN_X;
	s_ChangesPerView.gTilesPerRow = DivideByMultiple(Width, TILE_SIZE);
	s_ChangesPerView.gTilesPerCol = DivideByMultiple(Height, TILE_SIZE);

	// For now, UAV load support for R11G11B10 is required to read-modify-write the color buffer, but
	// the compositing could be deferred.
	WARN_ONCE_IF(EnableTiledRendering && !g_bTypedUAVLoadSupport_R11G11B10_FLOAT,
		"Unable to composite tiled particles without support for R11G11B10F UAV loads");
	EnableTiledRendering = EnableTiledRendering && g_bTypedUAVLoadSupport_R11G11B10_FLOAT;

	if (EnableTiledRendering)
	{
		ComputeContext& CompContext = Context.GetComputeContext();
		CompContext.TransitionResource(ColorTarget, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
		CompContext.TransitionResource(BinCounters[0], D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
		CompContext.TransitionResource(BinCounters[1], D3D12_RESOURCE_STATE_UNORDERED_ACCESS, true);

		CompContext.ClearUAV(BinCounters[0]);
		CompContext.ClearUAV(BinCounters[1]);
		CompContext.SetRootSignature(RootSig);
		CompContext.SetDynamicConstantBufferView(1, sizeof(CBChangesPerView), &s_ChangesPerView);

		RenderTiles(CompContext, ColorTarget, LinearDepth);

		CompContext.InsertUAVBarrier(ColorTarget);
	}
	else
	{
		GraphicsContext& GrContext = Context.GetGraphicsContext();
		GrContext.SetRootSignature(RootSig);
		GrContext.SetDynamicConstantBufferView(1, sizeof(CBChangesPerView), &s_ChangesPerView);	
		RenderSprites(GrContext, ColorTarget, DepthTarget, LinearDepth);
	}

}
Пример #8
0
	void GraphicsContext::ClearRenderTarget(const ColorBuffer& target, const float4& color)
	{
		deviceContext_->ClearRenderTargetView(target.GetRTV(), color.ToFloatArray());
	}
Пример #9
0
bool GpuResourceManager::determineScalingFactor(float& scalingFactor, GLenum textureFormat)
{
   bool factorFound = false;
   scalingFactor = 3.0f;  // value for ForceWare versions 94.22 and earlier

#ifdef CG_SUPPORTED
   unsigned int texWidth(64);
   unsigned int texHeight(64);

   // create color buffer object to be used to load data to the graphics card
   GLenum dataType = ImageUtilities::convertEncodingType(FLT4BYTES);
   GLint internalFormat = ImageUtilities::getInternalFormat(dataType, textureFormat);
   unsigned int alpha(255);
   auto_ptr<ColorBuffer> pColorBuffer(new (nothrow) ColorBuffer(GL_TEXTURE_RECTANGLE_ARB, internalFormat, 
      texWidth, texHeight, textureFormat, dataType, alpha));

   if ((pColorBuffer.get() != NULL) && (pColorBuffer->getTextureObjectId() != 0))
   {
      auto_ptr<ImageLoader> pImageLoader(new (nothrow) ImageLoader(pColorBuffer.get()));

      vector<float> testData(texHeight * texWidth * ImageUtilities::getNumColorChannels(textureFormat), alpha);
      for (unsigned int row = 0; row < texHeight; ++row)
      {
         for (unsigned int col = 0; col < texWidth; ++col)
         {
            const unsigned int offset = (row * texWidth + col) * ImageUtilities::getNumColorChannels(textureFormat);
            testData[offset] = static_cast<float>(col + 1);
         }
      }
      void* pData = reinterpret_cast<void*>(&testData.front());
      pImageLoader->loadData(pData);

      Service<ImageFilterManager> pManager;

      // ImageFilterDescriptor's destructor is protected so wrap the imp in auto-ptr since
      // we have to destroy it when finished.
      auto_ptr<ImageFilterDescriptorImp> pFilterDesc(dynamic_cast<ImageFilterDescriptorImp*>(
         pManager->createFilterDescriptor("ByPass")));
      ImageFilter filter(dynamic_cast<ImageFilterDescriptor*>(pFilterDesc.get()));
      filter.setImage(pColorBuffer.release());
      ColorBuffer* pResultsBuffer = filter.applyFilter();

      internalFormat = pResultsBuffer->getInternalFormat();
      textureFormat = pResultsBuffer->getTextureFormat();
      dataType = pResultsBuffer->getDataType();
      alpha = pResultsBuffer->getAlpha();
      int numValues = texWidth * texHeight * ImageUtilities::getNumColorChannels(textureFormat);

      ImageBuffer* pImageBuffer = filter.getImageBuffer(pResultsBuffer);
      pImageBuffer->readFromBuffer(pResultsBuffer);

      auto_ptr<ImageLoader> pImageReader(new (nothrow) ImageLoader(pResultsBuffer));

      vector<float> data(numValues);
      GLvoid* pPixels = reinterpret_cast<GLvoid*>(&data.front());
      pImageReader->read(0, 0, texWidth, texHeight, textureFormat, dataType, pPixels);
      filter.setImage(NULL);
      pImageBuffer->detachBuffer(pResultsBuffer);

      // make sure something was actually read back from filter buffer
      if (data.front() > 0.0f)
      {
         scalingFactor = data.front();
         factorFound = true;
      }
   }
#else
   factorFound = true;
#endif

   return factorFound;
}
Пример #10
0
void MotionBlur::RenderObjectBlur( CommandContext& BaseContext, ColorBuffer& velocityBuffer )
{
	ScopedTimer _prof(L"MotionBlur", BaseContext);

	if (!Enable)
		return;

	uint32_t Width = g_SceneColorBuffer.GetWidth();
	uint32_t Height = g_SceneColorBuffer.GetHeight();

	ComputeContext& Context = BaseContext.GetComputeContext();

	Context.SetRootSignature(s_RootSignature);

	Context.TransitionResource(g_MotionPrepBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
	Context.TransitionResource(g_SceneColorBuffer, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
	Context.TransitionResource(velocityBuffer, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);

	Context.SetDynamicDescriptor(2, 0, g_MotionPrepBuffer.GetUAV());
	Context.SetDynamicDescriptor(3, 0, g_SceneColorBuffer.GetSRV());
	Context.SetDynamicDescriptor(3, 1, velocityBuffer.GetSRV());

	Context.SetPipelineState(s_MotionBlurPrePassCS);
	Context.Dispatch2D(g_MotionPrepBuffer.GetWidth(), g_MotionPrepBuffer.GetHeight());

	if (g_bTypedUAVLoadSupport_R11G11B10_FLOAT)
	{
		Context.SetPipelineState(s_MotionBlurFinalPassCS);

		Context.TransitionResource(g_SceneColorBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
		Context.TransitionResource(velocityBuffer, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
		Context.TransitionResource(g_MotionPrepBuffer, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);

		Context.SetDynamicDescriptor(2, 0, g_SceneColorBuffer.GetUAV());
		Context.SetDynamicDescriptor(3, 0, velocityBuffer.GetSRV());
		Context.SetDynamicDescriptor(3, 1, g_MotionPrepBuffer.GetSRV());
		Context.SetConstants(0, 1.0f / Width, 1.0f / Height);

		Context.Dispatch2D(Width, Height);

		Context.InsertUAVBarrier(g_SceneColorBuffer);
	}
	else
	{
		GraphicsContext& GrContext = BaseContext.GetGraphicsContext();
		GrContext.SetRootSignature(s_RootSignature);
		GrContext.SetPipelineState(s_MotionBlurFinalPassPS);

		GrContext.TransitionResource(g_SceneColorBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET);
		GrContext.TransitionResource(g_VelocityBuffer, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
		GrContext.TransitionResource(g_MotionPrepBuffer, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);

		GrContext.SetDynamicDescriptor(3, 0, g_VelocityBuffer.GetSRV());
		GrContext.SetDynamicDescriptor(3, 1, g_MotionPrepBuffer.GetSRV());
		GrContext.SetConstants(0, 1.0f / Width, 1.0f / Height);
		GrContext.SetRenderTarget(g_SceneColorBuffer.GetRTV());
		GrContext.SetViewportAndScissor(0, 0, Width, Height);

		GrContext.Draw(3);
	}
}
Пример #11
0
bool ImageFilter::populateTextureParameters(ColorBuffer* pInputColorBuffer)
{
   if (pInputColorBuffer == NULL)
   {
      return false;
   }

#if defined(CG_SUPPORTED)
   CgContext* pCgContext = CgContext::instance();
   if (pCgContext == NULL)
   {
      return false;
   }
#endif

   bool success = false;

   vector<GpuProgram*>::iterator gpuProgramIter = mGpuPrograms.begin();
   map<string, ColorBuffer*> colorBuffers;

   // put the input image parameter value into the GPU program descriptor parameter lists
   while (gpuProgramIter != mGpuPrograms.end())
   {
      GpuProgramDescriptor& gpuProgramDescriptor((*gpuProgramIter)->getGpuProgramDescriptor());

      const DynamicObject* pParameters = gpuProgramDescriptor.getParameters();
      if (pParameters == NULL)
      {
         return false;
      }

      string parameterName;
      DataVariant parameterValue;
#if defined(CG_SUPPORTED)
      vector<CGparameter> programParameters = 
         pCgContext->getParameters((*gpuProgramIter)->getProgramId());
      vector<CGparameter>::const_iterator progParameterIter = programParameters.begin();
      while (progParameterIter != programParameters.end())
      {
         parameterName = cgGetParameterName(*progParameterIter);
         parameterValue = pParameters->getAttribute(parameterName);
         if (parameterValue.isValid())
         {
            // get the type of the parameter
            CGtype cgParameterType = cgGetParameterNamedType(*progParameterIter);
            if (cgParameterType == CG_SAMPLERRECT)
            {
               // check to see if color buffer was already created
               map<string, ColorBuffer*>::const_iterator colorBufferIter = colorBuffers.find(parameterName);
               if (colorBufferIter == colorBuffers.end())
               {
                  if (colorBuffers.empty())
                  {
                     DataVariant value(pInputColorBuffer->getTextureObjectId());
                     if (value.isValid())
                     {
                        gpuProgramDescriptor.setParameter(parameterName, value);
                     }

                     colorBuffers.insert(pair<const string, ColorBuffer*>(parameterName, pInputColorBuffer));

                  }
                  else
                  {
                     ColorBuffer* pColorBuffer = copyColorBuffer(pInputColorBuffer);
                     if (pColorBuffer == NULL)
                     {
                        return false;
                     }

                     success = attachToImageBuffer(pColorBuffer);
                     if (success == false)
                     {
                        delete pColorBuffer;
                        pColorBuffer = NULL;
                        return false;
                     }

                     DataVariant value(pColorBuffer->getTextureObjectId());
                     if (value.isValid())
                     {
                        gpuProgramDescriptor.setParameter(parameterName, value);
                     }

                     colorBuffers.insert(pair<const string, ColorBuffer*>(parameterName, pColorBuffer));

                     mBuffers.push_back(pColorBuffer);  
                  }
               }
               else
               {
                  DataVariant value(colorBufferIter->second->getTextureObjectId());
                  if (value.isValid())
                  {
                     gpuProgramDescriptor.setParameter(parameterName, value);
                  }
               }
            }
         }

         ++progParameterIter;
      }
#endif
      ++gpuProgramIter;
   }

   // get the number of off-screen color buffers
   // NOTE: There is one input color buffer.
   size_t numColorBuffers = colorBuffers.size() - 1;
   if (numColorBuffers < mGpuPrograms.size())
   {
      // set which color buffer to render to first
      ColorBuffer* pColorBuffer = copyColorBuffer(pInputColorBuffer);
      if (pColorBuffer == NULL)
      {
         return false;
      }

      success = attachToImageBuffer(pColorBuffer);
      if (success)
      {
         string resultsBufferName = "resultsBuffer";
         colorBuffers.insert(pair<const string, ColorBuffer*>(resultsBufferName, pColorBuffer));

         mBuffers.push_back(pColorBuffer);
      }
      else
      {
         delete pColorBuffer;
         pColorBuffer = NULL;
         return false;
      }
   }
   
   // check the color buffers vector
   if (colorBuffers.empty())
   {
      return false;
   }

   gpuProgramIter = mGpuPrograms.begin();
   while (gpuProgramIter != mGpuPrograms.end())
   {
      (*gpuProgramIter)->setColorBuffers(colorBuffers);
      ++gpuProgramIter;
   }

   if (getFilterType() == ImageFilterDescriptor::FEEDBACK_FILTER && mRunBackwards == true)
   {
      reverse(mBuffers.begin(), mBuffers.end());

      mpResultsBuffer = mBuffers.front();
   }
   else
   {
      mpResultsBuffer = mBuffers.back();
   }

   return success;
}