internal void initData(v2f input, Context3D context3D, FragementShader shader) { this.input = input; this.samplers = context3D.samplers; this.output = new float4(0, 0, 0, 0); fragementShader = shader; isdiscard = false; }
public void drawTriangles(IndexBuffer3D indexBuffer, int firstIndex = 0, int numTriangles = -1) { if (currentVertexBuffer == null) { throw new InvalidOperationException("VertexBuffer无效"); } if (current_program3D == null) { throw new InvalidOperationException("尚未设置着色器"); } if (!current_program3D.Validate()) { throw new InvalidOperationException("着色器校验失败"); } if (numTriangles == -1) { numTriangles = (indexBuffer.data.Count - firstIndex) / 3; } if (rasters == null || rasters.Length < currentRenderTarget.renderTarget.rt_width * currentRenderTarget.renderTarget.rt_height) { rasters = new Rasterizer.Raster[currentRenderTarget.renderTarget.rt_width * currentRenderTarget.renderTarget.rt_height]; for (int i = 0; i < rasters.Length; i++) { rasters[i] = new Rasterizer.Raster(); } } current_program3D.vertexShader.constants = programConstants; current_program3D.fragementShader.constants = programConstants; v2f[] v2fs = new v2f[currentVertexBuffer.vertices.Count]; #region 计算所有顶点 var vs = current_program3D.vertexShader; for (int i = 0; i < v2fs.Length; i++) { vs.appdata = currentVertexBuffer.vertices[i]; v2fs[i] = vs.Execute(); } #endregion for (int i = 0; i < numTriangles; i++) { if (firstIndex + i * 3 > indexBuffer.data.Count - 3) { throw new ArgumentException("index数量不足"); } uint idx1 = indexBuffer.data[firstIndex + i * 3]; uint idx2 = indexBuffer.data[firstIndex + i * 3 + 1]; uint idx3 = indexBuffer.data[firstIndex + i * 3 + 2]; #region 顶点着色器运算 v2f v1; v2f v2; v2f v3; //var vs = current_program3D.vertexShader; { //vs.appdata = currentVertexBuffer.vertices[(int)idx1]; //v1=vs.Execute(); v1 = v2fs[(int)idx1]; } { //vs.appdata = currentVertexBuffer.vertices[(int)idx2]; //v2=vs.Execute(); v2 = v2fs[(int)idx2]; } { //vs.appdata = currentVertexBuffer.vertices[(int)idx3]; //v3=vs.Execute(); v3 = v2fs[(int)idx3]; } #endregion #region clip and cull { float3 xyz1 = v1.SV_POSITION.xyz / v1.SV_POSITION.w; float3 xyz2 = v2.SV_POSITION.xyz / v2.SV_POSITION.w; float3 xyz3 = v3.SV_POSITION.xyz / v3.SV_POSITION.w; #region 视景体外裁剪 { if ((xyz1.z > 1 || xyz1.z < 0 || xyz1.x < -1 || xyz1.x > 1 || xyz1.y < -1 || xyz1.y > 1 || double.IsNaN(xyz1.x) || double.IsNaN(xyz1.y) || double.IsNaN(xyz1.z)) && (xyz2.z > 1 || xyz2.z < 0 || xyz2.x < -1 || xyz2.x > 1 || xyz2.y < -1 || xyz2.y > 1 || double.IsNaN(xyz2.x) || double.IsNaN(xyz2.y) || double.IsNaN(xyz2.z)) && (xyz3.z > 1 || xyz3.z < 0 || xyz3.x < -1 || xyz3.x > 1 || xyz3.y < -1 || xyz3.y > 1 || double.IsNaN(xyz3.x) || double.IsNaN(xyz3.y) || double.IsNaN(xyz3.z)) ) { continue; } } #endregion //背面剔除 { float area = Rasterizer.TriangleDoubleArea(xyz1.xy, xyz2.xy, xyz3.xy); switch (_culling) { case Context3DTriangleFace.BACK: if (area < 0) { continue; } break; case Context3DTriangleFace.FRONT: if (area > 0) { continue; } break; case Context3DTriangleFace.FRONT_AND_BACK: continue; case Context3DTriangleFace.NONE: break; default: continue; } } } #endregion int totalraises; Rasterizer.Triangle(currentRenderTarget.renderTarget, v1, v2, v3 , rasters , out totalraises ); var pixels = currentRenderTarget.renderTarget.getRealPixels(); for (int j = 0; j < totalraises; j++) { var r = rasters[j]; if (r.vsout.SV_POSITION.z < -1.192092896e-07F || r.vsout.SV_POSITION.z > r.vsout.SV_POSITION.w || double.IsNaN(r.vsout.SV_POSITION.z)) { //在远近裁剪面外,去掉 continue; } r.isclippass = true; } var zBuffer = currentRenderTarget.zBuffer; var currentRenderBuffer = currentRenderTarget.renderTarget; for (int j = 0; j < totalraises; j += 4) { var r0 = rasters[j]; var r1 = rasters[j + 1]; var r2 = rasters[j + 2]; var r3 = rasters[j + 3]; quadFragementUnit.setQuadUnit(this, current_program3D.fragementShader, r0.vsout, r1.vsout, r2.vsout, r3.vsout); for (int k = 0; k < 4; k++) { var r = rasters[j + k]; if (r.isclippass && r.rasterize) { var unit = quadFragementUnit[k]; debugger.FrameDebugData debugData = null; if (DebugBuffer != null && current_program3D.fragementShader.HasDebug) { debugData = DebugBuffer.buffer[r.x][r.y]; debugData.inputdata = unit.input; debugData.i = r.x; debugData.j = r.y; debugData.Ready(); } float4 oc = pixels[r.x][r.y]; current_program3D.fragementShader.Run(unit, debugData); float4 color = unit.output; color.a = Mathf.clamp01(color.a); if (unit.isdiscard) { continue; } #region 深度检测处理 { //float depth = r.vsout.SV_POSITION.z / r.vsout.SV_POSITION.w; ushort depth = zBuffer.Convert(r.vsout.SV_POSITION.z / r.vsout.SV_POSITION.w); //读出zBuffer //var buffer = zBuffer.buffer; var di = (int)(r.x * 1.0f * zBuffer.width / currentRenderBuffer.rt_width); var dj = (int)(r.y * 1.0f * zBuffer.height / currentRenderBuffer.rt_height); //float depth_buffer = buffer[di][dj]; ushort depth_buffer = zBuffer[di, dj]; #region 深度检测 switch (_depthtest_passCompareMode) { case Context3DCompareMode.ALWAYS: break; case Context3DCompareMode.EQUAL: if (!(depth == depth_buffer)) { continue; } break; case Context3DCompareMode.GREATER: if (!(depth > depth_buffer)) { continue; } break; case Context3DCompareMode.GREATER_EQUAL: if (!(depth >= depth_buffer)) { continue; } break; case Context3DCompareMode.LESS: if (!(depth < depth_buffer)) { continue; } break; case Context3DCompareMode.LESS_EQUAL: if (!(depth <= depth_buffer)) { continue; } break; case Context3DCompareMode.NEVER: //永远测试不通过 continue; case Context3DCompareMode.NOT_EQUAL: if (!(depth != depth_buffer)) { continue; } break; default: continue; } #endregion //深度测试通过 if (_depthtest_depthMask) //需要写深度 { //buffer[di][dj] = depth; zBuffer.WriteDepth(di, dj, depth); } } #endregion //***Blend*** float4 final = oc * (1 - color.a) + color * color.a; pixels[r.x][r.y] = final; } } } #region 线框 //var linepos1 = v1.SV_POSITION / v1.SV_POSITION.w; //var linepos2 = v2.SV_POSITION / v2.SV_POSITION.w; //var linepos3 = v3.SV_POSITION / v3.SV_POSITION.w; //Rasterizer.Line(currentRenderBuffer, // linepos1.xy, linepos2.xy, new float4(0, 0, 0, 1)); //Rasterizer.Line(currentRenderBuffer, // linepos2.xy, linepos3.xy, new float4(0, 0, 0, 1)); //Rasterizer.Line(currentRenderBuffer, // linepos3.xy, linepos1.xy, new float4(0, 0, 0, 1)); #endregion #region 显示经着色器计算后的法线和切线 { //DrawDirLine(currentRenderBuffer, 0.2f, v1.worldPos, v1.worldNormal, new float4(0, 0, 1, 1)); //DrawDirLine(currentRenderBuffer, 0.2f, v1.worldPos, v1.worldTangent, new float4(1, 1, 0, 1)); } #endregion } }
private static void interpolation(ref v2f v2f, ref v2f p1, ref v2f p2, ref v2f p3, float a, float b, float c) { v2f.SV_POSITION = (p1.SV_POSITION * a / p1.SV_POSITION.w + p2.SV_POSITION * b / p2.SV_POSITION.w + p3.SV_POSITION * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.objPos = (p1.objPos * a / p1.SV_POSITION.w + p2.objPos * b / p2.SV_POSITION.w + p3.objPos * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.worldPos = (p1.worldPos * a / p1.SV_POSITION.w + p2.worldPos * b / p2.SV_POSITION.w + p3.worldPos * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.color = (p1.color * a / p1.SV_POSITION.w + p2.color * b / p2.SV_POSITION.w + p3.color * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.uv = (p1.uv * a / p1.SV_POSITION.w + p2.uv * b / p2.SV_POSITION.w + p3.uv * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.worldNormal = (p1.worldNormal * a / p1.SV_POSITION.w + p2.worldNormal * b / p2.SV_POSITION.w + p3.worldNormal * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.objNormal = (p1.objNormal * a / p1.SV_POSITION.w + p2.objNormal * b / p2.SV_POSITION.w + p3.objNormal * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.worldTangent = (p1.worldTangent * a / p1.SV_POSITION.w + p2.worldTangent * b / p2.SV_POSITION.w + p3.worldTangent * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.tSpace0 = (p1.tSpace0 * a / p1.SV_POSITION.w + p2.tSpace0 * b / p2.SV_POSITION.w + p3.tSpace0 * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.tSpace1 = (p1.tSpace1 * a / p1.SV_POSITION.w + p2.tSpace1 * b / p2.SV_POSITION.w + p3.tSpace1 * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; v2f.tSpace2 = (p1.tSpace2 * a / p1.SV_POSITION.w + p2.tSpace2 * b / p2.SV_POSITION.w + p3.tSpace2 * c / p3.SV_POSITION.w) / ( a / p1.SV_POSITION.w + b / p2.SV_POSITION.w + c / p3.SV_POSITION.w ) ; }
public static void Triangle(IRenderTarget renderBuffer, v2f p1, v2f p2, v2f p3, Raster[] rasters, out int totalrasters) { /* * D3D将点按z字形分成两个三角形做光栅化,而三角形的光栅化遵守TOP-LEFT规则: * 在屏幕上,若某条边位于三角形的左侧,则这条边称为LEFT边;若某条边是平行边,且位于三角形的上侧,则这条边称为TOP边。简单地讲,LEFT边是“左侧的边”,TOP边是“上面的平行边”。 * D3D规定: * (1)如果一个像素中心刚好落在三角形的一条边上,则仅当这条边为TOP或LEFT边时才画该像素; * (2)如果一个像素中心刚好落在三角形两条边的交点,则仅当两条边分别为TOP和LEFT边时才画该像素。 * TOP-LEFT规则保证了当两个三角形有重合边时,像素不会被重复渲染。对于拆分成两个三角形的点,则保证了一个顶点只覆盖一个像素。 */ totalrasters = 0; float2 pos1 = toRenderTargetPos(renderBuffer, p1.SV_POSITION.xy / p1.SV_POSITION.w); float2 pos2 = toRenderTargetPos(renderBuffer, p2.SV_POSITION.xy / p2.SV_POSITION.w); float2 pos3 = toRenderTargetPos(renderBuffer, p3.SV_POSITION.xy / p3.SV_POSITION.w); float area = TriangleDoubleArea(pos1, pos2, pos3); if (area == 0) { return; } //****确定矩形区域**** int top = (int)Math.Floor(Mathf.min(Mathf.min(pos1.y, pos2.y), pos3.y)); int bottom = (int)Math.Ceiling(Mathf.max(Mathf.max(pos1.y, pos2.y), pos3.y)); int left = (int)Math.Floor(Mathf.min(Mathf.min(pos1.x, pos2.x), pos3.x)); int right = (int)Math.Ceiling(Mathf.max(Mathf.max(pos1.x, pos2.x), pos3.x)); TriangleEdge edge1 = new TriangleEdge() { rtpos1 = pos1, rtpos2 = pos2, anotherrtpos = pos3 }; TriangleEdge edge2 = new TriangleEdge() { rtpos1 = pos2, rtpos2 = pos3, anotherrtpos = pos1 }; TriangleEdge edge3 = new TriangleEdge() { rtpos1 = pos3, rtpos2 = pos1, anotherrtpos = pos2 }; //如果用定义判断,需要处理顶边和左边,这里不用定义判断,所以不需要执行了 //edge1.checkedgeattribue(); //edge2.checkedgeattribue(); //edge3.checkedgeattribue(); if (left < 0) { left = 0; } if (top < 0) { top = 0; } //***将矩形区域扩展成2的倍数 if (left % 2 == 1) { left--; } if (right % 2 == 1) { right++; } if (top % 2 == 1) { top--; } if (bottom % 2 == 1) { bottom++; } for (int i = left; i < right; i += 2) { if (i < 0 || i >= renderBuffer.rt_width) { continue; } for (int j = top; j < bottom; j += 2) { if (j < 0 || j >= renderBuffer.rt_height) { continue; } float2 pixelpositon = new float2(i + 0.5f, j + 0.5f); //***检测相邻的四个像素。片段着色器阶段四个相邻像素Z形执行 float[] A = new float[4]; float[] B = new float[4]; float[] C = new float[4]; bool[] r_pass = new bool[4]; r_pass[0] = need_rasterize(edge1, edge2, edge3, new float2(pixelpositon.x, pixelpositon.y), area, out A[0], out B[0], out C[0]); r_pass[1] = need_rasterize(edge1, edge2, edge3, new float2(pixelpositon.x + 1, pixelpositon.y), area, out A[1], out B[1], out C[1]); r_pass[2] = need_rasterize(edge1, edge2, edge3, new float2(pixelpositon.x, pixelpositon.y + 1), area, out A[2], out B[2], out C[2]); r_pass[3] = need_rasterize(edge1, edge2, edge3, new float2(pixelpositon.x + 1, pixelpositon.y + 1), area, out A[3], out B[3], out C[3]); if (!(r_pass[0] || r_pass[1] || r_pass[2] || r_pass[3])) { continue; } for (int jj = 0; jj < 2; jj++) { for (int ii = 0; ii < 2; ii++) { int idx = jj * 2 + ii; //重心坐标系 float a = A[idx] / area; float b = B[idx] / area; float c = C[idx] / area; //返回光栅化结果 var r = rasters[totalrasters]; r.isclippass = false; r.rasterize = r_pass[idx]; r.x = i + ii; r.y = j + jj; v2f v2f = new v2f(); interpolation(ref v2f, ref p1, ref p2, ref p3, a, b, c); r.vsout = v2f; totalrasters++; } } } } }
public void setQuadUnit(Context3D context3D, FragementShader fragementShader, v2f i0, v2f i1, v2f i2, v2f i3) { unit0.initData(i0, context3D, fragementShader); unit1.initData(i1, context3D, fragementShader); unit2.initData(i2, context3D, fragementShader); unit3.initData(i3, context3D, fragementShader); unit0.dpdx_v1 = unit0; unit0.dpdx_v2 = unit1; unit0.dpdy_v1 = unit0; unit0.dpdy_v2 = unit2; unit1.dpdx_v1 = unit0; unit1.dpdx_v2 = unit1; unit1.dpdy_v1 = unit1; unit1.dpdy_v2 = unit3; unit2.dpdx_v1 = unit2; unit2.dpdx_v2 = unit3; unit2.dpdy_v1 = unit0; unit2.dpdy_v2 = unit2; unit3.dpdx_v1 = unit2; unit3.dpdx_v2 = unit3; unit3.dpdy_v1 = unit1; unit3.dpdy_v2 = unit3; }
protected abstract float4 Execute(v2f IN);