├── .gitattributes ├── rt.exe ├── README.md ├── src ├── framework │ ├── gui │ │ └── Keys.hpp │ ├── base │ │ ├── Array.cpp │ │ ├── Deque.cpp │ │ ├── BinaryHeap.cpp │ │ ├── Main.hpp │ │ ├── Timer.cpp │ │ ├── UnionFind.hpp │ │ ├── UnionFind.cpp │ │ ├── Hash.cpp │ │ ├── Timer.hpp │ │ ├── Math.cpp │ │ ├── MulticoreLauncher.hpp │ │ └── Main.cpp │ ├── io │ │ ├── ImageRawPngIO.hpp │ │ ├── ImageBmpIO.hpp │ │ ├── ImageTiffIO.hpp │ │ ├── ImageLodePngIO.hpp │ │ ├── ImageTargaIO.hpp │ │ ├── MeshWavefrontIO.hpp │ │ ├── AviExporter.hpp │ │ ├── ImageBinaryIO.hpp │ │ ├── MeshBinaryIO.hpp │ │ ├── StateDump.cpp │ │ ├── ImageBinaryIO.cpp │ │ ├── ImageLodePngIO.cpp │ │ ├── StateDump.hpp │ │ └── ImageRawPngIO.cpp │ ├── 3d │ │ ├── TextureAtlas.hpp │ │ ├── Texture.hpp │ │ └── ConvexPolyhedron.hpp │ └── gpu │ │ └── CudaModule.hpp └── rt │ ├── kernels │ └── tesla_persistent_packet.cu │ ├── bvh │ ├── Platform.cpp │ ├── Platform.hpp │ ├── BVHNode.hpp │ ├── BVH.hpp │ ├── BVHNode.cpp │ └── SplitBVHBuilder.hpp │ ├── ray │ ├── PixelTable.hpp │ ├── RayGen.hpp │ ├── RayGenKernels.hpp │ ├── RayBufferKernels.hpp │ ├── RayBuffer.hpp │ └── PixelTable.cpp │ ├── cuda │ ├── CudaTracer.hpp │ ├── RendererKernels.hpp │ └── Renderer.hpp │ ├── Scene.hpp │ ├── Scene.cpp │ ├── Util.cpp │ ├── Util.hpp │ └── App.hpp ├── scenes └── rt │ ├── sibenik │ ├── sibenik.obj │ └── sibenik.mtl │ ├── conference │ ├── conference.obj │ ├── NOTICE │ └── conference.mtl │ ├── fairyforest │ └── fairyforest.obj │ └── sanmiguel │ ├── sanmiguel.obj │ └── NOTICE ├── LICENSE.nvidia ├── rt.sln ├── benchmark.cmd └── rt.vcxproj.filters /.gitattributes: -------------------------------------------------------------------------------- 1 | *.obj filter=lfs diff=lfs merge=lfs -text 2 | -------------------------------------------------------------------------------- /rt.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lispbub/simd-ray-traversal/HEAD/rt.exe -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simd-ray-traversal 2 | Source for our HPG Paper "CPU-Style SIMD Ray Traversal on GPUs" 3 | -------------------------------------------------------------------------------- /src/framework/gui/Keys.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lispbub/simd-ray-traversal/HEAD/src/framework/gui/Keys.hpp -------------------------------------------------------------------------------- /src/rt/kernels/tesla_persistent_packet.cu: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lispbub/simd-ray-traversal/HEAD/src/rt/kernels/tesla_persistent_packet.cu -------------------------------------------------------------------------------- /scenes/rt/sibenik/sibenik.obj: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:b1c649c7feec2cb7f60aa2fde6bdae2e422f8fb843e9072db38dabcc87852551 3 | size 24510508 4 | -------------------------------------------------------------------------------- /scenes/rt/conference/conference.obj: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:b115542f78d7ae7abfd124666202a5380991ab2b59b9e3292b23706daf102a33 3 | size 11973007 4 | -------------------------------------------------------------------------------- /scenes/rt/fairyforest/fairyforest.obj: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:b433aa0fcd7f36eeb2219fa2ec5b9b14a5e1d9a0fdf5a75a3b5efb7266f79f68 3 | size 15579692 4 | -------------------------------------------------------------------------------- /scenes/rt/sanmiguel/sanmiguel.obj: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:dbea65187c0d39ef094074988a0a46c22baa75353d5e246e3b6e0f2e3d9b5e01 3 | size 475511198 4 | -------------------------------------------------------------------------------- /scenes/rt/sanmiguel/NOTICE: -------------------------------------------------------------------------------- 1 | This scene was created by Guillermo M. Leal Llaguno (g.llaguno@evvisual.com) 2 | of Evolucion Visual (www.evvisual.com), based on a hacienda that he visited in 3 | San Miguel de Allende, Mexico. The scene was modeled in 3ds max and exported 4 | to the pbrt file format with a custom script written by Guillermo. The pbrt 5 | model was further converted to Wavefront OBJ file format by Tero Karras 6 | (tkarras@nvidia.com) using a custom exporter. 7 | -------------------------------------------------------------------------------- /scenes/rt/conference/NOTICE: -------------------------------------------------------------------------------- 1 | This model was created by Anat Grynberg and Greg Ward 2 | under project funding by the Department of Energy 3 | at the Lawrence Berkeley Laboratory. 4 | It is available in its original form as a Radiance 5 | model with procedural textures from the following site: 6 | 7 | http://radsite.lbl.gov/radiance/pub/models/ 8 | 9 | An MGF (Materials & Geometry Format) file of the 10 | model without textures may be obtained from: 11 | 12 | http://radsite.lbl.gov/mgf/scenes.html 13 | 14 | Please give credit to the authors in any publications 15 | exhibiting this work. 16 | -------------------------------------------------------------------------------- /LICENSE.nvidia: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2011, NVIDIA Corporation 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of NVIDIA Corporation nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /src/rt/bvh/Platform.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "bvh/Platform.hpp" 29 | #include "bvh/BVHNode.hpp" 30 | -------------------------------------------------------------------------------- /src/framework/base/Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "base/Array.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | -------------------------------------------------------------------------------- /src/framework/base/Deque.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "base/Deque.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | -------------------------------------------------------------------------------- /src/framework/base/BinaryHeap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "base/BinaryHeap.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | -------------------------------------------------------------------------------- /src/framework/io/ImageRawPngIO.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Defs.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class Image; 36 | class OutputStream; 37 | 38 | //------------------------------------------------------------------------ 39 | 40 | void exportRawPngImage (OutputStream& stream, const Image* image); 41 | 42 | //------------------------------------------------------------------------ 43 | } 44 | -------------------------------------------------------------------------------- /src/framework/io/ImageBmpIO.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Defs.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class Image; 36 | class InputStream; 37 | class OutputStream; 38 | 39 | //------------------------------------------------------------------------ 40 | 41 | Image* importBmpImage (InputStream& stream); 42 | void exportBmpImage (OutputStream& stream, const Image* image); 43 | 44 | //------------------------------------------------------------------------ 45 | } 46 | -------------------------------------------------------------------------------- /src/framework/io/ImageTiffIO.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Defs.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class Image; 36 | class InputStream; 37 | class OutputStream; 38 | 39 | //------------------------------------------------------------------------ 40 | 41 | Image* importTiffImage (InputStream& stream); 42 | void exportTiffImage (OutputStream& stream, const Image* image); 43 | 44 | //------------------------------------------------------------------------ 45 | } 46 | -------------------------------------------------------------------------------- /src/framework/io/ImageLodePngIO.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Defs.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class Image; 36 | class InputStream; 37 | class OutputStream; 38 | 39 | //------------------------------------------------------------------------ 40 | 41 | Image* importLodePngImage (InputStream& stream); 42 | void exportLodePngImage (OutputStream& stream, const Image* image); 43 | 44 | //------------------------------------------------------------------------ 45 | } 46 | -------------------------------------------------------------------------------- /src/framework/io/ImageTargaIO.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Defs.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class Image; 36 | class InputStream; 37 | class OutputStream; 38 | 39 | //------------------------------------------------------------------------ 40 | 41 | Image* importTargaImage (InputStream& stream); 42 | void exportTargaImage (OutputStream& stream, const Image* image); 43 | 44 | //------------------------------------------------------------------------ 45 | } 46 | -------------------------------------------------------------------------------- /rt.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27004.2002 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "framework", "framework.vcxproj", "{8E0B71BD-6F14-4F0C-AC34-45985043856F}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "rt", "rt.vcxproj", "{EC32C587-08D5-4F90-8E47-463278872364}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Win32 = Debug|Win32 13 | Debug|x64 = Debug|x64 14 | Release|Win32 = Release|Win32 15 | Release|x64 = Release|x64 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {8E0B71BD-6F14-4F0C-AC34-45985043856F}.Debug|Win32.ActiveCfg = Debug|Win32 19 | {8E0B71BD-6F14-4F0C-AC34-45985043856F}.Debug|Win32.Build.0 = Debug|Win32 20 | {8E0B71BD-6F14-4F0C-AC34-45985043856F}.Debug|x64.ActiveCfg = Debug|x64 21 | {8E0B71BD-6F14-4F0C-AC34-45985043856F}.Debug|x64.Build.0 = Debug|x64 22 | {8E0B71BD-6F14-4F0C-AC34-45985043856F}.Release|Win32.ActiveCfg = Release|Win32 23 | {8E0B71BD-6F14-4F0C-AC34-45985043856F}.Release|Win32.Build.0 = Release|Win32 24 | {8E0B71BD-6F14-4F0C-AC34-45985043856F}.Release|x64.ActiveCfg = Release|x64 25 | {8E0B71BD-6F14-4F0C-AC34-45985043856F}.Release|x64.Build.0 = Release|x64 26 | {EC32C587-08D5-4F90-8E47-463278872364}.Debug|Win32.ActiveCfg = Debug|Win32 27 | {EC32C587-08D5-4F90-8E47-463278872364}.Debug|Win32.Build.0 = Debug|Win32 28 | {EC32C587-08D5-4F90-8E47-463278872364}.Debug|x64.ActiveCfg = Debug|x64 29 | {EC32C587-08D5-4F90-8E47-463278872364}.Debug|x64.Build.0 = Debug|x64 30 | {EC32C587-08D5-4F90-8E47-463278872364}.Release|Win32.ActiveCfg = Release|Win32 31 | {EC32C587-08D5-4F90-8E47-463278872364}.Release|Win32.Build.0 = Release|Win32 32 | {EC32C587-08D5-4F90-8E47-463278872364}.Release|x64.ActiveCfg = Release|x64 33 | {EC32C587-08D5-4F90-8E47-463278872364}.Release|x64.Build.0 = Release|x64 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | GlobalSection(ExtensibilityGlobals) = postSolution 39 | SolutionGuid = {85C00A8C-BE62-4B4C-8FD6-F88E723762F3} 40 | EndGlobalSection 41 | EndGlobal 42 | -------------------------------------------------------------------------------- /src/framework/io/MeshWavefrontIO.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "3d/Mesh.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class BufferedInputStream; 36 | class BufferedOutputStream; 37 | 38 | //------------------------------------------------------------------------ 39 | 40 | Mesh* importWavefrontMesh (BufferedInputStream& stream, const String& fileName); 41 | void exportWavefrontMesh (BufferedOutputStream& stream, const MeshBase* mesh, const String& fileName); 42 | 43 | //------------------------------------------------------------------------ 44 | } 45 | -------------------------------------------------------------------------------- /src/framework/base/Main.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Defs.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | extern int argc; 36 | extern char** argv; 37 | extern int exitCode; 38 | 39 | //------------------------------------------------------------------------ 40 | 41 | void init (void); // Main entry point, defined by the application. 42 | 43 | void disableLeakCheck (void); // Call to disable memory leak check at application exit in debug build. 44 | 45 | //------------------------------------------------------------------------ 46 | } 47 | -------------------------------------------------------------------------------- /benchmark.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | set LOG=benchmark.log 3 | 4 | rem Find executable. 5 | 6 | set EXE=rt_x64_Release.exe 7 | if not exist %EXE% set EXE=rt_Win32_Release.exe 8 | if not exist %EXE% set EXE=rt.exe 9 | 10 | rem Benchmark conference, fairyforest, and sibenik. 11 | 12 | %EXE% benchmark --log=%LOG% --mesh=scenes/rt/conference/conference.obj --sbvh-alpha=1.0e-5 --ao-radius=5 --camera="6omr/04j3200bR6Z/0/3ZEAz/x4smy19///c/05frY109Qx7w////m100" --camera="Lpmr/07k3200CS6Z/0/QqOIz1qfnsx19///c/05frY109Qx7w////m100" --camera="Y1BR00IkZd/0aA9X/0/Gy8Px1ca7Tw19///c/05frY109Qx7w////m100" --camera="XYDl00Gqv600byxY/00IQE4x/jN1jx/9///c/05frY109Qx7w////m100" --camera="w:ie00yxXX00ND1b/03TZ6qy1egt3x/9///c/05frY109Qx7w////m100" 13 | %EXE% benchmark --log=%LOG% --mesh=scenes/rt/fairyforest/fairyforest.obj --sbvh-alpha=1.0e-5 --ao-radius=0.3 --camera="cIxMx/sK/Ty/EFu3z/5m9mWx/YPA5z/8///m007toC10AnAHx///Uy200" --camera="KI/Qz/zlsUy/TTy6z13BdCZy/LRxzy/8///m007toC10AnAHx///Uy200" --camera="mF5Gz1SuO1z/ZMooz11Q0bGz/CCNxx18///m007toC10AnAHx///Uy200" --camera="vH7Jy19GSHx/YN45x//P2Wpx1MkhWy18///m007toC10AnAHx///Uy200" --camera="ViGsx/KxTFz/Ypn8/05TJTmx1ljevx18///m007toC10AnAHx///Uy200" 14 | %EXE% benchmark --log=%LOG% --mesh=scenes/rt/sibenik/sibenik.obj --sbvh-alpha=1.0e-5 --ao-radius=5 --camera="ytIa02G35kz1i:ZZ/0//iSay/5W6Ex19///c/05frY109Qx7w////m100" --camera=":Wp802ACAD/2x9OQ/0/waE8z/IOKbx/9///c/05frY109Qx7w////m100" --camera="CFtpy/s6ea/28btX/0172CFy/K5g1z/9///c/05frY109Qx7w////m100" --camera="steO/0TlN1z1tsDg/03InaMz/bqZxx/9///c/05frY109Qx7w////m100" --camera="HJv//034:Rx1S4Xh/03dpXux1BVmGw/9///c/05frY109Qx7w////m100" 15 | 16 | rem Benchmark San Miguel. 17 | rem - Requires 64-bit build to avoid running out of CPU virtual address space. 18 | rem - Do not use Tesla kernels, since they require more than 1.5 GB of GPU memory for the BVH. 19 | 20 | if "%EXE%"=="rt_x64_Release.exe" goto run_sanmiguel 21 | echo San Miguel requires 64-bit build. Skipping. 22 | goto done 23 | 24 | :run_sanmiguel 25 | %EXE% benchmark --log=%LOG% --mesh=scenes/rt/sanmiguel/sanmiguel.obj --sbvh-alpha=1.0e-6 --ao-radius=1.5 --kernel=fermi_speculative_while_while --kernel=kepler_dynamic_fetch --camera="Yciwz1oRQmz/Xvsm005CwjHx/b70nx18tVI7005frY108Y/:x/v3/z100" --camera="NhL2/2tO1w/0OIZh005DPZMz/xC9Cz18tVI7005frY108Y/:x/v3/z100" --camera="AbE3/0LWiZz/4Ccj005X5X1z1qJ13x/8BfRky/5frY108Y/:x/v3/z100" 26 | 27 | :done 28 | echo Done. 29 | -------------------------------------------------------------------------------- /src/framework/base/Timer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "base/Timer.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | 34 | F64 Timer::s_ticksToSecsCoef = -1.0; 35 | S64 Timer::s_prevTicks = 0; 36 | 37 | //------------------------------------------------------------------------ 38 | 39 | void Timer::staticInit(void) 40 | { 41 | LARGE_INTEGER freq; 42 | if (!QueryPerformanceFrequency(&freq)) 43 | failWin32Error("QueryPerformanceFrequency"); 44 | s_ticksToSecsCoef = max(1.0 / (F64)freq.QuadPart, 0.0); 45 | } 46 | 47 | //------------------------------------------------------------------------ 48 | -------------------------------------------------------------------------------- /src/rt/ray/PixelTable.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "gpu/Buffer.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class PixelTable 36 | { 37 | public: 38 | PixelTable (void); 39 | ~PixelTable (void); 40 | 41 | void setSize (const Vec2i& size); 42 | 43 | const Vec2i& getSize (void) const { return m_size; } 44 | Buffer& getIndexToPixel (void) { return m_indexToPixel; } 45 | Buffer& getPixelToIndex (void) { return m_pixelToIndex; } 46 | 47 | private: 48 | void recalculate (void); 49 | 50 | Vec2i m_size; 51 | Buffer m_indexToPixel; // S32 52 | Buffer m_pixelToIndex; // S32 53 | }; 54 | 55 | //------------------------------------------------------------------------ 56 | } 57 | -------------------------------------------------------------------------------- /src/framework/base/UnionFind.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Array.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class UnionFind 36 | { 37 | public: 38 | explicit UnionFind (int capacity = 0) { setCapacity(capacity); } 39 | UnionFind (const UnionFind& other) { set(other); } 40 | ~UnionFind (void) {} 41 | 42 | int unionSets (int idxA, int idxB); 43 | int findSet (int idx) const; 44 | bool isSameSet (int idxA, int idxB) const { return (findSet(idxA) == findSet(idxB)); } 45 | 46 | void clear (void) { m_sets.clear(); } 47 | void reset (void) { m_sets.reset(); } 48 | void setCapacity (int capacity) { m_sets.setCapacity(capacity); } 49 | void set (const UnionFind& other) { m_sets = other.m_sets; } 50 | 51 | UnionFind& operator= (const UnionFind& other) { set(other); return *this; } 52 | int operator[] (int idx) const { return findSet(idx); } 53 | 54 | private: 55 | mutable Array m_sets; 56 | }; 57 | 58 | //------------------------------------------------------------------------ 59 | } 60 | -------------------------------------------------------------------------------- /src/rt/cuda/CudaTracer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "gpu/CudaCompiler.hpp" 30 | #include "cuda/CudaBVH.hpp" 31 | #include "ray/RayBuffer.hpp" 32 | 33 | namespace FW 34 | { 35 | //------------------------------------------------------------------------ 36 | 37 | class CudaTracer 38 | { 39 | public: 40 | CudaTracer (void); 41 | ~CudaTracer (void); 42 | 43 | void setMessageWindow (Window* window) { m_compiler.setMessageWindow(window); } 44 | void setKernel (const String& kernelName); 45 | BVHLayout getDesiredBVHLayout (void) const { return (BVHLayout)m_kernelConfig.bvhLayout; } 46 | void setBVH (CudaBVH* bvh) { m_bvh = bvh; } 47 | 48 | F32 traceBatch (RayBuffer& rays); // returns launch time in seconds 49 | 50 | private: 51 | CudaModule* compileKernel (void); 52 | 53 | private: 54 | CudaTracer (const CudaTracer&); // forbidden 55 | CudaTracer& operator= (const CudaTracer&); // forbidden 56 | 57 | private: 58 | CudaCompiler m_compiler; 59 | String m_kernelName; 60 | KernelConfig m_kernelConfig; 61 | CudaBVH* m_bvh; 62 | }; 63 | 64 | //------------------------------------------------------------------------ 65 | } 66 | -------------------------------------------------------------------------------- /src/framework/io/AviExporter.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "io/File.hpp" 30 | #include "gui/Image.hpp" 31 | 32 | namespace FW 33 | { 34 | 35 | //------------------------------------------------------------------------ 36 | 37 | class AviExporter 38 | { 39 | public: 40 | AviExporter (const String& fileName, Vec2i size, int fps); 41 | ~AviExporter (void); 42 | 43 | const Image& getFrame (void) const { return m_frame; } 44 | Image& getFrame (void) { return m_frame; } 45 | int getFPS (void) const { return m_fps; } 46 | 47 | void exportFrame (void); 48 | void flush (void); 49 | 50 | private: 51 | void setTag (int ofs, const char* tag); 52 | void setS32 (int ofs, S32 value); 53 | void writeHeader (void); 54 | 55 | private: 56 | AviExporter (const AviExporter&); // forbidden 57 | AviExporter& operator= (const AviExporter&); // forbidden 58 | 59 | private: 60 | File m_file; 61 | Vec2i m_size; 62 | Image m_frame; 63 | S32 m_fps; 64 | 65 | S32 m_lineBytes; 66 | S32 m_frameBytes; 67 | S32 m_numFrames; 68 | Array m_buffer; 69 | }; 70 | 71 | //------------------------------------------------------------------------ 72 | } 73 | -------------------------------------------------------------------------------- /scenes/rt/sibenik/sibenik.mtl: -------------------------------------------------------------------------------- 1 | newmtl scene_material 2 | Ka 0.300000 0.300000 0.300000 3 | Kd 0.700000 0.700000 0.700000 4 | Ks 1.000000 1.000000 1.000000 5 | Ns 50.000000 6 | Tr 0.000000 7 | illum 2 8 | newmtl staklo_zuto_XSIPOLYCLS.staklo_zuto 9 | Ka 0.000000 0.000000 0.000000 10 | Kd 0.500000 0.445098 0.003922 11 | Ks 1.000000 0.890196 0.007843 12 | Ns 128.000000 13 | Tr 0.000000 14 | illum 2 15 | newmtl sprljci_XSIPOLYCLS.sprljci 16 | Ka 0.000000 0.000000 0.000000 17 | Kd 0.784314 0.784314 0.784314 18 | Ks 0.000000 0.000000 0.000000 19 | Ns 8.000000 20 | Tr 0.000000 21 | illum 2 22 | newmtl pod_rub_XSIPOLYCLS.pod_rub 23 | Ka 0.000000 0.000000 0.000000 24 | Kd 0.797000 0.713000 0.644000 25 | Ks 0.000000 0.000000 0.000000 26 | Ns 8.000000 27 | Tr 0.000000 28 | illum 2 29 | newmtl kamen_zid_parapet_XSIPOLYCLS.kamen_zid_parapet 30 | Ka 0.000000 0.000000 0.000000 31 | Kd 0.867000 0.790000 0.730000 32 | Ks 0.000000 0.000000 0.000000 33 | Ns 8.000000 34 | Tr 0.000000 35 | illum 2 36 | newmtl pod_XSIPOLYCLS.pod 37 | Ka 0.000000 0.000000 0.000000 38 | Kd 0.653000 0.679000 0.714000 39 | Ks 1.000000 1.000000 1.000000 40 | Ns 8.000000 41 | Tr 0.000000 42 | illum 2 43 | newmtl staklo_plavo_XSIPOLYCLS.staklo_plavo 44 | Ka 0.000000 0.000000 0.000000 45 | Kd 0.041177 0.072549 0.392157 46 | Ks 0.082353 0.145098 0.784314 47 | Ns 128.000000 48 | Tr 0.000000 49 | illum 2 50 | newmtl stupovi_XSIPOLYCLS.stupovi 51 | Ka 0.000000 0.000000 0.000000 52 | Kd 0.867000 0.790000 0.730000 53 | Ks 0.000000 0.000000 0.000000 54 | Ns 8.000000 55 | Tr 0.000000 56 | illum 2 57 | newmtl staklo_zeleno_XSIPOLYCLS.staklo_zeleno 58 | Ka 0.000000 0.000000 0.000000 59 | Kd 0.033333 0.315686 0.037255 60 | Ks 0.066667 0.631373 0.074510 61 | Ns 128.000000 62 | Tr 0.000000 63 | illum 2 64 | newmtl kamen_zid_XSIPOLYCLS.kamen_zid 65 | Ka 0.000000 0.000000 0.000000 66 | Kd 0.797000 0.713000 0.644000 67 | Ks 0.000000 0.000000 0.000000 68 | Ns 8.000000 69 | Tr 0.000000 70 | illum 2 71 | newmtl kamen_zid_prozor_XSIPOLYCLS.kamen_zid_prozor 72 | Ka 0.000000 0.000000 0.000000 73 | Kd 0.797000 0.713000 0.644000 74 | Ks 0.000000 0.000000 0.000000 75 | Ns 8.000000 76 | Tr 0.000000 77 | illum 2 78 | newmtl zid_vani_XSIPOLYCLS.zid_vani 79 | Ka 0.000000 0.000000 0.000000 80 | Kd 0.797000 0.713000 0.644000 81 | Ks 0.000000 0.000000 0.000000 82 | Ns 8.000000 83 | Tr 0.000000 84 | illum 2 85 | pointdensity 0.0 86 | newmtl staklo_XSIPOLYCLS.staklo 87 | Ka 0.000000 0.000000 0.000000 88 | Kd 0.784314 0.784314 0.784314 89 | Ks 0.784314 0.784314 0.784314 90 | Ns 128.000000 91 | Tr 0.000000 92 | illum 2 93 | newmtl pod_tepih_XSIPOLYCLS.pod_tepih 94 | Ka 0.000000 0.000000 0.000000 95 | Kd 0.546274 0.219608 0.183922 96 | Ks 0.000000 0.000000 0.000000 97 | Ns 8.000000 98 | Tr 0.000000 99 | illum 2 100 | newmtl staklo_crveno_XSIPOLYCLS.staklo_crveno 101 | Ka 0.000000 0.000000 0.000000 102 | Kd 0.441176 0.103922 0.049020 103 | Ks 0.882353 0.207843 0.098039 104 | Ns 128.000000 105 | Tr 0.000000 106 | illum 2 107 | newmtl rozeta_XSIPOLYCLS.rozeta 108 | Ka 0.000000 0.000000 0.000000 109 | Kd 0.867000 0.790000 0.730000 110 | Ks 0.000000 0.000000 0.000000 111 | Ns 8.000000 112 | Tr 0.000000 113 | illum 2 114 | -------------------------------------------------------------------------------- /src/framework/base/UnionFind.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "base/UnionFind.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | 34 | int UnionFind::unionSets(int idxA, int idxB) 35 | { 36 | // Grow the array. 37 | 38 | FW_ASSERT(idxA >= 0 && idxB >= 0); 39 | int oldSize = m_sets.getSize(); 40 | int newSize = max(idxA, idxB) + 1; 41 | if (newSize > oldSize) 42 | { 43 | m_sets.resize(newSize); 44 | for (int i = oldSize; i < newSize; i++) 45 | m_sets[i] = i; 46 | } 47 | 48 | // Union the sets. 49 | 50 | int root = findSet(idxA); 51 | m_sets[findSet(idxB)] = root; 52 | return root; 53 | } 54 | 55 | //------------------------------------------------------------------------ 56 | 57 | int UnionFind::findSet(int idx) const 58 | { 59 | // Out of the array => isolated. 60 | 61 | if (idx < 0 || idx >= m_sets.getSize()) 62 | return idx; 63 | 64 | // Find the root set. 65 | 66 | int root = idx; 67 | for (;;) 68 | { 69 | int parent = m_sets[root]; 70 | if (parent == root) 71 | break; 72 | root = parent; 73 | } 74 | 75 | // Update parent links to point directly to the root. 76 | 77 | for (;;) 78 | { 79 | int parent = m_sets[idx]; 80 | if (parent == root) 81 | break; 82 | m_sets[idx] = root; 83 | idx = parent; 84 | } 85 | return root; 86 | } 87 | 88 | //------------------------------------------------------------------------ 89 | -------------------------------------------------------------------------------- /src/rt/ray/RayGen.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Math.hpp" 30 | #include "base/Array.hpp" 31 | #include "gpu/CudaCompiler.hpp" 32 | #include "ray/RayBuffer.hpp" 33 | #include "ray/PixelTable.hpp" 34 | #include "Scene.hpp" 35 | 36 | namespace FW 37 | { 38 | 39 | class RayGen 40 | { 41 | public: 42 | RayGen(S32 maxBatchSize = 8*1024*1024); 43 | 44 | // true if batch continues 45 | void primary(RayBuffer& orays, const Vec3f& origin, const Mat4f& nscreenToWorld, S32 w,S32 h,float maxDist); 46 | bool shadow (RayBuffer& orays, RayBuffer& irays, int numSamples, const Vec3f& lightPos, float lightRadius, bool& newBatch, U32 randomSeed=0); 47 | bool ao (RayBuffer& orays, RayBuffer& irays, Scene& scene, int numSamples, float maxDist, bool& newBatch, U32 randomSeed=0); // non-const because of Buffer transfers 48 | 49 | // these are hack for various tests 50 | bool random (RayBuffer& orays, const AABB& bounds, int numRays, bool closestHit, bool PosDir=false, U32 randomSeed=0); 51 | bool random (RayBuffer& orays, const AABB& bounds, int numRays, bool closestHit, bool PosDir, bool& newBatch, U32 randomSeed=0); 52 | bool randomReflection (RayBuffer& orays, RayBuffer& irays, Scene& scene, int numSamples, float maxDist, bool& newBatch, U32 randomSeed=0); 53 | 54 | private: 55 | bool batching(S32 numInputRays,S32 numSamples,S32& startIdx,bool& newBatch, S32& lo,S32& hi); 56 | 57 | S32 m_maxBatchSize; 58 | CudaCompiler m_compiler; 59 | PixelTable m_pixelTable; 60 | 61 | S32 m_shadowStartIdx; 62 | S32 m_aoStartIdx; 63 | S32 m_randomStartIdx; 64 | }; 65 | 66 | } // 67 | -------------------------------------------------------------------------------- /src/framework/io/ImageBinaryIO.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Defs.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class Image; 36 | class InputStream; 37 | class OutputStream; 38 | 39 | //------------------------------------------------------------------------ 40 | 41 | Image* importBinaryImage (InputStream& stream); 42 | void exportBinaryImage (OutputStream& stream, const Image* image); 43 | 44 | //------------------------------------------------------------------------ 45 | /* 46 | 47 | Binary image file format v1 48 | --------------------------- 49 | 50 | - the basic units of data are 32-bit little-endian ints and floats 51 | 52 | BinaryImage 53 | 0 7 struct ImageHeader 54 | 3 n*6 struct array of ImageChannel (ImageHeader.numChannels) 55 | ? ? struct image data (ImageHeader.width * ImageHeader.height * ImageHeader.bpp) 56 | ? 57 | 58 | ImageHeader 59 | 0 2 bytes formatID (must be "BinImage") 60 | 2 1 int formatVersion (must be 1) 61 | 3 1 int width 62 | 4 1 int height 63 | 5 1 int bpp 64 | 6 1 int numChannels 65 | 7 66 | 67 | ImageChannel 68 | 0 1 int type (see ImageFormat::ChannelType) 69 | 1 1 int format (see ImageFormat::ChannelFormat) 70 | 2 1 int wordOfs 71 | 3 1 int wordSize 72 | 4 1 int fieldOfs 73 | 5 1 int fieldSize 74 | 6 75 | 76 | */ 77 | //------------------------------------------------------------------------ 78 | } 79 | -------------------------------------------------------------------------------- /src/rt/Scene.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Math.hpp" 30 | # include "3d/Mesh.hpp" 31 | 32 | namespace FW 33 | { 34 | //------------------------------------------------------------------------ 35 | 36 | class Scene 37 | { 38 | public: 39 | Scene (const MeshBase& mesh); 40 | ~Scene (void); 41 | 42 | int getNumTriangles (void) const { return m_numTriangles; } 43 | int getNumVertices (void) const { return m_numVertices; } 44 | 45 | Buffer& getTriVtxIndexBuffer (void) { return m_triVtxIndex; } 46 | Buffer& getTriNormalBuffer (void) { return m_triNormal; } 47 | Buffer& getTriMaterialColorBuffer (void) { return m_triMaterialColor; } 48 | Buffer& getTriShadedColorBuffer (void) { return m_triShadedColor; } 49 | Buffer& getVtxPosBuffer (void) { return m_vtxPos; } 50 | 51 | U32 hash (void); 52 | 53 | private: 54 | Scene (const Scene&); // forbidden 55 | Scene& operator= (const Scene&); // forbidden 56 | 57 | private: 58 | S32 m_numTriangles; 59 | S32 m_numVertices; 60 | Buffer m_triVtxIndex; // Vec3i[numTriangles] 61 | Buffer m_triNormal; // Vec3f[numTriangles] 62 | Buffer m_triMaterialColor; // U32[numTriangles], ABGR 63 | Buffer m_triShadedColor; // U32[numTriangles], ABGR 64 | Buffer m_vtxPos; // Vec3f[numVertices] 65 | }; 66 | 67 | //------------------------------------------------------------------------ 68 | } 69 | -------------------------------------------------------------------------------- /src/framework/3d/TextureAtlas.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "3d/Texture.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class TextureAtlas 36 | { 37 | private: 38 | struct Item 39 | { 40 | Texture texture; 41 | S32 border; 42 | bool wrap; 43 | Vec2i size; 44 | Vec2i pos; 45 | }; 46 | 47 | public: 48 | TextureAtlas (const ImageFormat& format = ImageFormat::ABGR_8888); 49 | ~TextureAtlas (void); 50 | 51 | void clear (void); 52 | bool addTexture (const Texture& tex, int border = 1, bool wrap = true); 53 | 54 | Vec2i getAtlasSize (void) { validate(); return m_atlasSize; } 55 | Vec2i getTexturePos (const Texture& tex); 56 | const Texture& getAtlasTexture (void) { validate(); return m_atlasTexture; } 57 | 58 | private: 59 | TextureAtlas (const TextureAtlas&); // forbidden 60 | TextureAtlas& operator= (const TextureAtlas&); // forbidden 61 | 62 | private: 63 | void validate (void); 64 | void layoutItems (void); 65 | void createAtlas (void); 66 | 67 | private: 68 | ImageFormat m_format; 69 | Array m_items; 70 | Hash m_itemHash; 71 | 72 | Vec2i m_atlasSize; 73 | Texture m_atlasTexture; 74 | }; 75 | 76 | //------------------------------------------------------------------------ 77 | } 78 | -------------------------------------------------------------------------------- /src/rt/cuda/RendererKernels.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/DLLImports.hpp" 30 | #include "Util.hpp" 31 | 32 | namespace FW 33 | { 34 | //------------------------------------------------------------------------ 35 | 36 | enum 37 | { 38 | CountHits_BlockWidth = 32, 39 | CountHits_BlockHeight = 8, 40 | }; 41 | 42 | //------------------------------------------------------------------------ 43 | 44 | struct ReconstructInput 45 | { 46 | S32 numRaysPerPrimary; 47 | S32 firstPrimary; 48 | S32 numPrimary; 49 | bool isPrimary; 50 | bool isAO; 51 | bool isDiffuse; 52 | CUdeviceptr primarySlotToID; // const S32* 53 | CUdeviceptr primaryResults; // const RayResult* 54 | CUdeviceptr batchIDToSlot; // const S32* 55 | CUdeviceptr batchResults; // const RayResult* 56 | CUdeviceptr triMaterialColor; // const U32* ABGR 57 | CUdeviceptr triShadedColor; // const U32* ABGR 58 | CUdeviceptr pixels; // U32* ABGR 59 | }; 60 | 61 | //------------------------------------------------------------------------ 62 | 63 | struct CountHitsInput 64 | { 65 | S32 numRays; 66 | CUdeviceptr rayResults; // const RayResult* 67 | S32 raysPerThread; 68 | }; 69 | 70 | //------------------------------------------------------------------------ 71 | 72 | #if FW_CUDA 73 | extern "C" 74 | { 75 | 76 | __constant__ ReconstructInput c_ReconstructInput; 77 | __global__ void reconstructKernel(void); 78 | 79 | __constant__ CountHitsInput c_CountHitsInput; 80 | __device__ S32 g_CountHitsOutput; 81 | __global__ void countHitsKernel(void); 82 | 83 | } 84 | #endif 85 | 86 | //------------------------------------------------------------------------ 87 | } 88 | -------------------------------------------------------------------------------- /src/rt/ray/RayGenKernels.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/DLLImports.hpp" 30 | #include "base/Math.hpp" 31 | #include "Util.hpp" 32 | 33 | namespace FW 34 | { 35 | //------------------------------------------------------------------------ 36 | 37 | struct RayGenPrimaryInput 38 | { 39 | Vec3f origin; 40 | Mat4f nscreenToWorld; 41 | S32 w; 42 | S32 h; 43 | F32 maxDist; 44 | CUdeviceptr rays; // Ray* 45 | CUdeviceptr idToSlot; // S32* 46 | CUdeviceptr slotToID; // S32* 47 | CUdeviceptr indexToPixel; // const S32* 48 | }; 49 | 50 | //------------------------------------------------------------------------ 51 | 52 | struct RayGenAOInput 53 | { 54 | S32 firstInputSlot; 55 | S32 numInputRays; 56 | S32 numSamples; 57 | F32 maxDist; 58 | U32 randomSeed; 59 | CUdeviceptr inRays; // const Ray* 60 | CUdeviceptr inResults; // const RayResult* 61 | CUdeviceptr outRays; // Ray* 62 | CUdeviceptr outIDToSlot; // S32* 63 | CUdeviceptr outSlotToID; // S32* 64 | CUdeviceptr normals; // const Vec3f* 65 | }; 66 | 67 | //------------------------------------------------------------------------ 68 | 69 | struct RayGenShadowInput 70 | { 71 | S32 firstInputSlot; 72 | S32 numInputRays; 73 | S32 numSamples; 74 | F32 lightPositionX; 75 | F32 lightPositionY; 76 | F32 lightPositionZ; 77 | F32 lightRadius; 78 | U32 randomSeed; 79 | CUdeviceptr inRays; // const Ray* 80 | CUdeviceptr inResults; // const RayResult* 81 | CUdeviceptr outRays; // Ray* 82 | CUdeviceptr outIDToSlot; // S32* 83 | CUdeviceptr outSlotToID; // S32* 84 | }; 85 | 86 | //------------------------------------------------------------------------ 87 | 88 | #if FW_CUDA 89 | extern "C" 90 | { 91 | 92 | __constant__ int4 c_RayGenPrimaryInput[(sizeof(RayGenPrimaryInput) + sizeof(int4) - 1) / sizeof(int4)]; 93 | __global__ void rayGenPrimaryKernel(void); 94 | 95 | __constant__ RayGenAOInput c_RayGenAOInput; 96 | __global__ void rayGenAOKernel(void); 97 | 98 | __constant__ RayGenShadowInput c_RayGenShadowInput; 99 | __global__ void rayGenShadowKernel(void); 100 | 101 | } 102 | #endif 103 | 104 | //------------------------------------------------------------------------ 105 | } 106 | -------------------------------------------------------------------------------- /src/rt/ray/RayBufferKernels.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/DLLImports.hpp" 30 | #include "base/Math.hpp" 31 | #include "Util.hpp" 32 | 33 | namespace FW 34 | { 35 | //------------------------------------------------------------------------ 36 | 37 | enum 38 | { 39 | FindAABB_BlockWidth = 32, 40 | FindAABB_BlockHeight = 8, 41 | }; 42 | 43 | //------------------------------------------------------------------------ 44 | 45 | struct MortonKey 46 | { 47 | S32 oldSlot; 48 | U32 hash[6]; // 192-bit Morton key 49 | }; 50 | 51 | //------------------------------------------------------------------------ 52 | 53 | struct FindAABBInput 54 | { 55 | S32 numRays; 56 | S32 raysPerThread; 57 | CUdeviceptr inRays; // const Ray* 58 | }; 59 | 60 | //------------------------------------------------------------------------ 61 | 62 | struct FindAABBOutput 63 | { 64 | Vec3f aabbLo; 65 | Vec3f aabbHi; 66 | }; 67 | 68 | //------------------------------------------------------------------------ 69 | 70 | struct GenMortonKeysInput 71 | { 72 | S32 numRays; 73 | Vec3f aabbLo; 74 | Vec3f aabbHi; 75 | CUdeviceptr inRays; // const Ray* 76 | CUdeviceptr outKeys; // MortonKey* 77 | }; 78 | 79 | //------------------------------------------------------------------------ 80 | 81 | struct ReorderRaysInput 82 | { 83 | S32 numRays; 84 | CUdeviceptr inKeys; // const MortonKey* 85 | CUdeviceptr inRays; // const Ray* 86 | CUdeviceptr inSlotToID; // const S32* 87 | CUdeviceptr outRays; // Ray* 88 | CUdeviceptr outIDToSlot; // S32* 89 | CUdeviceptr outSlotToID; // S32* 90 | }; 91 | 92 | //------------------------------------------------------------------------ 93 | 94 | #if FW_CUDA 95 | extern "C" 96 | { 97 | 98 | __constant__ FindAABBInput c_FindAABBInput; 99 | __device__ int4 c_FindAABBOutput[(sizeof(FindAABBOutput) + sizeof(int4) - 1) / sizeof(int4)]; 100 | __global__ void findAABBKernel(void); 101 | 102 | __constant__ int4 c_GenMortonKeysInput[(sizeof(GenMortonKeysInput) + sizeof(int4) - 1) / sizeof(int4)]; 103 | __global__ void genMortonKeysKernel(void); 104 | 105 | __constant__ ReorderRaysInput c_ReorderRaysInput; 106 | __global__ void reorderRaysKernel(void); 107 | 108 | } 109 | #endif 110 | 111 | //------------------------------------------------------------------------ 112 | } 113 | -------------------------------------------------------------------------------- /src/framework/base/Hash.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "base/Hash.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | 34 | U32 FW::hashBuffer(const void* ptr, int size) 35 | { 36 | FW_ASSERT(size >= 0); 37 | FW_ASSERT(ptr || !size); 38 | 39 | if ((((S32)(UPTR)ptr | size) & 3) == 0) 40 | return hashBufferAlign(ptr, size); 41 | 42 | const U8* src = (const U8*)ptr; 43 | U32 a = FW_HASH_MAGIC; 44 | U32 b = FW_HASH_MAGIC; 45 | U32 c = FW_HASH_MAGIC; 46 | 47 | while (size >= 12) 48 | { 49 | a += src[0] + (src[1] << 8) + (src[2] << 16) + (src[3] << 24); 50 | b += src[4] + (src[5] << 8) + (src[6] << 16) + (src[7] << 24); 51 | c += src[8] + (src[9] << 8) + (src[10] << 16) + (src[11] << 24); 52 | FW_JENKINS_MIX(a, b, c); 53 | src += 12; 54 | size -= 12; 55 | } 56 | 57 | switch (size) 58 | { 59 | case 11: c += src[10] << 16; 60 | case 10: c += src[9] << 8; 61 | case 9: c += src[8]; 62 | case 8: b += src[7] << 24; 63 | case 7: b += src[6] << 16; 64 | case 6: b += src[5] << 8; 65 | case 5: b += src[4]; 66 | case 4: a += src[3] << 24; 67 | case 3: a += src[2] << 16; 68 | case 2: a += src[1] << 8; 69 | case 1: a += src[0]; 70 | case 0: break; 71 | } 72 | 73 | c += size; 74 | FW_JENKINS_MIX(a, b, c); 75 | return c; 76 | } 77 | 78 | //------------------------------------------------------------------------ 79 | 80 | U32 FW::hashBufferAlign(const void* ptr, int size) 81 | { 82 | FW_ASSERT(size >= 0); 83 | FW_ASSERT(ptr || !size); 84 | FW_ASSERT(((UPTR)ptr & 3) == 0); 85 | FW_ASSERT((size & 3) == 0); 86 | 87 | const U32* src = (const U32*)ptr; 88 | U32 a = FW_HASH_MAGIC; 89 | U32 b = FW_HASH_MAGIC; 90 | U32 c = FW_HASH_MAGIC; 91 | 92 | while (size >= 12) 93 | { 94 | a += src[0]; 95 | b += src[1]; 96 | c += src[2]; 97 | FW_JENKINS_MIX(a, b, c); 98 | src += 3; 99 | size -= 12; 100 | } 101 | 102 | switch (size) 103 | { 104 | case 8: b += src[1]; 105 | case 4: a += src[0]; 106 | case 0: break; 107 | } 108 | 109 | c += size; 110 | FW_JENKINS_MIX(a, b, c); 111 | return c; 112 | } 113 | 114 | //------------------------------------------------------------------------ 115 | -------------------------------------------------------------------------------- /src/framework/io/MeshBinaryIO.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Defs.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class MeshBase; 36 | class InputStream; 37 | class OutputStream; 38 | 39 | //------------------------------------------------------------------------ 40 | 41 | MeshBase* importBinaryMesh (InputStream& stream); 42 | void exportBinaryMesh (OutputStream& stream, const MeshBase* mesh); 43 | 44 | //------------------------------------------------------------------------ 45 | /* 46 | 47 | Binary mesh file format v4 48 | -------------------------- 49 | 50 | - the basic units of data are 32-bit little-endian ints and floats 51 | 52 | BinaryMesh 53 | 0 6 struct v1 MeshHeader 54 | 6 n*3 struct v1 array of AttribSpec (MeshHeader.numAttribs) 55 | ? n*? struct v1 array of Vertex (MeshHeader.numVertices) 56 | ? n*? struct v2 array of Texture (MeshHeader.numTextures) 57 | ? n*? struct v1 array of Submesh (MeshHeader.numSubmeshes) 58 | ? 59 | 60 | MeshHeader 61 | 0 2 bytes v1 formatID (must be "BinMesh ") 62 | 2 1 int v1 formatVersion (must be 4) 63 | 3 1 int v1 numAttribs 64 | 4 1 int v1 numVertices 65 | 5 1 int v2 numTextures 66 | 6 1 int v1 numSubmeshes 67 | 7 68 | 69 | AttribSpec 70 | 0 1 int v1 type (see MeshBase::AttribType) 71 | 1 1 int v1 format (see MeshBase::AttribFormat) 72 | 2 1 int v1 length 73 | 3 74 | 75 | Vertex 76 | 0 ? bytes v1 array of values (dictated by the set of AttribSpecs) 77 | ? 78 | 79 | Texture 80 | 0 1 int v2 idLength 81 | 1 ? bytes v2 idString 82 | ? ? struct v2 BinaryImage (see Image.hpp) 83 | ? 84 | 85 | Submesh 86 | 0 3 float v1 ambient (ignored) 87 | 3 4 float v1 diffuse 88 | 7 3 float v1 specular 89 | 10 1 float v1 glossiness 90 | 11 1 float v3 displacementCoef 91 | 12 1 float v3 displacementBias 92 | 13 1 int v2 diffuseTexture (-1 if none) 93 | 14 1 int v2 alphaTexture (-1 if none) 94 | 15 1 int v3 displacementTexture (-1 if none) 95 | 16 1 int v4 normalTexture (-1 if none) 96 | 17 1 int v4 environmentTexture (-1 if none) 97 | 18 1 int v1 numTriangles 98 | 19 n*3 int v1 indices 99 | ? 100 | 101 | */ 102 | //------------------------------------------------------------------------ 103 | } 104 | -------------------------------------------------------------------------------- /scenes/rt/conference/conference.mtl: -------------------------------------------------------------------------------- 1 | newmtl default 2 | Kd 1 1 1 3 | newmtl wall_mat 4 | Kd 0.640000 0.600000 0.610000 5 | Ka 0.640000 0.600000 0.610000 6 | Ks 1.000000 1.000000 1.000000 7 | newmtl door_mat 8 | Kd 0.740000 0.400000 0.160000 9 | Ka 0.740000 0.400000 0.160000 10 | Ks 1.000000 1.000000 1.000000 11 | newmtl floor_mat 12 | Kd 0.180000 0.065000 0.058000 13 | Ka 0.180000 0.065000 0.058000 14 | Ks 1.000000 1.000000 1.000000 15 | newmtl ceiling_mat 16 | Kd 0.900000 0.900000 0.900000 17 | Ka 0.900000 0.900000 0.900000 18 | Ks 1.000000 1.000000 1.000000 19 | newmtl baseboard_mat 20 | Kd 0.069000 0.069000 0.069000 21 | Ka 0.069000 0.069000 0.069000 22 | Ks 1.000000 1.000000 1.000000 23 | newmtl wood_base_mat 24 | Kd 0.692300 0.426300 0.175000 25 | Ka 0.692300 0.426300 0.175000 26 | Ks 1.000000 1.000000 1.000000 27 | newmtl door_jam_mat 28 | Kd 0.700000 0.700000 0.700000 29 | Ka 0.700000 0.700000 0.700000 30 | Ks 1.000000 1.000000 1.000000 31 | newmtl on_door_mat 32 | Kd 0.370000 0.370000 0.370000 33 | Ka 0.370000 0.370000 0.370000 34 | Ks 0.370000 0.370000 0.370000 35 | newmtl shaft_wood_mat 36 | Kd 0.640000 0.395000 0.127000 37 | Ka 0.640000 0.395000 0.127000 38 | Ks 1.000000 1.000000 1.000000 39 | newmtl screw_mat 40 | Kd 0.709000 0.709000 0.709000 41 | Ka 0.709000 0.709000 0.709000 42 | Ks 0.709000 0.709000 0.709000 43 | newmtl window_board_mat 44 | Kd 0.600000 0.600000 0.600000 45 | Ka 0.600000 0.600000 0.600000 46 | Ks 1.000000 1.000000 1.000000 47 | newmtl green_board 48 | Kd 0.123000 0.263000 0.151000 49 | Ka 0.123000 0.263000 0.151000 50 | Ks 1.000000 1.000000 1.000000 51 | newmtl edge_mat 52 | Kd 0.430000 0.430000 0.430000 53 | Ka 0.430000 0.430000 0.430000 54 | Ks 0.430000 0.430000 0.430000 55 | newmtl white_mat 56 | Kd 0.516000 0.450000 0.340000 57 | Ka 0.516000 0.450000 0.340000 58 | Ks 1.000000 1.000000 1.000000 59 | newmtl on_shaft_mat 60 | Kd 0.400000 0.400000 0.400000 61 | Ka 0.400000 0.400000 0.400000 62 | Ks 0.400000 0.400000 0.400000 63 | newmtl door_closer_mat 64 | Kd 0.450000 0.450000 0.450000 65 | Ka 0.450000 0.450000 0.450000 66 | Ks 0.450000 0.450000 0.450000 67 | newmtl sign_mat 68 | Kd 0.500000 0.500000 0.500000 69 | Ka 0.500000 0.500000 0.500000 70 | Ks 1.000000 1.000000 1.000000 71 | newmtl sign_mat_inside 72 | Kd 0.400000 0.400000 0.400000 73 | Ka 0.400000 0.400000 0.400000 74 | Ks 1.000000 1.000000 1.000000 75 | newmtl support_mat 76 | Kd 0.880000 0.720000 0.580000 77 | Ka 0.880000 0.720000 0.580000 78 | Ks 1.000000 1.000000 1.000000 79 | newmtl ivory_plastic 80 | Kd 0.830000 0.700000 0.430000 81 | Ka 0.830000 0.700000 0.430000 82 | Ks 1.000000 1.000000 1.000000 83 | newmtl dark_fabric 84 | Kd 0.080000 0.080000 0.080000 85 | Ka 0.080000 0.080000 0.080000 86 | Ks 1.000000 1.000000 1.000000 87 | newmtl wood_mat 88 | Kd 0.290000 0.150000 0.130000 89 | Ka 0.290000 0.150000 0.130000 90 | Ks 1.000000 1.000000 1.000000 91 | newmtl red_paint 92 | Kd 0.820000 0.090000 0.070000 93 | Ka 0.820000 0.090000 0.070000 94 | Ks 1.000000 1.000000 1.000000 95 | newmtl stainless_steel 96 | Kd 0.700000 0.700000 0.700000 97 | Ka 0.700000 0.700000 0.700000 98 | Ks 0.700000 0.700000 0.700000 99 | newmtl steel 100 | Kd 0.600000 0.600000 0.600000 101 | Ka 0.600000 0.600000 0.600000 102 | Ks 0.600000 0.600000 0.600000 103 | newmtl rubber 104 | Kd 0.100000 0.100000 0.100000 105 | Ka 0.100000 0.100000 0.100000 106 | Ks 1.000000 1.000000 1.000000 107 | newmtl vent_mat 108 | Kd 0.469000 0.452000 0.433000 109 | Ka 0.469000 0.452000 0.433000 110 | Ks 1.000000 1.000000 1.000000 111 | newmtl beige_vinyl 112 | Kd 0.640000 0.440000 0.316000 113 | Ka 0.640000 0.440000 0.316000 114 | Ks 1.000000 1.000000 1.000000 115 | newmtl table_mat 116 | Kd 0.290000 0.150000 0.130000 117 | Ka 0.290000 0.150000 0.130000 118 | Ks 1.000000 1.000000 1.000000 119 | newmtl leg_mat 120 | Kd 0.060000 0.060000 0.060000 121 | Ka 0.060000 0.060000 0.060000 122 | Ks 1.000000 1.000000 1.000000 123 | newmtl cloth1_mat 124 | Kd 0.980000 0.183000 0.165000 125 | Ka 0.980000 0.183000 0.165000 126 | Ks 1.000000 1.000000 1.000000 127 | newmtl frame_mat 128 | Kd 0.600000 0.600000 0.600000 129 | Ka 0.600000 0.600000 0.600000 130 | Ks 0.600000 0.600000 0.600000 131 | newmtl arm_mat 132 | Kd 0.480000 0.236000 0.121000 133 | Ka 0.480000 0.236000 0.121000 134 | Ks 1.000000 1.000000 1.000000 135 | newmtl black_mat 136 | Kd 0.050000 0.050000 0.050000 137 | Ka 0.050000 0.050000 0.050000 138 | Ks 1.000000 1.000000 1.000000 139 | newmtl curtain_mat 140 | Kd 0.600000 0.600000 0.600000 141 | Ka 0.600000 0.600000 0.600000 142 | Ks 1.000000 1.000000 1.000000 143 | -------------------------------------------------------------------------------- /src/rt/bvh/Platform.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/String.hpp" 30 | #include "base/Hash.hpp" 31 | 32 | namespace FW 33 | { 34 | 35 | class LeafNode; 36 | class BVHNode; 37 | class Platform 38 | { 39 | public: 40 | Platform() { m_name=String("Default"); m_SAHNodeCost = 1.f; m_SAHTriangleCost = 1.f; m_nodeBatchSize = 1; m_triBatchSize = 1; m_minLeafSize=1; m_maxLeafSize=0x7FFFFFF; } 41 | Platform(const String& name,float nodeCost=1.f, float triCost=1.f, S32 nodeBatchSize=1, S32 triBatchSize=1) { m_name=name; m_SAHNodeCost = nodeCost; m_SAHTriangleCost = triCost; m_nodeBatchSize = nodeBatchSize; m_triBatchSize = triBatchSize; m_minLeafSize=1; m_maxLeafSize=0x7FFFFFF; } 42 | 43 | const String& getName() const { return m_name; } 44 | 45 | // SAH weights 46 | float getSAHTriangleCost() const { return m_SAHTriangleCost; } 47 | float getSAHNodeCost() const { return m_SAHNodeCost; } 48 | 49 | // SAH costs, raw and batched 50 | float getCost(int numChildNodes,int numTris) const { return getNodeCost(numChildNodes) + getTriangleCost(numTris); } 51 | float getTriangleCost(S32 n) const { return roundToTriangleBatchSize(n) * m_SAHTriangleCost; } 52 | float getNodeCost(S32 n) const { return roundToNodeBatchSize(n) * m_SAHNodeCost; } 53 | 54 | // batch processing (how many ops at the price of one) 55 | S32 getTriangleBatchSize() const { return m_triBatchSize; } 56 | S32 getNodeBatchSize() const { return m_nodeBatchSize; } 57 | void setTriangleBatchSize(S32 triBatchSize) { m_triBatchSize = triBatchSize; } 58 | void setNodeBatchSize(S32 nodeBatchSize) { m_nodeBatchSize= nodeBatchSize; } 59 | S32 roundToTriangleBatchSize(S32 n) const { return ((n+m_triBatchSize-1)/m_triBatchSize)*m_triBatchSize; } 60 | S32 roundToNodeBatchSize(S32 n) const { return ((n+m_nodeBatchSize-1)/m_nodeBatchSize)*m_nodeBatchSize; } 61 | 62 | // leaf preferences 63 | void setLeafPreferences(S32 minSize,S32 maxSize) { m_minLeafSize=minSize; m_maxLeafSize=maxSize; } 64 | S32 getMinLeafSize() const { return m_minLeafSize; } 65 | S32 getMaxLeafSize() const { return m_maxLeafSize; } 66 | 67 | U32 computeHash() const { return hashBits(hash(m_name), floatToBits(m_SAHNodeCost), floatToBits(m_SAHTriangleCost), hashBits(m_triBatchSize, m_nodeBatchSize, m_minLeafSize, m_maxLeafSize)); } 68 | 69 | private: 70 | String m_name; 71 | float m_SAHNodeCost; 72 | float m_SAHTriangleCost; 73 | S32 m_triBatchSize; 74 | S32 m_nodeBatchSize; 75 | S32 m_minLeafSize; 76 | S32 m_maxLeafSize; 77 | }; 78 | 79 | 80 | } // -------------------------------------------------------------------------------- /src/framework/io/StateDump.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "io/StateDump.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | 34 | void StateDump::readFromStream(InputStream& s) 35 | { 36 | clear(); 37 | S32 num; 38 | s >> num; 39 | for (int i = 0; i < num; i++) 40 | { 41 | String id; 42 | Array* data = new Array; 43 | s >> id >> *data; 44 | setInternal(data, id); 45 | } 46 | } 47 | 48 | //------------------------------------------------------------------------ 49 | 50 | void StateDump::writeToStream(OutputStream& s) const 51 | { 52 | s << m_values.getSize(); 53 | for (int i = m_values.firstSlot(); i != -1; i = m_values.nextSlot(i)) 54 | s << m_values.getSlot(i).key << *m_values.getSlot(i).value; 55 | } 56 | 57 | //------------------------------------------------------------------------ 58 | 59 | void StateDump::clear(void) 60 | { 61 | for (int i = m_values.firstSlot(); i != -1; i = m_values.nextSlot(i)) 62 | delete m_values.getSlot(i).value; 63 | m_values.clear(); 64 | } 65 | 66 | //------------------------------------------------------------------------ 67 | 68 | void StateDump::add(const StateDump& other) 69 | { 70 | if (&other != this) 71 | for (int i = other.m_values.firstSlot(); i != -1; i = other.m_values.nextSlot(i)) 72 | setInternal(new Array(*other.m_values.getSlot(i).value), other.m_values.getSlot(i).key); 73 | } 74 | 75 | //------------------------------------------------------------------------ 76 | 77 | const Array* StateDump::get(const String& id) const 78 | { 79 | Array* const* data = m_values.search(xlateId(id)); 80 | return (data) ? *data : NULL; 81 | } 82 | 83 | //------------------------------------------------------------------------ 84 | 85 | bool StateDump::get(void* ptr, int size, const String& id) 86 | { 87 | FW_ASSERT(size >= 0); 88 | FW_ASSERT(ptr || !size); 89 | 90 | Array* const* data = m_values.search(xlateId(id)); 91 | if (!data) 92 | return false; 93 | 94 | FW_ASSERT((*data)->getNumBytes() == size); 95 | memcpy(ptr, (*data)->getPtr(), size); 96 | return true; 97 | } 98 | 99 | //------------------------------------------------------------------------ 100 | 101 | void StateDump::set(const void* ptr, int size, const String& id) 102 | { 103 | setInternal(new Array((const U8*)ptr, size), xlateId(id)); 104 | } 105 | 106 | //------------------------------------------------------------------------ 107 | 108 | void StateDump::setInternal(Array* data, const String& id) 109 | { 110 | if (m_values.contains(id)) 111 | delete m_values.remove(id); 112 | if (data) 113 | m_values.add(id, data); 114 | } 115 | 116 | //------------------------------------------------------------------------ 117 | -------------------------------------------------------------------------------- /src/framework/3d/Texture.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "gui/Image.hpp" 30 | #include "base/Hash.hpp" 31 | #include "base/Thread.hpp" 32 | 33 | namespace FW 34 | { 35 | //------------------------------------------------------------------------ 36 | 37 | class Texture 38 | { 39 | private: 40 | struct Data 41 | { 42 | S32 refCount; 43 | Spinlock refCountLock; 44 | 45 | String id; 46 | bool isInHash; 47 | Image* image; 48 | GLuint glTexture; 49 | CUarray cudaArray; 50 | Texture* nextMip; 51 | }; 52 | 53 | public: 54 | Texture (void) : m_data(NULL) {} 55 | Texture (const Texture& other) : m_data(NULL) { set(other); } 56 | Texture (Image* image, const String& id = ""); // takes ownership of the image 57 | ~Texture (void) { if (m_data) unreferData(m_data); } 58 | 59 | static Texture find (const String& id); 60 | static Texture import (const String& fileName); 61 | 62 | bool exists (void) const { return (m_data && m_data->image && m_data->image->getSize().min() > 0); } 63 | String getID (void) const { return (m_data) ? m_data->id : ""; } 64 | const Image* getImage (void) const { return (m_data) ? m_data->image : NULL; } 65 | Vec2i getSize (void) const { return (exists()) ? getImage()->getSize() : 0; } 66 | 67 | void clear (void) { if (m_data) unreferData(m_data); m_data = NULL; } 68 | void set (const Texture& other); 69 | 70 | GLuint getGLTexture (const ImageFormat::ID desiredFormat = ImageFormat::ID_Max, bool generateMipmaps = true) const; 71 | CUarray getCudaArray (const ImageFormat::ID desiredFormat = ImageFormat::ID_Max) const; 72 | Texture getMipLevel (int level) const; 73 | 74 | Texture& operator= (const Texture& other) { set(other); return *this; } 75 | bool operator== (const Texture& other) const { return (m_data == other.m_data); } 76 | bool operator!= (const Texture& other) const { return (m_data != other.m_data); } 77 | 78 | private: 79 | static Data* findData (const String& id); 80 | static Data* createData (const String& id); 81 | static void referData (Data* data); 82 | static void unreferData (Data* data); 83 | 84 | private: 85 | static Hash* s_hash; 86 | 87 | Data* m_data; 88 | }; 89 | 90 | //------------------------------------------------------------------------ 91 | } 92 | -------------------------------------------------------------------------------- /src/rt/bvh/BVHNode.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Array.hpp" 30 | #include "bvh/Platform.hpp" 31 | #include "Util.hpp" 32 | 33 | namespace FW 34 | { 35 | 36 | // TODO: remove m_probability. Node needed after all? 37 | 38 | enum BVH_STAT 39 | { 40 | BVH_STAT_NODE_COUNT, 41 | BVH_STAT_INNER_COUNT, 42 | BVH_STAT_LEAF_COUNT, 43 | BVH_STAT_TRIANGLE_COUNT, 44 | BVH_STAT_CHILDNODE_COUNT, 45 | }; 46 | 47 | class BVHNode 48 | { 49 | public: 50 | BVHNode() : m_probability(1.f),m_parentProbability(1.f),m_treelet(-1),m_index(-1) {} 51 | virtual bool isLeaf() const = 0; 52 | virtual S32 getNumChildNodes() const = 0; 53 | virtual BVHNode* getChildNode(S32 i) const = 0; 54 | virtual S32 getNumTriangles() const { return 0; } 55 | 56 | float getArea() const { return m_bounds.area(); } 57 | 58 | AABB m_bounds; 59 | 60 | // These are somewhat experimental, for some specific test and may be invalid... 61 | float m_probability; // probability of coming here (widebvh uses this) 62 | float m_parentProbability; // probability of coming to parent (widebvh uses this) 63 | 64 | int m_treelet; // for queuing tests (qmachine uses this) 65 | int m_index; // in linearized tree (qmachine uses this) 66 | 67 | // Subtree functions 68 | int getSubtreeSize(BVH_STAT stat=BVH_STAT_NODE_COUNT) const; 69 | void computeSubtreeProbabilities(const Platform& p, float parentProbability, float& sah); 70 | float computeSubtreeSAHCost(const Platform& p) const; // NOTE: assumes valid probabilities 71 | void deleteSubtree(); 72 | 73 | void assignIndicesDepthFirst (S32 index=0, bool includeLeafNodes=true); 74 | void assignIndicesBreadthFirst(S32 index=0, bool includeLeafNodes=true); 75 | }; 76 | 77 | 78 | class InnerNode : public BVHNode 79 | { 80 | public: 81 | InnerNode(const AABB& bounds,BVHNode* child0,BVHNode* child1) { m_bounds=bounds; m_children[0]=child0; m_children[1]=child1; } 82 | 83 | bool isLeaf() const { return false; } 84 | S32 getNumChildNodes() const { return 2; } 85 | BVHNode* getChildNode(S32 i) const { FW_ASSERT(i>=0 && i<2); return m_children[i]; } 86 | 87 | BVHNode* m_children[2]; 88 | }; 89 | 90 | 91 | class LeafNode : public BVHNode 92 | { 93 | public: 94 | LeafNode(const AABB& bounds,int lo,int hi) { m_bounds=bounds; m_lo=lo; m_hi=hi; } 95 | LeafNode(const LeafNode& s) { *this = s; } 96 | 97 | bool isLeaf() const { return true; } 98 | S32 getNumChildNodes() const { return 0; } 99 | BVHNode* getChildNode(S32) const { return NULL; } 100 | 101 | S32 getNumTriangles() const { return m_hi-m_lo; } 102 | S32 m_lo; 103 | S32 m_hi; 104 | }; 105 | 106 | } // 107 | -------------------------------------------------------------------------------- /src/rt/Scene.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "Scene.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | 34 | Scene::Scene(const MeshBase& mesh) 35 | { 36 | Vec3f light = Vec3f(1.0f, 2.0f, 3.0f).normalized(); 37 | 38 | // Convert mesh and allocate buffers. 39 | 40 | Mesh meshP(mesh); 41 | m_numTriangles = meshP.numTriangles(); 42 | m_numVertices = meshP.numVertices(); 43 | 44 | m_triVtxIndex.resizeDiscard(m_numTriangles * sizeof(Vec3i)); 45 | m_triNormal.resizeDiscard(m_numTriangles * sizeof(Vec3f)); 46 | m_triMaterialColor.resizeDiscard(m_numTriangles * sizeof(U32)); 47 | m_triShadedColor.resizeDiscard(m_numTriangles * sizeof(U32)); 48 | m_vtxPos.resizeDiscard(m_numVertices * sizeof(Vec3f)); 49 | 50 | Vec3i* triVtxIndex = (Vec3i*)m_triVtxIndex.getMutablePtr(); 51 | Vec3f* triNormal = (Vec3f*)m_triNormal.getMutablePtr(); 52 | U32* triMaterialColor = (U32*)m_triMaterialColor.getMutablePtr(); 53 | U32* triShadedColor = (U32*)m_triShadedColor.getMutablePtr(); 54 | Vec3f* vtxPos = (Vec3f*)m_vtxPos.getMutablePtr(); 55 | 56 | // Copy vertices. 57 | 58 | const VertexP* v = meshP.getVertexPtr(); 59 | for (int i = 0; i < m_numVertices; i++) 60 | vtxPos[i] = v[i].p; 61 | 62 | // Collapse submeshes to a single triangle list. 63 | 64 | for (int submesh = 0; submesh < meshP.numSubmeshes(); submesh++) 65 | { 66 | const Array& indices = meshP.indices(submesh); 67 | const MeshBase::Material& material = meshP.material(submesh); 68 | U32 colorU32 = material.diffuse.toABGR(); 69 | Vec3f colorVec3f = material.diffuse.getXYZ(); 70 | 71 | for (int i = 0; i < indices.getSize(); i++) 72 | { 73 | const Vec3i& vi = indices[i]; 74 | Vec3f normal = normalize(cross(vtxPos[vi.y] - vtxPos[vi.x], vtxPos[vi.z] - vtxPos[vi.x])); 75 | 76 | *triVtxIndex++ = vi; 77 | *triNormal++ = normal; 78 | *triMaterialColor++ = colorU32; 79 | *triShadedColor++ = Vec4f(colorVec3f * (dot(normal, light) * 0.5f + 0.5f), 1.0f).toABGR(); 80 | } 81 | } 82 | } 83 | 84 | //------------------------------------------------------------------------ 85 | 86 | Scene::~Scene(void) 87 | { 88 | } 89 | 90 | //------------------------------------------------------------------------ 91 | 92 | U32 Scene::hash(void) 93 | { 94 | return hashBits( 95 | hashBuffer(m_triVtxIndex.getPtr(), (int)m_triVtxIndex.getSize()), 96 | hashBuffer(m_triNormal.getPtr(), (int)m_triNormal.getSize()), 97 | hashBuffer(m_triMaterialColor.getPtr(), (int)m_triMaterialColor.getSize()), 98 | hashBuffer(m_triShadedColor.getPtr(), (int)m_triShadedColor.getSize()), 99 | hashBuffer(m_vtxPos.getPtr(), (int)m_vtxPos.getSize())); 100 | } 101 | 102 | //------------------------------------------------------------------------ 103 | -------------------------------------------------------------------------------- /src/rt/Util.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "Util.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | 34 | Vec2f Intersect::RayBox(const AABB& box, const Ray& ray) 35 | { 36 | const Vec3f& orig = ray.origin; 37 | const Vec3f& dir = ray.direction; 38 | 39 | Vec3f t0 = (box.min() - orig) / dir; 40 | Vec3f t1 = (box.max() - orig) / dir; 41 | 42 | float tmin = min(t0,t1).max(); 43 | float tmax = max(t0,t1).min(); 44 | 45 | return Vec2f(tmin,tmax); 46 | } 47 | 48 | //------------------------------------------------------------------------ 49 | 50 | Vec3f Intersect::RayTriangle(const Vec3f& v0, const Vec3f& v1, const Vec3f& v2, const Ray& ray) 51 | { 52 | // const float EPSILON = 0.000001f; // breaks FairyForest 53 | const float EPSILON = 0.f; // works better 54 | const Vec3f miss(FW_F32_MAX,FW_F32_MAX,FW_F32_MAX); 55 | 56 | Vec3f edge1 = v1-v0; 57 | Vec3f edge2 = v2-v0; 58 | Vec3f pvec = cross(ray.direction,edge2); 59 | float det = dot(edge1, pvec); 60 | 61 | Vec3f tvec = ray.origin - v0; 62 | float u = dot(tvec,pvec); 63 | 64 | Vec3f qvec = cross(tvec, edge1); 65 | float v = dot(ray.direction, qvec); 66 | 67 | // TODO: clear this 68 | if (det > EPSILON) 69 | { 70 | if (u < 0.0 || u > det) 71 | return miss; 72 | if (v < 0.0 || u + v > det) 73 | return miss; 74 | } 75 | else if(det < -EPSILON) 76 | { 77 | if (u > 0.0 || u < det) 78 | return miss; 79 | if (v > 0.0 || u + v < det) 80 | return miss; 81 | } 82 | else 83 | return miss; 84 | 85 | float inv_det = 1.f / det; 86 | float t = dot(edge2, qvec) * inv_det; 87 | u *= inv_det; 88 | v *= inv_det; 89 | 90 | if(t>ray.tmin && tray.tmin && t= 0) 115 | { 116 | float Ov = dot(vpleq,orig); 117 | float Dv = dot(vpleq,dir); 118 | float v = Ov + t*Dv; 119 | if (v >= 0 && (u+v) <= 1.f) 120 | { 121 | return Vec3f(u,v,t); 122 | } 123 | } 124 | } 125 | 126 | return miss; 127 | } 128 | 129 | //------------------------------------------------------------------------ 130 | -------------------------------------------------------------------------------- /src/rt/ray/RayBuffer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "gpu/Buffer.hpp" 30 | #include "Util.hpp" 31 | 32 | namespace FW 33 | { 34 | 35 | class RayBuffer 36 | { 37 | public: 38 | RayBuffer (S32 n=0, bool closestHit=true) : m_size(0), m_needClosestHit(closestHit) { resize(n); } 39 | 40 | S32 getSize () const { return m_size; } 41 | void resize (S32 n); 42 | 43 | void setRay (S32 slot, const Ray& ray) { setRay(slot, ray, slot); } 44 | void setRay (S32 slot, const Ray& ray, S32 id); 45 | void setResult (S32 slot, const RayResult& r) { getMutableResultForSlot(slot) = r; } 46 | 47 | // const Ray& operator[] (S32 slot) const { return getRayForSlot(slot); } 48 | const Ray& getRayForSlot (S32 slot) const { FW_ASSERT(slot >= 0 && slot < m_size); return ((const Ray*)m_rays.getPtr())[slot]; } 49 | const Ray& getRayForID (S32 id) const { return getRayForSlot(getSlotForID(id)); } 50 | 51 | const RayResult& getResultForSlot (S32 slot) const { FW_ASSERT(slot >= 0 && slot < m_size); return ((const RayResult*)m_results.getPtr())[slot]; } 52 | RayResult& getMutableResultForSlot (S32 slot) { FW_ASSERT(slot >= 0 && slot < m_size); return ((RayResult*)m_results.getMutablePtr())[slot]; } 53 | const RayResult& getResultForID (S32 id) const { return getResultForSlot(getSlotForID(id)); } 54 | RayResult& getMutableResultForID (S32 id) { return getMutableResultForSlot(getSlotForID(id)); } 55 | 56 | S32 getSlotForID (S32 id) const { FW_ASSERT(id >= 0 && id < m_size); return ((const S32*)m_IDToSlot.getPtr())[id]; } 57 | S32 getIDForSlot (S32 slot) const { FW_ASSERT(slot >= 0 && slot < m_size); return ((const S32*)m_slotToID.getPtr())[slot]; } 58 | 59 | void setNeedClosestHit (bool c) { m_needClosestHit = c; } 60 | bool getNeedClosestHit () const { return m_needClosestHit; } 61 | 62 | void mortonSort (); 63 | void randomSort (U32 randomSeed=0); 64 | 65 | Buffer& getRayBuffer () { return m_rays; } 66 | Buffer& getResultBuffer () { return m_results; } 67 | Buffer& getIDToSlotBuffer () { return m_IDToSlot; } 68 | Buffer& getSlotToIDBuffer () { return m_slotToID; } 69 | 70 | private: 71 | S32 m_size; 72 | mutable Buffer m_rays; // Ray 73 | mutable Buffer m_results; // RayResult 74 | mutable Buffer m_IDToSlot; // S32 75 | mutable Buffer m_slotToID; // S32 76 | 77 | bool m_needClosestHit; 78 | }; 79 | 80 | } // -------------------------------------------------------------------------------- /src/framework/base/Timer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/DLLImports.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class Timer 36 | { 37 | public: 38 | explicit inline Timer (bool started = false) : m_startTicks((started) ? queryTicks() : -1), m_totalTicks(0) {} 39 | inline Timer (const Timer& other) { operator=(other); } 40 | inline ~Timer (void) {} 41 | 42 | inline void start (void) { m_startTicks = queryTicks(); } 43 | inline void unstart (void) { m_startTicks = -1; } 44 | inline F32 getElapsed (void) { return ticksToSecs(getElapsedTicks()); } 45 | 46 | inline F32 end (void); // return elapsed, total += elapsed, restart 47 | inline F32 getTotal (void) const { return ticksToSecs(m_totalTicks); } 48 | inline void clearTotal (void) { m_totalTicks = 0; } 49 | 50 | inline Timer& operator= (const Timer& other); 51 | 52 | static void staticInit (void); 53 | static inline S64 queryTicks (void); 54 | static inline F32 ticksToSecs (S64 ticks); 55 | 56 | private: 57 | inline S64 getElapsedTicks (void); // return time since start, start if unstarted 58 | 59 | private: 60 | static F64 s_ticksToSecsCoef; 61 | static S64 s_prevTicks; 62 | 63 | S64 m_startTicks; 64 | S64 m_totalTicks; 65 | }; 66 | 67 | //------------------------------------------------------------------------ 68 | 69 | F32 Timer::end(void) 70 | { 71 | S64 elapsed = getElapsedTicks(); 72 | m_startTicks += elapsed; 73 | m_totalTicks += elapsed; 74 | return ticksToSecs(elapsed); 75 | } 76 | 77 | //------------------------------------------------------------------------ 78 | 79 | Timer& Timer::operator=(const Timer& other) 80 | { 81 | m_startTicks = other.m_startTicks; 82 | m_totalTicks = other.m_totalTicks; 83 | return *this; 84 | } 85 | 86 | //------------------------------------------------------------------------ 87 | 88 | S64 Timer::queryTicks(void) 89 | { 90 | LARGE_INTEGER ticks; 91 | QueryPerformanceCounter(&ticks); 92 | ticks.QuadPart = max(s_prevTicks, ticks.QuadPart); 93 | s_prevTicks = ticks.QuadPart; // increasing little endian => thread-safe 94 | return ticks.QuadPart; 95 | } 96 | 97 | //------------------------------------------------------------------------ 98 | 99 | F32 Timer::ticksToSecs(S64 ticks) 100 | { 101 | if (s_ticksToSecsCoef == -1.0) 102 | staticInit(); 103 | return (F32)((F64)ticks * s_ticksToSecsCoef); 104 | } 105 | 106 | //------------------------------------------------------------------------ 107 | 108 | S64 Timer::getElapsedTicks(void) 109 | { 110 | S64 curr = queryTicks(); 111 | if (m_startTicks == -1) 112 | m_startTicks = curr; 113 | return curr - m_startTicks; 114 | } 115 | 116 | //------------------------------------------------------------------------ 117 | } 118 | -------------------------------------------------------------------------------- /src/rt/bvh/BVH.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "Scene.hpp" 30 | #include "bvh/BVHNode.hpp" 31 | #include "ray/RayBuffer.hpp" 32 | 33 | namespace FW 34 | { 35 | 36 | struct RayStats 37 | { 38 | RayStats() { clear(); } 39 | void clear() { memset(this,0,sizeof(RayStats)); } 40 | void print() const { if(numRays>0) printf("Ray stats: (%s) %d rays, %.1f tris/ray, %.1f nodes/ray (cost=%.2f) %.2f treelets/ray\n", platform.getName().getPtr(), numRays, 1.f*numTriangleTests/numRays, 1.f*numNodeTests/numRays, (platform.getSAHTriangleCost()*numTriangleTests/numRays + platform.getSAHNodeCost()*numNodeTests/numRays), 1.f*numTreelets/numRays ); } 41 | 42 | S32 numRays; 43 | S32 numTriangleTests; 44 | S32 numNodeTests; 45 | S32 numTreelets; 46 | Platform platform; // set by whoever sets the stats 47 | }; 48 | 49 | class BVH 50 | { 51 | public: 52 | struct Stats 53 | { 54 | Stats() { clear(); } 55 | void clear() { memset(this, 0, sizeof(Stats)); } 56 | void print() const { printf("Tree stats: [bfactor=%d] %d nodes (%d+%d), %.2f SAHCost, %.1f children/inner, %.1f tris/leaf\n", branchingFactor,numLeafNodes+numInnerNodes, numLeafNodes,numInnerNodes, SAHCost, 1.f*numChildNodes/max(numInnerNodes,1), 1.f*numTris/max(numLeafNodes,1)); } 57 | 58 | F32 SAHCost; 59 | S32 branchingFactor; 60 | S32 numInnerNodes; 61 | S32 numLeafNodes; 62 | S32 numChildNodes; 63 | S32 numTris; 64 | }; 65 | 66 | struct BuildParams 67 | { 68 | Stats* stats; 69 | bool enablePrints; 70 | F32 splitAlpha; // spatial split area threshold 71 | 72 | BuildParams(void) 73 | { 74 | stats = NULL; 75 | enablePrints = true; 76 | splitAlpha = 1.0e-5f; 77 | } 78 | 79 | U32 computeHash(void) const 80 | { 81 | return hashBits(floatToBits(splitAlpha)); 82 | } 83 | }; 84 | 85 | public: 86 | BVH (Scene* scene, const Platform& platform, const BuildParams& params); 87 | ~BVH (void) { if(m_root) m_root->deleteSubtree(); } 88 | 89 | Scene* getScene (void) const { return m_scene; } 90 | const Platform& getPlatform (void) const { return m_platform; } 91 | BVHNode* getRoot (void) const { return m_root; } 92 | void trace (RayBuffer& rays, RayStats* stats = NULL) const; 93 | 94 | Array& getTriIndices (void) { return m_triIndices; } 95 | const Array& getTriIndices (void) const { return m_triIndices; } 96 | 97 | private: 98 | void traceRecursive (BVHNode* node, Ray& ray, RayResult& result, bool needClosestHit, RayStats* stats) const; 99 | 100 | Scene* m_scene; 101 | Platform m_platform; 102 | 103 | BVHNode* m_root; 104 | Array m_triIndices; 105 | }; 106 | 107 | } 108 | -------------------------------------------------------------------------------- /src/rt/Util.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Math.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class AABB 36 | { 37 | public: 38 | FW_CUDA_FUNC AABB (void) : m_mn(FW_F32_MAX, FW_F32_MAX, FW_F32_MAX), m_mx(-FW_F32_MAX, -FW_F32_MAX, -FW_F32_MAX) {} 39 | FW_CUDA_FUNC AABB (const Vec3f& mn, const Vec3f& mx) : m_mn(mn), m_mx(mx) {} 40 | 41 | FW_CUDA_FUNC void grow (const Vec3f& pt) { m_mn = m_mn.min(pt); m_mx = m_mx.max(pt); } 42 | FW_CUDA_FUNC void grow (const AABB& aabb) { grow(aabb.m_mn); grow(aabb.m_mx); } 43 | FW_CUDA_FUNC void intersect (const AABB& aabb) { m_mn = m_mn.max(aabb.m_mn); m_mx = m_mx.min(aabb.m_mx); } 44 | FW_CUDA_FUNC float volume (void) const { if(!valid()) return 0.0f; return (m_mx.x-m_mn.x) * (m_mx.y-m_mn.y) * (m_mx.z-m_mn.z); } 45 | FW_CUDA_FUNC float area (void) const { if(!valid()) return 0.0f; Vec3f d = m_mx - m_mn; return (d.x*d.y + d.y*d.z + d.z*d.x)*2.0f; } 46 | FW_CUDA_FUNC bool valid (void) const { return m_mn.x<=m_mx.x && m_mn.y<=m_mx.y && m_mn.z<=m_mx.z; } 47 | FW_CUDA_FUNC Vec3f midPoint (void) const { return (m_mn+m_mx)*0.5f; } 48 | FW_CUDA_FUNC const Vec3f& min (void) const { return m_mn; } 49 | FW_CUDA_FUNC const Vec3f& max (void) const { return m_mx; } 50 | FW_CUDA_FUNC Vec3f& min (void) { return m_mn; } 51 | FW_CUDA_FUNC Vec3f& max (void) { return m_mx; } 52 | 53 | FW_CUDA_FUNC AABB operator+ (const AABB& aabb) const { AABB u(*this); u.grow(aabb); return u; } 54 | 55 | private: 56 | Vec3f m_mn; 57 | Vec3f m_mx; 58 | }; 59 | 60 | //------------------------------------------------------------------------ 61 | 62 | struct Ray 63 | { 64 | FW_CUDA_FUNC Ray (void) : origin(0.0f), tmin(0.0f), direction(0.0f), tmax(0.0f) {} 65 | FW_CUDA_FUNC void degenerate (void) { tmax = tmin - 1.0f; } 66 | 67 | Vec3f origin; 68 | float tmin; 69 | Vec3f direction; 70 | float tmax; 71 | }; 72 | 73 | //------------------------------------------------------------------------ 74 | 75 | #define RAY_NO_HIT (-1) 76 | 77 | struct RayResult 78 | { 79 | FW_CUDA_FUNC RayResult (S32 ii = RAY_NO_HIT, float ti = 0.f) : id(ii), t(ti) {} 80 | FW_CUDA_FUNC bool hit (void) const { return (id != RAY_NO_HIT); } 81 | FW_CUDA_FUNC void clear (void) { id = RAY_NO_HIT; } 82 | 83 | S32 id; 84 | float t; 85 | S32 padA; 86 | S32 padB; 87 | }; 88 | 89 | //------------------------------------------------------------------------ 90 | 91 | namespace Intersect 92 | { 93 | Vec2f RayBox(const AABB& box, const Ray& ray); 94 | Vec3f RayTriangle(const Vec3f& v0, const Vec3f& v1, const Vec3f& v2, const Ray& ray); 95 | Vec3f RayTriangleWoop(const Vec4f& zpleq, const Vec4f& upleq, const Vec4f& vpleq, const Ray& ray); 96 | } 97 | 98 | //------------------------------------------------------------------------ 99 | } -------------------------------------------------------------------------------- /src/rt/bvh/BVHNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "bvh/BVHNode.hpp" 29 | 30 | namespace FW 31 | { 32 | 33 | 34 | int BVHNode::getSubtreeSize(BVH_STAT stat) const 35 | { 36 | int cnt; 37 | switch(stat) 38 | { 39 | default: FW_ASSERT(0); // unknown mode 40 | case BVH_STAT_NODE_COUNT: cnt = 1; break; 41 | case BVH_STAT_LEAF_COUNT: cnt = isLeaf() ? 1 : 0; break; 42 | case BVH_STAT_INNER_COUNT: cnt = isLeaf() ? 0 : 1; break; 43 | case BVH_STAT_TRIANGLE_COUNT: cnt = isLeaf() ? reinterpret_cast(this)->getNumTriangles() : 0; break; 44 | case BVH_STAT_CHILDNODE_COUNT: cnt = getNumChildNodes(); break; 45 | } 46 | 47 | if(!isLeaf()) 48 | { 49 | for(int i=0;igetSubtreeSize(stat); 51 | } 52 | 53 | return cnt; 54 | } 55 | 56 | 57 | void BVHNode::deleteSubtree() 58 | { 59 | for(int i=0;ideleteSubtree(); 61 | 62 | delete this; 63 | } 64 | 65 | 66 | void BVHNode::computeSubtreeProbabilities(const Platform& p,float probability, float& sah) 67 | { 68 | sah += probability * p.getCost(this->getNumChildNodes(),this->getNumTriangles()); 69 | 70 | m_probability = probability; 71 | 72 | for(int i=0;im_parentProbability = probability; 76 | float childProbability = 0.0f; 77 | if (probability > 0.0f) 78 | childProbability = probability * child->m_bounds.area()/this->m_bounds.area(); 79 | child->computeSubtreeProbabilities(p, childProbability, sah ); 80 | } 81 | } 82 | 83 | 84 | // TODO: requires valid probabilities... 85 | float BVHNode::computeSubtreeSAHCost(const Platform& p) const 86 | { 87 | float SAH = m_probability * p.getCost( getNumChildNodes(),getNumTriangles()); 88 | 89 | for(int i=0;icomputeSubtreeSAHCost(p); 91 | 92 | return SAH; 93 | } 94 | 95 | //------------------------------------------------------------- 96 | 97 | void assignIndicesDepthFirstRecursive( BVHNode* node, S32& index, bool includeLeafNodes ) 98 | { 99 | if(node->isLeaf() && !includeLeafNodes) 100 | return; 101 | 102 | node->m_index = index++; 103 | for(int i=0;igetNumChildNodes();i++) 104 | assignIndicesDepthFirstRecursive(node->getChildNode(i), index, includeLeafNodes); 105 | } 106 | 107 | void BVHNode::assignIndicesDepthFirst( S32 index, bool includeLeafNodes ) 108 | { 109 | assignIndicesDepthFirstRecursive( this, index, includeLeafNodes ); 110 | } 111 | 112 | //------------------------------------------------------------- 113 | 114 | void BVHNode::assignIndicesBreadthFirst( S32 index, bool includeLeafNodes ) 115 | { 116 | Array nodes; 117 | nodes.add(this); 118 | S32 head=0; 119 | 120 | while(head < nodes.getSize()) 121 | { 122 | // pop 123 | BVHNode* node = nodes[head++]; 124 | 125 | // discard 126 | if(node->isLeaf() && !includeLeafNodes) 127 | continue; 128 | 129 | // assign 130 | node->m_index = index++; 131 | 132 | // push children 133 | for(int i=0;igetNumChildNodes();i++) 134 | nodes.add(node->getChildNode(i)); 135 | } 136 | } 137 | 138 | 139 | } // 140 | -------------------------------------------------------------------------------- /src/rt/ray/PixelTable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ray/PixelTable.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | 34 | PixelTable::PixelTable(void) 35 | : m_size(0) 36 | { 37 | } 38 | 39 | //------------------------------------------------------------------------ 40 | 41 | PixelTable::~PixelTable(void) 42 | { 43 | } 44 | 45 | //------------------------------------------------------------------------ 46 | 47 | void PixelTable::setSize(const Vec2i& size) 48 | { 49 | bool recalc = (size != m_size); 50 | m_size = size; 51 | if (recalc) 52 | recalculate(); 53 | } 54 | 55 | //------------------------------------------------------------------------ 56 | 57 | void PixelTable::recalculate(void) 58 | { 59 | // construct LUTs 60 | m_indexToPixel.resizeDiscard(m_size.x * m_size.y * sizeof(S32)); 61 | m_pixelToIndex.resizeDiscard(m_size.x * m_size.y * sizeof(S32)); 62 | S32* idxtopos = (S32*)m_indexToPixel.getMutablePtr(); 63 | S32* postoidx = (S32*)m_pixelToIndex.getMutablePtr(); 64 | 65 | // dumb mode 66 | #if 0 67 | { 68 | for (int i=0; i < width*height; i++) 69 | { 70 | postoidx[i] = i; 71 | idxtopos[i] = i; 72 | } 73 | return; 74 | } 75 | #endif 76 | 77 | // smart mode 78 | int idx = 0; 79 | int bheight = m_size.y & ~7; 80 | int bwidth = m_size.x & ~7; 81 | 82 | // bulk of the image, sort blocks in in morton order 83 | int maxdim = (bwidth > bheight) ? bwidth : bheight; 84 | 85 | // round up to nearest power of two 86 | maxdim |= maxdim >> 1; 87 | maxdim |= maxdim >> 2; 88 | maxdim |= maxdim >> 4; 89 | maxdim |= maxdim >> 8; 90 | maxdim |= maxdim >> 16; 91 | maxdim = (maxdim + 1) >> 1; 92 | 93 | int width8 = bwidth >> 3; 94 | int height8 = bheight >> 3; 95 | for (int i=0; i < maxdim*maxdim; i++) 96 | { 97 | // get interleaved bit positions 98 | int tx = 0; 99 | int ty = 0; 100 | int val = i; 101 | int bit = 1; 102 | while (val) 103 | { 104 | if (val & 1) tx |= bit; 105 | if (val & 2) ty |= bit; 106 | bit += bit; 107 | val >>= 2; 108 | } 109 | if (tx < width8 && ty < height8) 110 | { 111 | for (int inner=0; inner < 64; inner++) 112 | { 113 | // swizzle ix and iy within blocks as well 114 | int ix = ((inner&1)>>0) | ((inner&4)>>1) | ((inner&16)>>2); 115 | int iy = ((inner&2)>>1) | ((inner&8)>>2) | ((inner&32)>>3); 116 | int pos = (ty*8 + iy) * m_size.x + (tx*8 + ix); 117 | 118 | postoidx[pos] = idx; 119 | idxtopos[idx++] = pos; 120 | } 121 | } 122 | } 123 | 124 | // if height not divisible, add horizontal stripe below bulk 125 | for (int px=0; px < bwidth; px++) 126 | for (int py=bheight; py < m_size.y; py++) 127 | { 128 | int pos = px + py * m_size.x; 129 | postoidx[pos] = idx; 130 | idxtopos[idx++] = pos; 131 | } 132 | 133 | // if width not divisible, add vertical stripe and the corner 134 | for (int py=0; py < m_size.y; py++) 135 | for (int px=bwidth; px < m_size.x; px++) 136 | { 137 | int pos = px + py * m_size.x; 138 | postoidx[pos] = idx; 139 | idxtopos[idx++] = pos; 140 | } 141 | 142 | // done! 143 | } 144 | -------------------------------------------------------------------------------- /src/rt/App.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "gui/Window.hpp" 30 | #include "gui/CommonControls.hpp" 31 | #include "3d/CameraControls.hpp" 32 | #include "cuda/Renderer.hpp" 33 | 34 | namespace FW 35 | { 36 | //------------------------------------------------------------------------ 37 | 38 | class App : public Window::Listener, public CommonControls::StateObject 39 | { 40 | private: 41 | enum Action 42 | { 43 | Action_None, 44 | 45 | Action_About, 46 | Action_LoadMesh, 47 | 48 | Action_ResetCamera, 49 | Action_ExportCameraSignature, 50 | Action_ImportCameraSignature, 51 | }; 52 | 53 | public: 54 | App (void); 55 | virtual ~App (void); 56 | 57 | virtual bool handleEvent (const Window::Event& ev); 58 | virtual void readState (StateDump& d); 59 | virtual void writeState (StateDump& d) const; 60 | 61 | void setWindowSize (const Vec2i& size) { m_window.setSize(size); } 62 | bool loadState (const String& fileName) { return m_commonCtrl.loadState(fileName); } 63 | void loadDefaultState(void) { if (!m_commonCtrl.loadState(m_commonCtrl.getStateFileName(1))) firstTimeInit(); } 64 | void flashButtonTitles(void) { m_commonCtrl.flashButtonTitles(); } 65 | 66 | private: 67 | void rebuildGui (void); 68 | void waitKey (void); 69 | void render (GLContext* gl); 70 | void renderGuiHelp (GLContext* gl); 71 | 72 | bool loadMesh (const String& fileName); 73 | void resetCamera (void); 74 | void firstTimeInit (void); 75 | 76 | private: 77 | App (const App&); // forbidden 78 | App& operator= (const App&); // forbidden 79 | 80 | private: 81 | Window m_window; 82 | CommonControls m_commonCtrl; 83 | CameraControls m_cameraCtrl; 84 | Renderer m_renderer; 85 | 86 | Action m_action; 87 | String m_meshFileName; 88 | MeshBase* m_mesh; 89 | Renderer::RayType m_rayType; 90 | F32 m_aoRadius; 91 | S32 m_numSamples; 92 | Array m_kernelNames; 93 | S32 m_kernelNameIdx; 94 | 95 | bool m_showHelp; 96 | bool m_showCameraControls; 97 | bool m_showKernelSelector; 98 | bool m_guiDirty; 99 | }; 100 | 101 | //------------------------------------------------------------------------ 102 | 103 | void listKernels (Array& kernelNames); 104 | void runInteractive (const Vec2i& frameSize, const String& stateFile); 105 | 106 | void runBenchmark( 107 | const Vec2i& frameSize, 108 | const String& meshFile, 109 | const Array& cameras, 110 | const Array& kernels, 111 | F32 sbvhAlpha, 112 | F32 aoRadius, 113 | int numSamples, 114 | bool sortSecondary, 115 | int warmupRepeats, 116 | int measureRepeats); 117 | 118 | //------------------------------------------------------------------------ 119 | } 120 | -------------------------------------------------------------------------------- /src/rt/bvh/SplitBVHBuilder.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "bvh/BVH.hpp" 30 | #include "base/Timer.hpp" 31 | 32 | namespace FW 33 | { 34 | //------------------------------------------------------------------------ 35 | 36 | class SplitBVHBuilder 37 | { 38 | private: 39 | enum 40 | { 41 | MaxDepth = 64, 42 | MaxSpatialDepth = 48, 43 | NumSpatialBins = 128, 44 | }; 45 | 46 | struct Reference 47 | { 48 | S32 triIdx; 49 | AABB bounds; 50 | 51 | Reference(void) : triIdx(-1) {} 52 | }; 53 | 54 | struct NodeSpec 55 | { 56 | S32 numRef; 57 | AABB bounds; 58 | 59 | NodeSpec(void) : numRef(0) {} 60 | }; 61 | 62 | struct ObjectSplit 63 | { 64 | F32 sah; 65 | S32 sortDim; 66 | S32 numLeft; 67 | AABB leftBounds; 68 | AABB rightBounds; 69 | 70 | ObjectSplit(void) : sah(FW_F32_MAX), sortDim(0), numLeft(0) {} 71 | }; 72 | 73 | struct SpatialSplit 74 | { 75 | F32 sah; 76 | S32 dim; 77 | F32 pos; 78 | 79 | SpatialSplit(void) : sah(FW_F32_MAX), dim(0), pos(0.0f) {} 80 | }; 81 | 82 | struct SpatialBin 83 | { 84 | AABB bounds; 85 | S32 enter; 86 | S32 exit; 87 | }; 88 | 89 | public: 90 | SplitBVHBuilder (BVH& bvh, const BVH::BuildParams& params); 91 | ~SplitBVHBuilder (void); 92 | 93 | BVHNode* run (void); 94 | 95 | private: 96 | static bool sortCompare (void* data, int idxA, int idxB); 97 | static void sortSwap (void* data, int idxA, int idxB); 98 | 99 | BVHNode* buildNode (NodeSpec spec, int level, F32 progressStart, F32 progressEnd); 100 | BVHNode* createLeaf (const NodeSpec& spec); 101 | 102 | ObjectSplit findObjectSplit (const NodeSpec& spec, F32 nodeSAH); 103 | void performObjectSplit (NodeSpec& left, NodeSpec& right, const NodeSpec& spec, const ObjectSplit& split); 104 | 105 | SpatialSplit findSpatialSplit (const NodeSpec& spec, F32 nodeSAH); 106 | void performSpatialSplit (NodeSpec& left, NodeSpec& right, const NodeSpec& spec, const SpatialSplit& split); 107 | void splitReference (Reference& left, Reference& right, const Reference& ref, int dim, F32 pos); 108 | 109 | private: 110 | SplitBVHBuilder (const SplitBVHBuilder&); // forbidden 111 | SplitBVHBuilder& operator= (const SplitBVHBuilder&); // forbidden 112 | 113 | private: 114 | BVH& m_bvh; 115 | const Platform& m_platform; 116 | const BVH::BuildParams& m_params; 117 | 118 | Array m_refStack; 119 | F32 m_minOverlap; 120 | Array m_rightBounds; 121 | S32 m_sortDim; 122 | SpatialBin m_bins[3][NumSpatialBins]; 123 | 124 | Timer m_progressTimer; 125 | S32 m_numDuplicates; 126 | }; 127 | 128 | //------------------------------------------------------------------------ 129 | } 130 | -------------------------------------------------------------------------------- /rt.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {71872d8d-a305-4e52-8848-baf034a82660} 6 | 7 | 8 | {82320b35-4c68-416e-a413-91a010154eea} 9 | 10 | 11 | {342d34a4-9d21-4559-8e4b-33dfbc57d47f} 12 | 13 | 14 | {3920db06-697c-4abf-a3ec-ed1c06b94c0c} 15 | 16 | 17 | 18 | 19 | bvh 20 | 21 | 22 | bvh 23 | 24 | 25 | bvh 26 | 27 | 28 | bvh 29 | 30 | 31 | cuda 32 | 33 | 34 | cuda 35 | 36 | 37 | cuda 38 | 39 | 40 | ray 41 | 42 | 43 | ray 44 | 45 | 46 | ray 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | bvh 55 | 56 | 57 | bvh 58 | 59 | 60 | bvh 61 | 62 | 63 | bvh 64 | 65 | 66 | cuda 67 | 68 | 69 | cuda 70 | 71 | 72 | cuda 73 | 74 | 75 | cuda 76 | 77 | 78 | kernels 79 | 80 | 81 | ray 82 | 83 | 84 | ray 85 | 86 | 87 | ray 88 | 89 | 90 | ray 91 | 92 | 93 | ray 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | cuda 102 | 103 | 104 | ray 105 | 106 | 107 | ray 108 | 109 | 110 | 111 | 112 | kernels 113 | 114 | 115 | kernels 116 | 117 | 118 | kernels 119 | 120 | 121 | kernels 122 | 123 | 124 | kernels 125 | 126 | 127 | kernels 128 | 129 | 130 | kernels 131 | 132 | 133 | kernels 134 | 135 | 136 | kernels 137 | 138 | 139 | kernels 140 | 141 | 142 | -------------------------------------------------------------------------------- /src/framework/io/ImageBinaryIO.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "io/ImageBinaryIO.hpp" 29 | #include "gui/Image.hpp" 30 | #include "io/Stream.hpp" 31 | 32 | using namespace FW; 33 | 34 | //------------------------------------------------------------------------ 35 | 36 | Image* FW::importBinaryImage(InputStream& stream) 37 | { 38 | // ImageHeader. 39 | 40 | char formatID[9]; 41 | stream.readFully(formatID, 8); 42 | formatID[8] = '\0'; 43 | if (String(formatID) != "BinImage") 44 | { 45 | setError("Not a binary image file!"); 46 | return NULL; 47 | } 48 | 49 | S32 version; 50 | stream >> version; 51 | if (version != 1) 52 | { 53 | setError("Unsupported binary image version!"); 54 | return NULL; 55 | } 56 | 57 | S32 width, height, bpp, numChannels; 58 | stream >> width >> height >> bpp >> numChannels; 59 | if (width < 0 || height < 0 || bpp < 0 || numChannels < 0) 60 | { 61 | setError("Corrupt binary image data!"); 62 | return NULL; 63 | } 64 | 65 | // Array of ImageChannel. 66 | 67 | ImageFormat format; 68 | for (int i = 0; i < numChannels; i++) 69 | { 70 | S32 ctype, cformat; 71 | ImageFormat::Channel c; 72 | stream >> ctype >> cformat >> c.wordOfs >> c.wordSize >> c.fieldOfs >> c.fieldSize; 73 | if (ctype < 0 || cformat < 0 || cformat >= ImageFormat::ChannelFormat_Max || 74 | c.wordOfs < 0 || (c.wordSize != 1 && c.wordSize != 2 && c.wordSize != 4) || 75 | c.fieldOfs < 0 || c.fieldSize <= 0 || c.fieldOfs + c.fieldSize > c.wordSize * 8 || 76 | (cformat == ImageFormat::ChannelFormat_Float && c.fieldSize != 32)) 77 | { 78 | setError("Corrupt binary image data!"); 79 | return NULL; 80 | } 81 | 82 | c.type = (ImageFormat::ChannelType)ctype; 83 | c.format = (ImageFormat::ChannelFormat)cformat; 84 | format.addChannel(c); 85 | } 86 | 87 | if (bpp != format.getBPP()) 88 | { 89 | setError("Corrupt binary image data!"); 90 | return NULL; 91 | } 92 | 93 | // Image data. 94 | 95 | Image* image = new Image(Vec2i(width, height), format); 96 | stream.readFully(image->getMutablePtr(), width * height * bpp); 97 | 98 | // Handle errors. 99 | 100 | if (hasError()) 101 | { 102 | delete image; 103 | return NULL; 104 | } 105 | return image; 106 | } 107 | 108 | //------------------------------------------------------------------------ 109 | 110 | void FW::exportBinaryImage(OutputStream& stream, const Image* image) 111 | { 112 | FW_ASSERT(image); 113 | const Vec2i& size = image->getSize(); 114 | const ImageFormat& format = image->getFormat(); 115 | int bpp = format.getBPP(); 116 | 117 | // ImageHeader. 118 | 119 | stream.write("BinImage", 8); 120 | stream << (S32)1 << (S32)size.x << (S32)size.y << (S32)bpp << (S32)format.getNumChannels(); 121 | 122 | // Array of ImageChannel. 123 | 124 | for (int i = 0; i < format.getNumChannels(); i++) 125 | { 126 | const ImageFormat::Channel& c = format.getChannel(i); 127 | stream << (S32)c.type << (S32)c.format << (S32)c.wordOfs << (S32)c.wordSize << (S32)c.fieldOfs << (S32)c.fieldSize; 128 | } 129 | 130 | // Image data. 131 | 132 | if (image->getStride() == size.x * bpp) 133 | stream.write(image->getPtr(), size.x * size.y * bpp); 134 | else 135 | for (int y = 0; y < size.y; y++) 136 | stream.write(image->getPtr(Vec2i(0, y)), size.x * bpp); 137 | } 138 | 139 | //------------------------------------------------------------------------ 140 | -------------------------------------------------------------------------------- /src/rt/cuda/Renderer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "3d/CameraControls.hpp" 30 | #include "base/Random.hpp" 31 | #include "cuda/CudaTracer.hpp" 32 | #include "ray/RayGen.hpp" 33 | 34 | namespace FW 35 | { 36 | //------------------------------------------------------------------------ 37 | 38 | class Renderer 39 | { 40 | public: 41 | enum RayType 42 | { 43 | RayType_Primary = 0, 44 | RayType_AO, 45 | RayType_Diffuse, 46 | 47 | RayType_Max 48 | }; 49 | 50 | struct Params 51 | { 52 | String kernelName; 53 | RayType rayType; 54 | F32 aoRadius; 55 | S32 numSamples; 56 | bool sortSecondary; 57 | 58 | Params(void) 59 | { 60 | kernelName = ""; 61 | rayType = RayType_Primary; 62 | aoRadius = 1.0f; 63 | numSamples = 32; 64 | sortSecondary = false; 65 | } 66 | }; 67 | 68 | public: 69 | Renderer (void); 70 | ~Renderer (void); 71 | 72 | void setMesh (MeshBase* mesh); 73 | void setBuildParams (const BVH::BuildParams& params) { invalidateBVH(); m_buildParams = params; } 74 | void invalidateBVH (void) { delete m_bvh; m_bvh = NULL; } 75 | 76 | void setParams (const Params& params); 77 | void setMessageWindow (Window* window) { m_window = window; m_compiler.setMessageWindow(window); m_tracer.setMessageWindow(window); } 78 | void setEnableRandom (bool enable) { m_enableRandom = enable; } 79 | 80 | CudaTracer& getCudaTracer (void) { return m_tracer; } 81 | Scene* getScene (void) const { return m_scene; } 82 | CudaBVH* getCudaBVH (void); 83 | 84 | F32 renderFrame (GLContext* gl, const CameraControls& camera); // returns total launch time 85 | 86 | void beginFrame (GLContext* gl, const CameraControls& camera); 87 | bool nextBatch (void); 88 | F32 traceBatch (void); // returns launch time 89 | void updateResult (void); // for current batch 90 | void displayResult (GLContext* gl); 91 | 92 | int getTotalNumRays (void); // for selected ray type, excluding degenerates 93 | 94 | private: 95 | Renderer (const Renderer&); // forbidden 96 | Renderer& operator= (const Renderer&); // forbidden 97 | 98 | private: 99 | CudaCompiler m_compiler; 100 | String m_bvhCachePath; 101 | Platform m_platform; 102 | BVH::BuildParams m_buildParams; 103 | RayGen m_raygen; 104 | CudaTracer m_tracer; 105 | Random m_random; 106 | 107 | Params m_params; 108 | Window* m_window; 109 | bool m_enableRandom; 110 | 111 | MeshBase* m_mesh; 112 | Scene* m_scene; 113 | CudaBVH* m_bvh; 114 | 115 | Image* m_image; 116 | F32 m_cameraFar; 117 | RayBuffer m_primaryRays; 118 | RayBuffer m_secondaryRays; 119 | 120 | bool m_newBatch; 121 | RayBuffer* m_batchRays; 122 | S32 m_batchStart; 123 | }; 124 | 125 | //------------------------------------------------------------------------ 126 | } 127 | -------------------------------------------------------------------------------- /src/framework/io/ImageLodePngIO.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "io/ImageLodePngIO.hpp" 29 | #include "io/Stream.hpp" 30 | #include "gui/Image.hpp" 31 | 32 | #include "3rdparty/lodepng/lodepng.h" 33 | 34 | using namespace FW; 35 | 36 | //------------------------------------------------------------------------ 37 | 38 | Image* FW::importLodePngImage(InputStream& stream) 39 | { 40 | // Read the entire input stream. 41 | 42 | Array dataBuffer; 43 | int blockSize = 4096; 44 | for (;;) 45 | { 46 | int pos = dataBuffer.getSize(); 47 | dataBuffer.resize(pos + blockSize); 48 | int num = stream.read(dataBuffer.getPtr(pos), blockSize); 49 | if (num < blockSize) 50 | { 51 | dataBuffer.resize(pos + num); 52 | break; 53 | } 54 | } 55 | 56 | if (hasError()) 57 | return NULL; 58 | 59 | // Decode image info. 60 | 61 | LodePNG::Decoder decoder; 62 | decoder.inspect(dataBuffer.getPtr(), dataBuffer.getSize()); 63 | Vec2i size(decoder.getWidth(), decoder.getHeight()); 64 | bool hasAlpha = (LodePNG_InfoColor_canHaveAlpha(&decoder.getInfoPng().color) != 0); 65 | 66 | if (decoder.hasError()) 67 | setError("importLodePngImage(): LodePNG error %d!", decoder.getError()); 68 | if (min(size) <= 0) 69 | setError("importLodePngImage(): Invalid image size!"); 70 | if (hasError()) 71 | return NULL; 72 | 73 | // Decode image data. 74 | 75 | int numBytes = size.x * size.y * ((hasAlpha) ? 4 : 3); 76 | std::vector pixelBuffer; 77 | pixelBuffer.reserve(numBytes); 78 | decoder.getInfoRaw().color.colorType = (hasAlpha) ? 6 : 2; 79 | decoder.decode(pixelBuffer, dataBuffer.getPtr(), dataBuffer.getSize()); 80 | 81 | if (decoder.hasError()) 82 | setError("importLodePngImage(): LodePNG error %d!", decoder.getError()); 83 | if ((int)pixelBuffer.size() != numBytes) 84 | setError("importLodePngImage(): Incorrect amount of pixel data!"); 85 | if (hasError()) 86 | return NULL; 87 | 88 | // Create image. 89 | 90 | Image* image = new Image(size, (hasAlpha) ? ImageFormat::R8_G8_B8_A8 : ImageFormat::R8_G8_B8); 91 | image->getBuffer().set(&pixelBuffer[0], numBytes); 92 | return image; 93 | } 94 | 95 | //------------------------------------------------------------------------ 96 | 97 | void FW::exportLodePngImage(OutputStream& stream, const Image* image) 98 | { 99 | // Select format and convert. 100 | 101 | FW_ASSERT(image); 102 | const Vec2i& size = image->getSize(); 103 | bool hasAlpha = image->getFormat().hasChannel(ImageFormat::ChannelType_A); 104 | ImageFormat::ID format = (hasAlpha) ? ImageFormat::R8_G8_B8_A8 : ImageFormat::R8_G8_B8; 105 | 106 | Image* converted = NULL; 107 | if (image->getFormat().getID() != format) 108 | { 109 | converted = new Image(size, format); 110 | *converted = *image; 111 | } 112 | 113 | // Encode image. 114 | 115 | LodePNG::Encoder encoder; 116 | int colorType = (hasAlpha) ? 6 : 2; 117 | encoder.getSettings().autoLeaveOutAlphaChannel = false; 118 | encoder.getInfoRaw().color.colorType = colorType; 119 | encoder.getInfoPng().color.colorType = colorType; 120 | 121 | std::vector dataBuffer; 122 | encoder.encode(dataBuffer, (U8*)((converted) ? converted : image)->getPtr(), size.x, size.y); 123 | 124 | if (encoder.hasError()) 125 | setError("exportLodePngImage(): LodePNG error %d!", encoder.getError()); 126 | 127 | // Write to the output stream. 128 | 129 | if (!hasError() && !dataBuffer.empty()) 130 | stream.write(&dataBuffer[0], (int)dataBuffer.size()); 131 | 132 | // Clean up. 133 | 134 | delete converted; 135 | } 136 | 137 | //------------------------------------------------------------------------ 138 | -------------------------------------------------------------------------------- /src/framework/base/Math.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "base/Math.hpp" 29 | 30 | using namespace FW; 31 | 32 | //------------------------------------------------------------------------ 33 | 34 | Vec4f Vec4f::fromABGR(U32 abgr) 35 | { 36 | return Vec4f( 37 | (F32)(abgr & 0xFF) * (1.0f / 255.0f), 38 | (F32)((abgr >> 8) & 0xFF) * (1.0f / 255.0f), 39 | (F32)((abgr >> 16) & 0xFF) * (1.0f / 255.0f), 40 | (F32)(abgr >> 24) * (1.0f / 255.0f)); 41 | } 42 | 43 | //------------------------------------------------------------------------ 44 | 45 | U32 Vec4f::toABGR(void) const 46 | { 47 | return 48 | ((((U32)(((U64)(FW::clamp(x, 0.0f, 1.0f) * exp2(56)) * 255) >> 55) + 1) >> 1) << 0) | 49 | ((((U32)(((U64)(FW::clamp(y, 0.0f, 1.0f) * exp2(56)) * 255) >> 55) + 1) >> 1) << 8) | 50 | ((((U32)(((U64)(FW::clamp(z, 0.0f, 1.0f) * exp2(56)) * 255) >> 55) + 1) >> 1) << 16) | 51 | ((((U32)(((U64)(FW::clamp(w, 0.0f, 1.0f) * exp2(56)) * 255) >> 55) + 1) >> 1) << 24); 52 | } 53 | 54 | //------------------------------------------------------------------------ 55 | 56 | Mat3f Mat4f::getXYZ(void) const 57 | { 58 | Mat3f r; 59 | for (int i = 0; i < 3; i++) 60 | r.col(i) = Vec4f(col(i)).getXYZ(); 61 | return r; 62 | } 63 | 64 | //------------------------------------------------------------------------ 65 | 66 | Mat4f Mat4f::fitToView(const Vec2f& pos, const Vec2f& size, const Vec2f& viewSize) 67 | { 68 | FW_ASSERT(size.x != 0.0f && size.y != 0.0f); 69 | FW_ASSERT(viewSize.x != 0.0f && viewSize.y != 0.0f); 70 | 71 | return 72 | Mat4f::scale(Vec3f(Vec2f(2.0f) / viewSize, 1.0f)) * 73 | Mat4f::scale(Vec3f((viewSize / size).min(), 1.0f)) * 74 | Mat4f::translate(Vec3f(-pos - size * 0.5f, 0.0f)); 75 | } 76 | 77 | //------------------------------------------------------------------------ 78 | 79 | Mat4f Mat4f::perspective(F32 fov, F32 nearDist, F32 farDist) 80 | { 81 | // Camera points towards -z. 0 < near < far. 82 | // Matrix maps z range [-near, -far] to [-1, 1], after homogeneous division. 83 | F32 f = rcp(tan(fov * FW_PI / 360.0f)); 84 | F32 d = rcp(nearDist - farDist); 85 | 86 | Mat4f r; 87 | r.setRow(0, Vec4f( f, 0.0f, 0.0f, 0.0f )); 88 | r.setRow(1, Vec4f( 0.0f, f, 0.0f, 0.0f )); 89 | r.setRow(2, Vec4f( 0.0f, 0.0f, (nearDist + farDist) * d, 2.0f * nearDist * farDist * d )); 90 | r.setRow(3, Vec4f( 0.0f, 0.0f, -1.0f, 0.0f )); 91 | return r; 92 | } 93 | 94 | //------------------------------------------------------------------------ 95 | 96 | Mat3f Mat3f::rotation(const Vec3f& axis, F32 angle) 97 | { 98 | Mat3f R; 99 | F32 cosa = cosf(angle); 100 | F32 sina = sinf(angle); 101 | R(0,0) = cosa + sqr(axis.x)*(1.0f-cosa); R(0,1) = axis.x*axis.y*(1.0f-cosa) - axis.z*sina; R(0,2) = axis.x*axis.z*(1.0f-cosa) + axis.y*sina; 102 | R(1,0) = axis.x*axis.y*(1.0f-cosa) + axis.z*sina; R(1,1) = cosa + sqr(axis.y)*(1.0f-cosa); R(1,2) = axis.y*axis.z*(1.0f-cosa) - axis.x*sina; 103 | R(2,0) = axis.z*axis.x*(1.0f-cosa) - axis.y*sina; R(2,1) = axis.z*axis.y*(1.0f-cosa) + axis.x*sina; R(2,2) = cosa + sqr(axis.z)*(1.0f-cosa); 104 | return R; 105 | } 106 | 107 | Mat3d Mat3d::rotation(const Vec3d& axis, F64 angle) 108 | { 109 | Mat3d R; 110 | F64 cosa = cos(angle); 111 | F64 sina = sin(angle); 112 | R(0,0) = cosa + sqr(axis.x)*(1.0-cosa); R(0,1) = axis.x*axis.y*(1.0-cosa) - axis.z*sina; R(0,2) = axis.x*axis.z*(1.0-cosa) + axis.y*sina; 113 | R(1,0) = axis.x*axis.y*(1.0-cosa) + axis.z*sina; R(1,1) = cosa + sqr(axis.y)*(1.0-cosa); R(1,2) = axis.y*axis.z*(1.0-cosa) - axis.x*sina; 114 | R(2,0) = axis.z*axis.x*(1.0-cosa) - axis.y*sina; R(2,1) = axis.z*axis.y*(1.0-cosa) + axis.x*sina; R(2,2) = cosa + sqr(axis.z)*(1.0-cosa); 115 | return R; 116 | } 117 | -------------------------------------------------------------------------------- /src/framework/base/MulticoreLauncher.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Thread.hpp" 30 | #include "base/Deque.hpp" 31 | 32 | namespace FW 33 | { 34 | //------------------------------------------------------------------------ 35 | // Launch a batch of tasks and wait: 36 | // MulticoreLauncher().push(myTaskFunc, &myTaskData, 0, numTasks); 37 | // 38 | // Display progress indicator: 39 | // MulticoreLauncher().push(myTaskFunc, &myTaskData, 0, numTasks).popAll("Processing..."); 40 | // 41 | // Grab results: 42 | // { 43 | // MulticoreLauncher launcher; 44 | // launcher.push(...); 45 | // while (launcher.getNumTasks()) 46 | // doSomething(launcher.pop().result); 47 | // } 48 | // 49 | // Asynchronous operation: 50 | // { 51 | // MulticoreLauncher launcher; 52 | // launcher.push(...); 53 | // while (launcher.getNumTasks()) 54 | // { 55 | // spendSomeIdleTime(); 56 | // while (launcher.getNumFinished()) 57 | // doSomething(launcher.pop().result); 58 | // } 59 | // } 60 | // 61 | // Create dependent tasks dynamically: 62 | // 63 | // void myTaskFunc(MulticoreLauncher::Task& task) 64 | // { 65 | // ... 66 | // task.launcher->push(...); 67 | // ... 68 | // } 69 | // 70 | // Per-thread temporary data: 71 | // 72 | // void myDeinitFunc(void* threadData) 73 | // { 74 | // ... 75 | // } 76 | // 77 | // void myTaskFunc(MulticoreLauncher::Task& task) 78 | // { 79 | // void* threadData = Thread::getCurrent()->getUserData("myID"); 80 | // if (!threadData) 81 | // { 82 | // threadData = ...; 83 | // Thread::getCurrent()->setUserData("myID", threadData, myDeinitFunc); 84 | // } 85 | // ... 86 | // } 87 | //------------------------------------------------------------------------ 88 | 89 | class MulticoreLauncher 90 | { 91 | public: 92 | struct Task; 93 | typedef void (*TaskFunc)(Task& task); 94 | 95 | struct Task 96 | { 97 | MulticoreLauncher* launcher; 98 | TaskFunc func; 99 | void* data; 100 | int idx; 101 | void* result; // Potentially written by TaskFunc. 102 | }; 103 | 104 | public: 105 | MulticoreLauncher (void); 106 | ~MulticoreLauncher (void); 107 | 108 | MulticoreLauncher& push (TaskFunc func, void* data, int firstIdx = 0, int numTasks = 1); 109 | Task pop (void); // Blocks until at least one task has finished. 110 | 111 | int getNumTasks (void) const; // Tasks that have been pushed but not popped. 112 | int getNumFinished (void) const; // Tasks that can be popped without blocking. 113 | 114 | void popAll (void) { while (getNumTasks()) pop(); } 115 | void popAll (const String& progressMessage); 116 | 117 | static int getNumCores (void); 118 | static void setNumThreads (int numThreads); 119 | 120 | private: 121 | static void applyNumThreads (void); 122 | static void threadFunc (void* param); 123 | 124 | private: 125 | MulticoreLauncher (const MulticoreLauncher&); // forbidden 126 | MulticoreLauncher& operator= (const MulticoreLauncher&); // forbidden 127 | 128 | private: 129 | static Spinlock s_lock; 130 | static S32 s_numInstances; 131 | static S32 s_desiredThreads; 132 | 133 | static Monitor* s_monitor; 134 | static Deque s_pending; 135 | static S32 s_numThreads; 136 | 137 | S32 m_numTasks; 138 | Deque m_finished; 139 | }; 140 | 141 | //------------------------------------------------------------------------ 142 | } 143 | -------------------------------------------------------------------------------- /src/framework/base/Main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "base/Main.hpp" 29 | #include "base/DLLImports.hpp" 30 | #include "base/Thread.hpp" 31 | #include "gui/Window.hpp" 32 | #include "gpu/GLContext.hpp" 33 | #include "gpu/CudaModule.hpp" 34 | #include "gpu/CudaCompiler.hpp" 35 | 36 | #include 37 | #include 38 | 39 | using namespace FW; 40 | 41 | //------------------------------------------------------------------------ 42 | 43 | int FW::argc = 0; 44 | char** FW::argv = NULL; 45 | int FW::exitCode = 0; 46 | 47 | static bool s_enableLeakCheck = true; 48 | 49 | //------------------------------------------------------------------------ 50 | 51 | int main(int argc, char* argv[]) 52 | { 53 | // Store arguments. 54 | 55 | FW::argc = argc; 56 | FW::argv = argv; 57 | 58 | // Force the main thread to run on a single core. 59 | 60 | SetThreadAffinityMask(GetCurrentThread(), 1); 61 | 62 | // Initialize CRTDBG. 63 | 64 | #if FW_DEBUG 65 | int flag = 0; 66 | flag |= _CRTDBG_ALLOC_MEM_DF; // use allocation guards 67 | // flag |= _CRTDBG_CHECK_ALWAYS_DF; // check whole memory on each alloc 68 | // flag |= _CRTDBG_DELAY_FREE_MEM_DF; // keep freed memory and check that it isn't written 69 | _CrtSetDbgFlag(flag); 70 | 71 | // _CrtSetBreakAlloc(64); 72 | _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); 73 | _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); 74 | #endif 75 | 76 | // Initialize the application. 77 | 78 | Thread::getCurrent(); 79 | FW::init(); 80 | failIfError(); 81 | 82 | // Message loop. 83 | 84 | while (Window::getNumOpen()) 85 | { 86 | // Wait for a message. 87 | 88 | MSG msg; 89 | if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 90 | { 91 | Window::realizeAll(); 92 | GetMessage(&msg, NULL, 0, 0); 93 | } 94 | 95 | // Process the message. 96 | 97 | TranslateMessage(&msg); 98 | DispatchMessage(&msg); 99 | 100 | // Nesting level was not restored => something fishy is going on. 101 | 102 | if (incNestingLevel(0) != 0) 103 | { 104 | fail( 105 | "Unhandled access violation detected!\n" 106 | "\n" 107 | "To get a stack trace, try the following:\n" 108 | "- Select \"Debug / Exceptions...\" in Visual Studio.\n" 109 | "- Expand the \"Win32 Exceptions\" category.\n" 110 | "- Check the \"Thrown\" box for \"Access violation\".\n" 111 | "- Re-run the application under debugger (F5)."); 112 | } 113 | } 114 | 115 | // Clean up. 116 | 117 | failIfError(); 118 | CudaCompiler::staticDeinit(); 119 | CudaModule::staticDeinit(); 120 | GLContext::staticDeinit(); 121 | Window::staticDeinit(); 122 | deinitDLLImports(); 123 | profileEnd(false); 124 | failIfError(); 125 | 126 | while (hasLogFile()) 127 | popLogFile(); 128 | 129 | delete Thread::getCurrent(); 130 | 131 | // Dump memory leaks. 132 | 133 | #if FW_DEBUG 134 | if (s_enableLeakCheck && _CrtDumpMemoryLeaks()) 135 | { 136 | printf("Press any key to continue . . . "); 137 | _getch(); 138 | printf("\n"); 139 | } 140 | #endif 141 | 142 | return exitCode; 143 | } 144 | 145 | //------------------------------------------------------------------------ 146 | 147 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 148 | { 149 | FW_UNREF(hInstance); 150 | FW_UNREF(hPrevInstance); 151 | FW_UNREF(lpCmdLine); // TODO: Parse this to argc/argv. 152 | FW_UNREF(nShowCmd); 153 | return main(0, NULL); 154 | } 155 | 156 | //------------------------------------------------------------------------ 157 | 158 | void FW::disableLeakCheck(void) 159 | { 160 | s_enableLeakCheck = false; 161 | } 162 | 163 | //------------------------------------------------------------------------ 164 | -------------------------------------------------------------------------------- /src/framework/io/StateDump.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "base/Hash.hpp" 30 | #include "io/Stream.hpp" 31 | 32 | namespace FW 33 | { 34 | 35 | //------------------------------------------------------------------------ 36 | 37 | class StateDump : public Serializable 38 | { 39 | public: 40 | StateDump (void) {} 41 | StateDump (const StateDump& other) { add(other); } 42 | virtual ~StateDump (void) { clear(); } 43 | 44 | virtual void readFromStream (InputStream& s); 45 | virtual void writeToStream (OutputStream& s) const; 46 | 47 | void clear (void); 48 | void add (const StateDump& other); 49 | void set (const StateDump& other) { if (&other != this) { clear(); add(other); } } 50 | 51 | void pushOwner (const String& id) { m_owners.add(xlateId(id + "::")); } 52 | void popOwner (void) { m_owners.removeLast(); } 53 | 54 | bool has (const String& id) const { return m_values.contains(xlateId(id)); } 55 | const Array* get (const String& id) const; 56 | bool get (void* ptr, int size, const String& id); 57 | template bool get (T& value, const String& id) const; 58 | template bool get (T& value, const String& id, const T& defValue) const; 59 | template T get (const String& id, const T& defValue) const; 60 | 61 | void set (const void* ptr, int size, const String& id); 62 | template void set (const T& value, const String& id); 63 | void unset (const String& id) { setInternal(NULL, xlateId(id)); } 64 | 65 | StateDump& operator= (const StateDump& other) { set(other); return *this; } 66 | 67 | private: 68 | String xlateId (const String& id) const { return (m_owners.getSize()) ? m_owners.getLast() + id : id; } 69 | void setInternal (Array* data, const String& id); // takes ownership of data 70 | 71 | private: 72 | Hash* > m_values; 73 | Array m_owners; 74 | 75 | mutable MemoryInputStream m_memIn; 76 | MemoryOutputStream m_memOut; 77 | }; 78 | 79 | //------------------------------------------------------------------------ 80 | 81 | template bool StateDump::get(T& value, const String& id) const 82 | { 83 | const Array* data = get(id); 84 | if (!data) 85 | return false; 86 | 87 | m_memIn.reset(*data); 88 | m_memIn >> value; 89 | FW_ASSERT(m_memIn.getOffset() == data->getSize()); 90 | return true; 91 | } 92 | 93 | //------------------------------------------------------------------------ 94 | 95 | template bool StateDump::get(T& value, const String& id, const T& defValue) const 96 | { 97 | if (get(value, id)) 98 | return true; 99 | value = defValue; 100 | return false; 101 | } 102 | 103 | //------------------------------------------------------------------------ 104 | 105 | template T StateDump::get(const String& id, const T& defValue) const 106 | { 107 | T value; 108 | get(value, id, defValue); 109 | return value; 110 | } 111 | 112 | //------------------------------------------------------------------------ 113 | 114 | template void StateDump::set(const T& value, const String& id) 115 | { 116 | m_memOut.clear(); 117 | m_memOut << value; 118 | Array& data = m_memOut.getData(); 119 | set(data.getPtr(), data.getNumBytes(), id); 120 | } 121 | 122 | //------------------------------------------------------------------------ 123 | } 124 | -------------------------------------------------------------------------------- /src/framework/gpu/CudaModule.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "gpu/GLContext.hpp" 30 | #include "gpu/CudaKernel.hpp" 31 | 32 | namespace FW 33 | { 34 | //------------------------------------------------------------------------ 35 | 36 | class CudaModule 37 | { 38 | public: 39 | CudaModule (const void* cubin); 40 | CudaModule (const String& cubinFile); 41 | ~CudaModule (void); 42 | 43 | CUmodule getHandle (void) { return m_module; } 44 | 45 | bool hasKernel (const String& name); 46 | CudaKernel getKernel (const String& name); 47 | 48 | Buffer& getGlobal (const String& name); 49 | void updateGlobals (bool async = false, CUstream stream = NULL); // copy to the device if modified 50 | 51 | CUtexref getTexRef (const String& name); 52 | void setTexRefMode (CUtexref texRef, bool wrap = true, bool bilinear = true, bool normalizedCoords = true, bool readAsInt = false); 53 | void setTexRef (const String& name, Buffer& buf, CUarray_format format, int numComponents); 54 | void setTexRef (const String& name, CUdeviceptr ptr, S64 size, CUarray_format format, int numComponents); 55 | void setTexRef (const String& name, CUarray cudaArray, bool wrap = true, bool bilinear = true, bool normalizedCoords = true, bool readAsInt = false); 56 | void setTexRef (const String& name, const Image& image, bool wrap = true, bool bilinear = true, bool normalizedCoords = true, bool readAsInt = false); 57 | void unsetTexRef (const String& name); 58 | void updateTexRefs (CUfunction kernel); 59 | 60 | CUsurfref getSurfRef (const String& name); 61 | void setSurfRef (const String& name, CUarray cudaArray); 62 | 63 | static void staticInit (void); 64 | static void staticDeinit (void); 65 | static bool isAvailable (void) { staticInit(); return s_available; } 66 | static S64 getMemoryUsed (void); 67 | static void sync (bool yield = true); // False = low latency but keeps the CPU busy. True = long latency but relieves the CPU. 68 | static void checkError (const char* funcName, CUresult res); 69 | static const char* decodeError (CUresult res); 70 | 71 | static CUdevice getDeviceHandle (void) { staticInit(); return s_device; } 72 | static int getDriverVersion (void); // e.g. 23 = 2.3 73 | static int getComputeCapability(void); // e.g. 13 = 1.3 74 | static int getDeviceAttribute (CUdevice_attribute attrib); 75 | static CUevent getStartEvent (void) { staticInit(); return s_startEvent; } 76 | static CUevent getEndEvent (void) { staticInit(); return s_endEvent; } 77 | 78 | private: 79 | static CUdevice selectDevice (void); 80 | static void printDeviceInfo (CUdevice device); 81 | static Vec2i selectGridSize (int numBlocks); // TODO: remove 82 | CUfunction findKernel (const String& name); 83 | 84 | private: 85 | CudaModule (const CudaModule&); // forbidden 86 | CudaModule& operator= (const CudaModule&); // forbidden 87 | 88 | private: 89 | static bool s_inited; 90 | static bool s_available; 91 | static CUdevice s_device; 92 | static CUcontext s_context; 93 | static CUevent s_startEvent; 94 | static CUevent s_endEvent; 95 | 96 | CUmodule m_module; 97 | Hash m_kernels; 98 | Array m_globals; 99 | Hash m_globalHash; 100 | Array m_texRefs; 101 | Hash m_texRefHash; 102 | }; 103 | 104 | //------------------------------------------------------------------------ 105 | } 106 | -------------------------------------------------------------------------------- /src/framework/io/ImageRawPngIO.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "io/ImageRawPngIO.hpp" 29 | #include "gui/Image.hpp" 30 | #include "io/Stream.hpp" 31 | 32 | using namespace FW; 33 | 34 | //------------------------------------------------------------------------ 35 | 36 | namespace FW 37 | { 38 | 39 | class Output 40 | { 41 | public: 42 | Output (OutputStream& s); 43 | ~Output (void) {} 44 | 45 | Output& operator<< (int byte) { m_stream << (U8)byte; m_crc = (m_crc >> 8) ^ m_crcTable[(m_crc ^ byte) & 0xFF]; return *this; } 46 | void writeDWord (U32 value) { *this << (value >> 24) << (value >> 16) << (value >> 8) << value; } 47 | 48 | void resetCRC (U32 value) { m_crc = value; } 49 | U32 getCRC (void) const { return m_crc; } 50 | 51 | private: 52 | Output (const Output&); // forbidden 53 | Output& operator= (const Output&); // forbidden 54 | 55 | private: 56 | OutputStream& m_stream; 57 | U32 m_crcTable[256]; 58 | U32 m_crc; 59 | }; 60 | 61 | } 62 | 63 | //------------------------------------------------------------------------ 64 | 65 | Output::Output(OutputStream& s) 66 | : m_stream (s), 67 | m_crc (0) 68 | { 69 | for (int i = 0; i < 256; i++) 70 | { 71 | U32 v = i; 72 | for (int j = 0; j < 8; j++) 73 | v = (v >> 1) ^ (0xedb88320u & (~(v & 1) + 1)); 74 | m_crcTable[i] = v; 75 | } 76 | } 77 | 78 | //------------------------------------------------------------------------ 79 | 80 | void FW::exportRawPngImage(OutputStream& stream, const Image* image) 81 | { 82 | FW_ASSERT(image); 83 | 84 | // Convert image. 85 | 86 | Vec2i size = image->getSize(); 87 | bool empty = (size.min() <= 0); 88 | bool hasAlpha = image->getFormat().hasChannel(ImageFormat::ChannelType_A); 89 | ImageFormat::ID format = (hasAlpha) ? ImageFormat::R8_G8_B8_A8 : ImageFormat::R8_G8_B8; 90 | const Image* source = image; 91 | Image* converted = NULL; 92 | 93 | if (empty || image->getFormat().getID() != format) 94 | { 95 | size = Vec2i(max(size.x, 1), max(size.y, 1)); 96 | converted = new Image(size, format); 97 | if (empty) 98 | converted->clear(); 99 | else 100 | *converted = *image; 101 | source = converted; 102 | } 103 | 104 | // Write header. 105 | 106 | Output out(stream); 107 | int bpp = (hasAlpha) ? 4 : 3; 108 | int blockLen = size.x * bpp + 1; 109 | 110 | out << 0x89 << 'P' << 'N' << 'G' << 0x0D << 0x0A << 0x1A << 0x0A; 111 | out << 0x00 << 0x00 << 0x00 << 0x0D << 'I' << 'H' << 'D' << 'R'; 112 | out.resetCRC(0x575e51f5); 113 | out.writeDWord(size.x); 114 | out.writeDWord(size.y); 115 | out << 8 << ((hasAlpha) ? 6 : 2) << 0 << 0 << 0; 116 | out.writeDWord(~out.getCRC()); 117 | out.writeDWord(size.y * (blockLen + 5) + 6); 118 | out << 'I' << 'D' << 'A' << 'T' << 0x78 << 0x01; 119 | out.resetCRC(0x13e5812d); 120 | 121 | // Write image data. 122 | 123 | int adlerA = 1; 124 | int adlerB = 0; 125 | 126 | for (int y = 0; y < size.y; y++) 127 | { 128 | out << ((y == size.y - 1) ? 1 : 0); 129 | out << blockLen << (blockLen >> 8) << ~blockLen << (~blockLen >> 8); 130 | out << 0; 131 | adlerB = (adlerA + adlerB) % 65521; 132 | 133 | const U8* scanline = (const U8*)source->getPtr(Vec2i(0, y)); 134 | for (int x = 0; x < blockLen - 1; x++) 135 | { 136 | out << scanline[x]; 137 | adlerA = (adlerA + scanline[x]) % 65521; 138 | adlerB = (adlerA + adlerB) % 65521; 139 | } 140 | } 141 | 142 | out << (adlerB >> 8) << adlerB << (adlerA >> 8) << adlerA; 143 | 144 | // Write footer. 145 | 146 | out.writeDWord(~out.getCRC()); 147 | out << 0x00 << 0x00 << 0x00 << 0x00; 148 | out << 'I' << 'E' << 'N' << 'D' << 0xAE << 0x42 << 0x60 << 0x82; 149 | delete converted; 150 | } 151 | 152 | //------------------------------------------------------------------------ 153 | -------------------------------------------------------------------------------- /src/framework/3d/ConvexPolyhedron.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, NVIDIA Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of NVIDIA Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #pragma once 29 | #include "3d/Mesh.hpp" 30 | 31 | namespace FW 32 | { 33 | //------------------------------------------------------------------------ 34 | 35 | class ConvexPolyhedron 36 | { 37 | public: 38 | struct Vertex 39 | { 40 | Vec3f pos; 41 | union 42 | { 43 | F32 orient; 44 | S32 remap; 45 | } aux; 46 | }; 47 | 48 | struct Edge 49 | { 50 | Vec2i verts; 51 | S32 remap; 52 | }; 53 | 54 | struct Face 55 | { 56 | Vec4f planeEq; 57 | S32 planeID; 58 | S32 firstEdge; 59 | S32 numEdges; 60 | S32 newEdge; 61 | }; 62 | 63 | struct FaceEdge 64 | { 65 | S32 edge; 66 | S32 old; 67 | }; 68 | 69 | public: 70 | ConvexPolyhedron (void) {} 71 | ConvexPolyhedron (const Vec3f& lo, const Vec3f& hi) { setCube(lo, hi); } 72 | ConvexPolyhedron (const ConvexPolyhedron& other) { set(other); } 73 | ~ConvexPolyhedron (void) {} 74 | 75 | int getNumVertices (void) const { return m_vertices.getSize(); } 76 | const Vec3f& getVertex (int idx) const { return m_vertices[idx].pos; } 77 | 78 | int getNumEdges (void) const { return m_edges.getSize(); } 79 | Vec2i getEdge (int idx) const { const Vec2i& e = m_edges[idx ^ (idx >> 31)].verts; return (idx >= 0) ? e : Vec2i(e.y, e.x); } 80 | int getEdgeStartVertex (int idx) const { return (idx >= 0) ? m_edges[idx].verts.x : m_edges[~idx].verts.y; } 81 | int getEdgeEndVertex (int idx) const { return (idx >= 0) ? m_edges[idx].verts.y : m_edges[~idx].verts.x; } 82 | const Vec3f& getEdgeStartPos (int idx) const { return m_vertices[getEdgeStartVertex(idx)].pos; } 83 | const Vec3f& getEdgeEndPos (int idx) const { return m_vertices[getEdgeEndVertex(idx)].pos; } 84 | 85 | int getNumFaces (void) const { return m_faces.getSize(); } 86 | const Vec4f& getFacePlaneEq (int idx) const { return m_faces[idx].planeEq; } 87 | int getFacePlaneID (int idx) const { return m_faces[idx].planeID; } 88 | int getFaceNumEdges (int idx) const { return m_faces[idx].numEdges; } 89 | int getFaceEdge (int faceIdx, int idx) { FW_ASSERT(idx >= 0 && idx < m_faces[faceIdx].numEdges); return m_faceEdges[m_faces[faceIdx].firstEdge + idx].edge; } 90 | 91 | void set (const ConvexPolyhedron& other) { m_vertices = other.m_vertices; m_edges = other.m_edges; m_faces = other.m_faces; m_faceEdges = other.m_faceEdges; } 92 | void setEmpty (void) { m_vertices.clear(); m_edges.clear(); m_faces.clear(); m_faceEdges.clear(); } 93 | void setCube (const Vec3f& lo, const Vec3f& hi); 94 | 95 | bool intersect (const Vec4f& planeEq, int planeID = -1); 96 | bool intersect (const ConvexPolyhedron& other); 97 | bool intersectCube (const Vec3f& lo, const Vec3f& hi) { ConvexPolyhedron t; t.setCube(lo, hi); return intersect(t); } 98 | 99 | F32 computeVolume (void) const; 100 | F32 computeArea (int faceIdx) const; 101 | F32 computeArea (void) const { F32 t = 0.0f; for (int i = 0; i < m_faces.getSize(); i++) t += computeArea(i); return t; } 102 | Vec3f computeCenterOfMass (int faceIdx) const; 103 | Vec3f computeCenterOfMass (void) const; 104 | Mesh* createMesh (void) const; 105 | 106 | ConvexPolyhedron& operator= (const ConvexPolyhedron& other) { set(other); return *this; } 107 | 108 | private: 109 | Array m_vertices; 110 | Array m_edges; 111 | Array m_faces; 112 | Array m_faceEdges; 113 | }; 114 | 115 | //------------------------------------------------------------------------ 116 | } 117 | --------------------------------------------------------------------------------