1313import java .util .Random ;
1414
1515public class SoftwareRasterizer {
16- public static final int TARGET_SIZE = ModelFactory .MODEL_TEXTURE_SIZE ;
17-
1816 private final Vector4f scratch = new Vector4f ();
1917
2018 private final Vector3f scratch1 = new Vector3f ();
@@ -38,7 +36,9 @@ public class SoftwareRasterizer {
3836
3937 private static final long DEPTH_MASK = ((1L <<24 )-1 )<<(64 -24 );
4038 private static final long CLEAR_VALUE = DEPTH_MASK ;//set the depth to max value and rest of bits to 0
41- private final long [] framebuffer = new long [TARGET_SIZE *TARGET_SIZE ];
39+
40+ private final int targetSize ;
41+ private final long [] framebuffer ;
4242
4343 private boolean cullBackFace ;
4444 private boolean doTheBlending ;
@@ -47,7 +47,9 @@ public class SoftwareRasterizer {
4747 private int samplerHeight ;
4848 private int [] samplerTexture ;
4949
50- public SoftwareRasterizer () {
50+ public SoftwareRasterizer (int targetSize ) {
51+ this .targetSize = targetSize ;
52+ this .framebuffer = new long [targetSize *targetSize ];
5153 }
5254
5355 public void setFaceCull (boolean isBackFaceCulling ) {
@@ -76,10 +78,12 @@ public void clear() {
7678 }
7779
7880 public void raster (Matrix4f mvp , ReuseVertexConsumer vertices ) {
79- if (vertices .isEmpty ()) return ;
80- int qc = vertices .quadCount ();
81- for (int i = 0 ; i < qc ; i ++) {
82- this .rasterQuad (mvp , vertices .getAddress ()+ReuseVertexConsumer .VERTEX_FORMAT_SIZE *4L *i );
81+ this .raster (mvp , vertices .getAddress (), vertices .quadCount ());
82+ }
83+ public void raster (Matrix4f mvp , long verticesAddr , int quadCount ) {
84+ if (quadCount == 0 ) return ;
85+ for (int i = 0 ; i < quadCount ; i ++) {
86+ this .rasterQuad (mvp , verticesAddr +ReuseVertexConsumer .VERTEX_FORMAT_SIZE *4L *i );
8387 }
8488 //Arrays.fill(this.framebuffer, -1);
8589 }
@@ -135,9 +139,9 @@ private void rasterTriangle(boolean orZero) {
135139 }*/
136140
137141 int minX = Math .max ((int ) Math .floor (Math .min (Math .min (v1 .x , v2 .x ), v3 .x )), 0 );
138- int maxX = Math .min ((int ) Math .ceil (Math .max (Math .max (v1 .x , v2 .x ), v3 .x )), TARGET_SIZE -1 );
142+ int maxX = Math .min ((int ) Math .ceil (Math .max (Math .max (v1 .x , v2 .x ), v3 .x )), this . targetSize -1 );
139143 int minY = Math .max ((int ) Math .floor (Math .min (Math .min (v1 .y , v2 .y ), v3 .y )), 0 );
140- int maxY = Math .min ((int ) Math .ceil (Math .max (Math .max (v1 .y , v2 .y ), v3 .y )), TARGET_SIZE -1 );
144+ int maxY = Math .min ((int ) Math .ceil (Math .max (Math .max (v1 .y , v2 .y ), v3 .y )), this . targetSize -1 );
141145
142146 float invArea = 1.0f /area ;
143147 for (int py = minY ; py <=maxY ; py ++) {
@@ -151,7 +155,7 @@ private void rasterTriangle(boolean orZero) {
151155 //Dont need to worry about perspective correction afak as it should already be all correct
152156
153157 //pixel is inside the triangle
154- this .rasterPixel (px +py *TARGET_SIZE , w1 , w2 , w3 );
158+ this .rasterPixel (px +py *this . targetSize , w1 , w2 , w3 );
155159 }
156160 }
157161 }
@@ -248,7 +252,7 @@ private void loadTransformPos(Matrix4f transform, long addr, int vert, Vector3f
248252 var vec = transform .transformProject (this .scratch );
249253 if (Math .abs (this .scratch .w -1.0f )>0.000001f )
250254 throw new IllegalStateException ();
251- out .set (maintainPrecision (Math .fma (vec .x , 0.5f , 0.5f )*TARGET_SIZE ), maintainPrecision (Math .fma (vec .y , 0.5f , 0.5f )*TARGET_SIZE ), vec .z );//TODO: dont know if z transform is correct
255+ out .set (maintainPrecision (Math .fma (vec .x , 0.5f , 0.5f )*this . targetSize ), maintainPrecision (Math .fma (vec .y , 0.5f , 0.5f )*this . targetSize ), vec .z );//TODO: dont know if z transform is correct
252256 }
253257
254258
0 commit comments