Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_WriteBufferOperation.cpp « operations « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fccff2a33bf3f4cfbae621b7a7e0f6ba8c3a9b35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright 2011, Blender Foundation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor: 
 *		Jeroen Bakker 
 *		Monique Dewanchand
 */

#include "COM_WriteBufferOperation.h"
#include "COM_defines.h"
#include <stdio.h>
#include "COM_OpenCLDevice.h"

WriteBufferOperation::WriteBufferOperation(DataType datatype) : NodeOperation()
{
	this->addInputSocket(datatype);
	this->m_memoryProxy = new MemoryProxy(datatype);
	this->m_memoryProxy->setWriteBufferOperation(this);
	this->m_memoryProxy->setExecutor(NULL);
}
WriteBufferOperation::~WriteBufferOperation()
{
	if (this->m_memoryProxy) {
		delete this->m_memoryProxy;
		this->m_memoryProxy = NULL;
	}
}

void WriteBufferOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
{
	this->m_input->readSampled(output, x, y, sampler);
}

void WriteBufferOperation::initExecution()
{
	this->m_input = this->getInputOperation(0);
	this->m_memoryProxy->allocate(this->m_width, this->m_height);
}

void WriteBufferOperation::deinitExecution()
{
	this->m_input = NULL;
	this->m_memoryProxy->free();
}

void WriteBufferOperation::executeRegion(rcti *rect, unsigned int tileNumber)
{
	MemoryBuffer *memoryBuffer = this->m_memoryProxy->getBuffer();
	float *buffer = memoryBuffer->getBuffer();
	const int num_channels = memoryBuffer->get_num_channels();
	if (this->m_input->isComplex()) {
		void *data = this->m_input->initializeTileData(rect);
		int x1 = rect->xmin;
		int y1 = rect->ymin;
		int x2 = rect->xmax;
		int y2 = rect->ymax;
		int x;
		int y;
		bool breaked = false;
		for (y = y1; y < y2 && (!breaked); y++) {
			int offset4 = (y * memoryBuffer->getWidth() + x1) * num_channels;
			for (x = x1; x < x2; x++) {
				this->m_input->read(&(buffer[offset4]), x, y, data);
				offset4 += num_channels;
			}
			if (isBreaked()) {
				breaked = true;
			}

		}
		if (data) {
			this->m_input->deinitializeTileData(rect, data);
			data = NULL;
		}
	}
	else {
		int x1 = rect->xmin;
		int y1 = rect->ymin;
		int x2 = rect->xmax;
		int y2 = rect->ymax;

		int x;
		int y;
		bool breaked = false;
		for (y = y1; y < y2 && (!breaked); y++) {
			int offset4 = (y * memoryBuffer->getWidth() + x1) * num_channels;
			for (x = x1; x < x2; x++) {
				this->m_input->readSampled(&(buffer[offset4]), x, y, COM_PS_NEAREST);
				offset4 += num_channels;
			}
			if (isBreaked()) {
				breaked = true;
			}
		}
	}
	memoryBuffer->setCreatedState();
}

void WriteBufferOperation::executeOpenCLRegion(OpenCLDevice *device, rcti *rect, unsigned int chunkNumber,
                                               MemoryBuffer **inputMemoryBuffers, MemoryBuffer *outputBuffer)
{
	float *outputFloatBuffer = outputBuffer->getBuffer();
	cl_int error;
	/*
	 * 1. create cl_mem from outputbuffer
	 * 2. call NodeOperation (input) executeOpenCLChunk(.....)
	 * 3. schedule readback from opencl to main device (outputbuffer)
	 * 4. schedule native callback
	 *
	 * note: list of cl_mem will be filled by 2, and needs to be cleaned up by 4
	 */
	// STEP 1
	const unsigned int outputBufferWidth = outputBuffer->getWidth();
	const unsigned int outputBufferHeight = outputBuffer->getHeight();

	const cl_image_format *imageFormat = device->determineImageFormat(outputBuffer);

	cl_mem clOutputBuffer = clCreateImage2D(device->getContext(), CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, imageFormat, outputBufferWidth, outputBufferHeight, 0, outputFloatBuffer, &error);
	if (error != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error));  }
	
	// STEP 2
	list<cl_mem> *clMemToCleanUp = new list<cl_mem>();
	clMemToCleanUp->push_back(clOutputBuffer);
	list<cl_kernel> *clKernelsToCleanUp = new list<cl_kernel>();

	this->m_input->executeOpenCL(device, outputBuffer, clOutputBuffer, inputMemoryBuffers, clMemToCleanUp, clKernelsToCleanUp);

	// STEP 3

	size_t origin[3] = {0, 0, 0};
	size_t region[3] = {outputBufferWidth, outputBufferHeight, 1};

//	clFlush(queue);
//	clFinish(queue);

	error = clEnqueueBarrier(device->getQueue());
	if (error != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error));  }
	error = clEnqueueReadImage(device->getQueue(), clOutputBuffer, CL_TRUE, origin, region, 0, 0, outputFloatBuffer, 0, NULL, NULL);
	if (error != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error));  }
	
	this->getMemoryProxy()->getBuffer()->copyContentFrom(outputBuffer);

	// STEP 4
	while (!clMemToCleanUp->empty()) {
		cl_mem mem = clMemToCleanUp->front();
		error = clReleaseMemObject(mem);
		if (error != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error));  }
		clMemToCleanUp->pop_front();
	}

	while (!clKernelsToCleanUp->empty()) {
		cl_kernel kernel = clKernelsToCleanUp->front();
		error = clReleaseKernel(kernel);
		if (error != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error));  }
		clKernelsToCleanUp->pop_front();
	}
	delete clKernelsToCleanUp;
}

void WriteBufferOperation::determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2])
{
	NodeOperation::determineResolution(resolution, preferredResolution);
	/* make sure there is at least one pixel stored in case the input is a single value */
	m_single_value = false;
	if (resolution[0] == 0) {
		resolution[0] = 1;
		m_single_value = true;
	}
	if (resolution[1] == 0) {
		resolution[1] = 1;
		m_single_value = true;
	}
}

void WriteBufferOperation::readResolutionFromInputSocket()
{
	NodeOperation *inputOperation = this->getInputOperation(0);
	this->setWidth(inputOperation->getWidth());
	this->setHeight(inputOperation->getHeight());
}