<commit_before>/* rapt - RichieSam's Adventures in Path Tracing
 *
 * rapt is the legal property of <NAME>
 * Copyright <NAME> 2015
 */

#include "basic_path_tracer/basic_path_tracer.h"

#include "graphics/cuda_util.h"
#include "graphics/cuda_texture2d.h"
#include "graphics/d3d_texture2d.h"
#include "graphics/d3d_util.h"

#include <cuda_d3d11_interop.h>


void PathTraceNextFrame(void *buffer, uint width, uint height, size_t pitch, DeviceCamera *camera, Scene::Sphere *spheres, uint numSpheres, uint frameNumber);


namespace BasicPathTracer {

void BasicPathTracer::DrawFrame() {
	if (m_cameraMoved) {
		m_hostCamera.UpdateDeviceCameraWithInternalState(d_deviceCamera);
		m_cameraMoved = false;
	}

	// Render the next frame using a CUDA kernel
	PathTraceNextFrame(m_hdrTextureCuda->GetTextureData(), m_clientWidth, m_clientHeight, m_hdrTextureCuda->GetTexturePitch(), d_deviceCamera, d_spheres, m_numSpheres, m_frameNumber++);


	////////////////////////////////////////////////////////////////////////////////
	// Copy the data over to DirectX
	////////////////////////////////////////////////////////////////////////////////

	// We have to wrap the memcopy in a pair of cudaGraphics(Un)MapResources() calls
	// This creates a synchronization point that guarantees that all CUDA kernels 
	// have finished, and DirectX is finished using the resources.

	// Map the resources
	cudaGraphicsResource *resource = m_hdrTextureCuda->GetCurrentGraphicsResource();
	CE(cudaGraphicsMapResources(1, &resource));	

	// Copy the frame over to the D3D texture
	m_hdrTextureCuda->CopyTextureDataToRegisteredResource();

	// Unmap the resources
	CE(cudaGraphicsUnmapResources(1, &resource));


	////////////////////////////////////////////////////////////////////////////////
	// Use DirectX to copy the texture to screen
	////////////////////////////////////////////////////////////////////////////////

	// Write the constant buffer data for the pixel shader
	float invFrameCount = 1.0f / m_frameNumber;

	D3D11_MAPPED_SUBRESOURCE mappedResource;

	// Lock the constant buffer so it can be written to.
	HR(m_immediateContext->Map(m_copyCudaOutputPSConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));
	memcpy(mappedResource.pData, &invFrameCount, sizeof(float));
	m_immediateContext->Unmap(m_copyCudaOutputPSConstantBuffer, 0);

	m_immediateContext->PSSetConstantBuffers(0u, 1u, &m_copyCudaOutputPSConstantBuffer);

	// Draw the frame to the screen
	m_immediateContext->Draw(3u, 0u);

	m_swapChain->Present(0u, 0u);
}

} // End of namespace BasicPathTracer<commit_msg>BASIC_PATH_TRACER: Add comments to better explain the frame drawing<commit_after>/* rapt - RichieSam's Adventures in Path Tracing
 *
 * rapt is the legal property of <NAME>
 * Copyright <NAME> 2015
 */

#include "basic_path_tracer/basic_path_tracer.h"

#include "graphics/cuda_util.h"
#include "graphics/cuda_texture2d.h"
#include "graphics/d3d_texture2d.h"
#include "graphics/d3d_util.h"

#include <cuda_d3d11_interop.h>


void PathTraceNextFrame(void *buffer, uint width, uint height, size_t pitch, DeviceCamera *camera, Scene::Sphere *spheres, uint numSpheres, uint frameNumber);


namespace BasicPathTracer {

void BasicPathTracer::DrawFrame() {
	////////////////////////////////////////////////////////////////////////////////
	// Use CUDA to path trace the next frame
	// and add it to the accumulation buffer
	////////////////////////////////////////////////////////////////////////////////

	// If the camera has moved, copy the new camera data to the GPU
	if (m_cameraMoved) {
		m_hostCamera.UpdateDeviceCameraWithInternalState(d_deviceCamera);
		m_cameraMoved = false;
	}

	// Launch the CUDA kernel
	PathTraceNextFrame(m_hdrTextureCuda->GetTextureData(), m_clientWidth, m_clientHeight, m_hdrTextureCuda->GetTexturePitch(), d_deviceCamera, d_spheres, m_numSpheres, m_frameNumber++);


	////////////////////////////////////////////////////////////////////////////////
	// Copy the accumulation buffer over to DirectX
	////////////////////////////////////////////////////////////////////////////////

	// We have to wrap the memcopy in a pair of cudaGraphics(Un)MapResources() calls
	// This creates a synchronization point that guarantees that all CUDA kernels 
	// have finished, and DirectX is finished using the resources.

	// Map the resources
	cudaGraphicsResource *resource = m_hdrTextureCuda->GetCurrentGraphicsResource();
	CE(cudaGraphicsMapResources(1, &resource));	

	// Copy the frame over to the D3D texture
	m_hdrTextureCuda->CopyTextureDataToRegisteredResource();

	// Unmap the resources
	CE(cudaGraphicsUnmapResources(1, &resource));


	////////////////////////////////////////////////////////////////////////////////
	// Use DirectX to render the accumulation buffer to the screen
	////////////////////////////////////////////////////////////////////////////////

	// Write the constant buffer data for the pixel shader
	float invFrameCount = 1.0f / m_frameNumber;

	D3D11_MAPPED_SUBRESOURCE mappedResource;

	// Lock the constant buffer so it can be written to.
	HR(m_immediateContext->Map(m_copyCudaOutputPSConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));
	memcpy(mappedResource.pData, &invFrameCount, sizeof(float));
	m_immediateContext->Unmap(m_copyCudaOutputPSConstantBuffer, 0);

	m_immediateContext->PSSetConstantBuffers(0u, 1u, &m_copyCudaOutputPSConstantBuffer);

	// Draw a fullscreen triangle
	m_immediateContext->Draw(3u, 0u);

	m_swapChain->Present(0u, 0u);
}

} // End of namespace BasicPathTracer
{'fraction_non_alphanumeric': 0.2216998191681736, 'fraction_numerical': 0.011211573236889693, 'mean_word_length': 2.603257328990228, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '930062', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1530, 'n_words': 414}