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
|
2014-12-25 01:50:24 +00:00
|
|
|
* 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) {
|
2016-05-14 12:50:03 +00:00
|
|
|
foreach(Shader *shader, mesh->used_shaders)
|
|
|
|
if(shader->has_displacement)
|
2011-04-27 11:58:34 +00:00
|
|
|
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 */
|
2014-03-29 12:03:47 +00:00
|
|
|
size_t object_index = OBJECT_NONE;
|
2012-12-23 14:29:43 +00:00
|
|
|
|
|
|
|
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 */
|
2016-01-16 10:36:42 +00:00
|
|
|
const size_t num_verts = mesh->verts.size();
|
|
|
|
vector<bool> done(num_verts, false);
|
2011-04-27 11:58:34 +00:00
|
|
|
device_vector<uint4> d_input;
|
2016-01-16 10:36:42 +00:00
|
|
|
uint4 *d_input_data = d_input.resize(num_verts);
|
2012-12-26 20:59:35 +00:00
|
|
|
size_t d_input_size = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2016-05-07 22:09:08 +00:00
|
|
|
size_t num_triangles = mesh->num_triangles();
|
|
|
|
for(size_t i = 0; i < num_triangles; i++) {
|
|
|
|
Mesh::Triangle t = mesh->get_triangle(i);
|
2016-05-17 23:50:35 +00:00
|
|
|
int shader_index = mesh->shader[i];
|
|
|
|
Shader *shader = (shader_index < mesh->used_shaders.size()) ?
|
|
|
|
mesh->used_shaders[shader_index] : scene->default_surface;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-10-08 14:08:28 +00:00
|
|
|
switch(j) {
|
2013-05-09 15:20:00 +00:00
|
|
|
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();
|
2014-06-06 12:40:09 +00:00
|
|
|
task.num_samples = 1;
|
2014-01-02 21:05:07 +00:00
|
|
|
task.get_cancel = function_bind(&Progress::get_cancel, &progress);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
device->task_add(task);
|
|
|
|
device->task_wait();
|
|
|
|
|
2014-01-02 21:05:07 +00:00
|
|
|
if(progress.get_cancel()) {
|
|
|
|
device->mem_free(d_input);
|
|
|
|
device->mem_free(d_output);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/* read result */
|
|
|
|
done.clear();
|
2016-01-16 10:36:42 +00:00
|
|
|
done.resize(num_verts, false);
|
2011-04-27 11:58:34 +00:00
|
|
|
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
|
|
|
|
2016-01-16 10:36:42 +00:00
|
|
|
Attribute *attr_mP = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
|
2016-05-07 22:09:08 +00:00
|
|
|
for(size_t i = 0; i < num_triangles; i++) {
|
|
|
|
Mesh::Triangle t = mesh->get_triangle(i);
|
2016-05-17 23:50:35 +00:00
|
|
|
int shader_index = mesh->shader[i];
|
|
|
|
Shader *shader = (shader_index < mesh->used_shaders.size()) ?
|
|
|
|
mesh->used_shaders[shader_index] : scene->default_surface;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
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;
|
2016-01-16 10:36:42 +00:00
|
|
|
if(attr_mP != NULL) {
|
|
|
|
for(int step = 0; step < mesh->motion_steps - 1; step++) {
|
|
|
|
float3 *mP = attr_mP->data_float3() + step*num_verts;
|
|
|
|
mP[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
|
|
|
|
|