OpenCores
URL https://opencores.org/ocsvn/orsoc_graphics_accelerator/orsoc_graphics_accelerator/trunk

Subversion Repositories orsoc_graphics_accelerator

[/] [orsoc_graphics_accelerator/] [trunk/] [rtl/] [verilog/] [gfx/] [gfx_cuvz.v] - Blame information for rev 8

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 Orka
/*
2
ORSoC GFX accelerator core
3
Copyright 2012, ORSoC, Per Lenander, Anton Fosselius.
4
 
5
INTERPOLATION MODULE - Color, UV and Z calculator
6
 
7
 This file is part of orgfx.
8
 
9
 orgfx is free software: you can redistribute it and/or modify
10
 it under the terms of the GNU Lesser General Public License as published by
11
 the Free Software Foundation, either version 3 of the License, or
12
 (at your option) any later version.
13
 
14
 orgfx is distributed in the hope that it will be useful,
15
 but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 GNU Lesser General Public License for more details.
18
 
19
 You should have received a copy of the GNU Lesser General Public License
20
 along with orgfx.  If not, see <http://www.gnu.org/licenses/>.
21
 
22
*/
23
 
24
/*
25
This module uses the interpolation factors from the divider to calculate color, depth and texture coordinates
26
*/
27
module gfx_cuvz(
28
  clk_i, rst_i,
29
  ack_i, ack_o,
30
  write_i,
31
  // Variables needed for interpolation
32
  factor0_i, factor1_i,
33
  // Color
34
  color0_i, color1_i, color2_i, color_depth_i,
35
  color_o,
36
  // Depth
37
  z0_i, z1_i, z2_i,
38
  z_o,
39
  // Texture coordinates
40
  u0_i, v0_i, u1_i, v1_i, u2_i, v2_i,
41
  u_o, v_o,
42
  // Alpha
43
  a0_i, a1_i, a2_i,
44
  a_o,
45
  // Bezier shape calculation
46
  bezier_factor0_o, bezier_factor1_o,
47
  // Raster position
48
  x_i, y_i, x_o, y_o,
49
 
50
  write_o
51
  );
52
 
53
parameter point_width = 16;
54
 
55
input                        clk_i;
56
input                        rst_i;
57
 
58
input                        ack_i;
59
output reg                   ack_o;
60
 
61
input                        write_i;
62
 
63
// Input barycentric coordinates used to interpolate values
64
input      [point_width-1:0] factor0_i;
65
input      [point_width-1:0] factor1_i;
66
 
67
// Color for each point
68
input                 [31:0] color0_i;
69
input                 [31:0] color1_i;
70
input                 [31:0] color2_i;
71
input                  [1:0] color_depth_i;
72
// Interpolated color
73
output reg            [31:0] color_o;
74
 
75
// Depth for each point
76
input signed [point_width-1:0] z0_i;
77
input signed [point_width-1:0] z1_i;
78
input signed [point_width-1:0] z2_i;
79
// Interpolated depth
80
output reg signed [point_width-1:0] z_o;
81
 
82
// Alpha for each point
83
input      [7:0] a0_i;
84
input      [7:0] a1_i;
85
input      [7:0] a2_i;
86
// Interpolated alpha
87
output reg [7:0] a_o;
88
 
89
// Texture coordinates for each point
90
input      [point_width-1:0] u0_i;
91
input      [point_width-1:0] u1_i;
92
input      [point_width-1:0] u2_i;
93
input      [point_width-1:0] v0_i;
94
input      [point_width-1:0] v1_i;
95
input      [point_width-1:0] v2_i;
96
// Interpolated texture coordinates
97
output reg [point_width-1:0] u_o;
98
output reg [point_width-1:0] v_o;
99
 
100
// Bezier factors, used to draw bezier shapes
101
output reg [point_width-1:0] bezier_factor0_o;
102
output reg [point_width-1:0] bezier_factor1_o;
103
 
104
// Input and output pixel coordinate (passed on)
105
input      [point_width-1:0] x_i;
106
input      [point_width-1:0] y_i;
107
output reg [point_width-1:0] x_o;
108
output reg [point_width-1:0] y_o;
109
 
110
// Write pixel output signal
111
output reg                   write_o;
112
 
113
// Holds the barycentric coordinates for interpolation
114
reg        [point_width:0] factor0;
115
reg        [point_width:0] factor1;
116
reg        [point_width:0] factor2;
117
 
118
// State machine
119
reg [1:0] state;
120
parameter wait_state   = 2'b00,
121
          prep_state   = 2'b01,
122
          write_state  = 2'b10;
123
 
124
// Manage states
125
always @(posedge clk_i or posedge rst_i)
126
if(rst_i)
127
  state <= wait_state;
128
else
129
  case (state)
130
 
131
    wait_state:
132
      if(write_i)
133
        state <= prep_state;
134
 
135
    prep_state:
136
      state <= write_state;
137
 
138
    write_state:
139
      if(ack_i)
140
        state <= wait_state;
141
 
142
  endcase
143
 
144
// Interpolate texture coordinates, depth and alpha
145
wire [point_width*2-1:0] u = factor0 * u0_i + factor1 * u1_i + factor2 * u2_i;
146
wire [point_width*2-1:0] v = factor0 * v0_i + factor1 * v1_i + factor2 * v2_i;
147
wire [point_width+8-1:0] a = factor0 * a0_i + factor1 * a1_i + factor2 * a2_i;
148
 
