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_rasterizer.v] - Blame information for rev 6

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
RASTERIZER MODULE
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 takes a rect write or line write request and generates pixels in the span given by dest_pixel0-1.
26
Triangles can be declared by dest_pixel0-2, they are handled by the triangle module
27
 
28
The operation is clipped to the span given by clip_pixel0-1. Pixels outside the span are discarded.
29
 
30
If texturing is enabled, texture coordinates u (horizontal) and v (vertical) are emitted, offset and clipped by the span given by src_pixel0-1.
31
 
32
When all pixels have been generated and acked, the rasterizer acks the operation and returns to wait state.
33
*/
34
module gfx_rasterizer(clk_i, rst_i,
35
  clip_ack_i, interp_ack_i, ack_o,
36
  rect_write_i, line_write_i, triangle_write_i, interpolate_i, texture_enable_i,
37
  //source pixel 0 and pixel 1
38
  src_pixel0_x_i, src_pixel0_y_i, src_pixel1_x_i, src_pixel1_y_i,
39
  //destination point 0, 1, 2
40
  dest_pixel0_x_i, dest_pixel0_y_i,
41
  dest_pixel1_x_i, dest_pixel1_y_i,
42
  dest_pixel2_x_i, dest_pixel2_y_i,
43
  clipping_enable_i,
44
  //clip pixel 0 and pixel 1
45
  clip_pixel0_x_i, clip_pixel0_y_i, clip_pixel1_x_i, clip_pixel1_y_i,
46
  target_size_x_i, target_size_y_i,
47
  x_counter_o, y_counter_o, u_o,v_o,
48
  clip_write_o, interp_write_o,
49
  triangle_edge0_o, triangle_edge1_o, triangle_area_o
50
  );
51
 
52
parameter point_width    = 16;
53
parameter subpixel_width = 16;
54
parameter delay_width    = 5;
55
 
56
input clk_i;
57
input rst_i;
58
 
59
input clip_ack_i;
60
input interp_ack_i;
61
output reg ack_o;
62
 
63
input rect_write_i;
64
input line_write_i;
65
input triangle_write_i;
66
input interpolate_i;
67
input texture_enable_i;
68
 
69
//src pixels
70
input [point_width-1:0] src_pixel0_x_i;
71
input [point_width-1:0] src_pixel0_y_i;
72
input [point_width-1:0] src_pixel1_x_i;
73
input [point_width-1:0] src_pixel1_y_i;
74
 
75
//dest pixels
76
input signed [point_width-1:-subpixel_width] dest_pixel0_x_i;
77
input signed [point_width-1:-subpixel_width] dest_pixel0_y_i;
78
input signed [point_width-1:-subpixel_width] dest_pixel1_x_i;
79
input signed [point_width-1:-subpixel_width] dest_pixel1_y_i;
80
input signed [point_width-1:-subpixel_width] dest_pixel2_x_i;
81
input signed [point_width-1:-subpixel_width] dest_pixel2_y_i;
82
 
83
wire signed [point_width-1:0] p0_x = $signed(dest_pixel0_x_i[point_width-1:0]);
84
wire signed [point_width-1:0] p0_y = $signed(dest_pixel0_y_i[point_width-1:0]);
85
wire signed [point_width-1:0] p1_x = $signed(dest_pixel1_x_i[point_width-1:0]);
86
wire signed [point_width-1:0] p1_y = $signed(dest_pixel1_y_i[point_width-1:0]);
87
 
88
//clip pixels
89
input                   clipping_enable_i;
90
input [point_width-1:0] clip_pixel0_x_i;
91
input [point_width-1:0] clip_pixel0_y_i;
92
input [point_width-1:0] clip_pixel1_x_i;
93
input [point_width-1:0] clip_pixel1_y_i;
94
 
95
input [point_width-1:0] target_size_x_i;
96
input [point_width-1:0] target_size_y_i;
97
 
98
// Generated pixel coordinates
99
output reg [point_width-1:0] x_counter_o;
100
output reg [point_width-1:0] y_counter_o;
101
// Generated texture coordinates
102
output reg [point_width-1:0] u_o;
103
output reg [point_width-1:0] v_o;
104
// Write pixel output signals
105
output reg                   clip_write_o;
106
output reg                   interp_write_o;
107
 
108
output [2*point_width-1:0] triangle_edge0_o;
109
output [2*point_width-1:0] triangle_edge1_o;
110
output [2*point_width-1:0] triangle_area_o;
111
 
112
wire ack_i = interpolate_i ? interp_ack_i : clip_ack_i;
113
 
