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 02:50:24 +01:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2013-11-16 00:17:10 +01:00
|
|
|
ccl_device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb, uint use_alpha)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2016-10-22 23:38:42 +02:00
|
|
|
float4 r = kernel_tex_image_interp(kg, id, x, y);
|
2017-05-03 11:22:27 +02:00
|
|
|
const float alpha = r.w;
|
2014-02-11 17:39:55 +04:00
|
|
|
|
|
|
|
if(use_alpha && alpha != 1.0f && alpha != 0.0f) {
|
2017-05-03 11:22:27 +02:00
|
|
|
r /= alpha;
|
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
|
|
|
const int texture_type = kernel_tex_type(id);
|
2017-05-03 11:22:27 +02:00
|
|
|
if(texture_type == IMAGE_DATA_TYPE_BYTE4 ||
|
|
|
|
texture_type == IMAGE_DATA_TYPE_BYTE)
|
|
|
|
{
|
|
|
|
r = min(r, make_float4(1.0f, 1.0f, 1.0f, 1.0f));
|
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
|
|
|
}
|
2014-02-10 17:19:26 +04:00
|
|
|
r.w = alpha;
|
2014-01-13 18:21:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(srgb) {
|
2018-06-14 17:48:19 +02:00
|
|
|
/* TODO(lukas): Implement proper conversion for image textures. */
|
|
|
|
r = color_srgb_to_linear_v4(r);
|
2014-01-13 18:21:34 +04:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-01-22 00:37:09 +05:00
|
|
|
/* Remap coordnate from 0..1 box to -1..-1 */
|
|
|
|
ccl_device_inline float3 texco_remap_square(float3 co)
|
|
|
|
{
|
|
|
|
return (co - make_float3(0.5f, 0.5f, 0.5f)) * 2.0f;
|
|
|
|
}
|
|
|
|
|
2013-11-16 00:17:10 +01:00
|
|
|
ccl_device void svm_node_tex_image(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-05-13 14:32:08 +00:00
|
|
|
uint id = node.y;
|
2011-09-16 13:14:02 +00:00
|
|
|
uint co_offset, out_offset, alpha_offset, srgb;
|
2011-05-13 14:32:08 +00:00
|
|
|
|
2011-09-16 13:14:02 +00:00
|
|
|
decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
|
2011-05-13 14:32:08 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
float3 co = stack_load_float3(stack, co_offset);
|
2015-02-19 12:52:48 +05:00
|
|
|
float2 tex_co;
|
2013-04-05 16:43:53 +00:00
|
|
|
uint use_alpha = stack_valid(alpha_offset);
|
2015-01-21 22:44:30 +05:00
|
|
|
if(node.w == NODE_IMAGE_PROJ_SPHERE) {
|
2015-01-22 00:37:09 +05:00
|
|
|
co = texco_remap_square(co);
|
2015-02-19 12:52:48 +05:00
|
|
|
tex_co = map_to_sphere(co);
|
2015-01-21 22:44:30 +05:00
|
|
|
}
|
2015-01-22 00:37:09 +05:00
|
|
|
else if(node.w == NODE_IMAGE_PROJ_TUBE) {
|
|
|
|
co = texco_remap_square(co);
|
2015-02-19 12:52:48 +05:00
|
|
|
tex_co = map_to_tube(co);
|
2015-01-22 00:37:09 +05:00
|
|
|
}
|
2015-02-19 12:52:48 +05:00
|
|
|
else {
|
|
|
|
tex_co = make_float2(co.x, co.y);
|
|
|
|
}
|
|
|
|
float4 f = svm_image_texture(kg, id, tex_co.x, tex_co.y, srgb, use_alpha);
|
2011-05-13 14:32:08 +00:00
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
if(stack_valid(out_offset))
|
|
|
|
stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
|
|
|
|
if(stack_valid(alpha_offset))
|
|
|
|
stack_store_float(stack, alpha_offset, f.w);
|
|
|
|
}
|
|
|
|
|
2013-11-16 00:17:10 +01:00
|
|
|
ccl_device void svm_node_tex_image_box(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
|
2012-09-04 13:29:07 +00:00
|
|
|
{
|
|
|
|
/* get object space normal */
|
2017-02-16 06:24:13 -05:00
|
|
|
float3 N = sd->N;
|
2012-09-04 13:29:07 +00:00
|
|
|
|
2017-02-16 06:24:13 -05:00
|
|
|
N = sd->N;
|
2016-11-03 03:08:14 +01:00
|
|
|
object_inverse_normal_transform(kg, sd, &N);
|
2012-09-04 13:29:07 +00:00
|
|
|
|
|
|
|
/* project from direction vector to barycentric coordinates in triangles */
|
2017-09-05 18:11:13 +02:00
|
|
|
float3 signed_N = N;
|
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
N.x = fabsf(N.x);
|
|
|
|
N.y = fabsf(N.y);
|
|
|
|
N.z = fabsf(N.z);
|
|
|
|
|
|
|
|
N /= (N.x + N.y + N.z);
|
|
|
|
|
|
|
|
/* basic idea is to think of this as a triangle, each corner representing
|
|
|
|
* one of the 3 faces of the cube. in the corners we have single textures,
|
|
|
|
* in between we blend between two textures, and in the middle we a blend
|
|
|
|
* between three textures.
|
|
|
|
*
|
|
|
|
* the Nxyz values are the barycentric coordinates in an equilateral
|
2012-10-20 15:09:36 +00:00
|
|
|
* triangle, which in case of blending, in the middle has a smaller
|
2012-09-04 13:29:07 +00:00
|
|
|
* equilateral triangle where 3 textures blend. this divides things into
|
|
|
|
* 7 zones, with an if() test for each zone */
|
|
|
|
|
|
|
|
float3 weight = make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
float blend = __int_as_float(node.w);
|
|
|
|
float limit = 0.5f*(1.0f + blend);
|
|
|
|
|
|
|
|
/* first test for corners with single texture */
|
|
|
|
if(N.x > limit*(N.x + N.y) && N.x > limit*(N.x + N.z)) {
|
|
|
|
weight.x = 1.0f;
|
|
|
|
}
|
|
|
|
else if(N.y > limit*(N.x + N.y) && N.y > limit*(N.y + N.z)) {
|
|
|
|
weight.y = 1.0f;
|
2011-05-20 12:26:01 +00:00
|
|
|
}
|
2012-09-04 13:29:07 +00:00
|
|
|
else if(N.z > limit*(N.x + N.z) && N.z > limit*(N.y + N.z)) {
|
|
|
|
weight.z = 1.0f;
|
|
|
|
}
|
|
|
|
else if(blend > 0.0f) {
|
|
|
|
/* in case of blending, test for mixes between two textures */
|
|
|
|
if(N.z < (1.0f - limit)*(N.y + N.x)) {
|
|
|
|
weight.x = N.x/(N.x + N.y);
|
2015-04-28 00:13:03 +05:00
|
|
|
weight.x = saturate((weight.x - 0.5f*(1.0f - blend))/blend);
|
2012-09-04 13:29:07 +00:00
|
|
|
weight.y = 1.0f - weight.x;
|
|
|
|
}
|
|
|
|
else if(N.x < (1.0f - limit)*(N.y + N.z)) {
|
|
|
|
weight.y = N.y/(N.y + N.z);
|
2015-04-28 00:13:03 +05:00
|
|
|
weight.y = saturate((weight.y - 0.5f*(1.0f - blend))/blend);
|
2012-09-04 13:29:07 +00:00
|
|
|
weight.z = 1.0f - weight.y;
|
|
|
|
}
|
|
|
|
else if(N.y < (1.0f - limit)*(N.x + N.z)) {
|
|
|
|
weight.x = N.x/(N.x + N.z);
|
2015-04-28 00:13:03 +05:00
|
|
|
weight.x = saturate((weight.x - 0.5f*(1.0f - blend))/blend);
|
2012-09-04 13:29:07 +00:00
|
|
|
weight.z = 1.0f - weight.x;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* last case, we have a mix between three */
|
|
|
|
weight.x = ((2.0f - limit)*N.x + (limit - 1.0f))/(2.0f*limit - 1.0f);
|
|
|
|
weight.y = ((2.0f - limit)*N.y + (limit - 1.0f))/(2.0f*limit - 1.0f);
|
|
|
|
weight.z = ((2.0f - limit)*N.z + (limit - 1.0f))/(2.0f*limit - 1.0f);
|
|
|
|
}
|
|
|
|
}
|
2014-10-07 15:48:39 +02:00
|
|
|
else {
|
|
|
|
/* Desperate mode, no valid choice anyway, fallback to one side.*/
|
|
|
|
weight.x = 1.0f;
|
|
|
|
}
|
2012-09-04 13:29:07 +00:00
|
|
|
|
|
|
|
/* now fetch textures */
|
|
|
|
uint co_offset, out_offset, alpha_offset, srgb;
|
|
|
|
decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
|
|
|
|
|
|
|
|
float3 co = stack_load_float3(stack, co_offset);
|
|
|
|
uint id = node.y;
|
|
|
|
|
|
|
|
float4 f = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
2013-04-05 16:43:53 +00:00
|
|
|
uint use_alpha = stack_valid(alpha_offset);
|
2012-09-04 13:29:07 +00:00
|
|
|
|
2017-09-05 18:11:13 +02:00
|
|
|
/* Map so that no textures are flipped, rotation is somewhat arbitrary. */
|
|
|
|
if(weight.x > 0.0f) {
|
|
|
|
float2 uv = make_float2((signed_N.x < 0.0f)? 1.0f - co.y: co.y, co.z);
|
|
|
|
f += weight.x*svm_image_texture(kg, id, uv.x, uv.y, srgb, use_alpha);
|
|
|
|
}
|
|
|
|
if(weight.y > 0.0f) {
|
|
|
|
float2 uv = make_float2((signed_N.y > 0.0f)? 1.0f - co.x: co.x, co.z);
|
|
|
|
f += weight.y*svm_image_texture(kg, id, uv.x, uv.y, srgb, use_alpha);
|
|
|
|
}
|
|
|
|
if(weight.z > 0.0f) {
|
|
|
|
float2 uv = make_float2((signed_N.z > 0.0f)? 1.0f - co.y: co.y, co.x);
|
|
|
|
f += weight.z*svm_image_texture(kg, id, uv.x, uv.y, srgb, use_alpha);
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-16 13:14:02 +00:00
|
|
|
if(stack_valid(out_offset))
|
2012-09-04 13:29:07 +00:00
|
|
|
stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
|
2011-09-16 13:14:02 +00:00
|
|
|
if(stack_valid(alpha_offset))
|
|
|
|
stack_store_float(stack, alpha_offset, f.w);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-11-16 00:17:10 +01:00
|
|
|
ccl_device void svm_node_tex_environment(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-05-13 14:32:08 +00:00
|
|
|
uint id = node.y;
|
2011-09-16 13:14:02 +00:00
|
|
|
uint co_offset, out_offset, alpha_offset, srgb;
|
2012-03-08 19:52:58 +00:00
|
|
|
uint projection = node.w;
|
2011-05-13 14:32:08 +00:00
|
|
|
|
2011-09-16 13:14:02 +00:00
|
|
|
decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
|
2011-05-13 14:32:08 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
float3 co = stack_load_float3(stack, co_offset);
|
2012-03-08 19:52:58 +00:00
|
|
|
float2 uv;
|
|
|
|
|
2017-05-07 14:40:58 +02:00
|
|
|
co = safe_normalize(co);
|
|
|
|
|
2012-03-08 19:52:58 +00:00
|
|
|
if(projection == 0)
|
|
|
|
uv = direction_to_equirectangular(co);
|
|
|
|
else
|
|
|
|
uv = direction_to_mirrorball(co);
|
|
|
|
|
2013-04-05 16:43:53 +00:00
|
|
|
uint use_alpha = stack_valid(alpha_offset);
|
|
|
|
float4 f = svm_image_texture(kg, id, uv.x, uv.y, srgb, use_alpha);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-16 13:14:02 +00:00
|
|
|
if(stack_valid(out_offset))
|
2012-09-04 13:29:07 +00:00
|
|
|
stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
|
2011-09-16 13:14:02 +00:00
|
|
|
if(stack_valid(alpha_offset))
|
|
|
|
stack_store_float(stack, alpha_offset, f.w);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|