def CreateSteps(self): if not self.prepared: return self.ClearSteps() if self.liveCount < 1: return if self.source.width < 1 or self.source.height < 1: return postProcesses = [] for each in self.postProcesses: ppKey = getattr(each, 'key', None) if each is not None and (ppKey is None or ppKey == self.key): postProcesses.append(each) if self.destination is not None: self._AppendStep('Push Destination RT', trinity.TriStepPushRenderTarget(self.destination)) if len(postProcesses) > 1: self._AppendStep('Push Source RT', trinity.TriStepPushRenderTarget(self.source)) self._AppendStep('Push null depth stencil', trinity.TriStepPushDepthStencil(None)) if self.resolveTarget is not None: self._AppendStep( 'Resolve render target', trinity.TriStepResolve(self.resolveTarget, self.source)) self._AppendStep('Set render states', trinity.TriStepSetStdRndStates(trinity.RM_FULLSCREEN)) value = (1.0 / self.source.width, 1.0 / self.source.height, self.source.width, self.source.height) self._AppendStep('Set var texelSize', trinity.TriStepSetVariableStore('g_texelSize', value)) for pp in postProcesses: if pp == postProcesses[-1] and len(postProcesses) > 1: self._AppendStep('Pop source RT', trinity.TriStepPopRenderTarget()) self._DoPostProcess(pp, self.resolveTarget) if pp != postProcesses[-1] and self.resolveTarget is not None: self._AppendStep( 'Resolve render target', trinity.TriStepResolve(self.resolveTarget, self.source)) if self.destination is not None: self._AppendStep('Pop destination RT', trinity.TriStepPopRenderTarget()) self._AppendStep('Restore depth stencil', trinity.TriStepPopDepthStencil())
def SetRenderTargets(self, customBackBuffer, customDepthStencil, depthTexture, blitTexture, distortionTexture): self.RemoveStep('SET_DEPTH') if self.GetSwapChain() is not None: self.AddStep('SET_SWAPCHAIN_RT', trinity.TriStepSetRenderTarget(self.GetSwapChain().backBuffer)) self.AddStep('SET_SWAPCHAIN_DEPTH', trinity.TriStepSetDepthStencil(self.GetSwapChain().depthStencilBuffer)) else: self.RemoveStep('SET_SWAPCHAIN_RT') self.RemoveStep('SET_SWAPCHAIN_DEPTH') activePostProcessing = self.usePostProcessing and self.postProcessingJob.liveCount > 0 if customBackBuffer is not None: self.AddStep('SET_CUSTOM_RT', trinity.TriStepPushRenderTarget(customBackBuffer)) self.AddStep('SET_FINAL_RT', trinity.TriStepPopRenderTarget()) if self.msaaEnabled and not activePostProcessing: if self.hdrEnabled: self.AddStep('FINAL_BLIT', self._DoFormatConversionStep(blitTexture, customBackBuffer)) else: self.AddStep('FINAL_BLIT', trinity.TriStepResolve(self.GetBackBufferRenderTarget(), customBackBuffer)) elif self.hdrEnabled and not activePostProcessing and not self.msaaEnabled: self.AddStep('FINAL_BLIT', self._DoFormatConversionStep(customBackBuffer)) else: self.RemoveStep('FINAL_BLIT') if self.fxaaEnabled: self.AddStep('SET_VAR_GATHER', trinity.TriStepSetVariableStore('GatherMap', customBackBuffer)) self.RemoveStep('FINAL_BLIT') else: self.RemoveStep('SET_VAR_GATHER') else: self.RemoveStep('SET_CUSTOM_RT') self.RemoveStep('FINAL_BLIT') self.RemoveStep('SET_FINAL_RT') self.RemoveStep('SET_VAR_GATHER') if customDepthStencil is not None: self.AddStep('SET_DEPTH', trinity.TriStepPushDepthStencil(customDepthStencil)) self.AddStep('RESTORE_DEPTH', trinity.TriStepPopDepthStencil()) else: self.RemoveStep('RESTORE_DEPTH') if self.depthTexture is not None: if not self.doDepthPass: self.AddStep('SET_DEPTH', trinity.TriStepPushDepthStencil(depthTexture)) self.AddStep('RESTORE_DEPTH', trinity.TriStepPopDepthStencil()) self._SetDepthMap() self.AddStep('SET_VAR_DEPTH', trinity.TriStepSetVariableStore('DepthMap', depthTexture)) else: if not self.msaaEnabled: self.RemoveStep('SET_DEPTH') self.RemoveStep('RESTORE_DEPTH') self.RemoveStep('SET_VAR_DEPTH') self._RefreshPostProcessingJob(self.postProcessingJob, self.usePostProcessing and self.prepared) self._RefreshPostProcessingJob(self.distortionJob, self.distortionEffectsEnabled and self.prepared) self._RefreshPostProcessingJob(self.backgroundDistortionJob, self.distortionEffectsEnabled and self.prepared) if distortionTexture is not None: self.AddStep('DO_DISTORTIONS', trinity.TriStepRunJob(self.distortionJob)) distortionTriTextureRes = trinity.TriTextureRes() distortionTriTextureRes.SetFromRenderTarget(distortionTexture) self.distortionJob.SetPostProcessVariable('Distortion', 'TexDistortion', distortionTriTextureRes) self.backgroundDistortionJob.SetPostProcessVariable('Distortion', 'TexDistortion', distortionTriTextureRes) else: self.RemoveStep('DO_DISTORTIONS') self._CreateDepthPass()
def _CreateDepthPass(self): rj = trinity.TriRenderJob() if self.enabled and self.doDepthPass and self.depthTexture is not None: rj.steps.append(trinity.TriStepPushViewport()) rj.steps.append(trinity.TriStepPushRenderTarget(self._GetRTForDepthPass())) rj.steps.append(trinity.TriStepPushDepthStencil(self.depthTexture)) rj.steps.append(trinity.TriStepPopViewport()) rj.steps.append(trinity.TriStepPushViewport()) rj.steps.append(trinity.TriStepRenderPass(self.GetScene(), trinity.TRIPASS_DEPTH_PASS)) rj.steps.append(trinity.TriStepPopDepthStencil()) rj.steps.append(trinity.TriStepPopRenderTarget()) rj.steps.append(trinity.TriStepPopViewport()) self.AddStep('RENDER_DEPTH_PASS', trinity.TriStepRunJob(rj))
def Initialize(self, size, speed, amplitude, tiling, texturePath): """ Initializes the output texture, render job, builds render steps. Returns the output texture. """ def TextureDestroyed(): """ Closure for weakref callback. Destroys the render job. """ self.Destroy() texture = trinity.Tr2RenderTarget(size, size, 1, trinity.PIXEL_FORMAT.B8G8R8A8_UNORM) self.name = 'Caustics' self.size = size self.texture = blue.BluePythonWeakRef(texture) self.texture.callback = TextureDestroyed self.steps.append(trinity.TriStepPushRenderTarget(texture)) self.steps.append(trinity.TriStepClear((0, 0, 0, 0))) self.steps.append(trinity.TriStepSetStdRndStates( trinity.RM_FULLSCREEN)) material = trinity.Tr2ShaderMaterial() material.highLevelShaderName = 'Caustics' param = trinity.TriTexture2DParameter() param.name = 'Texture' param.resourcePath = texturePath material.parameters['Texture'] = param param = trinity.Tr2FloatParameter() param.name = 'Speed' param.value = speed material.parameters['Speed'] = param param = trinity.Tr2FloatParameter() param.name = 'Amplitude' param.value = amplitude material.parameters['Amplitude'] = param param = trinity.Tr2FloatParameter() param.name = 'Tiling' param.value = tiling material.parameters['Tiling'] = param material.BindLowLevelShader([]) self.steps.append(trinity.TriStepRenderFullScreenShader(material)) self.steps.append(trinity.TriStepPopRenderTarget()) trinity.renderJobs.recurring.append(self) return trinity.TriTextureRes(texture)
def DoPrepareResources(self): if self.GetSwapChain(): self.AddStep( 'SET_RENDERTARGET', trinity.TriStepPushRenderTarget( self.GetSwapChain().backBuffer)) self.AddStep( 'SET_DEPTH', trinity.TriStepPushDepthStencil( self.GetSwapChain().depthStencilBuffer)) self.AddStep('RESTORE_RENDERTARGET', trinity.TriStepPopRenderTarget()) self.AddStep('RESTORE_DEPTH', trinity.TriStepPopDepthStencil()) self.AddStep('CLEAR', trinity.TriStepClear((0.0, 0.0, 0.0, 0.0), 1.0)) else: self.RemoveStep('SET_RENDERTARGET') self.RemoveStep('SET_DEPTH') self.RemoveStep('RESTORE_RENDERTARGET') self.RemoveStep('RESTORE_DEPTH')
def AddSteps(self, rj): del self.steps[:] effects = self.postProcess.stages if len(effects) > 1: self._AppendStep(rj, trinity.TriStepPushRenderTarget(), 'Store RT') value = (1.0 / self.swapSize[0], 1.0 / self.swapSize[1], self.swapSize[0], self.swapSize[1]) self._AppendStep( rj, trinity.TriStepSetVariableStore('g_texelSize', value), 'Set swap texelSize') self._AppendStep(rj, trinity.TriStepSetRenderTarget(self.buffer1), 'Set RT') self._AppendStep(rj, trinity.TriStepRenderEffect(effects[0]), effects[0].name) for i in range(1, len(effects) - 1): self._AppendStep(rj, trinity.TriStepSetRenderTarget(self.buffer2), 'Swap RT') self._AppendStep( rj, trinity.TriStepSetVariableStore('BlitCurrent', self.buffer1), 'Override var BlitCurrent') self._AppendStep(rj, trinity.TriStepRenderEffect(effects[i]), effects[i].name) self._SwapBuffers() self._AppendStep( rj, trinity.TriStepSetVariableStore('BlitCurrent', self.buffer1), 'Override var BlitCurrent') self._AppendStep(rj, trinity.TriStepPopRenderTarget(), 'Restore RT') value = (1.0 / self.sourceSize[0], 1.0 / self.sourceSize[1], self.sourceSize[0], self.sourceSize[1]) self._AppendStep( rj, trinity.TriStepSetVariableStore('g_texelSize', value), 'Set source texelSize') self._AppendStep(rj, trinity.TriStepRenderEffect(effects[-1]), effects[-1].name)
def SetRenderTargets(self, *args): SceneRenderJobSpace.SetRenderTargets(self, *args) self.RemoveStep('PUSH_RENDER_TARGET') self.RemoveStep('PUSH_DEPTH_STENCIL') self.RemoveStep('POP_RENDER_TARGET') self.RemoveStep('POP_DEPTH_STENCIL') self.RemoveStep('SET_BLIT_VIEWPORT') self.RemoveStep('COPY_TO_BLIT_TEXTURE') self.RemoveStep('FINAL_BLIT_EMBEDDED') self.RemoveStep('PUSH_BLIT_DEPTH') self.RemoveStep('POP_BLIT_DEPTH') viewport = self.GetViewport() if viewport is not None: vpStep = self.GetStep('SET_VIEWPORT') if vpStep is not None: vpOrigin = trinity.TriViewport(0, 0, viewport.width, viewport.height) vpStep.viewport = vpOrigin if self.offscreenDepthStencil: self.AddStep('PUSH_DEPTH_STENCIL', trinity.TriStepPushDepthStencil(self.offscreenDepthStencil)) self.AddStep('POP_DEPTH_STENCIL', trinity.TriStepPopDepthStencil()) if self.offscreenRenderTarget: self.AddStep('PUSH_RENDER_TARGET', trinity.TriStepPushRenderTarget(self.offscreenRenderTarget)) self.AddStep('POP_RENDER_TARGET', trinity.TriStepPopRenderTarget()) self.AddStep('PUSH_BLIT_DEPTH', trinity.TriStepPushDepthStencil(None)) self.RemoveStep('FINAL_BLIT') if self.customBackBuffer is not None and self.finalTexture is not None: step = trinity.TriStepResolve(self.finalTexture, self.customBackBuffer) step.name = 'Resolve: finalTexture <== customBackBuffer' self.AddStep('COPY_TO_BLIT_TEXTURE', step) if self.doFinalBlit: self.AddStep('SET_BLIT_VIEWPORT', trinity.TriStepSetViewport(viewport)) if self.finalTexture: if self.stencilPath is None or self.stencilPath == '': self.AddStep('FINAL_BLIT_EMBEDDED', trinity.TriStepRenderTexture(self.finalTexture)) else: stencilTriTextureRes = trinity.TriTextureRes() stencilTriTextureRes.SetFromRenderTarget(self.finalTexture) self.blitMapParameter.SetResource(stencilTriTextureRes) self.AddStep('FINAL_BLIT_EMBEDDED', trinity.TriStepRenderEffect(self.stencilBlitEffect)) self.AddStep('POP_BLIT_DEPTH', trinity.TriStepPopDepthStencil())
def SetSettingsBasedOnPerformancePreferences(self): if not self.enabled: return aaQuality = settings.public.device.Get('antiAliasing', 0) msaaType = 4 if sm.IsServiceRunning('device'): msaaType = sm.GetService('device').GetMSAATypeFromQuality( aaQuality) if msaaType > 1: width, height = self.GetBackBufferSize() bbFormat = trinity.renderContext.GetBackBufferFormat() dsFormat = trinity.device.depthStencilFormat self.customBackBuffer = rtm.GetRenderTargetMsaaAL( width, height, bbFormat, msaaType, 0) self.AddStep( 'SET_BACKBUFFER', trinity.TriStepPushRenderTarget(self.customBackBuffer)) self.customDepthStencil = rtm.GetDepthStencilAL( width, height, dsFormat, msaaType) self.AddStep( 'SET_DEPTH_STENCIL', trinity.TriStepPushDepthStencil(self.customDepthStencil)) self.AddStep('RESTORE_BACKBUFFER', trinity.TriStepPopRenderTarget()) self.AddStep('RESTORE_DEPTH_STENCIL', trinity.TriStepPopDepthStencil()) self.AddStep( 'RESOLVE_IMAGE', trinity.TriStepResolve(self.GetBackBufferRenderTarget(), self.customBackBuffer)) else: self.customBackBuffer = None self.customDepthStencil = None self.RemoveStep('SET_BACKBUFFER') self.RemoveStep('SET_DEPTH_STENCIL') self.RemoveStep('RESTORE_BACKBUFFER') self.RemoveStep('RESTORE_DEPTH_STENCIL') self.RemoveStep('RESOLVE_IMAGE')
def CreateSsaoRenderJob(options, width, height, outputRT): randomTexture = CreateRandomTexture(options) if not randomTexture.isGood: return linearizeDepth = CreateMaterial('linearizeDepth') shaderSSAO = CreateMaterial('normalFree useNormals') linearizeDepthAndPackAODepth = CreateMaterial('linearizeDepthAndPack') blurX = CreateMaterial('blurX') if not options.disableBlending: blurY = CreateMaterial('blurY blend') else: blurY = CreateMaterial('blurY') SetMaterialConstants(options, width, height, linearizeDepth, linearizeDepthAndPackAODepth, blurX, blurY, shaderSSAO, randomTexture) curHistoryAOZRT = trinity.Tr2RenderTarget( width, height, 1, trinity.PIXEL_FORMAT.R16G16_FLOAT) fullResAOZRT = trinity.Tr2RenderTarget(width, height, 1, trinity.PIXEL_FORMAT.R16G16_FLOAT) if options.resolutionMode == SSAO_HALF_RES_AO: linDepthRT = trinity.Tr2RenderTarget(width / 2, height / 2, 1, trinity.PIXEL_FORMAT.R32_FLOAT) halfResAORT = trinity.Tr2RenderTarget(width / 2, height / 2, 1, trinity.PIXEL_FORMAT.R8_UNORM) outputAOZ = halfResAORT else: linDepthRT = trinity.Tr2RenderTarget(width, height, 1, trinity.PIXEL_FORMAT.R32_FLOAT) outputAOZ = curHistoryAOZRT AddMaterialParam(shaderSSAO, 'DepthMap', linDepthRT) AddMaterialParam(shaderSSAO, 'RandomMap', randomTexture) if options.resolutionMode == SSAO_HALF_RES_AO: AddMaterialParam(linearizeDepthAndPackAODepth, 'SSAOMap', halfResAORT) AddMaterialParam(blurX, 'SSAOMap', curHistoryAOZRT) AddMaterialParam(blurY, 'SSAOMap', fullResAOZRT) linearizeDepth.BindLowLevelShader([]) shaderSSAO.BindLowLevelShader([]) linearizeDepthAndPackAODepth.BindLowLevelShader([]) blurX.BindLowLevelShader([]) blurY.BindLowLevelShader([]) rj = trinity.TriRenderJob() AddStep(rj, 'SAVE_DS', trinity.TriStepPushDepthStencil(None)) AddStep(rj, 'SAVE_RT', trinity.TriStepPushRenderTarget()) cb = GetSSAOCallbackStep(options, width, height, shaderSSAO, linearizeDepth, linearizeDepthAndPackAODepth) AddStep(rj, 'UPDATE_CONSTANTS', trinity.TriStepPythonCB(cb)) AddStep(rj, 'SET_FULLSCREEN_STATES', trinity.TriStepSetStdRndStates(trinity.RM_FULLSCREEN)) AddStep(rj, 'SET_LINEAR_DEPTH_RT', trinity.TriStepSetRenderTarget(linDepthRT)) AddStep(rj, 'LINEARIZE_DEPTH', trinity.TriStepRenderFullScreenShader(linearizeDepth)) AddStep(rj, 'SET_AO_RT', trinity.TriStepSetRenderTarget(outputAOZ)) AddStep(rj, 'RENDER_AO', trinity.TriStepRenderFullScreenShader(shaderSSAO)) if options.resolutionMode == SSAO_HALF_RES_AO: AddStep(rj, 'SET_TEMP_AO_RT', trinity.TriStepSetRenderTarget(curHistoryAOZRT)) AddStep( rj, 'PACK_DEPTH_AND_AO', trinity.TriStepRenderFullScreenShader( linearizeDepthAndPackAODepth)) AddStep(rj, 'SET_FULL_AO_RT', trinity.TriStepSetRenderTarget(fullResAOZRT)) AddStep(rj, 'BLUR_X', trinity.TriStepRenderFullScreenShader(blurX)) if outputRT is None: AddStep(rj, 'RESTORE_RT', trinity.TriStepPopRenderTarget()) else: AddStep(rj, 'SET_OUTPUT_RT', trinity.TriStepSetRenderTarget(outputRT)) AddStep(rj, 'BLUR_Y', trinity.TriStepRenderFullScreenShader(blurY)) if outputRT: AddStep(rj, 'RESTORE_RT', trinity.TriStepPopRenderTarget()) AddStep(rj, 'RESTORE_DS', trinity.TriStepPopDepthStencil()) return rj