149
// Note: special case here since z is signed. To prevent incorrect multiplication, factor is treated as a signed value (with a 0 added in front)
150
wire signed [point_width*2-1:0] z = $signed({1'b0, factor0}) * z0_i + $signed({1'b0, factor1}) * z1_i + $signed({1'b0, factor2}) * z2_i;
151
 
152
// REF: Loop & Blinn, for rendering quadratic bezier shapes
153
wire [point_width-1:0] bezier_factor0 = (factor1/2) + factor2;
154
wire [point_width-1:0] bezier_factor1 = factor2;
155
 
156
// ***************** //
157
// Interpolate color //
158
// ***************** //
159
 
160
// Split colors
161
wire [7:0] color0_r = (color_depth_i == 2'b00) ? color0_i[7:0] :
162
                      (color_depth_i == 2'b01) ? color0_i[15:11] :
163
                      color0_i[23:16];
164
wire [7:0] color0_g = (color_depth_i == 2'b00) ? color0_i[7:0] :
165
                      (color_depth_i == 2'b01) ? color0_i[10:5] :
166
                      color0_i[15:8];
167
wire [7:0] color0_b = (color_depth_i == 2'b00) ? color0_i[7:0] :
168
                      (color_depth_i == 2'b01) ? color0_i[4:0] :
169
                      color0_i[7:0];
170
 
171
// Split colors
172
wire [7:0] color1_r = (color_depth_i == 2'b00) ? color1_i[7:0] :
173
                      (color_depth_i == 2'b01) ? color1_i[15:11] :
174
                      color1_i[23:16];
175
wire [7:0] color1_g = (color_depth_i == 2'b00) ? color1_i[7:0] :
176
                      (color_depth_i == 2'b01) ? color1_i[10:5] :
177
                      color1_i[15:8];
178
wire [7:0] color1_b = (color_depth_i == 2'b00) ? color1_i[7:0] :
179
                      (color_depth_i == 2'b01) ? color1_i[4:0] :
180
                      color1_i[7:0];
181
 
182
// Split colors
183
wire [7:0] color2_r = (color_depth_i == 2'b00) ? color2_i[7:0] :
184
                      (color_depth_i == 2'b01) ? color2_i[15:11] :
185
                      color2_i[23:16];
186
wire [7:0] color2_g = (color_depth_i == 2'b00) ? color2_i[7:0] :
187
                      (color_depth_i == 2'b01) ? color2_i[10:5] :
188
                      color2_i[15:8];
189
wire [7:0] color2_b = (color_depth_i == 2'b00) ? color2_i[7:0] :
190
                      (color_depth_i == 2'b01) ? color2_i[4:0] :
191
                      color2_i[7:0];
192
 
193
// Interpolation
194
wire [8+point_width-1:0] color_r = factor0*color0_r +  factor1*color1_r +  factor2*color2_r;
195
wire [8+point_width-1:0] color_g = factor0*color0_g +  factor1*color1_g +  factor2*color2_g;
196
wire [8+point_width-1:0] color_b = factor0*color0_b +  factor1*color1_b +  factor2*color2_b;
197
 
198
always @(posedge clk_i or posedge rst_i)
199
begin
200
  // Reset
201
  if(rst_i)
202
  begin
203
    ack_o       <= 1'b0;
204
    write_o     <= 1'b0;
205
    x_o         <= 1'b0;
206
    y_o         <= 1'b0;
207
    color_o     <= 1'b0;
208
    z_o         <= 1'b0;
209
    u_o         <= 1'b0;
210
    v_o         <= 1'b0;
211
    a_o         <= 1'b0;
212
    factor0     <= 1'b0;
213
    factor1     <= 1'b0;
214
    factor2     <= 1'b0;
215
  end
216
  else
217
    case (state)
218
 
219
      wait_state:
220
      begin
221
        ack_o     <= 1'b0;
222
        if(write_i)
223
        begin
224
          x_o     <= x_i;
225
          y_o     <= y_i;
226
          factor0 <= factor0_i;
227
          factor1 <= factor1_i;
228
          factor2 <= (factor0_i + factor1_i >= (1 << point_width)) ? 1'b0 : (1 << point_width) - factor0_i - factor1_i;
229
        end
230
      end
231
 
232
      prep_state:
233
      begin
234
        // Assign outputs
235
        // --------------
236
        write_o <= 1'b1;
237
        // Texture coordinates
238
        u_o     <= u[point_width*2-1:point_width];
239
        v_o     <= v[point_width*2-1:point_width];
240
        // Bezier calculations
241
        bezier_factor0_o <= bezier_factor0;
242
        bezier_factor1_o <= bezier_factor1;
243
        // Depth
244
        z_o     <= z[point_width*2-1:point_width];
245
        // Alpha
246
        a_o     <= a[point_width+8-1:point_width];
247
        // Color
248
        color_o <= (color_depth_i == 2'b00) ? {color_r[8+point_width-1:point_width]} : // 8 bit grayscale
249
                   (color_depth_i == 2'b01) ? {color_r[5+point_width-1:point_width], color_g[6+point_width-1:point_width], color_b[5+point_width-1:point_width]} : // 16 bit
250
                   {color_r[8+point_width-1:point_width], color_g[8+point_width-1:point_width], color_b[8+point_width-1:point_width]}; // 32 bit
251
      end
252
 
253
      write_state:
254
      begin
255
        write_o   <= 1'b0;
256
        if(ack_i)
257
          ack_o   <= 1'b1;
258
      end
259
    endcase
260
 
261
end
262
 
263
endmodule
264
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.