1 |
4 |
jguarin200 |
-- Author : Julian Andres Guarin Reyes.
|
2 |
|
|
-- Project : JART, Just Another Ray Tracer.
|
3 |
|
|
-- email : jguarin2002 at gmail.com, j.guarin at javeriana.edu.co
|
4 |
|
|
|
5 |
|
|
-- This code was entirely written by Julian Andres Guarin Reyes.
|
6 |
|
|
-- The following code is licensed under GNU Public License
|
7 |
|
|
-- http://www.gnu.org/licenses/gpl-3.0.txt.
|
8 |
|
|
|
9 |
|
|
-- This file is part of JART (Just Another Ray Tracer).
|
10 |
|
|
|
11 |
|
|
-- JART (Just Another Ray Tracer) is free software: you can redistribute it and/or modify
|
12 |
|
|
-- it under the terms of the GNU General Public License as published by
|
13 |
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
14 |
|
|
-- (at your option) any later version.
|
15 |
|
|
|
16 |
|
|
-- JART (Just Another Ray Tracer) is distributed in the hope that it will be useful,
|
17 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
|
|
-- GNU General Public License for more details.
|
20 |
|
|
|
21 |
|
|
-- You should have received a copy of the GNU General Public License
|
22 |
|
|
-- along with JART (Just Another Ray Tracer). If not, see <http://www.gnu.org/licenses/>.
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
-- *****************************************************************************************
|
26 |
|
|
-- This Block is the entire ray sphere intersection unit. An intersection unit is governed by the expression:
|
27 |
|
|
|
28 |
|
|
-- V.D +- ((V.D)^2 - (V^2-R^2))^2
|
29 |
|
|
-- So theres a root square.... if this root square has a non complex value, then a ray sphere intersection exists.
|
30 |
|
|
-- After several ray tracing algorithm performance simulations, I found that the most repeated operation is dot product and comparison. This are two operations that are performed when solving the root square inner expression. This expression is called discriminant.
|
31 |
|
|
-- That result can be interpreted as follows : the performance improvement or decay comes through when more or less discriminants are solved by time unit.
|
32 |
|
|
-- We assume that V^2 - R^2 is a contant because V is the position of the shpere in terms of the observer and R is the sphere's radius neither of those change in the frame calculation, we are going to call the root square of this expression the constant Ks.
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
-- We have then the inequality:
|
36 |
|
|
-- (V.D)^2 - (V^2-R^2) >= 0 (1)
|
37 |
|
|
-- (V.D)^2>=(V^2-R^2) (2)
|
38 |
|
|
-- (V.D)>=(V^2-R ^2)^(1/2) (3)
|
39 |
|
|
-- (V.D)>=Ks (4)
|
40 |
|
|
|
41 |
|
|
-- Assuming that there are more "no intersections" than "intersections" for all the V.D>=Ks pairs, is then a good stategy to use the (4) inequality instead of the (2) inequality because this way thre is a saving in time and silicon space, due of the lack of need an additional multiplication V.D x V.D.
|
42 |
|
|
-- ******************************************************************************************************
|
43 |
|
|
-- Using the altera quartus ii simulator, it was concluded that a combinatorial propagation time is larger than a 50 MHz frecuency period of about 25 - 32 ns. The obvious solution was to split the dot product and the comparison in a two stage pipe:
|
44 |
|
|
|
45 |
|
|
-- (V.D)-> FF-> (V.D>=Ks?)-> 1/0
|
46 |
|
|
-- Stage1 Stage2 Result.
|
47 |
|
|
|
48 |
|
|
-- This Block of code is devoted to instantiate the dot product, the comparison and to separate this operations in a two stage pipeline
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
library ieee;
|
52 |
|
|
use ieee.std_logic_1164.all;
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
entity bl02 is
|
58 |
|
|
port (
|
59 |
|
|
-- Global control. 50 MHZ clk.
|
60 |
|
|
rst: in std_logic;
|
61 |
|
|
clk: in std_logic;
|
62 |
|
|
|
63 |
|
|
-- Centro Esfera
|
64 |
|
|
Vx : in std_logic_vector (17 downto 0);
|
65 |
|
|
Vy : in std_logic_vector (17 downto 0);
|
66 |
|
|
Vz : in std_logic_vector (17 downto 0);
|
67 |
|
|
|
68 |
|
|
-- Direccion Rayo
|
69 |
|
|
Dx : in std_logic_vector (17 downto 0);
|
70 |
|
|
Dy : in std_logic_vector (17 downto 0);
|
71 |
|
|
Dz : in std_logic_vector (17 downto 0);
|
72 |
|
|
|
73 |
|
|
-- Sqrt(V² - r²)
|
74 |
|
|
K : in std_logic_vector (31 downto 0);
|
75 |
|
|
|
76 |
|
|
-- Producto punto
|
77 |
|
|
VD : out std_logic_vector (31 downto 0);
|
78 |
|
|
|
79 |
|
|
-- Interseccion
|
80 |
|
|
Iok : out std_logic
|
81 |
|
|
|
82 |
|
|
);
|
83 |
|
|
|
84 |
|
|
end entity;
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
architecture rtl of bl02 is
|
90 |
|
|
|
91 |
|
|
-- Component Headers (perhaps they should be on a package file).
|
92 |
|
|
|
93 |
|
|
component bl00
|
94 |
|
|
port (
|
95 |
|
|
-- <vx, vy, vz> y <dx, dy, dz> fixed point A(10,7) => 18 bits de representacion.
|
96 |
|
|
vx_A7_10 : in std_logic_vector (17 downto 0);
|
97 |
|
|
vy_A7_10 : in std_logic_vector (17 downto 0);
|
98 |
|
|
vz_A7_10 : in std_logic_vector (17 downto 0);
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
dx_A7_10 : in std_logic_vector (17 downto 0);
|
102 |
|
|
dy_A7_10 : in std_logic_vector (17 downto 0);
|
103 |
|
|
dz_A7_10 : in std_logic_vector (17 downto 0);
|
104 |
|
|
|
105 |
|
|
-- <vxdx + vydy + vzdz> fixed point A(23,8) => 32 bits de representacion.
|
106 |
|
|
vd_A15_16 : out std_logic_vector (31 downto 0)
|
107 |
|
|
|
108 |
|
|
);
|
109 |
|
|
end component;
|
110 |
|
|
|
111 |
|
|
component bl01
|
112 |
|
|
port (
|
113 |
|
|
vd : in std_logic_vector(31 downto 0);
|
114 |
|
|
k : in std_logic_vector(31 downto 0);
|
115 |
|
|
i : out std_logic
|
116 |
|
|
);
|
117 |
|
|
end component;
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
-- V.D pipe signals in order to achieve 50 MHZ.
|
122 |
|
|
signal vd_d : std_logic_vector (31 downto 0);
|
123 |
|
|
signal vd_q : std_logic_vector (31 downto 0);
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
begin
|
127 |
|
|
|
128 |
|
|
--Stage 1 : Instantiate Dot Product.
|
129 |
|
|
dotprod : bl00 port map(
|
130 |
|
|
vx_A7_10 => Vx,
|
131 |
|
|
vy_A7_10 => Vy,
|
132 |
|
|
vz_A7_10 => Vz,
|
133 |
|
|
dx_A7_10 => Dx,
|
134 |
|
|
dy_A7_10 => Dy,
|
135 |
|
|
dz_A7_10 => Dz,
|
136 |
|
|
vd_A15_16 => vd_d
|
137 |
|
|
);
|
138 |
|
|
|
139 |
|
|
--Stage 2 : Instantiate Comparison.
|
140 |
|
|
compare : bl01 port map(vd => vd_q, k => K, i => Iok);
|
141 |
|
|
|
142 |
|
|
pipe : process (rst,clk)
|
143 |
|
|
begin
|
144 |
|
|
if rst = '1' then
|
145 |
|
|
|
146 |
|
|
-- De esta manera siempre sera el numero mas pequeño cuando haya reset.
|
147 |
|
|
vd_q (30 downto 0) <= (others => '0');
|
148 |
|
|
vd_q (31) <= '1';
|
149 |
|
|
|
150 |
|
|
elsif rising_edge(clk) then
|
151 |
|
|
|
152 |
|
|
-- Pipe.
|
153 |
|
|
vd_q <= vd_d;
|
154 |
|
|
|
155 |
|
|
end if;
|
156 |
|
|
end process;
|
157 |
|
|
|
158 |
|
|
-- Conectar el Pipe 0 a la salida en VD.
|
159 |
|
|
VD <= vd_d;
|
160 |
|
|
|
161 |
|
|
end rtl;
|
162 |
|
|
|