Parallel Rendering Graphics Algorithms Using Opencl
Total Page:16
File Type:pdf, Size:1020Kb
PARALLEL RENDERING GRAPHICS ALGORITHMS USING OPENCL Gary Deng B.S., California Polytechnic State University, San Luis Obispo, 2006 PROJECT Submitted in partial satisfaction of the requirements for the degree of MASTER OF SCIENCE in COMPUTER SCIENCE at CALIFORNIA STATE UNIVERSITY, SACRAMENTO FALL 2011 PARALLEL RENDERING GRAPHICS ALGORITHMS USING OPENCL A Project by Gary Deng Approved by: __________________________________, Committee Chair John Clevenger, Ph.D. __________________________________, Second Reader V. Scott Gordon, Ph.D. ____________________________ Date ii Student: Gary Deng I certify that this student has met the requirements for format contained in the University format manual, and that this project is suitable for shelving in the Library and credit is to be awarded for the Project. __________________________, Graduate Coordinator ________________ Nikrouz Faroughi, Ph.D. Date Department of Computer Science iii Abstract of PARALLEL RENDERING GRAPHICS ALGORITHMS USING OPENCL by Gary Deng The developments of computing hardware architectures are heading in a direction toward parallel computing. Whereas better and faster CPUs used to mean higher clock rates, better and faster CPUs now means more cores per chip. Additionally, GPUs are emerging as powerful parallel processing devices when computing particular types of problems. Computers today have a tremendous amount of varied parallel processing power. Utilizing these different devices typically means wrestling with varied architecture, vendor, or platform specific programming models and code. OpenCL is an open-standard designed to provide developers with a standard interface for programming varied (heterogeneous) parallel devices. This standard allows single source codes to define algorithms to solve vectorized problems on various parallel devices on the same machine. These programs are also portable. This project explores OpenCL to implement a cross-platform, parallel solution to a vectorized problem. The domain of the problem is ray-tracing. Ray-tracing is a computer graphics iv rendering algorithm that determines how to visualize a scene. A significant number of calculations are performed to colorize each pixel based on the data of the 3D objects in the scene. Though heavy, the calculations for each pixel can be made completely independently from the calculations of any of the other pixels. The project has a GUI implemented in C++. The project has a ray-tracing engine implemented in C++. The ray-traced rendering routines come in four implementations: 1) written recursively in C++, executed on the CPU cores; 2) written iteratively in C++, executed on the CPU cores; 3) written iteratively in OpenCL C, executed in parallel on the CPU cores; 4) written iteratively in OpenCL C, executed in parallel on the GPU cores. The GUI reports the running time to perform the ray-tracing calculations to visualize the scene to the frame buffer for each ray-tracing implementation. _______________________, Committee Chair John Clevenger, Ph.D. _______________________ Date v ACKNOWLEDGMENTS I would like to express my gratitude to my project advisor Dr. John Clevenger who has taught me almost all I know about computer graphics. I would like to give thanks to my second reader Dr. V. Scott Gordon for his assistance. I would like to give my love and appreciation to my parents Yen-Hsi and Shu-Hsun who encouraged me to not give up when I just really, really wanted to. vi TABLE OF CONTENTS Page Acknowledgements .................................................................................................................. vi List of Tables ........................................................................................................................... ix List of Figures ........................................................................................................................... x List of Abbreviations ............................................................................................................ xiii Chapter 1. INTRODUCTION .............................................................................................................. 1 2. RELEVANT TOPICS AND TECHNOLOGIES ................................................................ 4 2.1 Ray-Tracing .................................................................................................... 4 2.2 OpenCL ........................................................................................................... 8 2.3 OpenCL and Vectorized Problems ................................................................. 9 2.4 OpenCL C Programming Language ............................................................. 11 2.5 JUCE Library ................................................................................................ 12 3. METHODOLOGY ........................................................................................................... 13 3.1 Application Overview ................................................................................... 13 3.2 Hardware and Operating System .................................................................. 14 3.3 The Engine ................................................................................................... 15 4. IMPLEMENTATION ....................................................................................................... 20 4.1 Primitives, Materials, and Colors.................................................................. 20 4.2 Ray-Tracing .................................................................................................. 20 4.3 GUI ............................................................................................................... 26 5. RESULTS AND CONCLUSIONS ................................................................................... 35 vii 5.1 Rendered Scene Images ................................................................................ 35 5.2 Implementation Ray Statistics ...................................................................... 47 5.3 Implementation Run Times .......................................................................... 50 6. FUTURE WORK .............................................................................................................. 60 6.1 Port to Other Platforms ................................................................................. 60 6.2 OpenMP Versus OpenCL CPU Implementations ......................................... 60 6.3 Movable Camera and Animation .................................................................. 60 6.4 Refraction Rays............................................................................................. 61 Appendix A. Source Code in C++ ........................................................................................ 63 Appendix B. Source Code in OpenCL C ............................................................................ 128 Appendix C. Permission from Jacco Bikker ....................................................................... 140 References ............................................................................................................................. 141 viii LIST OF TABLES Page 1. Table 4.1 The Four Ray-Tracing Implementations of the Project Application………21 ix LIST OF FIGURES Page 1. Figure 1.1 Amdahl’s Law Formula………………………….………………………... 3 2. Figure 2.1 Ray-Traced Scene from Whitted Article, Circa 1980…………………….. 4 3. Figure 2.2 Ray-Traced Scene by Gilles Tran Using POV-Ray Engine………………. 5 4. Figure 2.3 A Primary Ray Intersects with a Scene Object………………………...…. 6 5. Figure 2.4 Traditional Ray-Tracing Routine ……………………….….....………...... 6 6. Figure 2.5 Ray Tree Determines the Pixel’s Color……………………….………….. 7 7. Figure 2.6 The Vectorized Nature of Ray-Tracing for Each Pixel…………………… 8 8. Figure 2.7 OpenCL Architecture……………………….…………………………….. 9 9. Figure 2.8 OpenCL Platform Model……………………….………………………... 10 10. Figure 2.9 Many Instances of One Kernel on One Device……………………….…. 11 11. Figure 2.10 Parallel Execution on Similar Data……………………….…………..... 11 12. Figure 3.1 Application’s Structure……………………….…………………………. 13 13. Figure 4.1 General Rendering Loop……………………………………………….... 22 14. Figure 4.2 Recursive Ray-Trace Routine for One Pixel …………………………… 23 15. Figure 4.3 Iterative Ray-Trace Routine for One Pixel ……………………….…….. 25 16. Figure 4.4 Initial State of GUI……………………….……………………………... 27 17. Figure 4.5 Viewing Window During Scene Rendering ……………………………. 28 18. Figure 4.6 Viewing Window with Completely Rendered Scene…………………… 29 19. Figure 4.7 Timing Display Before Scene Rendering……………………….………. 30 20. Figure 4.8 Timing Display After Scene Rendering……………………….………… 30 21. Figure 4.9 Implementation ComboBox with Selectable Values…………….……… 31 22. Figure 4.10 Render Button……………………….………………………………….. 32 x 23. Figure 4.11 Maximum Trace Depth Slider………………………………………….. 32 24. Figure 4.12 OpenCL Recommended Workgroup Size Labels ……………………... 32 25. Figure 4.13 Pixel Cohort Size Combo Box with Selectable Values ………………... 33 26. Figure 4.14 Ray-Surface Intersections Labels……………………….……………… 34 27. Figure 4.15 Ray Misses Labels…………………………………………………….... 34 28. Figure 5.1 Rendered Scene: Maximum Ray-Trace Depth = 1………………………. 36 29. Figure 5.2 Rendered Scene: Maximum Ray-Trace Depth = 2………………………. 37 30. Figure 5.3 Rendered Scene: Maximum Ray-Trace Depth = 3………………………. 38 31. Figure 5.4 Rendered Scene: Maximum Ray-Trace Depth = 4………………………. 39 32. Figure 5.5 Rendered Scene: Maximum Ray-Trace