📄 graphics_pipeline.v
字号:
.Out2(normal_out3[2]),
.det(det)
);
// camera
camera cam1(
.position_1(position),
.data({
u[0],
u[1],
u[2],
v[0],
v[1],
v[2],
w[0],
w[1],
w[2],
e[0],
e[1],
e[2],
det
})
);
// vertex 1
calcMv mv1 (
.clk(VGA_CTRL_CLK),
.reset(reset | changed_position | r_done),
.xu(u[0]),
.yu(u[1]),
.zu(u[2]),
.xv(v[0]),
.yv(v[1]),
.zv(v[2]),
.xw(w[0]),
.yw(w[1]),
.zw(w[2]),
.xe(e[0]),
.ye(e[1]),
.ze(e[2]),
.x(vertex1_out[0]),
.y(vertex1_out[1]),
.z(vertex1_out[2]),
.Out0(vertex1[0]),
.Out1(vertex1[1]),
.Out2(vertex1[2])
);
// vertex 2
calcMv mv2 (
.clk(VGA_CTRL_CLK),
.reset(reset | changed_position | r_done),
.xu(u[0]),
.yu(u[1]),
.zu(u[2]),
.xv(v[0]),
.yv(v[1]),
.zv(v[2]),
.xw(w[0]),
.yw(w[1]),
.zw(w[2]),
.xe(e[0]),
.ye(e[1]),
.ze(e[2]),
.x(vertex2_out[0]),
.y(vertex2_out[1]),
.z(vertex2_out[2]),
.Out0(vertex2[0]),
.Out1(vertex2[1]),
.Out2(vertex2[2])
);
// vertex 3
calcMv mv3 (
.clk(VGA_CTRL_CLK),
.reset(reset | changed_position | r_done),
.xu(u[0]),
.yu(u[1]),
.zu(u[2]),
.xv(v[0]),
.yv(v[1]),
.zv(v[2]),
.xw(w[0]),
.yw(w[1]),
.zw(w[2]),
.xe(e[0]),
.ye(e[1]),
.ze(e[2]),
.x(vertex3_out[0]),
.y(vertex3_out[1]),
.z(vertex3_out[2]),
.Out0(vertex3[0]),
.Out1(vertex3[1]),
.Out2(vertex3[2])
);
// screen coordinates
Transform t1 (
.vertex1_x_world(vertex1r[0]),
.vertex1_y_world(vertex1r[1]),
.vertex1_z_world(vertex1r[2]),
.vertex2_x_world(vertex2r[0]),
.vertex2_y_world(vertex2r[1]),
.vertex2_z_world(vertex2r[2]),
.vertex3_x_world(vertex3r[0]),
.vertex3_y_world(vertex3r[1]),
.vertex3_z_world(vertex3r[2]),
.vertex1_x_screen(vertex1_x),
.vertex1_y_screen(vertex1_y),
.z_canonical(vertex1_z),
.vertex2_x_screen(vertex2_x),
.vertex2_y_screen(vertex2_y),
.vertex3_x_screen(vertex3_x),
.vertex3_y_screen(vertex3_y)
);
//rasterizer//////////////////////////////////////////
//stage 3/////////////////////////////////////////////
Rasterizer r1 (
.clk(VGA_CTRL_CLK),
.reset(reset | changed_position | r_done | ~mv_done),
.stall(stall),
.x1(vertex1_x[8:0]),
.y1(vertex1_y[8:0]),
.x2(vertex2_x[8:0]),
.y2(vertex2_y[8:0]),
.x3(vertex3_x[8:0]),
.y3(vertex3_y[8:0]),
.f12(f01),
.f23(f12),
.f31(f20),
.pixelOut(valid_fragment),
.alpha(alpha),
.beta(beta),
.gamma(gamma),
.out_pixel_x(pixel_raster[0]),
.out_pixel_y(pixel_raster[1]),
.done(r_done)
);
///////////////////////////////////////////////////////////
/////z-buffer and shader state machine ////////////////////
/////stage 4///////////////////////////////////////////////
///////////////////////////////////////////////////////////
/////outputs from rasterizer///////////////////////////////
/////pass through here and are written to memory///////////
///////////////////////////////////////////////////////////
z_buffer z1 (
.address((reset | changed_position) ? {Coord_X[9:1], Coord_Y[8:1]} : {pixel_buffer[0][8:0], pixel_buffer[1][7:0]}),
.data((reset | changed_position) ? 5'b00000 : z_value[4:0]),
.clock(VGA_CTRL_CLK),
.wren(!((!reset) & (!changed_position) & (z_we))),
.q(current_z[4:0])
);
// final shaded color for this pixel
assign color_light = (nlresultr[17] == 0) ? 14'b0 : {intRsum[4:0], intGsum[3:0], intBsum[4:0]};
assign LEDR = light_out[2];
assign LEDG[8:0] = beta1[16:9];//normal_out3[0][16:9];//intBsum;
assign current_z[6:5] = SRAM_DQ[15:14];
// do all of the multiplications and additions to do interpolative shading between vertices
int2fp i1(floatR1, {5'b0,color_vertex1[13:9]}, 8'b0);
int2fp i2(floatG1, {6'b0,color_vertex1[8:5]}, 8'b0);
int2fp i3(floatB1, {5'b0,color_vertex1[4:0]}, 8'b0);
int2fp i4(floatR2, {5'b0,color_vertex2[13:9]}, 8'b0);
int2fp i5(floatG2, {6'b0,color_vertex2[8:5]}, 8'b0);
int2fp i6(floatB2, {5'b0,color_vertex2[4:0]}, 8'b0);
int2fp i7(floatR3, {5'b0,color_vertex3[13:9]}, 8'b0);
int2fp i8(floatG3, {6'b0,color_vertex3[8:5]}, 8'b0);
int2fp i9(floatB3, {5'b0,color_vertex3[4:0]}, 8'b0);
fpmult fmult1(floatR1alpha, {1'b0,floatR1[16:0]}, alpha1);
fpmult fmult2(floatG1alpha, {1'b0,floatG1[16:0]}, alpha1);
fpmult fmult3(floatB1alpha, {1'b0,floatB1[16:0]}, alpha1);
fpmult fmult4(floatR2beta, {1'b0,floatR2[16:0]}, beta1);
fpmult fmult5(floatG2beta, {1'b0,floatG2[16:0]}, beta1);
fpmult fmult6(floatB2beta, {1'b0,floatB2[16:0]}, beta1);
fpmult fmult7(floatR3gamma, {1'b0,floatR3[16:0]}, gamma1);
fpmult fmult8(floatG3gamma, {1'b0,floatG3[16:0]}, gamma1);
fpmult fmult9(floatB3gamma, {1'b0,floatB3[16:0]}, gamma1);
fpmult fmult16(nv1a ,normal_out1[0], alpha1);
fpmult fmult17(nv2a ,normal_out1[1], alpha1);
fpmult fmult18(nv3a ,normal_out1[2], alpha1);
fpmult fmult19(nv1b ,normal_out2[0], beta1);
fpmult fmult20(nv2b ,normal_out2[1], beta1);
fpmult fmult21(nv3b ,normal_out2[2], beta1);
fpmult fmult22(nv1g ,normal_out3[0], gamma1);
fpmult fmult23(nv2g ,normal_out3[1], gamma1);
fpmult fmult24(nv3g ,normal_out3[2], gamma1);
fpadd fadd9(nv1, nv1ar, nv1br);
fpadd fadd10(normal1 , nv1r, nv1gr);
fpadd fadd11(nv2, nv2ar, nv2br);
fpadd fadd12(normal2 , nv2r, nv2gr);
fpadd fadd13(nv3, nv3ar, nv3br);
fpadd fadd14(normal3 , nv3r, nv3gr);
fpmult fmult10(nl1, normal[0], light_out[0]>>2);
fpmult fmult11(nl2, normal[1], light_out[1]>>2);
fpmult fmult12(nl3, normal[2], light_out[2]>>2);
fpadd fadd7(nla, nl1r, nl2r);
fpadd fadd8(nlresult, nl3r, nlar);
fpadd fadd1(floatRpresum ,floatR1alpha1, floatR2beta1);
fpadd fadd2(floatRsum ,floatR3gamma1, floatRpresum1);
fpadd fadd3(floatGpresum ,floatG1alpha1, floatG2beta1);
fpadd fadd4(floatGsum ,floatG3gamma1, floatGpresum1);
fpadd fadd5(floatBpresum ,floatB1alpha1, floatB2beta1);
fpadd fadd6(floatBsum ,floatB3gamma1, floatBpresum1);
fpmult fmult13(finalR ,floatRsumr, /*18'b010000001100000000);*/nlresultr);
fpmult fmult14(finalG ,floatGsumr, /*18'b010000001100000000);*/nlresultr);
fpmult fmult15(finalB ,floatBsumr, /*18'b010000001100000000);*/nlresultr);
fp2int fp1(intRsum, finalR, 8'b0);
fp2int fp2(intGsum, finalG, 8'b0);
fp2int fp3(intBsum, finalB, 8'b0);
// state machine for above multipliers and adders
always @ (posedge VGA_CTRL_CLK) begin
if (reset) begin
//reset variables
z_state <= 4'd6;
z_value <= 7'b1000000;
z_we <= 1'b1;
nl1r <= 18'b0;
nl2r <= 18'b0;
nl3r <= 18'b0;
nlar <= 18'b0;
nlresultr <= 18'b0;
alpha1 <= 18'b0;
beta1 <= 18'b0;
gamma1 <= 18'b0;
floatR1alpha1 <= 18'b0;
floatG1alpha1 <= 18'b0;
floatB1alpha1 <= 18'b0;
floatR2beta1 <= 18'b0;
floatG2beta1 <= 18'b0;
floatB2beta1 <= 18'b0;
floatR3gamma1 <= 18'b0;
floatG3gamma1 <= 18'b0;
floatB3gamma1 <= 18'b0;
pixel_buffer[0] <= 10'b0;
pixel_buffer[1] <= 10'b0;
floatRsumr <= 18'b0;
floatGsumr <= 18'b0;
floatBsumr <= 18'b0;
floatRpresum1 <= 18'b0;
floatGpresum1 <= 18'b0;
floatBpresum1 <= 18'b0;
end
/* make sure rasterizer is done with current pixel */
else if(valid_fragment) begin
z_state <= 4'd0;
z_value <= z_value;
z_we <= 1'b1;
alpha1 <= alpha;
beta1 <= beta;
gamma1 <= gamma;
pixel_buffer[0] <= pixel_raster[0];
pixel_buffer[1] <= pixel_raster[1];
end
//modify display during sync
else if (!stall) begin //sync is active low
case(z_state)
0: begin
floatR1alpha1 <= floatR1alpha;
floatG1alpha1 <= floatG1alpha;
floatB1alpha1 <= floatB1alpha;
floatR2beta1 <= floatR2beta;
floatG2beta1 <= floatG2beta;
floatB2beta1 <= floatB2beta;
floatR3gamma1 <= floatR3gamma;
floatG3gamma1 <= floatG3gamma;
floatB3gamma1 <= floatB3gamma;
//sumr <= sum;
nv1ar <= nv1a;
nv2ar <= nv2a;
nv3ar <= nv3a;
nv1br <= nv1b;
nv2br <= nv2b;
nv3br <= nv3b;
nv1gr <= nv1g;
nv2gr <= nv2g;
nv3gr <= nv3g;
z_we <= 1'b1;
z_state <= 4'd1;
z_value <= z_value;
end
1: begin
floatRpresum1 <= floatRpresum;
floatGpresum1 <= floatGpresum;
floatBpresum1 <= floatBpresum;
nv1r <= nv1;
nv2r <= nv2;
nv3r <= nv3;
z_we <= 1'b1;
z_state <= 4'd2;
z_value <= z_value;
end
2: begin //compare previous value
normal[0] <= normal1;
normal[1] <= normal2;
normal[2] <= normal3;
floatRsumr <= floatRsum;
floatGsumr <= floatGsum;
floatBsumr <= floatBsum;
z_state <= 4'd3;
end
3: //notify completion and sit here while other stages are completing
begin
nl1r <= nl1;
nl2r <= nl2;
nl3r <= nl3;
z_state <= 4'd4;
z_we <= 1'b1;
z_value <= z_value;
end
4:
begin
nlar <= nla;
z_state <= 4'd5;
end
5:
begin
nlresultr <= nlresult;
z_state <= 4'd6;
end
6:
begin
if(vertex1_z[7:1] > current_z) begin
z_value <= vertex1_z[7:1];
z_we <= 1'b0; //memory write
end
else begin
z_value <= 7'b1000000;
z_we <= 1'b1;
end
z_state <= 5'd7;
end
7:
begin
z_we <= 1'b1;
z_state <= z_state;
z_value <= z_value;
end
endcase
end
else begin
z_we <= z_we;
z_state <= z_state;
z_value <= z_value;
end
end
///////////////////////////////////////////////////////////
//declare our modules for vga support here
///////////////////////////////////////////////////////////
Reset_Delay d1 (
.iCLK(CLOCK_50),
.oRESET(DLY_RST)
);
VGA_Audio_PLL p1 (
.areset(~DLY_RST),
.inclk0(CLOCK_27),
.c0(VGA_CTRL_CLK),
.c2(VGA_CLK)
);
VGA_Controller vga1(
// Host Side
.iCursor_RGB_EN(4'b0111),
.oAddress(mVGA_ADDR),
.oCoord_X(Coord_X),
.oCoord_Y(Coord_Y),
.iRed(mVGA_R),
.iGreen(mVGA_G),
.iBlue(mVGA_B),
// VGA Side
.oVGA_R(VGA_R),
.oVGA_G(VGA_G),
.oVGA_B(VGA_B),
.oVGA_H_SYNC(VGA_HS),
.oVGA_V_SYNC(VGA_VS),
.oVGA_SYNC(VGA_SYNC),
.oVGA_BLANK(VGA_BLANK),
// Control Signal
.iCLK(VGA_CTRL_CLK),
.iRST_N(DLY_RST)
);
endmodule
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -