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

Subversion Repositories orsoc_graphics_accelerator

[/] [orsoc_graphics_accelerator/] [tags/] [version1.0/] [rtl/] [verilog/] [gfx/] [gfx_blender.v] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 maiden
/*
2
ORSoC GFX accelerator core
3
Copyright 2012, ORSoC, Per Lenander, Anton Fosselius.
4
 
5
PER-PIXEL COLORING MODULE, alpha blending
6
 
7
 
8
 This file is part of orgfx.
9
 
10
 orgfx is free software: you can redistribute it and/or modify
11
 it under the terms of the GNU Lesser General Public License as published by
12
 the Free Software Foundation, either version 3 of the License, or
13
 (at your option) any later version.
14
 
15
 orgfx is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU Lesser General Public License for more details.
19
 
20
 You should have received a copy of the GNU Lesser General Public License
21
 along with orgfx.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
/*
26
This module performs alpha blending by fetching the pixel from the target and mixing it with the texel based on the current alpha value.
27
 
28
The exact formula is:
29
color_out = color_in * alpha + color_target * (1-alpha)       , where alpha is defined from 0 to 1
30
 
31
alpha_i[7:0] is used, so the actual span is 0 (transparent) to 255 (opaque)
32
 
33
If alpha blending is disabled (blending_enable_i == 1'b0) the module just passes on the input pixel.
34
*/
35
module gfx_blender(clk_i, rst_i,
36
  blending_enable_i, target_base_i, target_size_x_i, target_size_y_i, color_depth_i,
37
  x_counter_i, y_counter_i, alpha_i, write_i, ack_o,                                      // from fragment
38
  target_ack_i, target_addr_o, target_data_i, target_sel_o, target_request_o, wbm_busy_i, // from/to wbm reader
39
  pixel_x_o, pixel_y_o, pixel_color_i, pixel_color_o, write_o, ack_i                      // to render
40
  );
41
 
42
input clk_i;
43
input rst_i;
44
 
45
input blending_enable_i;
46
input [31:2] target_base_i;
47
input [15:0] target_size_x_i;
48
input [15:0] target_size_y_i;
49
input [1:0] color_depth_i;
50
 
51
// from fragment
52
input [15:0] x_counter_i;
53
input [15:0] y_counter_i;
54
input [7:0] alpha_i;
55
input [31:0] pixel_color_i;
56
input write_i;
57
output reg ack_o;
58
 
59
// Interface against wishbone master (reader)
60
input target_ack_i;
61
output [31:2] target_addr_o;
62
input [31:0] target_data_i;
63
output reg [3:0] target_sel_o;
64
output reg target_request_o;
65
input wbm_busy_i;
66
 
67
//to render
68
output reg [15:0] pixel_x_o;
69
output reg [15:0] pixel_y_o;
70
output reg [31:0] pixel_color_o;
71
output reg write_o;
72
input ack_i;
73
 
74
// State machine
75
reg [1:0] state;
76
parameter wait_state = 2'b00, target_read_state = 2'b01, write_pixel_state = 2'b10;
77
 
78
// Calculate address of target pixel
79
// Addr[31:2] = Base + (Y*width + X) * ppb
80
wire [31:0] pixel_offset;
81
assign pixel_offset = (color_depth_i == 2'b00) ? (target_size_x_i*y_counter_i + {16'h0, x_counter_i})      : // 8  bit
82
                      (color_depth_i == 2'b01) ? (target_size_x_i*y_counter_i + {16'h0, x_counter_i}) << 1 : // 16 bit
