protected override unsafe JobHandle OnUpdate(JobHandle inputDeps) { var nativeBuffer = this.GetSingleton <DrawSystem.NativeTransformBufferData>().Transforms; var computeBuffer = this.GetSingleton <DrawSystem.ComputeTransformBufferData>().Transforms; computeBuffer.SetData(nativeBuffer.AsNativeArray(), 0, 0, nativeBuffer.length_); //Debug.Log("start"); //for (var i = 0; i < nativeBuffer.length_; i++) //{ // Debug.Log($"{i} {nativeBuffer.pBuffer[i]}"); //} this.Entities .WithoutBurst() .ForEach( ( in DrawModel.InstanceCounterData counter, in DrawModel.VectorBufferData offset, in DrawModel.ComputeArgumentsBufferData shaderArg, in DrawModel.GeometryData geom ) => { if (counter.InstanceCounter.Count == 0) { return; } var mesh = geom.Mesh; var mat = geom.Material; var args = shaderArg.InstanceArgumentsBuffer; var vectorOffset = offset.pVectorPerModelInBuffer - nativeBuffer.pBuffer; mat.SetInt("BoneVectorOffset", (int)vectorOffset); //mat.SetInt( "BoneLengthEveryInstance", mesh.bindposes.Length ); //mat.SetBuffer( "BoneVectorBuffer", computeBuffer ); //Debug.Log($"{mesh.name} {((uint4*)offset.pVectorOffsetPerModelInBuffer)[0]}"); //Debug.Log($"{mesh.name} {((uint4*)offset.pVectorOffsetPerModelInBuffer)[6]}"); var instanceCount = counter.InstanceCounter.Count; var argparams = new IndirectArgumentsForInstancing(mesh, instanceCount); args.SetData(ref argparams); var bounds = new Bounds() { center = Vector3.zero, size = Vector3.one * 1000.0f }; Graphics.DrawMeshInstancedIndirect(mesh, 0, mat, bounds, args); }
private void LateUpdate() { this.job.Complete(); var res = this.meshResources; res.instancesBuffer.SetData(this.cubeInstances.AsArray()); res.gridPositionBuffer.SetData(this.gridPositions.AsArray()); res.nearGridIdBuffer.SetData(this.nearGrids.AsArray()); var dargs = res.argsBufferForDispatch; var remain = (64 - (this.cubeInstances.Length & 0x3f)) & 0x3f; for (var i = 0; i < remain; i++) { this.cubeInstances.AddNoResize(new CubeInstance { instance = 0 }); } var dargparams = new IndirectArgumentsForDispatch(this.cubeInstances.Length >> 6, 1, 1); dargs.SetData(ref dargparams); //this.setGridCubeIdShader.Dispatch( 0, this.cubeInstances.Length >> 6, 1, 1 ); var mesh = res.mesh; var mat = this.Material; var iargs = res.argsBufferForInstancing; var instanceCount = this.cubeInstances.Length; var iargparams = new IndirectArgumentsForInstancing(mesh, instanceCount); iargs.SetData(ref iargparams); //var bounds = new Bounds() { center = Vector3.zero, size = Vector3.one * 1000.0f }; //Graphics.DrawMeshInstancedIndirect( mesh, 0, mat, bounds, args ); }
private unsafe void Update() { //var c = this.cubeGrids[ 5, 1, 3 ]; //c[ i, 0, 0 ] ^= 1; //i = i + 1 & 31; this.gridData.Clear(); this.cubeInstances.Clear(); Debug.Log($"pre {this.cubeInstances.Length} {this.gridData.Length}"); this.job = this.cubeGrids.BuildCubeInstanceData(this.gridData, this.cubeInstances); //} //private unsafe void LateUpdate() //{ this.job.Complete(); Debug.Log($"aft {this.cubeInstances.Length}/{this.cubeInstances.length} {this.gridData.Length}/{this.gridData.length}"); var res = this.meshResources; res.CubeInstancesBuffer.SetData(this.cubeInstances.AsArray()); Debug.Log($"CubeInstancesBuffer setdata {this.cubeInstances.Length} {this.gridData.Length}"); //res.GridBuffer.SetData( this.gridData.AsArray() ); var grids = new Vector4[this.gridData.Length * 2]; fixed(Vector4 *pdst = grids) { var psrc = (Vector4 *)this.gridData.GetUnsafeReadOnlyPtr(); UnsafeUtility.MemCpy(pdst, psrc, this.gridData.Length * 2 * sizeof(float4)); } this.Material.SetVectorArray("grids", grids); Debug.Log($"SetVectorArray {grids.Length}"); var remain = (64 - (this.cubeInstances.Length & 0x3f)) & 0x3f; for (var i = 0; i < remain; i++) { this.cubeInstances.AddNoResize(new CubeInstance { instance = 1 }); } var dargparams = new IndirectArgumentsForDispatch(this.cubeInstances.Length >> 6, 1, 1); var dargs = res.ArgsBufferForDispatch; dargs.SetData(ref dargparams); //this.setGridCubeIdShader.Dispatch( 0, this.cubeInstances.Length >> 6, 1, 1 );// var mesh = res.mesh; var mat = this.Material; var iargs = res.ArgsBufferForInstancing; var instanceCount = this.cubeInstances.Length; Debug.Log($"instanceCount {instanceCount}"); var iargparams = new IndirectArgumentsForInstancing(mesh, instanceCount); iargs.SetData(ref iargparams); //var bounds = new Bounds() { center = Vector3.zero, size = Vector3.one * 1000.0f };// //Graphics.DrawMeshInstancedIndirect( mesh, 0, mat, bounds, iargs );// }