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

bake.cpp « render « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e3022e2c026417b03ebb2e0c2059b56485614ff (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
/*
 * Copyright 2011-2014 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

#include "bake.h"

CCL_NAMESPACE_BEGIN

BakeData::BakeData(const int object, const int tri_offset, const int num_pixels):
m_object(object),
m_tri_offset(tri_offset),
m_num_pixels(num_pixels)
{
	m_primitive.resize(num_pixels);
	m_u.resize(num_pixels);
	m_v.resize(num_pixels);
}

BakeData::~BakeData()
{
	m_primitive.clear();
	m_u.clear();
	m_v.clear();
}

void BakeData::set(int i, int prim, float uv[2])
{
	m_primitive[i] = (prim == -1 ? -1 : m_tri_offset + prim);
	m_u[i] = uv[0];
	m_v[i] = uv[1];
}

int BakeData::object()
{
	return m_object;
}

int BakeData::size()
{
	return m_num_pixels;
}

bool BakeData::is_valid(int i)
{
	return m_primitive[i] != -1;
}

uint4 BakeData::data(int i)
{
	return make_uint4(
		m_object,
		m_primitive[i],
		__float_as_int(m_u[i]),
		__float_as_int(m_v[i])
		);
}

BakeManager::BakeManager()
{
	m_bake_data = NULL;
	m_is_baking = false;
	need_update = true;
}

BakeManager::~BakeManager()
{
	if(m_bake_data)
		delete m_bake_data;
}

bool BakeManager::get_baking()
{
	return m_is_baking;
}

void BakeManager::set_baking(const bool value)
{
	m_is_baking = value;
}

BakeData *BakeManager::init(const int object, const int tri_offset, const int num_pixels)
{
	m_bake_data = new BakeData(object, tri_offset, num_pixels);
	return m_bake_data;
}

bool BakeManager::bake(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress, ShaderEvalType shader_type, BakeData *bake_data, float result[])
{
	size_t limit = bake_data->size();

	/* setup input for device task */
	device_vector<uint4> d_input;
	uint4 *d_input_data = d_input.resize(limit);
	size_t d_input_size = 0;

	for(size_t i = 0; i < limit; i++) {
		d_input_data[d_input_size++] = bake_data->data(i);
	}

	if(d_input_size == 0)
		return false;

	/* run device task */
	device_vector<float4> d_output;
	d_output.resize(d_input_size);

	/* needs to be up to data for attribute access */
	device->const_copy_to("__data", &dscene->data, sizeof(dscene->data));

	device->mem_alloc(d_input, MEM_READ_ONLY);
	device->mem_copy_to(d_input);
	device->mem_alloc(d_output, MEM_WRITE_ONLY);

	DeviceTask task(DeviceTask::SHADER);
	task.shader_input = d_input.device_pointer;
	task.shader_output = d_output.device_pointer;
	task.shader_eval_type = shader_type;
	task.shader_x = 0;
	task.shader_w = d_output.size();
	task.get_cancel = function_bind(&Progress::get_cancel, &progress);

	device->task_add(task);
	device->task_wait();

	if(progress.get_cancel()) {
		device->mem_free(d_input);
		device->mem_free(d_output);
		m_is_baking = false;
		return false;
	}

	device->mem_copy_from(d_output, 0, 1, d_output.size(), sizeof(float4));
	device->mem_free(d_input);
	device->mem_free(d_output);

	/* read result */
	int k = 0;

	float4 *offset = (float4*)d_output.data_pointer;

	size_t depth = 4;
	for(size_t i = 0; i < limit; i++) {
		size_t index = i * depth;
		float4 out = offset[k++];

		if(bake_data->is_valid(i)) {
			for(size_t j=0; j < 4; j++) {
				result[index + j] = out[j];
			}
		}
	}

	m_is_baking = false;
	return true;
}

void BakeManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
{
	if(!need_update)
		return;

	if(progress.get_cancel()) return;

	need_update = false;
}

void BakeManager::device_free(Device *device, DeviceScene *dscene)
{
}

CCL_NAMESPACE_END