114
// Variables used in rect drawing
115
reg [point_width-1:0] rect_p0_x;
116
reg [point_width-1:0] rect_p0_y;
117
reg [point_width-1:0] rect_p1_x;
118
reg [point_width-1:0] rect_p1_y;
119
 
120
//line drawing reg & wires
121
wire raster_line_busy; // busy, drawing a line.
122
wire x_major; // is true if x is the major axis
123
wire valid_out;
124
reg  draw_line; // trigger the line drawing.
125
 
126
wire [point_width-1:0] major_out; // the major axis
127
wire [point_width-1:0] minor_out; // the minor axis
128
wire                   request_next_pixel;
129
 
130
// State machine
131
reg [2:0] state;
132
parameter wait_state           = 3'b000,
133
          rect_state           = 3'b001,
134
          line_state           = 3'b010,
135
          triangle_state       = 3'b011,
136
          triangle_final_state = 3'b100;
137
 
138
// Write/ack counter
139
reg [delay_width-1:0] ack_counter;
140
 
141
always @(posedge clk_i or posedge rst_i)
142
if(rst_i)
143
  ack_counter <= 1'b0;
144
else if(interpolate_i & ack_i & ~triangle_write_o)
145
  ack_counter <= ack_counter - 1'b1;
146
else if(interpolate_i & triangle_write_o & ~ack_i)
147
  ack_counter <= ack_counter + 1'b1;
148
 
149
// Rect drawing variables
150
always @(posedge clk_i or posedge rst_i)
151
begin
152
  if(rst_i)
153
  begin
154
    rect_p0_x <= 1'b0;
155
    rect_p0_y <= 1'b0;
156
    rect_p1_x <= 1'b0;
157
    rect_p1_y <= 1'b0;
158
  end
159
  else
160
  begin
161
    if(clipping_enable_i)
162
    begin
163
    // pixel0 x
164
    if(p0_x < $signed(clip_pixel0_x_i)) // check if pixel is left of screen
165
      rect_p0_x <= clip_pixel0_x_i;
166
    else if(p0_x > $signed(clip_pixel1_x_i)) // check if pixel is right of screen
167
      rect_p0_x <= clip_pixel1_x_i;
168
    else
169
      rect_p0_x <= p0_x;
170
 
171
    // pixel0 y
172
    if(p0_y < $signed(clip_pixel0_y_i)) // check if pixel is above the screen
173
      rect_p0_y <= clip_pixel0_y_i;
174
    else if(p0_y > $signed(clip_pixel1_y_i)) // check if pixel is below the screen
175
      rect_p0_y <= clip_pixel1_y_i;
176
    else
177
      rect_p0_y <= p0_y;
178
 
179
    // pixel1 x
180
    if(p1_x < $signed(clip_pixel0_x_i)) // check if pixel is left of screen
181
      rect_p1_x <= clip_pixel0_x_i;
182
    else if(p1_x > $signed(clip_pixel1_x_i -1)) // check if pixel is right of screen
183
      rect_p1_x <= clip_pixel1_x_i -1'b1;
184
    else
185
      rect_p1_x <= dest_pixel1_x_i[point_width-1:0] - 1;
186
 
187
    // pixel1 y
188
    if(p1_y < $signed(clip_pixel0_y_i)) // check if pixel is above the screen
189
      rect_p1_y <= clip_pixel0_y_i;
190
    else if(p1_y > $signed(clip_pixel1_y_i -1)) // check if pixel is below the screen
191
      rect_p1_y <= clip_pixel1_y_i -1'b1;
192
    else
193
      rect_p1_y <= p1_y - 1;
194
    end
195
    else
196
    begin
197
      rect_p0_x <= p0_x       >= 0 ? p0_x     : 1'b0;
198
      rect_p0_y <= p0_y       >= 0 ? p0_y     : 1'b0;
199
      rect_p1_x <= (p1_x - 1) >= 0 ? p1_x - 1 : 1'b0;
200
      rect_p1_y <= (p1_y - 1) >= 0 ? p1_y - 1 : 1'b0;
201
    end
202
  end
203
end
204
 
205
// Checks if the current line in the rect being drawn is complete
206
wire raster_rect_line_done = (x_counter_o >= rect_p1_x) | (texture_enable_i && (u_o >= src_pixel1_x_i-1));
207
// Checks if the current rect is completely drawn
208
// TODO: ugly fix to prevent the an extra pixel being written when texturing is enabled. Ugly.
209
//wire raster_rect_done = ack_i & (x_counter_o >= rect_p1_x) & ((y_counter_o >= rect_p1_y) | (texture_enable_i && (v_o >= src_pixel1_y_i-1)));
210
wire raster_rect_done = (ack_i | texture_enable_i) &
211
                        (x_counter_o >= rect_p1_x) &
