public VoxelShader(VXGI vxgi) { _vxgi = vxgi; _command = new CommandBuffer { name = "VXGI.VoxelShader" }; _arguments = new ComputeBuffer(3, sizeof(int), ComputeBufferType.IndirectArguments); _arguments.SetData(new int[] { 1, 1, 1 }); _lightSources = new ComputeBuffer(64, LightSource.size); _kernelAggregate = VXGIRenderPipeline.isD3D11Supported ? 0 : 1; _kernelClear = compute.FindKernel("CSClear"); _kernelRender = compute.FindKernel("CSRender"); _threadsAggregate = new NumThreads(compute, _kernelAggregate); _threadsClear = new NumThreads(compute, _kernelClear); _threadsTrace = new NumThreads(compute, _kernelRender); _descriptor = new RenderTextureDescriptor() { colorFormat = RenderTextureFormat.RInt, dimension = TextureDimension.Tex3D, enableRandomWrite = true, msaaSamples = 1, sRGB = false }; }
public Mipmapper(VXGI vxgi) { _vxgi = vxgi; _commandFilter = new CommandBuffer { name = "Mipmapper.Filter" }; _commandShift = new CommandBuffer { name = "Mipmapper.Shift" }; if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer) { _kernelFilter = 1; } else { _kernelFilter = 0; } _kernelShift = compute.FindKernel("CSShift"); _propDisplacement = Shader.PropertyToID("Displacement"); _propDst = Shader.PropertyToID("Dst"); _propDstRes = Shader.PropertyToID("DstRes"); _propSrc = Shader.PropertyToID("Src"); }
public void RenderMipmap(ScriptableRenderContext renderContext, Camera camera, VXGI vxgi) { var transform = Matrix4x4.TRS(vxgi.origin, Quaternion.identity, Vector3.one * vxgi.bound); _command.BeginSample(_command.name); if (vxgi.mipmapSampler == MipmapSampler.Point) { _command.EnableShaderKeyword("RADIANCE_POINT_SAMPLER"); } else { _command.DisableShaderKeyword("RADIANCE_POINT_SAMPLER"); } _command.SetGlobalFloat("MipmapLevel", Mathf.Min(vxgi.level, vxgi.radiances.Length)); _command.SetGlobalFloat("TracingStep", vxgi.step); _command.DrawProcedural(transform, VisualizationShader.material, (int)VisualizationShader.Pass.Mipmap, MeshTopology.Quads, 24, 1); _command.EndSample(_command.name); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); }
public Mipmapper(VXGI vxgi) { _vxgi = vxgi; _command = new CommandBuffer { name = "VXGI.Mipmapper" }; InitializeKernel(); }
public override void OnInspectorGUI() { DrawDefaultInspector(); VXGI vxgi = (VXGI)target; EditorGUILayout.LabelField("Volume", (vxgi.volume / 1000f).ToString("0.00k")); EditorGUILayout.LabelField("Voxel buffer scale", vxgi.bufferScale.ToString()); EditorGUILayout.LabelField("Voxel buffer count", (vxgi.volume * vxgi.bufferScale / 1000f).ToString("0.00k")); }
public Voxelizer(VXGI vxgi) { _vxgi = vxgi; _command = new CommandBuffer { name = "VXGI.Voxelizer" }; CreateCamera(); CreateCameraDescriptor(); CreateCameraSettings(); }
public Voxelizer(VXGI vxgi) { _vxgi = vxgi; _command = new CommandBuffer { name = "VXGI.Voxelizer" }; _rect = new Rect(0f, 0f, 1f, 1f); CreateCamera(); CreateCameraDescriptor(); CreateCameraSettings(); UpdateCamera(); }
public Voxelizer(VXGI vxgi) { _vxgi = vxgi; _command = new CommandBuffer { name = "Voxelizer" }; _lightColors = new List <Vector4>(16); _lightPositions = new List <Vector4>(16); _rect = new Rect(0f, 0f, 1f, 1f); _propDummyTarget = Shader.PropertyToID("DummyTarget"); CreateCamera(); CreateCameraDescriptor(); CreateCameraSettings(); UpdateCamera(); }
void RenderLighting(ScriptableRenderContext renderContext, Camera camera, VXGI vxgi) { Matrix4x4 clipToWorld = camera.cameraToWorldMatrix * GL.GetGPUProjectionMatrix(camera.projectionMatrix, false).inverse; _renderScale[2] = vxgi.diffuseResolutionScale; _command.BeginSample(_sampleRenderLighting); _command.SetGlobalMatrix(ShaderIDs.ClipToVoxel, vxgi.worldToVoxel * clipToWorld); _command.SetGlobalMatrix(ShaderIDs.ClipToWorld, clipToWorld); _command.SetGlobalMatrix(ShaderIDs.VoxelToWorld, vxgi.voxelToWorld); _command.SetGlobalMatrix(ShaderIDs.WorldToVoxel, vxgi.worldToVoxel); for (int i = 0; i < _lightingPasses.Length; i++) { _lightingPasses[i].Execute(_command, camera, ShaderIDs.FrameBuffer, _renderScale[i]); } _command.EndSample(_sampleRenderLighting); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); }
public VoxelShader(VXGI vxgi) { _vxgi = vxgi; _commandAverage = new CommandBuffer { name = "VoxelShader.Average" }; _commandRender = new CommandBuffer { name = "VoxelShader.Render" }; _arguments = new ComputeBuffer(3, sizeof(int), ComputeBufferType.IndirectArguments); _arguments.SetData(new int[] { 1, 1, 1 }); if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer) { _kernelAverage = 1; } else { _kernelAverage = 0; } _kernelRender = compute.FindKernel("CSRender"); _propLightColors = Shader.PropertyToID("LightColors"); _propLightPositions = Shader.PropertyToID("LightPositions"); _propRadianceBuffer = Shader.PropertyToID("RadianceBuffer"); _propResolution = Shader.PropertyToID("Resolution"); _propSunColor = Shader.PropertyToID("SunColor"); _propSunDirection = Shader.PropertyToID("SunDirection"); _propTarget = Shader.PropertyToID("Target"); _propVoxelBuffer = Shader.PropertyToID("VoxelBuffer"); _threadsAverage = new NumThreads(compute, _kernelAverage); _threadsTrace = new NumThreads(compute, _kernelRender); compute.SetVectorArray(_propLightColors, new Vector4[64]); compute.SetVectorArray(_propLightPositions, new Vector4[64]); }
public void RenderDeferred(ScriptableRenderContext renderContext, Camera camera, VXGI vxgi) { ScriptableCullingParameters cullingParams; if (!CullResults.GetCullingParameters(camera, out cullingParams)) { return; } CullResults.Cull(ref cullingParams, renderContext, ref _cullResults); renderContext.SetupCameraProperties(camera); int width = camera.pixelWidth; int height = camera.pixelHeight; _command.GetTemporaryRT(_cameraDepthTextureID, width, height, 24, FilterMode.Point, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(_cameraGBufferTexture0ID, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(_cameraGBufferTexture1ID, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(_cameraGBufferTexture2ID, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGB2101010, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(_cameraGBufferTexture3ID, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(_frameBufferID, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); _command.SetRenderTarget(_gBufferBinding); _command.ClearRenderTarget(true, true, Color.clear); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); var drawSettings = new DrawRendererSettings(camera, new ShaderPassName("Deferred")); drawSettings.flags = _renderPipeline.drawRendererFlags; drawSettings.rendererConfiguration = _renderPipeline.rendererConfiguration; drawSettings.sorting.flags = SortFlags.CommonOpaque; renderContext.DrawRenderers(_cullResults.visibleRenderers, ref drawSettings, _filterSettings); if (camera.cameraType != CameraType.SceneView) { _command.EnableShaderKeyword("PROJECTION_PARAMS_X"); } else { _command.DisableShaderKeyword("PROJECTION_PARAMS_X"); } _command.GetTemporaryRT(_dummyID, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); _command.Blit(_cameraDepthTextureID, BuiltinRenderTextureType.CameraTarget, UtilityShader.material, (int)UtilityShader.Pass.DepthCopy); _command.Blit(BuiltinRenderTextureType.CameraTarget, _dummyID); _command.Blit(_dummyID, _frameBufferID, UtilityShader.material, (int)UtilityShader.Pass.GrabCopy); _command.ReleaseTemporaryRT(_dummyID); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); Matrix4x4 clipToWorld = camera.cameraToWorldMatrix * GL.GetGPUProjectionMatrix(camera.projectionMatrix, false).inverse; _command.SetGlobalMatrix("ClipToWorld", clipToWorld); _command.SetGlobalMatrix("ClipToVoxel", vxgi.worldToVoxel * clipToWorld); _command.SetGlobalMatrix("WorldToVoxel", vxgi.worldToVoxel); _command.SetGlobalMatrix("VoxelToWorld", vxgi.voxelToWorld); bool depthNormalsNeeded = (camera.depthTextureMode & DepthTextureMode.DepthNormals) != DepthTextureMode.None; if (depthNormalsNeeded) { _command.GetTemporaryRT(_cameraDepthNormalsTextureID, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); _command.Blit(_cameraDepthTextureID, _cameraDepthNormalsTextureID, UtilityShader.material, (int)UtilityShader.Pass.EncodeDepthNormal); } renderContext.ExecuteCommandBuffer(_command); _command.Clear(); _renderScale[2] = vxgi.diffuseResolutionScale; for (int i = 0; i < _lightingPasses.Length; i++) { _lightingPasses[i].Execute(renderContext, camera, _frameBufferID, _renderScale[i]); } RenderPostProcessing(renderContext, camera); _command.Blit(_frameBufferID, BuiltinRenderTextureType.CameraTarget); RenderPostProcessingDebug(renderContext, camera); if (depthNormalsNeeded) { _command.ReleaseTemporaryRT(_cameraDepthNormalsTextureID); } _command.ReleaseTemporaryRT(_cameraDepthTextureID); _command.ReleaseTemporaryRT(_cameraGBufferTexture0ID); _command.ReleaseTemporaryRT(_cameraGBufferTexture1ID); _command.ReleaseTemporaryRT(_cameraGBufferTexture2ID); _command.ReleaseTemporaryRT(_cameraGBufferTexture3ID); _command.ReleaseTemporaryRT(_frameBufferID); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); }
void Awake() { _camera = GetComponent <Camera>(); _vxgi = GetComponent <VXGI>(); }
public void RenderDeferred(ScriptableRenderContext renderContext, Camera camera, VXGI vxgi) { ScriptableCullingParameters cullingParams; if (!CullResults.GetCullingParameters(camera, out cullingParams)) { return; } CullResults.Cull(ref cullingParams, renderContext, ref _cullResults); _command.BeginSample(_command.name); _command.GetTemporaryRT(_propDepth, camera.pixelWidth, camera.pixelHeight, 24, FilterMode.Point, RenderTextureFormat.Depth); _command.GetTemporaryRT(_propDiffuse, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf); _command.GetTemporaryRT(_propNormal, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Point, RenderTextureFormat.ARGBFloat); _command.GetTemporaryRT(_propEmission, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf); _command.GetTemporaryRT(_propOther, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf); _command.GetTemporaryRT(_propIrradiance, (int)(vxgi.diffuseResolutionScale * camera.pixelWidth), (int)(vxgi.diffuseResolutionScale * camera.pixelHeight), 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf ); var binding = new RenderTargetBinding( new RenderTargetIdentifier[] { _propDiffuse, _propNormal, _propEmission, _propOther }, new[] { RenderBufferLoadAction.DontCare, RenderBufferLoadAction.DontCare, RenderBufferLoadAction.DontCare, RenderBufferLoadAction.DontCare }, new[] { RenderBufferStoreAction.DontCare, RenderBufferStoreAction.DontCare, RenderBufferStoreAction.DontCare, RenderBufferStoreAction.DontCare }, _propDepth, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare ); _command.SetRenderTarget(binding); _command.ClearRenderTarget(true, true, Color.clear); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); var drawSettings = new DrawRendererSettings(camera, new ShaderPassName("Deferred")); drawSettings.sorting.flags = SortFlags.CommonOpaque; renderContext.DrawRenderers(_cullResults.visibleRenderers, ref drawSettings, _filterSettings); if (vxgi.pass == Pass.ConeTracing) { renderContext.SetupCameraProperties(camera); renderContext.DrawSkybox(camera); } if (_gBufferType != vxgi.gBufferType) { _command.DisableShaderKeyword(GetGBufferKeyword(_gBufferType)); _command.EnableShaderKeyword(GetGBufferKeyword(vxgi.gBufferType)); _gBufferType = vxgi.gBufferType; } if (_coneType != vxgi.coneType) { var keyword = GetConeKeyword(_coneType); if (keyword != null) { _command.DisableShaderKeyword(keyword); } keyword = GetConeKeyword(vxgi.coneType); if (keyword != null) { _command.EnableShaderKeyword(keyword); } _coneType = vxgi.coneType; } if (vxgi.skybox == null) { _command.DisableShaderKeyword("REFLECT_SKYBOX"); } else { _command.EnableShaderKeyword("REFLECT_SKYBOX"); _command.SetGlobalTexture("Skybox", vxgi.skybox); } Matrix4x4 clipToWorld = camera.cameraToWorldMatrix * GL.GetGPUProjectionMatrix(camera.projectionMatrix, false).inverse; _command.SetGlobalVector("CameraPosition", camera.transform.position); _command.SetGlobalMatrix("ClipToWorld", clipToWorld); _command.SetGlobalMatrix("ClipToVoxel", vxgi.worldToVoxel * clipToWorld); _command.SetGlobalMatrix("WorldToVoxel", vxgi.worldToVoxel); _command.SetGlobalMatrix("VoxelToWorld", vxgi.voxelToWorld); _command.EndSample(_command.name); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); if (vxgi.pass == Pass.ConeTracing) { _commandDiffuse.BeginSample(_commandDiffuse.name); _commandDiffuse.Blit(_propDiffuse, _propIrradiance, material, (int)Pass.DiffuseConeTracing); _commandDiffuse.EndSample(_commandDiffuse.name); renderContext.ExecuteCommandBuffer(_commandDiffuse); _commandDiffuse.Clear(); } _commandReflection.BeginSample(_commandReflection.name); _commandReflection.Blit(_propDiffuse, BuiltinRenderTextureType.CameraTarget, material, (int)vxgi.pass); _commandReflection.EndSample(_commandReflection.name); renderContext.ExecuteCommandBuffer(_commandReflection); _commandReflection.Clear(); _command.BeginSample(_command.name); _command.ReleaseTemporaryRT(_propDepth); _command.ReleaseTemporaryRT(_propDiffuse); _command.ReleaseTemporaryRT(_propNormal); _command.ReleaseTemporaryRT(_propEmission); _command.ReleaseTemporaryRT(_propOther); _command.ReleaseTemporaryRT(_propIrradiance); _command.EndSample(_command.name); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); }
void Start() { _vxgi = GetComponent <VXGI>(); }
public void RenderDeferred(ScriptableRenderContext renderContext, Camera camera, VXGI vxgi) { ScriptableCullingParameters cullingParams; if (!CullResults.GetCullingParameters(camera, out cullingParams)) { return; } CullResults.Cull(ref cullingParams, renderContext, ref _cullResults); renderContext.SetupCameraProperties(camera); int width = camera.pixelWidth; int height = camera.pixelHeight; _command.BeginSample(_command.name); if (camera.cameraType != CameraType.SceneView) { _command.EnableShaderKeyword("PROJECTION_PARAMS_X"); } else { _command.DisableShaderKeyword("PROJECTION_PARAMS_X"); } _command.GetTemporaryRT(ShaderIDs._CameraDepthTexture, width, height, 24, FilterMode.Point, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(ShaderIDs._CameraGBufferTexture0, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(ShaderIDs._CameraGBufferTexture1, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(ShaderIDs._CameraGBufferTexture2, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGB2101010, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(ShaderIDs._CameraGBufferTexture3, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); _command.GetTemporaryRT(ShaderIDs.FrameBuffer, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); _command.SetRenderTarget(_gBufferBinding); _command.ClearRenderTarget(true, true, Color.clear); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); TriggerCameraEvent(renderContext, camera, CameraEvent.BeforeGBuffer, vxgi); RenderGBuffers(renderContext, camera); TriggerCameraEvent(renderContext, camera, CameraEvent.AfterGBuffer, vxgi); CopyCameraTargetToFrameBuffer(renderContext, camera); bool depthNormalsNeeded = (camera.depthTextureMode & DepthTextureMode.DepthNormals) != DepthTextureMode.None; if (depthNormalsNeeded) { TriggerCameraEvent(renderContext, camera, CameraEvent.BeforeDepthNormalsTexture, vxgi); RenderCameraDepthNormalsTexture(renderContext, camera); TriggerCameraEvent(renderContext, camera, CameraEvent.AfterDepthNormalsTexture, vxgi); } TriggerCameraEvent(renderContext, camera, CameraEvent.BeforeLighting, vxgi); RenderLighting(renderContext, camera, vxgi); TriggerCameraEvent(renderContext, camera, CameraEvent.AfterLighting, vxgi); if (camera.clearFlags == CameraClearFlags.Skybox) { TriggerCameraEvent(renderContext, camera, CameraEvent.BeforeSkybox, vxgi); RenderSkyBox(renderContext, camera); TriggerCameraEvent(renderContext, camera, CameraEvent.AfterSkybox, vxgi); } UpdatePostProcessingLayer(renderContext, camera, vxgi); TriggerCameraEvent(renderContext, camera, CameraEvent.BeforeImageEffectsOpaque, vxgi); RenderPostProcessingOpaqueOnly(renderContext, camera); TriggerCameraEvent(renderContext, camera, CameraEvent.AfterImageEffectsOpaque, vxgi); TriggerCameraEvent(renderContext, camera, CameraEvent.BeforeForwardAlpha, vxgi); RenderTransparent(renderContext, camera); TriggerCameraEvent(renderContext, camera, CameraEvent.AfterForwardAlpha, vxgi); TriggerCameraEvent(renderContext, camera, CameraEvent.BeforeImageEffects, vxgi); RenderPostProcessing(renderContext, camera); _command.Blit(ShaderIDs.FrameBuffer, BuiltinRenderTextureType.CameraTarget); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); TriggerCameraEvent(renderContext, camera, CameraEvent.AfterImageEffects, vxgi); TriggerCameraEvent(renderContext, camera, CameraEvent.AfterEverything, vxgi); if (depthNormalsNeeded) { _command.ReleaseTemporaryRT(ShaderIDs._CameraDepthNormalsTexture); } _command.ReleaseTemporaryRT(ShaderIDs._CameraDepthTexture); _command.ReleaseTemporaryRT(ShaderIDs._CameraGBufferTexture0); _command.ReleaseTemporaryRT(ShaderIDs._CameraGBufferTexture1); _command.ReleaseTemporaryRT(ShaderIDs._CameraGBufferTexture2); _command.ReleaseTemporaryRT(ShaderIDs._CameraGBufferTexture3); _command.ReleaseTemporaryRT(ShaderIDs.FrameBuffer); _command.EndSample(_command.name); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); }
void UpdatePostProcessingLayer(ScriptableRenderContext renderContext, Camera camera, VXGI vxgi) { var layer = vxgi.GetComponent <PostProcessLayer>(); if (layer == null || !layer.isActiveAndEnabled) { return; } layer.UpdateVolumeSystem(camera, _command); renderContext.ExecuteCommandBuffer(_command); _command.Clear(); }
void TriggerCameraEvent(ScriptableRenderContext renderContext, Camera camera, CameraEvent cameraEvent, VXGI vxgi) { var commands = camera.GetCommandBuffers(cameraEvent); if (commands.Length == 0) { return; } _eventCommand.name = _sampleCameraEvent + cameraEvent.ToString(); _eventCommand.BeginSample(_eventCommand.name); _eventCommand.SetRenderTarget(ShaderIDs.FrameBuffer); renderContext.ExecuteCommandBuffer(_eventCommand); _eventCommand.Clear(); foreach (var command in commands) { renderContext.ExecuteCommandBuffer(command); } _eventCommand.EndSample(_eventCommand.name); renderContext.ExecuteCommandBuffer(_eventCommand); _eventCommand.Clear(); }