public void TestComputeShader() { const string CSVersion = "cs_5_0"; var rc = EngineNS.CEngine.Instance.RenderContext; var macros = new CShaderDefinitions(); macros.SetDefine("USE_STRUCTURED_BUFFERS", ""); var shaderFile = RName.GetRName("Shaders/Compute/test1.compute").Address; var shaderRName_extractID = RName.GetRName("Shaders/Compute/test1.compute"); var shaderRName_calcRate = RName.GetRName("Shaders/Compute/calcrate.compute"); //var shaderRName_clearID = RName.GetRName("Shaders/Compute/clearid.compute"); var csMain0 = rc.CreateShaderDesc(shaderRName_extractID, "CSMain", EShaderType.EST_ComputeShader, macros, CIPlatform.Instance.PlatformType); var csMain1 = rc.CreateShaderDesc(shaderRName_calcRate, "CSMain1", EShaderType.EST_ComputeShader, macros, CIPlatform.Instance.PlatformType); //var csMain2 = rc.CreateShaderDesc(shaderRName_clearID, "CSMain_Clear", EShaderType.EST_ComputeShader, macros, CIPlatform.Instance.PlatformType); var csExtractID = rc.CreateComputeShader(csMain0); var csCalcRate = rc.CreateComputeShader(csMain1); //var csClearID = rc.CreateComputeShader(csMain2); uint numElem = 8; var bfDesc = new CGpuBufferDesc(); bfDesc.SetMode(false, true); bfDesc.ByteWidth = 4 * numElem; bfDesc.StructureByteStride = 4; var buffer = rc.CreateGpuBuffer(bfDesc, IntPtr.Zero); var uavDesc = new CUnorderedAccessViewDesc(); uavDesc.ToDefault(); uavDesc.Buffer.NumElements = numElem; var uav = rc.CreateUnorderedAccessView(buffer, uavDesc); var buffer1 = rc.CreateGpuBuffer(bfDesc, IntPtr.Zero); var uav1 = rc.CreateUnorderedAccessView(buffer1, uavDesc); var cmd = rc.ImmCommandList; cmd.SetComputeShader(csExtractID); UInt32[] pUAVInitialCounts = new UInt32[1] { 1, }; var dsv = this.RenderPolicy.BaseSceneView.FrameBuffer.GetSRV_DepthStencil(); var viewWidth = this.RenderPolicy.BaseSceneView.Viewport.Width; var viewHeight = this.RenderPolicy.BaseSceneView.Viewport.Height; System.Diagnostics.Trace.WriteLine("[TestComputeShader] Framebuffer width :" + viewWidth.ToString() + ", height: " + viewHeight.ToString()); var camVP = this.RenderPolicy.Camera.CameraData.ViewProjection; //var srcTex = CEngine.Instance.TextureManager.GetShaderRView(rc, RName.GetRName("Texture/testActorID.txpic"), true); //if (srcTex != null) //{ // srcTex.PreUse(true); // cmd.CSSetShaderResource(0, srcTex); //} cmd.CSSetShaderResource(0, dsv); cmd.CSSetUnorderedAccessView(0, uav, pUAVInitialCounts); //uint texSizeW = 1092; //uint texSizeH = 516; cmd.CSDispatch((uint)viewWidth / 4, (uint)viewHeight / 4, 1); System.Diagnostics.Trace.WriteLine("[TestComputeShader] Dispatch width :" + viewWidth.ToString() + ", height: " + viewHeight.ToString()); //uint texSize = 512; //cmd.CSDispatch(texSize/8, texSize/8, 1); var blob = new EngineNS.Support.CBlobObject(); buffer.GetBufferData(rc, blob); var idArray = blob.ToUInts(); // fill uav1, clear uav0 cmd.SetComputeShader(csCalcRate); UInt32[] pUAVInitialCounts1 = new UInt32[1] { 1, }; var tempBufferData = new uint[8]; tempBufferData[0] = 9543; tempBufferData[1] = 3756; tempBufferData[2] = 2716; tempBufferData[3] = 297; tempBufferData[4] = 961; tempBufferData[5] = 45046; tempBufferData[6] = 0; tempBufferData[7] = 5686; unsafe { fixed(uint *pData = &tempBufferData[0]) { var buffer2 = rc.CreateGpuBuffer(bfDesc, (IntPtr)pData); var uav2 = rc.CreateUnorderedAccessView(buffer2, uavDesc); cmd.CSSetUnorderedAccessView(0, uav2, pUAVInitialCounts); } } //cmd.CSSetUnorderedAccessView(0, uav, pUAVInitialCounts); cmd.CSSetUnorderedAccessView(1, uav1, pUAVInitialCounts); var cbIndex = csMain1.FindCBufferDesc("CaptureEnv"); if (cbIndex != uint.MaxValue) { var cbuffer = rc.CreateConstantBuffer(csMain1, cbIndex); var varIdx = cbuffer.FindVar("rateWeight"); cbuffer.SetValue(varIdx, new EngineNS.Quaternion(100, 1000, 10000, 100000), 0); cbuffer.FlushContent(rc.ImmCommandList); var cbDesc = new EngineNS.CConstantBufferDesc(); if (csMain1.GetCBufferDesc(cbIndex, ref cbDesc)) { if (cbDesc.Type == EngineNS.ECBufferRhiType.SIT_CBUFFER) { cmd.CSSetConstantBuffer(cbDesc.CSBindPoint, cbuffer); } } } cmd.CSDispatch(8, 1, 1); // gbuffer1 buffer.GetBufferData(rc, blob); idArray = blob.ToUInts(); var blob1 = new EngineNS.Support.CBlobObject(); buffer1.GetBufferData(rc, blob1); var idArray1 = blob1.ToBytes(); var idUintArray1 = blob1.ToUInts(); }
static async System.Threading.Tasks.Task CookShadersWithFilter(string[] args, string smStr, EPlatformType platforms, HashSet <RName> MaterialAssets) { var rc = CEngine.Instance.RenderContext; int ShaderModel = System.Convert.ToInt32(smStr); string targetDir = CEngine.Instance.FileManager.CookingRoot + "deriveddatacache/" + "/sm" + smStr + "/"; if (FindArgument(args, "recompile") != null) { if (CEngine.Instance.FileManager.DirectoryExists(targetDir)) { CEngine.Instance.FileManager.DeleteDirectory(targetDir, true); } } CEngine.Instance.FileManager.CreateDirectory(targetDir); CEngine.Instance.FileManager.CreateDirectory(targetDir + "cs/"); CEngine.Instance.FileManager.CreateDirectory(targetDir + "vs/"); CEngine.Instance.FileManager.CreateDirectory(targetDir + "ps/"); EngineNS.CRenderContext.ShaderModel = ShaderModel; var shaderPath = CEngine.Instance.FileManager.DDCDirectory + "shaderinfo/"; var shaders = CEngine.Instance.FileManager.GetFiles(shaderPath, "*.xml"); int NumOfFailed = 0; int NumOfNotUsed = 0; foreach (var i in shaders) { using (var xml = EngineNS.IO.XmlHolder.LoadXML(i)) { EngineNS.Graphics.CGfxEffectDesc desc = await EngineNS.Graphics.CGfxEffectDesc.LoadEffectDescFromXml(rc, xml.RootNode, false); if (desc == null) { EngineNS.Profiler.Log.WriteLine(EngineNS.Profiler.ELogTag.Warning, "CookShader", $"CookShader Failed: {xml.GetTextString()}"); NumOfFailed++; continue; } if (MaterialAssets.Contains(desc.MtlShaderPatch.Name) == false) { NumOfNotUsed++; continue; } var effect = new EngineNS.Graphics.CGfxEffect(desc); await effect.CookEffect(rc, desc, ShaderModel, platforms); } } shaders = CEngine.Instance.FileManager.GetFiles(shaderPath, "*.shaderxml"); foreach (var i in shaders) { using (var xml = EngineNS.IO.XmlHolder.LoadXML(i)) { RName shaderName = RName.GetRName(xml.RootNode.FindAttrib("Shader").Value, RName.enRNameType.Engine); EShaderType type = EShaderType.EST_UnknownShader; switch (xml.RootNode.Name) { case "EST_ComputeShader": type = EShaderType.EST_ComputeShader; break; case "EST_VertexShader": type = EShaderType.EST_VertexShader; break; case "EST_PixelShader": type = EShaderType.EST_PixelShader; break; } string entry = xml.RootNode.FindAttrib("Entry").Value; var node = xml.RootNode.FindNode("Macro"); var attrs = node.GetAttribs(); CShaderDefinitions defines = new CShaderDefinitions(); foreach (var j in attrs) { defines.SetDefine(j.Name, j.Value); } CEngine.Instance.RenderContext.CookShaderDesc(shaderName, entry, type, defines, ShaderModel, platforms); } } }