2016-10-22 23:38:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016 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.
|
|
|
|
*/
|
|
|
|
|
2017-08-08 07:12:04 -04:00
|
|
|
/* For OpenCL we do manual lookup and interpolation. */
|
|
|
|
|
2017-10-06 21:47:41 +02:00
|
|
|
ccl_device_inline ccl_global TextureInfo* kernel_tex_info(KernelGlobals *kg, uint id) {
|
2017-08-08 07:12:04 -04:00
|
|
|
const uint tex_offset = id
|
2017-10-06 21:47:41 +02:00
|
|
|
#define KERNEL_TEX(type, name) + 1
|
2017-08-08 07:12:04 -04:00
|
|
|
#include "kernel/kernel_textures.h"
|
|
|
|
;
|
|
|
|
|
2017-10-06 21:47:41 +02:00
|
|
|
return &((ccl_global TextureInfo*)kg->buffers[0])[tex_offset];
|
2017-08-08 07:12:04 -04:00
|
|
|
}
|
|
|
|
|
2017-10-06 21:47:41 +02:00
|
|
|
#define tex_fetch(type, info, index) ((ccl_global type*)(kg->buffers[info->cl_buffer] + info->data))[(index)]
|
2016-10-22 23:38:42 +02:00
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
ccl_device_inline int svm_image_texture_wrap_periodic(int x, int width)
|
|
|
|
{
|
|
|
|
x %= width;
|
|
|
|
if(x < 0)
|
|
|
|
x += width;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccl_device_inline int svm_image_texture_wrap_clamp(int x, int width)
|
|
|
|
{
|
|
|
|
return clamp(x, 0, width-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ccl_device_inline float4 svm_image_texture_read(KernelGlobals *kg, const ccl_global TextureInfo *info, int id, int offset)
|
2016-10-22 23:38:42 +02:00
|
|
|
{
|
2017-05-02 15:39:18 +02:00
|
|
|
const int texture_type = kernel_tex_type(id);
|
2017-08-08 07:12:04 -04:00
|
|
|
|
2016-10-22 23:38:42 +02:00
|
|
|
/* Float4 */
|
Unlimited number of textures for Cycles
This patch allows for an unlimited number of textures in Cycles where the hardware allows. It replaces a number static arrays with dynamic arrays and changes the way the flat_slot indices are calculated. Eventually, I'd like to get to a point where there are only flat slots left and textures off all kinds are stored in a single array.
Note that the arrays in DeviceScene are changed from containing device_vector<T> objects to device_vector<T>* pointers. Ideally, I'd like to store objects, but dynamic resizing of a std:vector in pre-C++11 calls the copy constructor, which for a good reason is not implemented for device_vector. Once we require C++11 for Cycles builds, we can implement a move constructor for device_vector and store objects again.
The limits for CUDA Fermi hardware still apply.
Reviewers: tod_baudais, InsigMathK, dingto, #cycles
Reviewed By: dingto, #cycles
Subscribers: dingto, smellslikedonkey
Differential Revision: https://developer.blender.org/D2650
2017-04-27 09:34:51 +02:00
|
|
|
if(texture_type == IMAGE_DATA_TYPE_FLOAT4) {
|
2017-08-08 07:12:04 -04:00
|
|
|
return tex_fetch(float4, info, offset);
|
2016-10-22 23:38:42 +02:00
|
|
|
}
|
|
|
|
/* Byte4 */
|
Unlimited number of textures for Cycles
This patch allows for an unlimited number of textures in Cycles where the hardware allows. It replaces a number static arrays with dynamic arrays and changes the way the flat_slot indices are calculated. Eventually, I'd like to get to a point where there are only flat slots left and textures off all kinds are stored in a single array.
Note that the arrays in DeviceScene are changed from containing device_vector<T> objects to device_vector<T>* pointers. Ideally, I'd like to store objects, but dynamic resizing of a std:vector in pre-C++11 calls the copy constructor, which for a good reason is not implemented for device_vector. Once we require C++11 for Cycles builds, we can implement a move constructor for device_vector and store objects again.
The limits for CUDA Fermi hardware still apply.
Reviewers: tod_baudais, InsigMathK, dingto, #cycles
Reviewed By: dingto, #cycles
Subscribers: dingto, smellslikedonkey
Differential Revision: https://developer.blender.org/D2650
2017-04-27 09:34:51 +02:00
|
|
|
else if(texture_type == IMAGE_DATA_TYPE_BYTE4) {
|
2017-08-08 07:12:04 -04:00
|
|
|
uchar4 r = tex_fetch(uchar4, info, offset);
|
2016-10-22 23:38:42 +02:00
|
|
|
float f = 1.0f/255.0f;
|
|
|
|
return make_float4(r.x*f, r.y*f, r.z*f, r.w*f);
|
|
|
|
}
|
2018-07-05 12:37:52 +02:00
|
|
|
/* Ushort4 */
|
|
|
|
else if(texture_type == IMAGE_DATA_TYPE_USHORT4) {
|
|
|
|
ushort4 r = tex_fetch(ushort4, info, offset);
|
|
|
|
float f = 1.0f/65535.f;
|
|
|
|
return make_float4(r.x*f, r.y*f, r.z*f, r.w*f);
|
|
|
|
}
|
2016-10-22 23:38:42 +02:00
|
|
|
/* Float */
|
Unlimited number of textures for Cycles
This patch allows for an unlimited number of textures in Cycles where the hardware allows. It replaces a number static arrays with dynamic arrays and changes the way the flat_slot indices are calculated. Eventually, I'd like to get to a point where there are only flat slots left and textures off all kinds are stored in a single array.
Note that the arrays in DeviceScene are changed from containing device_vector<T> objects to device_vector<T>* pointers. Ideally, I'd like to store objects, but dynamic resizing of a std:vector in pre-C++11 calls the copy constructor, which for a good reason is not implemented for device_vector. Once we require C++11 for Cycles builds, we can implement a move constructor for device_vector and store objects again.
The limits for CUDA Fermi hardware still apply.
Reviewers: tod_baudais, InsigMathK, dingto, #cycles
Reviewed By: dingto, #cycles
Subscribers: dingto, smellslikedonkey
Differential Revision: https://developer.blender.org/D2650
2017-04-27 09:34:51 +02:00
|
|
|
else if(texture_type == IMAGE_DATA_TYPE_FLOAT) {
|
2017-08-08 07:12:04 -04:00
|
|
|
float f = tex_fetch(float, info, offset);
|
2016-10-22 23:38:42 +02:00
|
|
|
return make_float4(f, f, f, 1.0f);
|
|
|
|
}
|
2018-07-05 12:37:52 +02:00
|
|
|
/* UShort */
|
|
|
|
else if(texture_type == IMAGE_DATA_TYPE_USHORT) {
|
|
|
|
ushort r = tex_fetch(ushort, info, offset);
|
|
|
|
float f = r * (1.0f / 65535.0f);
|
|
|
|
return make_float4(f, f, f, 1.0f);
|
|
|
|
}
|
2016-10-22 23:38:42 +02:00
|
|
|
/* Byte */
|
|
|
|
else {
|
2017-08-08 07:12:04 -04:00
|
|
|
uchar r = tex_fetch(uchar, info, offset);
|
2016-10-22 23:38:42 +02:00
|
|
|
float f = r * (1.0f/255.0f);
|
|
|
|
return make_float4(f, f, f, 1.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
ccl_device_inline float4 svm_image_texture_read_2d(KernelGlobals *kg, int id, int x, int y)
|
2016-10-22 23:38:42 +02:00
|
|
|
{
|
2017-10-15 17:40:01 +02:00
|
|
|
const ccl_global TextureInfo *info = kernel_tex_info(kg, id);
|
|
|
|
|
|
|
|
/* Wrap */
|
|
|
|
if(info->extension == EXTENSION_REPEAT) {
|
|
|
|
x = svm_image_texture_wrap_periodic(x, info->width);
|
|
|
|
y = svm_image_texture_wrap_periodic(y, info->height);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x = svm_image_texture_wrap_clamp(x, info->width);
|
|
|
|
y = svm_image_texture_wrap_clamp(y, info->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
int offset = x + info->width * y;
|
|
|
|
return svm_image_texture_read(kg, info, id, offset);
|
2016-10-22 23:38:42 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
ccl_device_inline float4 svm_image_texture_read_3d(KernelGlobals *kg, int id, int x, int y, int z)
|
2016-10-22 23:38:42 +02:00
|
|
|
{
|
2017-10-15 17:40:01 +02:00
|
|
|
const ccl_global TextureInfo *info = kernel_tex_info(kg, id);
|
|
|
|
|
|
|
|
/* Wrap */
|
|
|
|
if(info->extension == EXTENSION_REPEAT) {
|
|
|
|
x = svm_image_texture_wrap_periodic(x, info->width);
|
|
|
|
y = svm_image_texture_wrap_periodic(y, info->height);
|
|
|
|
z = svm_image_texture_wrap_periodic(z, info->depth);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x = svm_image_texture_wrap_clamp(x, info->width);
|
|
|
|
y = svm_image_texture_wrap_clamp(y, info->height);
|
|
|
|
z = svm_image_texture_wrap_clamp(z, info->depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
int offset = x + info->width * y + info->width * info->height * z;
|
|
|
|
return svm_image_texture_read(kg, info, id, offset);
|
2016-10-22 23:38:42 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
|
2016-10-22 23:38:42 +02:00
|
|
|
ccl_device_inline float svm_image_texture_frac(float x, int *ix)
|
|
|
|
{
|
|
|
|
int i = float_to_int(x) - ((x < 0.0f)? 1: 0);
|
|
|
|
*ix = i;
|
|
|
|
return x - (float)i;
|
|
|
|
}
|
|
|
|
|
2017-10-08 02:36:05 +02:00
|
|
|
#define SET_CUBIC_SPLINE_WEIGHTS(u, t) \
|
|
|
|
{ \
|
|
|
|
u[0] = (((-1.0f/6.0f)* t + 0.5f) * t - 0.5f) * t + (1.0f/6.0f); \
|
|
|
|
u[1] = (( 0.5f * t - 1.0f) * t ) * t + (2.0f/3.0f); \
|
|
|
|
u[2] = (( -0.5f * t + 0.5f) * t + 0.5f) * t + (1.0f/6.0f); \
|
|
|
|
u[3] = (1.0f / 6.0f) * t * t * t; \
|
|
|
|
} (void)0
|
|
|
|
|
2017-04-28 15:20:34 +02:00
|
|
|
ccl_device float4 kernel_tex_image_interp(KernelGlobals *kg, int id, float x, float y)
|
|
|
|
{
|
2017-10-06 21:47:41 +02:00
|
|
|
const ccl_global TextureInfo *info = kernel_tex_info(kg, id);
|
2017-08-08 07:12:04 -04:00
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
if(info->extension == EXTENSION_CLIP) {
|
|
|
|
if(x < 0.0f || y < 0.0f || x > 1.0f || y > 1.0f) {
|
|
|
|
return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 07:12:04 -04:00
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
if(info->interpolation == INTERPOLATION_CLOSEST) {
|
|
|
|
/* Closest interpolation. */
|
2017-10-08 02:36:05 +02:00
|
|
|
int ix, iy;
|
2017-10-15 17:40:01 +02:00
|
|
|
svm_image_texture_frac(x*info->width, &ix);
|
|
|
|
svm_image_texture_frac(y*info->height, &iy);
|
2016-10-22 23:38:42 +02:00
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
return svm_image_texture_read_2d(kg, id, ix, iy);
|
|
|
|
}
|
|
|
|
else if(info->interpolation == INTERPOLATION_LINEAR) {
|
|
|
|
/* Bilinear interpolation. */
|
|
|
|
int ix, iy;
|
|
|
|
float tx = svm_image_texture_frac(x*info->width - 0.5f, &ix);
|
|
|
|
float ty = svm_image_texture_frac(y*info->height - 0.5f, &iy);
|
|
|
|
|
|
|
|
float4 r;
|
|
|
|
r = (1.0f - ty)*(1.0f - tx)*svm_image_texture_read_2d(kg, id, ix, iy);
|
|
|
|
r += (1.0f - ty)*tx*svm_image_texture_read_2d(kg, id, ix+1, iy);
|
|
|
|
r += ty*(1.0f - tx)*svm_image_texture_read_2d(kg, id, ix, iy+1);
|
|
|
|
r += ty*tx*svm_image_texture_read_2d(kg, id, ix+1, iy+1);
|
|
|
|
return r;
|
2016-10-22 23:38:42 +02:00
|
|
|
}
|
2017-10-08 02:36:05 +02:00
|
|
|
else {
|
|
|
|
/* Bicubic interpolation. */
|
2017-10-15 17:40:01 +02:00
|
|
|
int ix, iy;
|
|
|
|
float tx = svm_image_texture_frac(x*info->width - 0.5f, &ix);
|
|
|
|
float ty = svm_image_texture_frac(y*info->height - 0.5f, &iy);
|
2017-10-08 02:36:05 +02:00
|
|
|
|
|
|
|
float u[4], v[4];
|
|
|
|
SET_CUBIC_SPLINE_WEIGHTS(u, tx);
|
|
|
|
SET_CUBIC_SPLINE_WEIGHTS(v, ty);
|
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
float4 r = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
|
|
|
|
for(int y = 0; y < 4; y++) {
|
|
|
|
for(int x = 0; x < 4; x++) {
|
|
|
|
float weight = u[x]*v[y];
|
|
|
|
r += weight*svm_image_texture_read_2d(kg, id, ix+x-1, iy+y-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
2016-10-22 23:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-07 02:15:12 +02:00
|
|
|
ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals *kg, int id, float x, float y, float z, int interp)
|
2016-10-22 23:38:42 +02:00
|
|
|
{
|
2017-10-06 21:47:41 +02:00
|
|
|
const ccl_global TextureInfo *info = kernel_tex_info(kg, id);
|
2017-08-08 07:12:04 -04:00
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
if(info->extension == EXTENSION_CLIP) {
|
|
|
|
if(x < 0.0f || y < 0.0f || z < 0.0f ||
|
|
|
|
x > 1.0f || y > 1.0f || z > 1.0f)
|
|
|
|
{
|
|
|
|
return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 02:15:12 +02:00
|
|
|
uint interpolation = (interp == INTERPOLATION_NONE)? info->interpolation: interp;
|
2017-08-08 07:12:04 -04:00
|
|
|
|
2016-10-22 23:38:42 +02:00
|
|
|
if(interpolation == INTERPOLATION_CLOSEST) {
|
2017-10-15 17:40:01 +02:00
|
|
|
/* Closest interpolation. */
|
2017-10-08 02:36:05 +02:00
|
|
|
int ix, iy, iz;
|
2017-10-15 17:40:01 +02:00
|
|
|
svm_image_texture_frac(x*info->width, &ix);
|
|
|
|
svm_image_texture_frac(y*info->height, &iy);
|
|
|
|
svm_image_texture_frac(z*info->depth, &iz);
|
|
|
|
|
|
|
|
return svm_image_texture_read_3d(kg, id, ix, iy, iz);
|
|
|
|
}
|
|
|
|
else if(interpolation == INTERPOLATION_LINEAR) {
|
|
|
|
/* Bilinear interpolation. */
|
|
|
|
int ix, iy, iz;
|
|
|
|
float tx = svm_image_texture_frac(x*info->width - 0.5f, &ix);
|
|
|
|
float ty = svm_image_texture_frac(y*info->height - 0.5f, &iy);
|
|
|
|
float tz = svm_image_texture_frac(z*info->depth - 0.5f, &iz);
|
|
|
|
|
|
|
|
float4 r;
|
|
|
|
r = (1.0f - tz)*(1.0f - ty)*(1.0f - tx)*svm_image_texture_read_3d(kg, id, ix, iy, iz);
|
|
|
|
r += (1.0f - tz)*(1.0f - ty)*tx*svm_image_texture_read_3d(kg, id, ix+1, iy, iz);
|
|
|
|
r += (1.0f - tz)*ty*(1.0f - tx)*svm_image_texture_read_3d(kg, id, ix, iy+1, iz);
|
|
|
|
r += (1.0f - tz)*ty*tx*svm_image_texture_read_3d(kg, id, ix+1, iy+1, iz);
|
|
|
|
|
|
|
|
r += tz*(1.0f - ty)*(1.0f - tx)*svm_image_texture_read_3d(kg, id, ix, iy, iz+1);
|
|
|
|
r += tz*(1.0f - ty)*tx*svm_image_texture_read_3d(kg, id, ix+1, iy, iz+1);
|
|
|
|
r += tz*ty*(1.0f - tx)*svm_image_texture_read_3d(kg, id, ix, iy+1, iz+1);
|
|
|
|
r += tz*ty*tx*svm_image_texture_read_3d(kg, id, ix+1, iy+1, iz+1);
|
|
|
|
return r;
|
2016-10-22 23:38:42 +02:00
|
|
|
}
|
2017-10-08 02:36:05 +02:00
|
|
|
else {
|
|
|
|
/* Bicubic interpolation. */
|
2017-10-15 17:40:01 +02:00
|
|
|
int ix, iy, iz;
|
|
|
|
float tx = svm_image_texture_frac(x*info->width - 0.5f, &ix);
|
|
|
|
float ty = svm_image_texture_frac(y*info->height - 0.5f, &iy);
|
|
|
|
float tz = svm_image_texture_frac(z*info->depth - 0.5f, &iz);
|
2017-10-08 02:36:05 +02:00
|
|
|
|
|
|
|
float u[4], v[4], w[4];
|
|
|
|
SET_CUBIC_SPLINE_WEIGHTS(u, tx);
|
|
|
|
SET_CUBIC_SPLINE_WEIGHTS(v, ty);
|
|
|
|
SET_CUBIC_SPLINE_WEIGHTS(w, tz);
|
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
float4 r = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
2017-10-08 02:36:05 +02:00
|
|
|
|
2017-10-15 17:40:01 +02:00
|
|
|
for(int z = 0; z < 4; z++) {
|
|
|
|
for(int y = 0; y < 4; y++) {
|
|
|
|
for(int x = 0; x < 4; x++) {
|
|
|
|
float weight = u[x]*v[y]*w[z];
|
|
|
|
r += weight*svm_image_texture_read_3d(kg, id, ix+x-1, iy+y-1, iz+z-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
2016-10-22 23:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-08 02:36:05 +02:00
|
|
|
|
|
|
|
#undef SET_CUBIC_SPLINE_WEIGHTS
|