Learning ray tracing (15) concurrent computation ---

0. Introduction

The're coming, as the number of objects in the scene, increasing the complexity of the reflected light, calculation time became a problem, generating an image of 4096x4096 better tens of minutes, in which if added after refraction calculation, random diffuse reflectance can not see the result of the estimation.

1. Preliminary concurrent modifications

Concurrent processing can be appropriately accelerated, I made the following changes in the main function.

This is the original.

	for (int i = 0; i < img.rows; i++)
	{
		for (int j = 0; j < img.cols; j++)
		{
			vec3 color = render(i, j, img.rows, img.cols, s, camera).color;
			if (color.z > 255)
				color.z = 255;
			if (color.y > 255)
				color.y = 255;
			if (color.x > 255)
				color.x = 255;
			img.at<Vec3b>(i, j) = Vec3b(color.z, color.y, color.x);
		}
	}

It is now, in fact, there had been early in the source code, but I have been secretly using ^ _ ^.

thread t1([&]() {
		for (int i = 0; i < img.rows/2; i++)
		{
			for (int j = 0; j < img.cols/2; j++)
			{
				vec3 color = render(i, j, img.rows, img.cols, s, camera).color;
				if (color.z > 255)
					color.z = 255;
				if (color.y > 255)
					color.y = 255;
				if (color.x > 255)
					color.x = 255;
				img.at<Vec3b>(i, j) = Vec3b(color.z, color.y, color.x);
			}
		}
		});
	thread t2([&]() {
		for (int i = img.rows/2; i < img.rows; i++)
		{
			for (int j = 0; j < img.cols/2; j++)
			{
				vec3 color = render(i, j, img.rows, img.cols, s, camera).color;
				if (color.z > 255)
					color.z = 255;
				if (color.y > 255)
					color.y = 255;
				if (color.x > 255)
					color.x = 255;
				img.at<Vec3b>(i, j) = Vec3b(color.z, color.y, color.x);
			}
		}
		});
	thread t3([&]() {
		for (int i = 0; i < img.rows/2; i++)
		{
			for (int j = img.cols / 2; j < img.cols; j++)
			{
				vec3 color = render(i, j, img.rows, img.cols, s, camera).color;
				if (color.z > 255)
					color.z = 255;
				if (color.y > 255)
					color.y = 255;
				if (color.x > 255)
					color.x = 255;
				img.at<Vec3b>(i, j) = Vec3b(color.z, color.y, color.x);
			}
		}
		});
	thread t4([&]() {
		for (int i = img.rows / 2; i < img.rows; i++)
		{
			for (int j = img.cols / 2; j < img.cols; j++)
			{
				vec3 color = render(i, j, img.rows, img.cols, s, camera).color;
				if (color.z > 255)
					color.z = 255;
				if (color.y > 255)
					color.y = 255;
				if (color.x > 255)
					color.x = 255;
				img.at<Vec3b>(i, j) = Vec3b(color.z, color.y, color.x);
			}
		}
		});
	t1.join();
	t2.join();
	t3.join();
	t4.join();

The image is divided into four, while the calculation, because the read data conflict does not occur, so this it.

2. expand

Recent always encounter something about concurrency, and I gradually discovered this knowledge to add something, so buy a related book to learn and is still learning.

3. Source

release0.10

Published 64 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/ARTELE/article/details/103971087