| 1 |
6 |
Orka |
/*
|
| 2 |
|
|
ORSoC GFX accelerator core
|
| 3 |
|
|
Copyright 2012, ORSoC, Per Lenander, Anton Fosselius.
|
| 4 |
|
|
|
| 5 |
|
|
PER-PIXEL COLORING 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 adds color to the pixel generated by the rasterizer. It can either draw a flat color (using pixel_color_i) or
|
| 26 |
|
|
colors from a texture by using the u and v coordinates generated by the rasterizer.
|
| 27 |
|
|
*/
|
| 28 |
|
|
module gfx_fragment_processor(clk_i, rst_i,
|
| 29 |
|
|
pixel_alpha_i,
|
| 30 |
|
|
x_counter_i, y_counter_i, z_i, u_i, v_i, bezier_factor0_i, bezier_factor1_i, bezier_inside_i, write_i, curve_write_i, ack_o, // from raster
|
| 31 |
|
|
pixel_x_o, pixel_y_o, pixel_z_o, pixel_color_i, pixel_color_o, pixel_alpha_o, write_o, ack_i, // to blender
|
| 32 |
|
|
texture_ack_i, texture_data_i, texture_addr_o, texture_sel_o, texture_request_o, // to/from wishbone master read
|
| 33 |
|
|
texture_enable_i, tex0_base_i, tex0_size_x_i, tex0_size_y_i, color_depth_i, colorkey_enable_i, colorkey_i // from wishbone slave
|
| 34 |
|
|
);
|
| 35 |
|
|
|
| 36 |
|
|
parameter point_width = 16;
|
| 37 |
|
|
|
| 38 |
|
|
input clk_i;
|
| 39 |
|
|
input rst_i;
|
| 40 |
|
|
|
| 41 |
|
|
input [7:0] pixel_alpha_i;
|
| 42 |
|
|
|
| 43 |
|
|
// from raster
|
| 44 |
|
|
input [point_width-1:0] x_counter_i;
|
| 45 |
|
|
input [point_width-1:0] y_counter_i;
|
| 46 |
|
|
input signed [point_width-1:0] z_i;
|
| 47 |
|
|
input [point_width-1:0] u_i; // x-ish texture coordinate
|
| 48 |
|
|
input [point_width-1:0] v_i; // y-ish texture coordinate
|
| 49 |
|
|
input [point_width-1:0] bezier_factor0_i; // Used for curve writing
|
| 50 |
|
|
input [point_width-1:0] bezier_factor1_i; // Used for curve writing
|
| 51 |
|
|
input bezier_inside_i;
|
| 52 |
|
|
input [31:0] pixel_color_i;
|
| 53 |
|
|
input write_i;
|
| 54 |
|
|
input curve_write_i;
|
| 55 |
|
|
output reg ack_o;
|
| 56 |
|
|
|
| 57 |
|
|
//to render
|
| 58 |
|
|
output reg [point_width-1:0] pixel_x_o;
|
| 59 |
|
|
output reg [point_width-1:0] pixel_y_o;
|
| 60 |
|
|
output reg signed [point_width-1:0] pixel_z_o;
|
| 61 |
|
|
output reg [31:0] pixel_color_o;
|
| 62 |
|
|
output reg [7:0] pixel_alpha_o;
|
| 63 |
|
|
output reg write_o;
|
| 64 |
|
|
input ack_i;
|
| 65 |
|
|
|
| 66 |
|
|
// to/from wishbone master read
|
| 67 |
|
|
input texture_ack_i;
|
| 68 |
|
|
input [31:0] texture_data_i;
|
| 69 |
|
|
output [31:2] texture_addr_o;
|
| 70 |
|
|
output reg [ 3:0] texture_sel_o;
|
| 71 |
|
|
output reg texture_request_o;
|
| 72 |
|
|
|
| 73 |
|
|
// from wishbone slave
|
| 74 |
|
|
input texture_enable_i;
|
| 75 |
|
|
input [31:2] tex0_base_i;
|
| 76 |
|
|
input [point_width-1:0] tex0_size_x_i;
|
| 77 |
|
|
input [point_width-1:0] tex0_size_y_i;
|
| 78 |
|
|
input [ 1:0] color_depth_i;
|
| 79 |
|
|
input colorkey_enable_i;
|
| 80 |
|
|
input [31:0] colorkey_i;
|
| 81 |
|
|
|
| 82 |
|
|
wire [31:0] pixel_offset;
|
| 83 |
|
|
|
| 84 |
|
|
// Calculate the memory address of the texel to read
|
| 85 |
|
|
assign pixel_offset = (color_depth_i == 2'b00) ? (tex0_size_x_i*v_i + {16'h0, u_i}) : // 8 bit
|
| 86 |
|
|
(color_depth_i == 2'b01) ? (tex0_size_x_i*v_i + {16'h0, u_i}) << 1 : // 16 bit
|
| 87 |
|
|
(tex0_size_x_i*v_i + {16'h0, u_i}) << 2 ; // 32 bit
|
| 88 |
|
|
assign texture_addr_o = tex0_base_i + pixel_offset[31:2];
|
| 89 |
|
|
|
| 90 |
|
|
// State machine
|
| 91 |
|
|
reg [1:0] state;
|
| 92 |
|
|
parameter wait_state = 2'b00, texture_read_state = 2'b01, write_pixel_state = 2'b10;
|
| 93 |
|
|
|
| 94 |
|
|
wire [31:0] mem_conv_color_o;
|
| 95 |
|
|
|
| 96 |
|
|
// Color converter
|
| 97 |
|
|
memory_to_color color_proc(
|
| 98 |
|
|
.color_depth_i (color_depth_i),
|
| 99 |
|
|
.mem_i (texture_data_i),
|
| 100 |
|
|
.mem_lsb_i (u_i[1:0]),
|
| 101 |
|
|
.color_o (mem_conv_color_o),
|
| 102 |
|
|
.sel_o ()
|
| 103 |
|
|
);
|
| 104 |
|
|
|
| 105 |
|
|
// Does the fetched texel match the colorkey?
|
| 106 |
|
|
wire transparent_pixel = (color_depth_i == 2'b00) ? (mem_conv_color_o[7:0] == colorkey_i[7:0]) : // 8 bit
|
| 107 |
|
|
(color_depth_i == 2'b01) ? (mem_conv_color_o[15:0] == colorkey_i[15:0]) : // 16 bit
|
| 108 |
|
|
(mem_conv_color_o == colorkey_i); // 32 bit
|
| 109 |
|
|
|
| 110 |
|
|
// These variables are used when rendering bezier shapes. If bezier_draw is true, pixel is drawn, if it is false, pixel is discarded.
|
| 111 |
|
|
// These variables are only used if curve_write_i is high
|
| 112 |
|
|
|
| 113 |
|
|
// Calculate if factor0*factor0 > factor1
|
| 114 |
|
|
// Values are in the range [0..1], represented by a [point_width-1:0] bit array
|
| 115 |
|
|
wire [2*point_width-1:0] bezier_factor0_squared = bezier_factor0_i*bezier_factor0_i;
|
| 116 |
|
|
wire bezier_eval = bezier_factor0_squared[2*point_width-1:point_width] > bezier_factor1_i;
|
| 117 |
|
|
wire bezier_draw = bezier_inside_i ^ bezier_eval; // inside xor eval
|
| 118 |
|
|
|
| 119 |
|
|
// Acknowledge when a command has completed
|
| 120 |
|
|
always @(posedge clk_i or posedge rst_i)
|
| 121 |
|
|
begin
|
| 122 |
|
|
// reset, init component
|
| 123 |
|
|
if(rst_i)
|
| 124 |
|
|
begin
|
| 125 |
|
|
ack_o <= 1'b0;
|
| 126 |
|
|
write_o <= 1'b0;
|
| 127 |
|
|
pixel_x_o <= 1'b0;
|
| 128 |
|
|
pixel_y_o <= 1'b0;
|
| 129 |
|
|
pixel_z_o <= 1'b0;
|
| 130 |
|
|
pixel_color_o <= 1'b0;
|
| 131 |
|
|
pixel_alpha_o <= 1'b0;
|
| 132 |
|
|
texture_request_o <= 1'b0;
|
| 133 |
|
|
texture_sel_o <= 4'b1111;
|
| 134 |
|
|
end
|
| 135 |
|
|
// Else, set outputs for next cycle
|
| 136 |
|
|
else
|
| 137 |
|
|
begin
|
| 138 |
|
|
case (state)
|
| 139 |
|
|
|
| 140 |
|
|
wait_state:
|
| 141 |
|
|
begin
|
| 142 |
|
|
ack_o <= write_i & curve_write_i & ~bezier_draw;
|
| 143 |
|
|
|
| 144 |
|
|
if(write_i & texture_enable_i & (~curve_write_i | bezier_draw))
|
| 145 |
|
|
texture_request_o <= 1'b1;
|
| 146 |
|
|
else if(write_i & (~curve_write_i | bezier_draw))
|
| 147 |
|
|
begin
|
| 148 |
|
|
pixel_x_o <= x_counter_i;
|
| 149 |
|
|
pixel_y_o <= y_counter_i;
|
| 150 |
|
|
pixel_z_o <= z_i;
|
| 151 |
|
|
pixel_color_o <= pixel_color_i;
|
| 152 |
|
|
pixel_alpha_o <= pixel_alpha_i;
|
| 153 |
|
|
write_o <= 1'b1; // Note, colorkey only supported for texture reads
|
| 154 |
|
|
end
|
| 155 |
|
|
end
|
| 156 |
|
|
|
| 157 |
|
|
|
| 158 |
|
|
texture_read_state:
|
| 159 |
|
|
if(texture_ack_i)
|
| 160 |
|
|
begin
|
| 161 |
|
|
pixel_x_o <= x_counter_i;
|
| 162 |
|
|
pixel_y_o <= y_counter_i;
|
| 163 |
|
|
pixel_z_o <= z_i;
|
| 164 |
|
|
pixel_color_o <= mem_conv_color_o;
|
| 165 |
|
|
pixel_alpha_o <= pixel_alpha_i;
|
| 166 |
|
|
texture_request_o <= 1'b0;
|
| 167 |
|
|
if(colorkey_enable_i & transparent_pixel)
|
| 168 |
|
|
ack_o <= 1'b1; // Colorkey enabled: Only write if the pixel doesn't match the colorkey
|
| 169 |
|
|
else
|
| 170 |
|
|
write_o <= 1'b1;
|
| 171 |
|
|
end
|
| 172 |
|
|
|
| 173 |
|
|
|
| 174 |
|
|
write_pixel_state:
|
| 175 |
|
|
begin
|
| 176 |
|
|
write_o <= 1'b0;
|
| 177 |
|
|
ack_o <= ack_i;
|
| 178 |
|
|
end
|
| 179 |
|
|
|
| 180 |
|
|
endcase
|
| 181 |
|
|
end
|
| 182 |
|
|
end
|
| 183 |
|
|
|
| 184 |
|
|
// State machine
|
| 185 |
|
|
always @(posedge clk_i or posedge rst_i)
|
| 186 |
|
|
begin
|
| 187 |
|
|
// reset, init component
|
| 188 |
|
|
if(rst_i)
|
| 189 |
|
|
state <= wait_state;
|
| 190 |
|
|
// Move in statemachine
|
| 191 |
|
|
else
|
| 192 |
|
|
case (state)
|
| 193 |
|
|
|
| 194 |
|
|
wait_state:
|
| 195 |
|
|
if(write_i & texture_enable_i & (~curve_write_i | bezier_draw))
|
| 196 |
|
|
state <= texture_read_state;
|
| 197 |
|
|
else if(write_i & (~curve_write_i | bezier_draw))
|
| 198 |
|
|
state <= write_pixel_state;
|
| 199 |
|
|
|
| 200 |
|
|
texture_read_state:
|
| 201 |
|
|
// Check for texture ack. If we have colorkeying enabled, only goto the write state if the texture doesn't match the colorkey
|
| 202 |
|
|
if(texture_ack_i & colorkey_enable_i)
|
| 203 |
|
|
state <= transparent_pixel ? wait_state : write_pixel_state;
|
| 204 |
|
|
else if(texture_ack_i)
|
| 205 |
|
|
state <= write_pixel_state;
|
| 206 |
|
|
|
| 207 |
|
|
write_pixel_state:
|
| 208 |
|
|
if(ack_i)
|
| 209 |
|
|
state <= wait_state;
|
| 210 |
|
|
|
| 211 |
|
|
endcase
|
| 212 |
|
|
end
|
| 213 |
|
|
|
| 214 |
|
|
endmodule
|
| 215 |
|
|
|