83
                      (target_size_x_i*y_counter_i + {16'h0, x_counter_i})                            << 2 ; // 32 bit
84
assign target_addr_o = target_base_i + pixel_offset[31:2];
85
 
86
// Split colors for alpha blending (render color)
87
wire [15:0] blend_color_r = (color_depth_i == 2'b00) ? pixel_color_i[7:0] :
88
                            (color_depth_i == 2'b01) ? pixel_color_i[15:11] :
89
                            pixel_color_i[23:16];
90
wire [15:0] blend_color_g = (color_depth_i == 2'b00) ? pixel_color_i[7:0] :
91
                            (color_depth_i == 2'b01) ? pixel_color_i[10:5] :
92
                            pixel_color_i[15:8];
93
wire [15:0] blend_color_b = (color_depth_i == 2'b00) ? pixel_color_i[7:0] :
94
                            (color_depth_i == 2'b01) ? pixel_color_i[4:0] :
95
                            pixel_color_i[7:0];
96
 
97
// Split colors for alpha blending (from target surface)
98
wire [15:0] target_color_r = (color_depth_i == 2'b00) ? dest_color[7:0] :
99
                             (color_depth_i == 2'b01) ? dest_color[15:11] :
100
                             target_data_i[23:16];
101
wire [15:0] target_color_g = (color_depth_i == 2'b00) ? dest_color[7:0] :
102
                             (color_depth_i == 2'b01) ? dest_color[10:5] :
103
                             target_data_i[15:8];
104
wire [15:0] target_color_b = (color_depth_i == 2'b00) ? dest_color[7:0] :
105
                             (color_depth_i == 2'b01) ? dest_color[4:0] :
106
                             target_data_i[7:0];
107
 
108
// Alpha blending (per color channel):
109
// rgb = (alpha1)(rgb1) + (1-alpha1)(rgb2)
110
wire [15:0] alpha_color_r = blend_color_r * alpha_i + target_color_r * (8'hff - alpha_i);
111
wire [15:0] alpha_color_g = blend_color_g * alpha_i + target_color_g * (8'hff - alpha_i);
112
wire [15:0] alpha_color_b = blend_color_b * alpha_i + target_color_b * (8'hff - alpha_i);
113
 
114
wire [31:0] dest_color;
115
// Memory to color converter
116
memory_to_color memory_proc(
117
.color_depth_i (color_depth_i),
118
.mem_i (target_data_i),
119
.mem_lsb_i (x_counter_i[1:0]),
120
.color_o (dest_color),
121
.sel_o ()
122
);
123
 
124
// Acknowledge when a command has completed
125
always @(posedge clk_i or posedge rst_i)
126
begin
127
  // reset, init component
128
  if(rst_i)
129
  begin
130
    ack_o            <= 1'b0;
131
    write_o          <= 1'b0;
132
    pixel_x_o        <= 1'b0;
133
    pixel_y_o        <= 1'b0;
134
    pixel_color_o    <= 1'b0;
135
    target_request_o <= 1'b0;
136
    target_sel_o     <= 4'b1111;
137
  end
138
  // Else, set outputs for next cycle
139
  else
140
  begin
141
    case (state)
142
 
143
      wait_state:
144
      begin
145
        ack_o <= 1'b0;
146
 
147
        if(write_i)
148
        begin
149
          if(!blending_enable_i)
150
          begin
151
            pixel_x_o     <= x_counter_i;
152
            pixel_y_o     <= y_counter_i;
153
            pixel_color_o <= pixel_color_i;
154
            write_o       <= 1'b1;
155
          end
156
          else
157
            target_request_o <= !wbm_busy_i;
158
        end
159
      end
160
 
161
 
162
      target_read_state:
163
        if(target_ack_i)
164
        begin
165
          write_o          <= 1'b1;
166
          pixel_x_o        <= x_counter_i;
167
          pixel_y_o        <= y_counter_i;
168
          target_request_o <= 1'b0;
169
 
170
          // Recombine colors
171
          pixel_color_o    <= (color_depth_i == 2'b00) ? {alpha_color_r[15:8]} : // 8 bit grayscale
172
                              (color_depth_i == 2'b01) ? {alpha_color_r[12:8], alpha_color_g[13:8], alpha_color_b[12:8]} : // 16 bit
173
                              {alpha_color_r[15:8], alpha_color_g[15:8], alpha_color_b[15:8]}; // 32 bit
174
        end
175
        else
176
          target_request_o <= !wbm_busy_i | target_request_o;
177
 
178
 
179
      write_pixel_state:
180
      begin
181
        write_o <= 1'b0;
182
        if(ack_i)
183
          ack_o <= 1'b1;
184
      end
185
 
186
    endcase
187
  end
188
end
189
 
190
// State machine
191
always @(posedge clk_i or posedge rst_i)
192
begin
193
  // reset, init component
194
  if(rst_i)
195
    state <= wait_state;
196
  // Move in statemachine
197
  else
198
    case (state)
199
 
200
      wait_state:
201
        if(write_i & blending_enable_i)
202
          state <= target_read_state;
203
        else if(write_i)
204
          state <= write_pixel_state;
205
 
206
      target_read_state:
207
        if(target_ack_i)
208
          state <= write_pixel_state;
209
 
210
      write_pixel_state:
211
        if(ack_i)
212
          state <= wait_state;
213
 
214
    endcase
215
end
216
 
217
endmodule
218
 

powered by: WebSVN 2.1.0

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