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

Subversion Repositories wf3d

[/] [wf3d/] [trunk/] [rtl/] [core/] [fm_geo_cull.v] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 specular
//=======================================================================
2
// Project Monophony
3
//   Wire-Frame 3D Graphics Accelerator IP Core
4
//
5
// File:
6
//   fm_geo_cull.v
7
//
8
// Abstract:
9
//   Back-face culling module
10
//
11
// Author:
12 9 specular
//   Kenji Ishimaru (info.info.wf3d@gmail.com)
13 2 specular
//
14
//======================================================================
15
//
16
// Copyright (c) 2015, Kenji Ishimaru
17
// All rights reserved.
18
//
19
// Redistribution and use in source and binary forms, with or without
20
// modification, are permitted provided that the following conditions are met:
21
//
22
//  -Redistributions of source code must retain the above copyright notice,
23
//   this list of conditions and the following disclaimer.
24
//  -Redistributions in binary form must reproduce the above copyright notice,
25
//   this list of conditions and the following disclaimer in the documentation
26
//   and/or other materials provided with the distribution.
27
//
28
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
35
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
38
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
//
40
// Revision History
41
 
42
`include "fm_3d_define.v"
43
module fm_geo_cull (
44
  // system
45
  input         clk_core,
46
  input         rst_x,
47
  // curring
48
  input         i_en_cull,
49
  input         i_ccw,
50
  output        o_state,
51
  // triangle input 
52
  input         i_en,
53
  output        o_ack,
54
  input  [`D3D_FTOI_WIDTH-1:0] i_v0_x,
55
  input  [`D3D_FTOI_WIDTH-1:0] i_v0_y,
56
  input  [`D3D_FTOI_WIDTH-1:0] i_v1_x,
57
  input  [`D3D_FTOI_WIDTH-1:0] i_v1_y,
58
  input  [`D3D_FTOI_WIDTH-1:0] i_v2_x,
59
  input  [`D3D_FTOI_WIDTH-1:0] i_v2_y,
60
  // triangle output
61
  output        o_en,
62
  input         i_ack,
63
  output reg [`D3D_FTOI_WIDTH-1:0] o_v0_x,
64
  output reg [`D3D_FTOI_WIDTH-1:0] o_v0_y,
65
  output reg [`D3D_FTOI_WIDTH-1:0] o_v1_x,
66
  output reg [`D3D_FTOI_WIDTH-1:0] o_v1_y,
67
  output reg [`D3D_FTOI_WIDTH-1:0] o_v2_x,
68
  output reg [`D3D_FTOI_WIDTH-1:0] o_v2_y
69
);
70
localparam P_IDLE = 'd0;
71
localparam P_SUM1 = 'd1;
72
localparam P_SUM2 = 'd2;
73
localparam P_OUT  = 'd3;
74
 
75
reg [1:0]  r_state;
76
reg [`D3D_FTOI_WIDTH+`D3D_FTOI_WIDTH-1:0] r_sum;
77
wire [`D3D_FTOI_WIDTH+`D3D_FTOI_WIDTH-1:0] w_sum;
78
wire [`D3D_FTOI_WIDTH-1:0] w_v0_x;
79
wire [`D3D_FTOI_WIDTH-1:0] w_v0_y;
80
wire [`D3D_FTOI_WIDTH-1:0] w_v1_x;
81
wire [`D3D_FTOI_WIDTH-1:0] w_v1_y;
82
wire w_cull;
83
wire w_set_tri;
84
// a = ((vx0vy1)-(vx1vy0)) +
85
//     ((vx1vy2)-(vx2vy1)) +
86
//     ((vx2vy0)-(vx0vy2))
87
assign w_v0_x = (r_state == P_SUM1) ? o_v1_x :
88
                (r_state == P_SUM2) ? o_v2_x :
89
                                      i_v0_x;
90
assign w_v0_y = (r_state == P_SUM1) ? o_v1_y :
91
                (r_state == P_SUM2) ? o_v2_y :
92
                                      i_v0_y;
93
assign w_v1_x = (r_state == P_SUM1) ? o_v2_x :
94
                (r_state == P_SUM2) ? o_v0_x :
95
                                      i_v1_x;
96
assign w_v1_y = (r_state == P_SUM1) ? o_v2_y :
97
                (r_state == P_SUM2) ? o_v0_y :
98
                                      i_v1_y;
99
assign w_cull = (i_ccw) ? r_sum[`D3D_FTOI_WIDTH+`D3D_FTOI_WIDTH-1] :
100
                         ~r_sum[`D3D_FTOI_WIDTH+`D3D_FTOI_WIDTH-1];
101
//assign w_cull = 0;
102
assign o_en = (i_en_cull) ? (r_state == P_OUT) & !w_cull : (r_state == P_OUT);
103
assign o_ack = (r_state == P_IDLE);
104
assign w_sum = f_multi(w_v0_x,w_v0_y,w_v1_x,w_v1_y);
105
assign w_set_tri = (r_state == P_IDLE) & i_en;
106
assign o_state = (r_state == P_IDLE);
107
 
108
function [`D3D_FTOI_WIDTH+`D3D_FTOI_WIDTH-1:0] f_multi;
109
  input [`D3D_FTOI_WIDTH-1:0] v0x;
110
  input [`D3D_FTOI_WIDTH-1:0] v0y;
111
  input [`D3D_FTOI_WIDTH-1:0] v1x;
112
  input [`D3D_FTOI_WIDTH-1:0] v1y;
113
  begin
114
    f_multi = v0x*v1y - v1x*v0y;
115
  end
116
endfunction
117
 
118
always @(posedge clk_core) begin
119
  if (w_set_tri) begin
120
    o_v0_x <= i_v0_x;
121
    o_v0_y <= i_v0_y;
122
    o_v1_x <= i_v1_x;
123
    o_v1_y <= i_v1_y;
124
    o_v2_x <= i_v2_x;
125
    o_v2_y <= i_v2_y;
126
  end
127
end
128
`ifdef D3D_SYNC_RESET
129
always @(posedge clk_core) begin
130
`else
131
always @(posedge clk_core or negedge rst_x) begin
132
`endif
133
  if (rst_x == `D3D_RESET_POL) begin
134
    r_state <= P_IDLE;
135
    r_sum <= 'd0;
136
  end else begin
137
    case (r_state)
138
      P_IDLE: begin
139
       if (i_en) begin
140
         if (i_en_cull) begin
141
           r_state <= P_SUM1;
142
           r_sum <= w_sum;
143
          end else begin
144
           r_state <= P_OUT;
145
          end
146
       end
147
      end
148
      P_SUM1: begin
149
       r_state <= P_SUM2;
150
       r_sum <= r_sum + w_sum;
151
      end
152
      P_SUM2: begin
153
       r_sum <= r_sum + w_sum;
154
       r_state <= P_OUT;
155
      end
156
      P_OUT:begin
157
        if (i_en_cull) begin
158
          if (!w_cull) begin
159
            if (i_ack) begin
160
              r_state <= P_IDLE;
161
              r_sum <= 'd0;
162
            end
163
          end else begin
164
            r_state <= P_IDLE;
165
            r_sum <= 'd0;
166
          end
167
        end else begin
168
          if (i_ack) begin
169
            r_state <= P_IDLE;
170
            r_sum <= 'd0;
171
          end
172
        end
173
      end
174
    endcase
175
  end
176
end
177
 
178
endmodule

powered by: WebSVN 2.1.0

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