212
                        ((y_counter_o >= rect_p1_y) | (texture_enable_i && (v_o >= src_pixel1_y_i-1)));
213
// Special check if there are no pixels to draw at all (send ack immediately)
214
wire empty_raster = (rect_p0_x > rect_p1_x) | (rect_p0_y > rect_p1_y);
215
 
216
wire triangle_finished = ~interpolate_i | (ack_counter == 1'b0);
217
 
218
// Manage states
219
always @(posedge clk_i or posedge rst_i)
220
if(rst_i)
221
  state <= wait_state;
222
else
223
  case (state)
224
 
225
    wait_state:
226
      if(triangle_write_i)
227
        state <= triangle_state;
228
      else if(rect_write_i & !empty_raster) // if request for drawing a rect, go to rect drawing state
229
        state <= rect_state;
230
      else if(line_write_i)
231
        state <= line_state; // if request for drawing a line, go to line drawing state
232
 
233
    rect_state:
234
      if(raster_rect_done) // if we are done drawing a rect, go to wait state
235
        state <= wait_state;
236
 
237
    line_state:
238
      if(!raster_line_busy & !draw_line)  // if we are done drawing a line, go to wait state
239
        state <= wait_state;
240
 
241
    triangle_state:
242
      if(triangle_ack & triangle_finished)
243
        state <= wait_state;
244
      else if(triangle_ack)
245
        state <= triangle_final_state;
246
 
247
    triangle_final_state:
248
      if(triangle_finished)
249
        state <= wait_state;
250
 
251
  endcase
252
 
253
// If interpolation is active, only write to interp module if queue is not full. 
254
wire interp_ready   = interpolate_i ? (ack_counter <= point_width)   & ~interp_write_o : ack_i;
255
wire triangle_ready = interpolate_i ? (ack_counter <= point_width-1) & ~interp_write_o : ack_i;
256
 
257
// Manage outputs (mealy machine)
258
always @(posedge clk_i or posedge rst_i)
259
begin
260
  // Reset
261
  if(rst_i)
262
  begin
263
    ack_o          <= 1'b0;
264
    x_counter_o    <= 1'b0;
265
    y_counter_o    <= 1'b0;
266
    clip_write_o   <= 1'b0;
267
    interp_write_o <= 1'b0;
268
    u_o            <= 1'b0;
269
    v_o            <= 1'b0;
270
 
271
    //reset line regs
272
    draw_line      <= 1'b0;
273
  end
274
  else
275
  begin
276
    case (state)
277
 
278
      // Wait for incoming instructions
279
      wait_state:
280
 
281
        if(rect_write_i & !empty_raster) // Start a raster rectangle operation
282
        begin
283
          ack_o        <= 1'b0;
284
          clip_write_o <= 1'b1;
285
          // Generate pixel coordinates
286
          x_counter_o  <= rect_p0_x;
287
          y_counter_o  <= rect_p0_y;
288
          // Generate texture coordinates
289
          u_o          <= (($signed(clip_pixel0_x_i) < p0_x) ? 1'b0 :
290
                            $signed(clip_pixel0_x_i) - p0_x) + src_pixel0_x_i;
291
          v_o          <= (($signed(clip_pixel0_y_i) < p0_y) ? 1'b0 :
292
                            $signed(clip_pixel0_y_i) - p0_y) + src_pixel0_y_i;
293
        end
294
 
295
        else if(rect_write_i & empty_raster & !ack_o) // Start a raster rectangle operation
296
          ack_o     <= 1'b1;
297
 
298
        // Start a raster line operation
299
        else if(line_write_i)
300
        begin
301
          draw_line <= 1'b1;
302
          ack_o     <= 1'b0;
303
        end
304
 
305
        else
306
          ack_o     <= 1'b0;
307
 
308
 
309
      // Rasterize a rectangle between p0 and p1 (rasterize = generate the pixels)
310
      rect_state:
311
      begin
312
        if(ack_i) // If our last coordinate was acknowledged by a fragment processor
313
        begin
314
          if(raster_rect_line_done) // iterate through width of rect
315
          begin
316
            x_counter_o <= rect_p0_x;
317
            y_counter_o <= y_counter_o + 1'b1;
318
            u_o         <= ($signed(clip_pixel0_x_i) < p0_x ? 1'b0 :
319
                            $signed(clip_pixel0_x_i) - p0_x) + $signed(src_pixel0_x_i);
320
            v_o         <= v_o + 1'b1;
321
          end
322
          else
323
          begin
324
            x_counter_o <= x_counter_o + 1'b1;
325
            u_o         <= u_o + 1'b1;
326
          end
327
        end
328
 
329
        if(raster_rect_done) // iterate through height of rect (are we done?)
330
        begin
331
          clip_write_o <= 1'b0; // Only send ack when we get ack_i (see wait state)
332
          ack_o        <= 1'b1;
333
        end
334
      end
335
 
336
      // Rasterize a line between dest_pixel0 and dest_pixel1 (rasterize = generate the pixels)
337
      line_state:
338
      begin
339
        draw_line    <= 1'b0;
340
        clip_write_o <= raster_line_busy & valid_out;
341
        x_counter_o  <= x_major ? major_out : minor_out;
342
        y_counter_o  <= x_major ? minor_out : major_out;
343
        ack_o        <= !raster_line_busy & !draw_line;
344
      end
345
 
346
      triangle_state:
347
      if(triangle_ack)
348
      begin
349
        interp_write_o <= 1'b0;
350
        clip_write_o   <= 1'b0;
351
        if(triangle_finished)
352
          ack_o <= 1'b1;
353
      end
354
      else if(~interpolate_i)
355
      begin
356
        x_counter_o    <= triangle_x_o;
357
        y_counter_o    <= triangle_y_o;
358
        clip_write_o   <= triangle_write_o ;
359
      end
360
      else if(interpolate_i & interp_ready)
361
      begin
362
        x_counter_o    <= triangle_x_o;
363
        y_counter_o    <= triangle_y_o;
364
        // edge0, edge1 and area are set by the triangle module
365
        interp_write_o <= triangle_write_o;
366
      end
367
      else
368
      begin
369
        interp_write_o <= 1'b0;
370
        clip_write_o   <= 1'b0;
371
      end
372
 
373
      triangle_final_state:
374
        if(triangle_finished)
375
          ack_o <= 1'b1;
376
 
377
    endcase
378
  end
379
end
380
 
381
// Request wire for line drawing module
382
assign request_next_pixel = ack_i & raster_line_busy;
383
 
384
// Instansiation of bresenham line drawing module
385
bresenham_line bresenham(
386
.clk_i                  ( clk_i                ),  //clock
387
.rst_i                  ( rst_i                ),  //rest
388
.pixel0_x_i             ( dest_pixel0_x_i      ),  // left pixel x
389
.pixel0_y_i             ( dest_pixel0_y_i      ),  // left pixel y
390
.pixel1_x_i             ( dest_pixel1_x_i      ),  // right pixel x
391
.pixel1_y_i             ( dest_pixel1_y_i      ),  // right pixel y
392
.draw_line_i            ( draw_line            ), // trigger for drawing a line
393
.read_pixel_i           ( request_next_pixel   ), // request next pixel
394
.busy_o                 ( raster_line_busy     ), // is true while line is drawn
395
.x_major_o              ( x_major              ), // is true if x is the major axis
396
.major_o                ( major_out            ), // the major axis pixel coordinate
397
.minor_o                ( minor_out            ), // the minor axis pixel coordinate
398
.valid_o                ( valid_out            )
399
);
400
 
401
defparam bresenham.point_width = point_width;
402
defparam bresenham.subpixel_width = subpixel_width;
403
 
404
wire                   triangle_ack;
405
wire [point_width-1:0] triangle_x_o;
406
wire [point_width-1:0] triangle_y_o;
407
wire                   triangle_write_o;
408
 
409
// Triangle module instanciated
410
gfx_triangle triangle(
411
.clk_i                  (clk_i),
412
.rst_i                  (rst_i),
413
.ack_i                  (triangle_ready),
414
.ack_o                  (triangle_ack),
415
.triangle_write_i       (triangle_write_i),
416
.texture_enable_i       (texture_enable_i),
417
.dest_pixel0_x_i        (dest_pixel0_x_i),
418
.dest_pixel0_y_i        (dest_pixel0_y_i),
419
.dest_pixel1_x_i        (dest_pixel1_x_i),
420
.dest_pixel1_y_i        (dest_pixel1_y_i),
421
.dest_pixel2_x_i        (dest_pixel2_x_i),
422
.dest_pixel2_y_i        (dest_pixel2_y_i),
423
.x_counter_o            (triangle_x_o),
424
.y_counter_o            (triangle_y_o),
425
.triangle_edge0_o       (triangle_edge0_o),
426
.triangle_edge1_o       (triangle_edge1_o),
427
.triangle_area_o        (triangle_area_o),
428
.write_o                (triangle_write_o)
429
);
430
 
431
defparam triangle.point_width    = point_width;
432
defparam triangle.subpixel_width = subpixel_width;
433
 
434
endmodule
435
 

powered by: WebSVN 2.1.0

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