123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using System.Collections.Generic;
- using Unity.Collections;
- using Unity.Mathematics;
- using UnityEngine;
- using UnityEngine.Rendering;
- using Utility;
- namespace Core.BRG
- {
- public class BRGRender<T> : BRGRenderBasic where T : BGRGameObjectInfo
- {
- /// <summary>
- /// BRG容器对象
- /// </summary>
- // private BRGRenderBasic _mBrgRenderBasic;
- // private int m_itemCount;
- public bool isUpdate = true;
- public List<T> m_gameObjectInfos = new List<T>();
- private List<BatchShaderBind> m_batchShaderBinds = new List<BatchShaderBind>();
- private Map<string, BatchShaderBind> m_batchShaderBindMap = new Map<string, BatchShaderBind>();
- public void InitRender(BRGSamples samples, List<T> gameObjectInfos)
- {
- this.m_gameObjectInfos = gameObjectInfos;
- int maxItemSize = samples.GetAllShaderValueSize();
- Init(samples, gameObjectInfos.Count, maxItemSize);
- UploadGpuData(gameObjectInfos.Count);
- }
- protected override NativeArray<MetadataValue> ProInitBatchMetadata(int startOffset, int m_maxInstancePerWindow)
- {
- NativeArray<MetadataValue> metadataValues =
- new NativeArray<MetadataValue>(m_samples.AllShaderValues.Count, Allocator.Temp,
- NativeArrayOptions.UninitializedMemory);
- m_batchShaderBinds.Clear();
- // int count= m_maxInstancePerWindow;
- for (int i = 0; i < m_samples.AllShaderValues.Count; i++)
- {
- BRGShaderValue shaderValue = m_samples.AllShaderValues[i];
- int shaderId = Shader.PropertyToID(shaderValue.key);
- BatchShaderBind batchShaderBind = new BatchShaderBind();
- metadataValues[i] = CreateMetadataValue(shaderId, startOffset, true);
- batchShaderBind.shaderValue = shaderValue;
- batchShaderBind.offset = startOffset;
- batchShaderBind.InitBuffer(m_maxInstancePerWindow);
- startOffset += shaderValue.GetSize() * m_maxInstancePerWindow;
- m_batchShaderBinds.Add(batchShaderBind);
- m_batchShaderBindMap.Add(shaderValue.key, batchShaderBind);
- }
- return metadataValues;
- }
- public void UpdatePos()
- {
- if (!isUpdate)
- {
- return;
- }
- int totalGpuBufferSize;
- int alignedWindowSize;
- NativeArray<float3x4> sysmemBuffer =
- GetSysmemBuffer(out totalGpuBufferSize, out alignedWindowSize);
- // 检查NativeArray是否有效
- if (!sysmemBuffer.IsCreated)
- {
- Debug.LogError("NativeArray has been deallocated or not created properly");
- return;
- }
- int m_itemCount = m_gameObjectInfos.Count;
- m_batchShaderBindMap.TryGetValue("_MainColor", out var baseColorBind);
- for (int i = 0; i < m_gameObjectInfos.Count; i++)
- {
- BGRGameObjectInfo info = m_gameObjectInfos[i];
- Matrix4x4 matrix4X4 = info.objectToWorld;
- float4x4 float3X4 = matrix4X4;
- sysmemBuffer[i] = new float3x4(
- float3X4.c0.xyz, float3X4.c1.xyz,
- float3X4.c2.xyz, float3X4.c3.xyz
- );
- float4x4 inverse = matrix4X4.inverse;
- sysmemBuffer[i + m_itemCount] = new float3x4(
- inverse.c0.xyz, inverse.c1.xyz,
- inverse.c2.xyz, inverse.c3.xyz
- );
- if (baseColorBind != null)
- {
- baseColorBind.SetData(i, info.color);
- }
- }
- // 每帧更新后重新上传数据到GPU
- UploadGpuData(m_itemCount, m_batchShaderBinds);
- }
- }
- }
|