2011-04-27 11:58:34 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "buffers.h"
|
|
|
|
#include "device.h"
|
|
|
|
|
|
|
|
#include "util_debug.h"
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
#include "util_foreach.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_hash.h"
|
|
|
|
#include "util_image.h"
|
2011-08-28 13:55:59 +00:00
|
|
|
#include "util_math.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_opengl.h"
|
|
|
|
#include "util_time.h"
|
|
|
|
#include "util_types.h"
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
/* Buffer Params */
|
|
|
|
|
|
|
|
BufferParams::BufferParams()
|
|
|
|
{
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
|
|
|
|
full_x = 0;
|
|
|
|
full_y = 0;
|
|
|
|
full_width = 0;
|
|
|
|
full_height = 0;
|
|
|
|
|
|
|
|
Pass::add(PASS_COMBINED, passes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferParams::get_offset_stride(int& offset, int& stride)
|
|
|
|
{
|
|
|
|
offset = -(full_x + full_y*width);
|
|
|
|
stride = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BufferParams::modified(const BufferParams& params)
|
|
|
|
{
|
|
|
|
return !(full_x == params.full_x
|
|
|
|
&& full_y == params.full_y
|
|
|
|
&& width == params.width
|
|
|
|
&& height == params.height
|
|
|
|
&& full_width == params.full_width
|
|
|
|
&& full_height == params.full_height
|
|
|
|
&& Pass::equals(passes, params.passes));
|
|
|
|
}
|
|
|
|
|
|
|
|
int BufferParams::get_passes_size()
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
foreach(Pass& pass, passes)
|
|
|
|
size += pass.components;
|
|
|
|
|
2012-01-27 13:58:32 +00:00
|
|
|
return align_up(size, 4);
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Render Buffers */
|
|
|
|
|
|
|
|
RenderBuffers::RenderBuffers(Device *device_)
|
|
|
|
{
|
|
|
|
device = device_;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderBuffers::~RenderBuffers()
|
|
|
|
{
|
|
|
|
device_free();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderBuffers::device_free()
|
|
|
|
{
|
|
|
|
if(buffer.device_pointer) {
|
|
|
|
device->mem_free(buffer);
|
|
|
|
buffer.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rng_state.device_pointer) {
|
|
|
|
device->mem_free(rng_state);
|
|
|
|
rng_state.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 12:25:37 +00:00
|
|
|
void RenderBuffers::reset(Device *device, BufferParams& params_)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-12-20 12:25:37 +00:00
|
|
|
params = params_;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* free existing buffers */
|
|
|
|
device_free();
|
|
|
|
|
|
|
|
/* allocate buffer */
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
buffer.resize(params.width*params.height*params.get_passes_size());
|
2011-04-27 11:58:34 +00:00
|
|
|
device->mem_alloc(buffer, MEM_READ_WRITE);
|
|
|
|
device->mem_zero(buffer);
|
|
|
|
|
|
|
|
/* allocate rng state */
|
2011-12-20 12:25:37 +00:00
|
|
|
rng_state.resize(params.width, params.height);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-20 12:25:37 +00:00
|
|
|
uint *init_state = rng_state.resize(params.width, params.height);
|
|
|
|
int x, y, width = params.width, height = params.height;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-06-09 18:56:12 +00:00
|
|
|
for(x = 0; x < width; x++)
|
|
|
|
for(y = 0; y < height; y++)
|
2012-04-16 09:52:25 +00:00
|
|
|
init_state[x + y*width] = hash_int_2d(params.full_x+x, params.full_y+y);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
device->mem_alloc(rng_state, MEM_READ_WRITE);
|
|
|
|
device->mem_copy_to(rng_state);
|
|
|
|
}
|
|
|
|
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
bool RenderBuffers::copy_from_device()
|
2011-08-28 13:55:59 +00:00
|
|
|
{
|
|
|
|
if(!buffer.device_pointer)
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
return false;
|
2011-08-28 13:55:59 +00:00
|
|
|
|
2012-01-26 19:07:01 +00:00
|
|
|
device->mem_copy_from(buffer, 0, params.width, params.height, params.get_passes_size()*sizeof(float));
|
2011-08-28 13:55:59 +00:00
|
|
|
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RenderBuffers::get_pass(PassType type, float exposure, int sample, int components, float *pixels)
|
|
|
|
{
|
|
|
|
int pass_offset = 0;
|
|
|
|
|
|
|
|
foreach(Pass& pass, params.passes) {
|
|
|
|
if(pass.type != type) {
|
|
|
|
pass_offset += pass.components;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
float *in = (float*)buffer.data_pointer + pass_offset;
|
|
|
|
int pass_stride = params.get_passes_size();
|
|
|
|
|
|
|
|
float scale = (pass.filter)? 1.0f/(float)sample: 1.0f;
|
|
|
|
float scale_exposure = (pass.exposure)? scale*exposure: scale;
|
|
|
|
|
|
|
|
int size = params.width*params.height;
|
|
|
|
|
|
|
|
if(components == 1) {
|
|
|
|
assert(pass.components == components);
|
|
|
|
|
|
|
|
/* scalar */
|
2012-01-26 14:55:25 +00:00
|
|
|
if(type == PASS_DEPTH) {
|
|
|
|
for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
|
|
|
|
float f = *in;
|
|
|
|
pixels[0] = (f == 0.0f)? 1e10f: f*scale_exposure;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
|
|
|
|
float f = *in;
|
|
|
|
pixels[0] = f*scale_exposure;
|
|
|
|
}
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(components == 3) {
|
|
|
|
assert(pass.components == 4);
|
|
|
|
|
2012-03-28 12:18:12 +00:00
|
|
|
if(pass.divide_type != PASS_NONE) {
|
|
|
|
/* RGB lighting passes that need to divide out color */
|
|
|
|
pass_offset = 0;
|
|
|
|
foreach(Pass& color_pass, params.passes) {
|
|
|
|
if(color_pass.type == pass.divide_type)
|
|
|
|
break;
|
|
|
|
pass_offset += color_pass.components;
|
|
|
|
}
|
|
|
|
|
|
|
|
float *in_divide = (float*)buffer.data_pointer + pass_offset;
|
|
|
|
|
|
|
|
for(int i = 0; i < size; i++, in += pass_stride, in_divide += pass_stride, pixels += 3) {
|
|
|
|
float3 f = make_float3(in[0], in[1], in[2]);
|
|
|
|
float3 f_divide = make_float3(in_divide[0], in_divide[1], in_divide[2]);
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
|
2012-03-28 12:18:12 +00:00
|
|
|
f = safe_divide_color(f*exposure, f_divide);
|
|
|
|
|
|
|
|
pixels[0] = f.x;
|
|
|
|
pixels[1] = f.y;
|
|
|
|
pixels[2] = f.z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* RGB/vector */
|
|
|
|
for(int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
|
|
|
|
float3 f = make_float3(in[0], in[1], in[2]);
|
|
|
|
|
|
|
|
pixels[0] = f.x*scale_exposure;
|
|
|
|
pixels[1] = f.y*scale_exposure;
|
|
|
|
pixels[2] = f.z*scale_exposure;
|
|
|
|
}
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(components == 4) {
|
|
|
|
assert(pass.components == components);
|
|
|
|
|
|
|
|
/* RGBA */
|
2012-03-28 10:39:21 +00:00
|
|
|
if(type == PASS_SHADOW) {
|
|
|
|
for(int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
|
|
|
|
float4 f = make_float4(in[0], in[1], in[2], in[3]);
|
|
|
|
float invw = (f.w > 0.0f)? 1.0f/f.w: 1.0f;
|
|
|
|
|
|
|
|
pixels[0] = f.x*invw;
|
|
|
|
pixels[1] = f.y*invw;
|
|
|
|
pixels[2] = f.z*invw;
|
|
|
|
pixels[3] = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 12:49:26 +00:00
|
|
|
else if(type == PASS_MOTION) {
|
|
|
|
/* need to normalize by number of samples accumulated for motion */
|
|
|
|
pass_offset = 0;
|
|
|
|
foreach(Pass& color_pass, params.passes) {
|
|
|
|
if(color_pass.type == PASS_MOTION_WEIGHT)
|
|
|
|
break;
|
|
|
|
pass_offset += color_pass.components;
|
|
|
|
}
|
|
|
|
|
|
|
|
float *in_weight = (float*)buffer.data_pointer + pass_offset;
|
|
|
|
|
|
|
|
for(int i = 0; i < size; i++, in += pass_stride, in_weight += pass_stride, pixels += 4) {
|
|
|
|
float4 f = make_float4(in[0], in[1], in[2], in[3]);
|
|
|
|
float w = in_weight[0];
|
|
|
|
float invw = (w > 0.0f)? 1.0f/w: 0.0f;
|
|
|
|
|
|
|
|
pixels[0] = f.x*invw;
|
|
|
|
pixels[1] = f.y*invw;
|
|
|
|
pixels[2] = f.z*invw;
|
|
|
|
pixels[3] = f.w*invw;
|
|
|
|
}
|
|
|
|
}
|
2012-03-28 10:39:21 +00:00
|
|
|
else {
|
|
|
|
for(int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
|
|
|
|
float4 f = make_float4(in[0], in[1], in[2], in[3]);
|
2011-08-28 13:55:59 +00:00
|
|
|
|
2012-03-28 10:39:21 +00:00
|
|
|
pixels[0] = f.x*scale_exposure;
|
|
|
|
pixels[1] = f.y*scale_exposure;
|
|
|
|
pixels[2] = f.z*scale_exposure;
|
2011-08-28 13:55:59 +00:00
|
|
|
|
2012-03-28 10:39:21 +00:00
|
|
|
/* clamp since alpha might be > 1.0 due to russian roulette */
|
|
|
|
pixels[3] = clamp(f.w*scale, 0.0f, 1.0f);
|
|
|
|
}
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-28 13:55:59 +00:00
|
|
|
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
return true;
|
2011-08-28 13:55:59 +00:00
|
|
|
}
|
|
|
|
|
Cycles: Render Passes
Currently supported passes:
* Combined, Z, Normal, Object Index, Material Index, Emission, Environment,
Diffuse/Glossy/Transmission x Direct/Indirect/Color
Not supported yet:
* UV, Vector, Mist
Only enabled for CPU devices at the moment, will do GPU tweaks tommorrow,
also for environment importance sampling.
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Passes
2012-01-25 17:23:52 +00:00
|
|
|
return false;
|
2011-08-28 13:55:59 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Display Buffer */
|
|
|
|
|
|
|
|
DisplayBuffer::DisplayBuffer(Device *device_)
|
|
|
|
{
|
|
|
|
device = device_;
|
|
|
|
draw_width = 0;
|
|
|
|
draw_height = 0;
|
2011-08-28 13:55:59 +00:00
|
|
|
transparent = true; /* todo: determine from background */
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DisplayBuffer::~DisplayBuffer()
|
|
|
|
{
|
|
|
|
device_free();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisplayBuffer::device_free()
|
|
|
|
{
|
|
|
|
if(rgba.device_pointer) {
|
|
|
|
device->pixels_free(rgba);
|
|
|
|
rgba.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 12:25:37 +00:00
|
|
|
void DisplayBuffer::reset(Device *device, BufferParams& params_)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
draw_width = 0;
|
|
|
|
draw_height = 0;
|
|
|
|
|
2011-12-20 12:25:37 +00:00
|
|
|
params = params_;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* free existing buffers */
|
|
|
|
device_free();
|
|
|
|
|
|
|
|
/* allocate display pixels */
|
2011-12-20 12:25:37 +00:00
|
|
|
rgba.resize(params.width, params.height);
|
2011-04-27 11:58:34 +00:00
|
|
|
device->pixels_alloc(rgba);
|
|
|
|
}
|
|
|
|
|
2011-12-20 12:25:37 +00:00
|
|
|
void DisplayBuffer::draw_set(int width, int height)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-12-20 12:25:37 +00:00
|
|
|
assert(width <= params.width && height <= params.height);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-20 12:25:37 +00:00
|
|
|
draw_width = width;
|
|
|
|
draw_height = height;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DisplayBuffer::draw(Device *device)
|
|
|
|
{
|
2012-06-06 23:27:43 +00:00
|
|
|
if(draw_width != 0 && draw_height != 0) {
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(params.full_x, params.full_y, 0.0f);
|
|
|
|
|
2012-01-04 18:06:32 +00:00
|
|
|
device->draw_pixels(rgba, 0, draw_width, draw_height, 0, params.width, params.height, transparent);
|
2012-06-06 23:27:43 +00:00
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DisplayBuffer::draw_ready()
|
|
|
|
{
|
|
|
|
return (draw_width != 0 && draw_height != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisplayBuffer::write(Device *device, const string& filename)
|
|
|
|
{
|
|
|
|
int w = draw_width;
|
|
|
|
int h = draw_height;
|
|
|
|
|
|
|
|
if(w == 0 || h == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* read buffer from device */
|
|
|
|
device->pixels_copy_from(rgba, 0, w, h);
|
|
|
|
|
|
|
|
/* write image */
|
|
|
|
ImageOutput *out = ImageOutput::create(filename);
|
|
|
|
ImageSpec spec(w, h, 4, TypeDesc::UINT8);
|
|
|
|
int scanlinesize = w*4*sizeof(uchar);
|
|
|
|
|
|
|
|
out->open(filename, spec);
|
|
|
|
|
|
|
|
/* conversion for different top/bottom convention */
|
|
|
|
out->write_image(TypeDesc::UINT8,
|
|
|
|
(uchar*)rgba.data_pointer + (h-1)*scanlinesize,
|
|
|
|
AutoStride,
|
|
|
|
-scanlinesize,
|
|
|
|
AutoStride);
|
|
|
|
|
|
|
|
out->close();
|
|
|
|
|
|
|
|
delete out;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|