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 "device.h"
|
|
|
|
#include "image.h"
|
|
|
|
#include "scene.h"
|
|
|
|
|
|
|
|
#include "util_foreach.h"
|
|
|
|
#include "util_image.h"
|
|
|
|
#include "util_path.h"
|
|
|
|
#include "util_progress.h"
|
|
|
|
|
2011-05-31 16:21:30 +00:00
|
|
|
#ifdef WITH_OSL
|
|
|
|
#include <OSL/oslexec.h>
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
ImageManager::ImageManager()
|
|
|
|
{
|
|
|
|
need_update = true;
|
2012-05-13 12:32:44 +00:00
|
|
|
pack_images = false;
|
2011-05-31 16:21:30 +00:00
|
|
|
osl_texture_system = NULL;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageManager::~ImageManager()
|
|
|
|
{
|
2012-03-07 12:27:18 +00:00
|
|
|
for(size_t slot = 0; slot < images.size(); slot++)
|
2011-04-27 11:58:34 +00:00
|
|
|
assert(!images[slot]);
|
2012-03-07 12:27:18 +00:00
|
|
|
for(size_t slot = 0; slot < float_images.size(); slot++)
|
|
|
|
assert(!float_images[slot]);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2012-05-13 12:32:44 +00:00
|
|
|
void ImageManager::set_pack_images(bool pack_images_)
|
|
|
|
{
|
|
|
|
pack_images = pack_images_;
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:21:30 +00:00
|
|
|
void ImageManager::set_osl_texture_system(void *texture_system)
|
|
|
|
{
|
|
|
|
osl_texture_system = texture_system;
|
|
|
|
}
|
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
static bool is_float_image(const string& filename)
|
|
|
|
{
|
|
|
|
ImageInput *in = ImageInput::create(filename);
|
|
|
|
bool is_float = false;
|
|
|
|
|
|
|
|
if(in) {
|
|
|
|
ImageSpec spec;
|
|
|
|
|
|
|
|
if(in->open(filename, spec)) {
|
|
|
|
/* check the main format, and channel formats;
|
2012-06-09 17:22:52 +00:00
|
|
|
* if any take up more than one byte, we'll need a float texture slot */
|
2012-05-10 17:09:36 +00:00
|
|
|
if(spec.format.basesize() > 1)
|
2012-03-07 12:27:18 +00:00
|
|
|
is_float = true;
|
|
|
|
|
|
|
|
for(size_t channel = 0; channel < spec.channelformats.size(); channel++) {
|
2012-05-10 17:09:36 +00:00
|
|
|
if(spec.channelformats[channel].basesize() > 1)
|
2012-03-07 12:27:18 +00:00
|
|
|
is_float = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
in->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete in;
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_float;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ImageManager::add_image(const string& filename, bool& is_float)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
Image *img;
|
|
|
|
size_t slot;
|
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
/* load image info and find out if we need a float texture */
|
2012-05-13 12:32:44 +00:00
|
|
|
is_float = (pack_images)? false: is_float_image(filename);
|
2012-03-07 12:27:18 +00:00
|
|
|
|
|
|
|
if(is_float) {
|
|
|
|
/* find existing image */
|
|
|
|
for(slot = 0; slot < float_images.size(); slot++) {
|
|
|
|
if(float_images[slot] && float_images[slot]->filename == filename) {
|
|
|
|
float_images[slot]->users++;
|
|
|
|
return slot+TEX_IMAGE_FLOAT_START;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2012-03-07 12:27:18 +00:00
|
|
|
|
|
|
|
/* find free slot */
|
|
|
|
for(slot = 0; slot < float_images.size(); slot++) {
|
|
|
|
if(!float_images[slot])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(slot == float_images.size()) {
|
|
|
|
/* max images limit reached */
|
2012-07-04 11:48:42 +00:00
|
|
|
if(float_images.size() == TEX_NUM_FLOAT_IMAGES) {
|
|
|
|
printf("ImageManager::add_image: byte image limit reached %d, skipping '%s'\n",
|
|
|
|
TEX_NUM_IMAGES, filename.c_str());
|
2012-03-07 12:27:18 +00:00
|
|
|
return -1;
|
2012-07-04 11:48:42 +00:00
|
|
|
}
|
2012-03-07 12:27:18 +00:00
|
|
|
|
|
|
|
float_images.resize(float_images.size() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add new image */
|
|
|
|
img = new Image();
|
|
|
|
img->filename = filename;
|
|
|
|
img->need_load = true;
|
|
|
|
img->users = 1;
|
|
|
|
|
|
|
|
float_images[slot] = img;
|
|
|
|
/* report slot out of total set of textures */
|
|
|
|
slot += TEX_IMAGE_FLOAT_START;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2012-03-07 12:27:18 +00:00
|
|
|
else {
|
|
|
|
for(slot = 0; slot < images.size(); slot++) {
|
|
|
|
if(images[slot] && images[slot]->filename == filename) {
|
|
|
|
images[slot]->users++;
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
/* find free slot */
|
|
|
|
for(slot = 0; slot < images.size(); slot++) {
|
|
|
|
if(!images[slot])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(slot == images.size()) {
|
|
|
|
/* max images limit reached */
|
2012-07-04 11:48:42 +00:00
|
|
|
if(images.size() == TEX_NUM_IMAGES) {
|
|
|
|
printf("ImageManager::add_image: byte image limit reached %d, skipping '%s'\n",
|
|
|
|
TEX_NUM_IMAGES, filename.c_str());
|
2012-03-07 12:27:18 +00:00
|
|
|
return -1;
|
2012-07-04 11:48:42 +00:00
|
|
|
}
|
2012-03-07 12:27:18 +00:00
|
|
|
|
|
|
|
images.resize(images.size() + 1);
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
/* add new image */
|
|
|
|
img = new Image();
|
|
|
|
img->filename = filename;
|
|
|
|
img->need_load = true;
|
|
|
|
img->users = 1;
|
|
|
|
|
|
|
|
images[slot] = img;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
need_update = true;
|
|
|
|
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:21:30 +00:00
|
|
|
void ImageManager::remove_image(const string& filename)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-05-31 16:21:30 +00:00
|
|
|
size_t slot;
|
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
for(slot = 0; slot < images.size(); slot++) {
|
|
|
|
if(images[slot] && images[slot]->filename == filename) {
|
|
|
|
/* decrement user count */
|
|
|
|
images[slot]->users--;
|
|
|
|
assert(images[slot]->users >= 0);
|
|
|
|
|
|
|
|
/* don't remove immediately, rather do it all together later on. one of
|
2012-06-09 17:22:52 +00:00
|
|
|
* the reasons for this is that on shader changes we add and remove nodes
|
|
|
|
* that use them, but we do not want to reload the image all the time. */
|
2012-03-07 12:27:18 +00:00
|
|
|
if(images[slot]->users == 0)
|
|
|
|
need_update = true;
|
|
|
|
|
2011-05-31 16:21:30 +00:00
|
|
|
break;
|
2012-03-07 12:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-31 16:21:30 +00:00
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
if(slot == images.size()) {
|
|
|
|
/* see if it's in a float texture slot */
|
|
|
|
for(slot = 0; slot < float_images.size(); slot++) {
|
|
|
|
if(float_images[slot] && float_images[slot]->filename == filename) {
|
|
|
|
/* decrement user count */
|
|
|
|
float_images[slot]->users--;
|
|
|
|
assert(float_images[slot]->users >= 0);
|
|
|
|
|
|
|
|
/* don't remove immediately, rather do it all together later on. one of
|
2012-06-09 17:22:52 +00:00
|
|
|
* the reasons for this is that on shader changes we add and remove nodes
|
|
|
|
* that use them, but we do not want to reload the image all the time. */
|
2012-03-07 12:27:18 +00:00
|
|
|
if(float_images[slot]->users == 0)
|
|
|
|
need_update = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageManager::file_load_image(Image *img, device_vector<uchar4>& tex_img)
|
|
|
|
{
|
|
|
|
if(img->filename == "")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* load image from file through OIIO */
|
|
|
|
ImageInput *in = ImageInput::create(img->filename);
|
|
|
|
|
|
|
|
if(!in)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ImageSpec spec;
|
|
|
|
|
|
|
|
if(!in->open(img->filename, spec)) {
|
|
|
|
delete in;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we only handle certain number of components */
|
|
|
|
int width = spec.width;
|
|
|
|
int height = spec.height;
|
|
|
|
int components = spec.nchannels;
|
|
|
|
|
|
|
|
if(!(components == 1 || components == 3 || components == 4)) {
|
2011-08-15 19:30:24 +00:00
|
|
|
in->close();
|
2011-04-27 11:58:34 +00:00
|
|
|
delete in;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read RGBA pixels */
|
|
|
|
uchar *pixels = (uchar*)tex_img.resize(width, height);
|
|
|
|
int scanlinesize = width*components*sizeof(uchar);
|
|
|
|
|
|
|
|
in->read_image(TypeDesc::UINT8,
|
|
|
|
(uchar*)pixels + (height-1)*scanlinesize,
|
|
|
|
AutoStride,
|
|
|
|
-scanlinesize,
|
|
|
|
AutoStride);
|
|
|
|
|
|
|
|
in->close();
|
2011-08-15 19:30:24 +00:00
|
|
|
delete in;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
if(components == 3) {
|
|
|
|
for(int i = width*height-1; i >= 0; i--) {
|
|
|
|
pixels[i*4+3] = 255;
|
|
|
|
pixels[i*4+2] = pixels[i*3+2];
|
|
|
|
pixels[i*4+1] = pixels[i*3+1];
|
|
|
|
pixels[i*4+0] = pixels[i*3+0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(components == 1) {
|
|
|
|
for(int i = width*height-1; i >= 0; i--) {
|
|
|
|
pixels[i*4+3] = 255;
|
|
|
|
pixels[i*4+2] = pixels[i];
|
|
|
|
pixels[i*4+1] = pixels[i];
|
|
|
|
pixels[i*4+0] = pixels[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
bool ImageManager::file_load_float_image(Image *img, device_vector<float4>& tex_img)
|
|
|
|
{
|
|
|
|
if(img->filename == "")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* load image from file through OIIO */
|
|
|
|
ImageInput *in = ImageInput::create(img->filename);
|
|
|
|
|
|
|
|
if(!in)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ImageSpec spec;
|
|
|
|
|
|
|
|
if(!in->open(img->filename, spec)) {
|
|
|
|
delete in;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we only handle certain number of components */
|
|
|
|
int width = spec.width;
|
|
|
|
int height = spec.height;
|
|
|
|
int components = spec.nchannels;
|
|
|
|
|
|
|
|
if(!(components == 1 || components == 3 || components == 4)) {
|
|
|
|
in->close();
|
|
|
|
delete in;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read RGBA pixels */
|
|
|
|
float *pixels = (float*)tex_img.resize(width, height);
|
|
|
|
int scanlinesize = width*components*sizeof(float);
|
|
|
|
|
|
|
|
in->read_image(TypeDesc::FLOAT,
|
|
|
|
(uchar*)pixels + (height-1)*scanlinesize,
|
|
|
|
AutoStride,
|
|
|
|
-scanlinesize,
|
|
|
|
AutoStride);
|
|
|
|
|
|
|
|
in->close();
|
|
|
|
delete in;
|
|
|
|
|
|
|
|
if(components == 3) {
|
|
|
|
for(int i = width*height-1; i >= 0; i--) {
|
|
|
|
pixels[i*4+3] = 1.0f;
|
|
|
|
pixels[i*4+2] = pixels[i*3+2];
|
|
|
|
pixels[i*4+1] = pixels[i*3+1];
|
|
|
|
pixels[i*4+0] = pixels[i*3+0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(components == 1) {
|
|
|
|
for(int i = width*height-1; i >= 0; i--) {
|
|
|
|
pixels[i*4+3] = 1.0f;
|
|
|
|
pixels[i*4+2] = pixels[i];
|
|
|
|
pixels[i*4+1] = pixels[i];
|
|
|
|
pixels[i*4+0] = pixels[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-05 19:44:33 +00:00
|
|
|
void ImageManager::device_load_image(Device *device, DeviceScene *dscene, int slot, Progress *progress)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-05-05 19:44:33 +00:00
|
|
|
if(progress->get_cancel())
|
|
|
|
return;
|
2011-05-31 16:21:30 +00:00
|
|
|
if(osl_texture_system)
|
|
|
|
return;
|
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
Image *img;
|
|
|
|
bool is_float;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
if(slot < TEX_IMAGE_FLOAT_START) {
|
|
|
|
img = images[slot];
|
|
|
|
is_float = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
img = float_images[slot - TEX_IMAGE_FLOAT_START];
|
|
|
|
is_float = true;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
if(is_float) {
|
2012-05-06 22:06:14 +00:00
|
|
|
string filename = path_filename(float_images[slot - TEX_IMAGE_FLOAT_START]->filename);
|
2012-05-05 19:44:33 +00:00
|
|
|
progress->set_status("Updating Images", "Loading " + filename);
|
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
device_vector<float4>& tex_img = dscene->tex_float_image[slot - TEX_IMAGE_FLOAT_START];
|
|
|
|
|
|
|
|
if(tex_img.device_pointer)
|
|
|
|
device->tex_free(tex_img);
|
|
|
|
|
|
|
|
if(!file_load_float_image(img, tex_img)) {
|
2012-07-04 11:48:42 +00:00
|
|
|
/* on failure to load, we set a 1x1 pixels pink image */
|
2012-03-07 12:27:18 +00:00
|
|
|
float *pixels = (float*)tex_img.resize(1, 1);
|
|
|
|
|
2012-07-04 11:48:42 +00:00
|
|
|
pixels[0] = TEX_IMAGE_MISSING_R;
|
|
|
|
pixels[1] = TEX_IMAGE_MISSING_G;
|
|
|
|
pixels[2] = TEX_IMAGE_MISSING_B;
|
|
|
|
pixels[3] = TEX_IMAGE_MISSING_A;
|
2012-03-07 12:27:18 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
string name;
|
|
|
|
|
|
|
|
if(slot >= 10) name = string_printf("__tex_image_float_0%d", slot);
|
|
|
|
else name = string_printf("__tex_image_float_00%d", slot);
|
|
|
|
|
2012-05-13 12:32:44 +00:00
|
|
|
if(!pack_images)
|
|
|
|
device->tex_alloc(name.c_str(), tex_img, true, true);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2012-03-07 12:27:18 +00:00
|
|
|
else {
|
2012-05-05 19:44:33 +00:00
|
|
|
string filename = path_filename(images[slot]->filename);
|
|
|
|
progress->set_status("Updating Images", "Loading " + filename);
|
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
device_vector<uchar4>& tex_img = dscene->tex_image[slot];
|
|
|
|
|
|
|
|
if(tex_img.device_pointer)
|
|
|
|
device->tex_free(tex_img);
|
|
|
|
|
|
|
|
if(!file_load_image(img, tex_img)) {
|
2012-07-04 11:48:42 +00:00
|
|
|
/* on failure to load, we set a 1x1 pixels pink image */
|
2012-03-07 12:27:18 +00:00
|
|
|
uchar *pixels = (uchar*)tex_img.resize(1, 1);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-07-04 11:48:42 +00:00
|
|
|
pixels[0] = (TEX_IMAGE_MISSING_R * 255);
|
|
|
|
pixels[1] = (TEX_IMAGE_MISSING_G * 255);
|
|
|
|
pixels[2] = (TEX_IMAGE_MISSING_B * 255);
|
|
|
|
pixels[3] = (TEX_IMAGE_MISSING_A * 255);
|
2012-03-07 12:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string name;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
if(slot >= 10) name = string_printf("__tex_image_0%d", slot);
|
|
|
|
else name = string_printf("__tex_image_00%d", slot);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-05-13 12:32:44 +00:00
|
|
|
if(!pack_images)
|
|
|
|
device->tex_alloc(name.c_str(), tex_img, true, true);
|
2012-03-07 12:27:18 +00:00
|
|
|
}
|
2012-05-05 19:44:33 +00:00
|
|
|
|
|
|
|
img->need_load = false;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImageManager::device_free_image(Device *device, DeviceScene *dscene, int slot)
|
|
|
|
{
|
2012-03-07 12:27:18 +00:00
|
|
|
Image *img;
|
|
|
|
bool is_float;
|
|
|
|
|
|
|
|
if(slot < TEX_IMAGE_FLOAT_START) {
|
|
|
|
img = images[slot];
|
|
|
|
is_float = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
img = float_images[slot - TEX_IMAGE_FLOAT_START];
|
|
|
|
is_float = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(img) {
|
2011-05-31 16:21:30 +00:00
|
|
|
if(osl_texture_system) {
|
|
|
|
#ifdef WITH_OSL
|
|
|
|
ustring filename(images[slot]->filename);
|
|
|
|
((OSL::TextureSystem*)osl_texture_system)->invalidate(filename);
|
|
|
|
#endif
|
|
|
|
}
|
2012-03-07 12:27:18 +00:00
|
|
|
else if(is_float) {
|
|
|
|
device->tex_free(dscene->tex_float_image[slot - TEX_IMAGE_FLOAT_START]);
|
|
|
|
dscene->tex_float_image[slot - TEX_IMAGE_FLOAT_START].clear();
|
|
|
|
|
|
|
|
delete float_images[slot - TEX_IMAGE_FLOAT_START];
|
|
|
|
float_images[slot - TEX_IMAGE_FLOAT_START] = NULL;
|
|
|
|
}
|
2011-05-31 16:21:30 +00:00
|
|
|
else {
|
|
|
|
device->tex_free(dscene->tex_image[slot]);
|
|
|
|
dscene->tex_image[slot].clear();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
delete images[slot];
|
|
|
|
images[slot] = NULL;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageManager::device_update(Device *device, DeviceScene *dscene, Progress& progress)
|
|
|
|
{
|
|
|
|
if(!need_update)
|
|
|
|
return;
|
2012-05-05 19:44:33 +00:00
|
|
|
|
|
|
|
TaskPool pool;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
for(size_t slot = 0; slot < images.size(); slot++) {
|
2012-05-05 19:44:33 +00:00
|
|
|
if(!images[slot])
|
|
|
|
continue;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-05-05 19:44:33 +00:00
|
|
|
if(images[slot]->users == 0) {
|
|
|
|
device_free_image(device, dscene, slot);
|
|
|
|
}
|
|
|
|
else if(images[slot]->need_load) {
|
|
|
|
if(!osl_texture_system)
|
|
|
|
pool.push(function_bind(&ImageManager::device_load_image, this, device, dscene, slot, &progress));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 12:27:18 +00:00
|
|
|
for(size_t slot = 0; slot < float_images.size(); slot++) {
|
2012-05-05 19:44:33 +00:00
|
|
|
if(!float_images[slot])
|
|
|
|
continue;
|
2012-03-07 12:27:18 +00:00
|
|
|
|
2012-05-05 19:44:33 +00:00
|
|
|
if(float_images[slot]->users == 0) {
|
|
|
|
device_free_image(device, dscene, slot + TEX_IMAGE_FLOAT_START);
|
|
|
|
}
|
|
|
|
else if(float_images[slot]->need_load) {
|
|
|
|
if(!osl_texture_system)
|
|
|
|
pool.push(function_bind(&ImageManager::device_load_image, this, device, dscene, slot + TEX_IMAGE_FLOAT_START, &progress));
|
2012-03-07 12:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 19:44:33 +00:00
|
|
|
pool.wait_work();
|
|
|
|
|
2012-05-13 12:32:44 +00:00
|
|
|
if(pack_images)
|
|
|
|
device_pack_images(device, dscene, progress);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
need_update = false;
|
|
|
|
}
|
|
|
|
|
2012-05-13 12:32:44 +00:00
|
|
|
void ImageManager::device_pack_images(Device *device, DeviceScene *dscene, Progress& progess)
|
|
|
|
{
|
|
|
|
/* for OpenCL, we pack all image textures inside a single big texture, and
|
2012-06-09 17:22:52 +00:00
|
|
|
* will do our own interpolation in the kernel */
|
2012-05-13 12:32:44 +00:00
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
for(size_t slot = 0; slot < images.size(); slot++) {
|
|
|
|
if(!images[slot])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
device_vector<uchar4>& tex_img = dscene->tex_image[slot];
|
|
|
|
size += tex_img.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint4 *info = dscene->tex_image_packed_info.resize(images.size());
|
|
|
|
uchar4 *pixels = dscene->tex_image_packed.resize(size);
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
for(size_t slot = 0; slot < images.size(); slot++) {
|
|
|
|
if(!images[slot])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
device_vector<uchar4>& tex_img = dscene->tex_image[slot];
|
|
|
|
|
|
|
|
info[slot] = make_uint4(tex_img.data_width, tex_img.data_height, offset, 1);
|
|
|
|
|
|
|
|
memcpy(pixels+offset, (void*)tex_img.data_pointer, tex_img.memory_size());
|
|
|
|
offset += tex_img.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dscene->tex_image_packed.size())
|
|
|
|
device->tex_alloc("__tex_image_packed", dscene->tex_image_packed);
|
|
|
|
if(dscene->tex_image_packed_info.size())
|
|
|
|
device->tex_alloc("__tex_image_packed_info", dscene->tex_image_packed_info);
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
void ImageManager::device_free(Device *device, DeviceScene *dscene)
|
|
|
|
{
|
|
|
|
for(size_t slot = 0; slot < images.size(); slot++)
|
|
|
|
device_free_image(device, dscene, slot);
|
2012-03-07 12:27:18 +00:00
|
|
|
for(size_t slot = 0; slot < float_images.size(); slot++)
|
|
|
|
device_free_image(device, dscene, slot + TEX_IMAGE_FLOAT_START);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-05-13 12:32:44 +00:00
|
|
|
device->tex_free(dscene->tex_image_packed);
|
|
|
|
dscene->tex_image_packed.clear();
|
|
|
|
|
|
|
|
device->tex_free(dscene->tex_image_packed_info);
|
|
|
|
dscene->tex_image_packed_info.clear();
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
images.clear();
|
2012-03-07 12:27:18 +00:00
|
|
|
float_images.clear();
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|