2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* 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
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* 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
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "device.h"
|
|
|
|
|
|
|
|
#include "mesh.h"
|
2012-12-23 14:29:43 +00:00
|
|
|
#include "object.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "scene.h"
|
|
|
|
#include "shader.h"
|
|
|
|
|
|
|
|
#include "util_foreach.h"
|
|
|
|
#include "util_progress.h"
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2013-04-27 02:54:24 +00:00
|
|
|
bool MeshManager::displace(Device *device, DeviceScene *dscene, Scene *scene, Mesh *mesh, Progress& progress)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* verify if we have a displacement shader */
|
|
|
|
bool has_displacement = false;
|
|
|
|
|
|
|
|
if(mesh->displacement_method != Mesh::DISPLACE_BUMP) {
|
|
|
|
foreach(uint sindex, mesh->used_shaders)
|
|
|
|
if(scene->shaders[sindex]->has_displacement)
|
|
|
|
has_displacement = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!has_displacement)
|
|
|
|
return false;
|
|
|
|
|
2012-12-23 14:29:43 +00:00
|
|
|
string msg = string_printf("Computing Displacement %s", mesh->name.c_str());
|
|
|
|
progress.set_status("Updating Mesh", msg);
|
|
|
|
|
|
|
|
/* find object index. todo: is arbitrary */
|
|
|
|
size_t object_index = ~0;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < scene->objects.size(); i++) {
|
|
|
|
if(scene->objects[i]->mesh == mesh) {
|
|
|
|
object_index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* setup input for device task */
|
|
|
|
vector<bool> done(mesh->verts.size(), false);
|
|
|
|
device_vector<uint4> d_input;
|
|
|
|
uint4 *d_input_data = d_input.resize(mesh->verts.size());
|
2012-12-26 20:59:35 +00:00
|
|
|
size_t d_input_size = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
for(size_t i = 0; i < mesh->triangles.size(); i++) {
|
|
|
|
Mesh::Triangle t = mesh->triangles[i];
|
|
|
|
Shader *shader = scene->shaders[mesh->shader[i]];
|
|
|
|
|
|
|
|
if(!shader->has_displacement)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for(int j = 0; j < 3; j++) {
|
|
|
|
if(done[t.v[j]])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
done[t.v[j]] = true;
|
|
|
|
|
|
|
|
/* set up object, primitive and barycentric coordinates */
|
2012-12-23 14:29:43 +00:00
|
|
|
/* when used, non-instanced convention: object = ~object */
|
|
|
|
int object = ~object_index;
|
2011-04-27 11:58:34 +00:00
|
|
|
int prim = mesh->tri_offset + i;
|
|
|
|
float u, v;
|
2013-05-09 15:20:00 +00:00
|
|
|
|
|
|
|
switch (j) {
|
|
|
|
case 0:
|
|
|
|
u = 1.0f;
|
|
|
|
v = 0.0f;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
u = 0.0f;
|
|
|
|
v = 1.0f;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
u = 0.0f;
|
|
|
|
v = 0.0f;
|
|
|
|
break;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* back */
|
|
|
|
uint4 in = make_uint4(object, prim, __float_as_int(u), __float_as_int(v));
|
2012-12-26 20:59:35 +00:00
|
|
|
d_input_data[d_input_size++] = in;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-26 20:59:35 +00:00
|
|
|
if(d_input_size == 0)
|
2011-04-27 11:58:34 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* run device task */
|
2012-01-20 17:49:17 +00:00
|
|
|
device_vector<float4> d_output;
|
2012-12-26 20:59:35 +00:00
|
|
|
d_output.resize(d_input_size);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-04-27 02:54:24 +00:00
|
|
|
/* needs to be up to data for attribute access */
|
|
|
|
device->const_copy_to("__data", &dscene->data, sizeof(dscene->data));
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
device->mem_alloc(d_input, MEM_READ_ONLY);
|
|
|
|
device->mem_copy_to(d_input);
|
2011-12-31 15:18:13 +00:00
|
|
|
device->mem_alloc(d_output, MEM_WRITE_ONLY);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-31 15:18:13 +00:00
|
|
|
DeviceTask task(DeviceTask::SHADER);
|
|
|
|
task.shader_input = d_input.device_pointer;
|
|
|
|
task.shader_output = d_output.device_pointer;
|
|
|
|
task.shader_eval_type = SHADER_EVAL_DISPLACE;
|
|
|
|
task.shader_x = 0;
|
2012-12-26 20:59:35 +00:00
|
|
|
task.shader_w = d_output.size();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
device->task_add(task);
|
|
|
|
device->task_wait();
|
|
|
|
|
2012-01-20 17:49:17 +00:00
|
|
|
device->mem_copy_from(d_output, 0, 1, d_output.size(), sizeof(float4));
|
2011-04-27 11:58:34 +00:00
|
|
|
device->mem_free(d_input);
|
2011-12-31 15:18:13 +00:00
|
|
|
device->mem_free(d_output);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
if(progress.get_cancel())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* read result */
|
|
|
|
done.clear();
|
|
|
|
done.resize(mesh->verts.size(), false);
|
|
|
|
int k = 0;
|
|
|
|
|
2012-01-20 17:49:17 +00:00
|
|
|
float4 *offset = (float4*)d_output.data_pointer;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
for(size_t i = 0; i < mesh->triangles.size(); i++) {
|
|
|
|
Mesh::Triangle t = mesh->triangles[i];
|
|
|
|
Shader *shader = scene->shaders[mesh->shader[i]];
|
|
|
|
|
|
|
|
if(!shader->has_displacement)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for(int j = 0; j < 3; j++) {
|
|
|
|
if(!done[t.v[j]]) {
|
|
|
|
done[t.v[j]] = true;
|
2012-01-20 17:49:17 +00:00
|
|
|
float3 off = float4_to_float3(offset[k++]);
|
|
|
|
mesh->verts[t.v[j]] += off;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for displacement method both, we only need to recompute the face
|
|
|
|
* normals, as bump mapping in the shader will already alter the
|
|
|
|
* vertex normal, so we start from the non-displaced vertex normals
|
|
|
|
* to avoid applying the perturbation twice. */
|
2012-04-30 12:49:26 +00:00
|
|
|
mesh->attributes.remove(ATTR_STD_FACE_NORMAL);
|
2011-04-27 11:58:34 +00:00
|
|
|
mesh->add_face_normals();
|
|
|
|
|
|
|
|
if(mesh->displacement_method == Mesh::DISPLACE_TRUE) {
|
2012-04-30 12:49:26 +00:00
|
|
|
mesh->attributes.remove(ATTR_STD_VERTEX_NORMAL);
|
2011-04-27 11:58:34 +00:00
|
|
|
mesh->add_vertex_normals();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|