def pixel_debug(self, draw: rd.DrawcallDescription): pipe: rd.PipeState = self.controller.GetPipelineState() if pipe.GetShader(rd.ShaderStage.Pixel) == rd.ResourceId.Null(): rdtest.log.print("No pixel shader bound at {}: {}".format(draw.eventId, draw.name)) return if len(pipe.GetOutputTargets()) == 0 and pipe.GetDepthTarget().resourceId == rd.ResourceId.Null(): rdtest.log.print("No render targets bound at {}: {}".format(draw.eventId, draw.name)) return if not (draw.flags & rd.DrawFlags.Drawcall): rdtest.log.print("{}: {} is not a debuggable drawcall".format(draw.eventId, draw.name)) return viewport = pipe.GetViewport(0) # TODO, query for some pixel this drawcall actually touched. x = int(random.random()*viewport.width + viewport.x) y = int(random.random()*viewport.height + viewport.y) target = rd.ResourceId.Null() if len(pipe.GetOutputTargets()) > 0: valid_targets = [o.resourceId for o in pipe.GetOutputTargets() if o.resourceId != rd.ResourceId.Null()] rdtest.log.print("Valid targets at {} are {}".format(draw.eventId, valid_targets)) if len(valid_targets) > 0: target = valid_targets[int(random.random()*len(valid_targets))] if target == rd.ResourceId.Null(): target = pipe.GetDepthTarget().resourceId if target == rd.ResourceId.Null(): rdtest.log.print("No targets bound! Can't fetch history at {}".format(draw.eventId)) return rdtest.log.print("Fetching history for %d,%d on target %s" % (x, y, str(target))) history = self.controller.PixelHistory(target, x, y, rd.Subresource(0, 0, 0), rd.CompType.Typeless) rdtest.log.success("Pixel %d,%d has %d history events" % (x, y, len(history))) lastmod: rd.PixelModification = None for i in reversed(range(len(history))): mod = history[i] draw = self.find_draw('', mod.eventId) if draw is None or not (draw.flags & rd.DrawFlags.Drawcall): continue rdtest.log.print(" hit %d at %d is %s (%s)" % (i, mod.eventId, draw.name, str(draw.flags))) lastmod = history[i] rdtest.log.print("Got a hit on a drawcall at event %d" % lastmod.eventId) if mod.sampleMasked or mod.backfaceCulled or mod.depthClipped or mod.viewClipped or mod.scissorClipped or mod.shaderDiscarded or mod.depthTestFailed or mod.stencilTestFailed: rdtest.log.print("This hit failed, looking for one that passed....") lastmod = None continue if not mod.shaderOut.IsValid(): rdtest.log.print("This hit's shader out is not valid, looking for one that valid....") lastmod = None continue break if target == pipe.GetDepthTarget().resourceId: rdtest.log.print("Not doing pixel debug for depth output") return if lastmod is not None: rdtest.log.print("Debugging pixel {},{} @ {}, primitive {}".format(x, y, lastmod.eventId, lastmod.primitiveID)) self.controller.SetFrameEvent(lastmod.eventId, True) pipe: rd.PipeState = self.controller.GetPipelineState() trace = self.controller.DebugPixel(x, y, 0, lastmod.primitiveID) if trace.debugger is None: self.controller.FreeTrace(trace) rdtest.log.print("No debug result") return cycles, variables = self.process_trace(trace) output_index = [o.resourceId for o in pipe.GetOutputTargets()].index(target) if draw.outputs[0] == rd.ResourceId.Null(): rdtest.log.success('Successfully debugged pixel in {} cycles, skipping result check due to no output'.format(cycles)) self.controller.FreeTrace(trace) elif (draw.flags & rd.DrawFlags.Instanced) and draw.numInstances > 1: rdtest.log.success('Successfully debugged pixel in {} cycles, skipping result check due to instancing'.format(cycles)) self.controller.FreeTrace(trace) elif pipe.GetColorBlends()[output_index].writeMask == 0: rdtest.log.success('Successfully debugged pixel in {} cycles, skipping result check due to write mask'.format(cycles)) self.controller.FreeTrace(trace) else: rdtest.log.print("At event {} the target is index {}".format(lastmod.eventId, output_index)) output_sourcevar = self.find_output_source_var(trace, rd.ShaderBuiltin.ColorOutput, output_index) if output_sourcevar is not None: debugged = self.evaluate_source_var(output_sourcevar, variables) self.controller.FreeTrace(trace) debuggedValue = list(debugged.value.f32v[0:4]) # For now, ignore debugged values that are uninitialised. This is an application bug but it causes # false reports of problems for idx in range(4): if debugged.value.u32v[idx] == 0xcccccccc: debuggedValue[idx] = lastmod.shaderOut.col.floatValue[idx] # Unfortunately we can't ever trust that we should get back a matching results, because some shaders # rely on undefined/inaccurate maths that we don't emulate. # So the best we can do is log an error for manual verification is_eq, diff_amt = rdtest.value_compare_diff(lastmod.shaderOut.col.floatValue, debuggedValue, eps=5.0E-06) if not is_eq: rdtest.log.error( "Debugged value {} at EID {} {},{}: {} difference. {} doesn't exactly match history shader output {}".format( debugged.name, lastmod.eventId, x, y, diff_amt, debuggedValue, lastmod.shaderOut.col.floatValue)) rdtest.log.success('Successfully debugged pixel in {} cycles, result matches'.format(cycles)) else: # This could be an application error - undefined but seen in the wild rdtest.log.error("At EID {} No output variable declared for index {}".format(lastmod.eventId, output_index)) self.controller.SetFrameEvent(draw.eventId, True)
def vert_debug(self, draw: rd.DrawcallDescription): pipe: rd.PipeState = self.controller.GetPipelineState() refl: rd.ShaderReflection = pipe.GetShaderReflection(rd.ShaderStage.Vertex) if pipe.GetShader(rd.ShaderStage.Vertex) == rd.ResourceId.Null(): rdtest.log.print("No vertex shader bound at {}: {}".format(draw.eventId, draw.name)) return if not (draw.flags & rd.DrawFlags.Drawcall): rdtest.log.print("{}: {} is not a debuggable drawcall".format(draw.eventId, draw.name)) return vtx = int(random.random()*draw.numIndices) inst = 0 idx = vtx if draw.numIndices == 0: rdtest.log.print("Empty drawcall (0 vertices), skipping") return if draw.flags & rd.DrawFlags.Instanced: inst = int(random.random()*draw.numInstances) if draw.numInstances == 0: rdtest.log.print("Empty drawcall (0 instances), skipping") return if draw.flags & rd.DrawFlags.Indexed: ib = pipe.GetIBuffer() mesh = rd.MeshFormat() mesh.indexResourceId = ib.resourceId mesh.indexByteStride = ib.byteStride mesh.indexByteOffset = ib.byteOffset + draw.indexOffset * ib.byteStride mesh.indexByteSize = ib.byteSize mesh.baseVertex = draw.baseVertex indices = rdtest.fetch_indices(self.controller, draw, mesh, 0, vtx, 1) if len(indices) < 1: rdtest.log.print("No index buffer, skipping") return idx = indices[0] striprestart_index = pipe.GetStripRestartIndex() & ((1 << (ib.byteStride*8)) - 1) if pipe.IsStripRestartEnabled() and idx == striprestart_index: return rdtest.log.print("Debugging vtx %d idx %d (inst %d)" % (vtx, idx, inst)) postvs = self.get_postvs(draw, rd.MeshDataStage.VSOut, first_index=vtx, num_indices=1, instance=inst) trace: rd.ShaderDebugTrace = self.controller.DebugVertex(vtx, inst, idx, 0) if trace.debugger is None: self.controller.FreeTrace(trace) rdtest.log.print("No debug result") return cycles, variables = self.process_trace(trace) outputs = 0 for var in trace.sourceVars: var: rd.SourceVariableMapping if var.variables[0].type == rd.DebugVariableType.Variable and var.signatureIndex >= 0: name = var.name if name not in postvs[0].keys(): raise rdtest.TestFailureException("Don't have expected output for {}".format(name)) expect = postvs[0][name] value = self.evaluate_source_var(var, variables) if len(expect) != value.columns: raise rdtest.TestFailureException( "Output {} at EID {} has different size ({} values) to expectation ({} values)" .format(name, draw.eventId, value.columns, len(expect))) compType = rd.VarTypeCompType(value.type) if compType == rd.CompType.UInt: debugged = list(value.value.u32v[0:value.columns]) elif compType == rd.CompType.SInt: debugged = list(value.value.s32v[0:value.columns]) else: debugged = list(value.value.f32v[0:value.columns]) # For now, ignore debugged values that are uninitialised. This is an application bug but it causes false # reports of problems for comp in range(4): if value.value.u32v[comp] == 0xcccccccc: debugged[comp] = expect[comp] # Unfortunately we can't ever trust that we should get back a matching results, because some shaders # rely on undefined/inaccurate maths that we don't emulate. # So the best we can do is log an error for manual verification is_eq, diff_amt = rdtest.value_compare_diff(expect, debugged, eps=5.0E-06) if not is_eq: rdtest.log.error( "Debugged value {} at EID {} vert {} (idx {}) instance {}: {} difference. {} doesn't exactly match postvs output {}".format( name, draw.eventId, vtx, idx, inst, diff_amt, debugged, expect)) outputs = outputs + 1 rdtest.log.success('Successfully debugged vertex in {} cycles, {}/{} outputs match' .format(cycles, outputs, len(refl.outputSignature))) self.controller.FreeTrace(trace)
def check_capture(self): last_action: rd.ActionDescription = self.get_last_action() self.controller.SetFrameEvent(last_action.eventId, True) self.check_triangle(out=last_action.copyDestination) rdtest.log.success("Triangle output looks correct") action = self.find_action('CmdDraw') self.controller.SetFrameEvent(action.eventId, True) pipe: rd.PipeState = self.controller.GetPipelineState() vkpipe = self.controller.GetVulkanPipelineState() binding = vkpipe.graphics.descriptorSets[0].bindings[0] if binding.dynamicallyUsedCount != 1: raise rdtest.TestFailureException( "Bind 0 doesn't have the right used count {}".format( binding.dynamicallyUsedCount)) if not binding.binds[15].dynamicallyUsed: raise rdtest.TestFailureException( "Graphics bind 0[15] isn't dynamically used") refl: rd.ShaderReflection = pipe.GetShaderReflection( rd.ShaderStage.Vertex) self.check(len(refl.readOnlyResources) == 0) postvs = self.get_postvs(action, rd.MeshDataStage.VSOut, first_index=0, num_indices=1, instance=0) trace: rd.ShaderDebugTrace = self.controller.DebugVertex(0, 0, 0, 0) if trace.debugger is None: raise rdtest.TestFailureException("No vertex debug result") cycles, variables = self.process_trace(trace) outputs = 0 for var in trace.sourceVars: var: rd.SourceVariableMapping if var.variables[ 0].type == rd.DebugVariableType.Variable and var.signatureIndex >= 0: name = var.name if name not in postvs[0].keys(): raise rdtest.TestFailureException( "Don't have expected output for {}".format(name)) expect = postvs[0][name] value = self.evaluate_source_var(var, variables) if len(expect) != value.columns: raise rdtest.TestFailureException( "Vertex output {} has different size ({} values) to expectation ({} values)" .format(name, action.eventId, value.columns, len(expect))) compType = rd.VarTypeCompType(value.type) if compType == rd.CompType.UInt: debugged = list(value.value.u32v[0:value.columns]) elif compType == rd.CompType.SInt: debugged = list(value.value.s32v[0:value.columns]) else: debugged = list(value.value.f32v[0:value.columns]) is_eq, diff_amt = rdtest.value_compare_diff(expect, debugged, eps=5.0E-06) if not is_eq: rdtest.log.error( "Debugged vertex output value {}: {} difference. {} doesn't exactly match postvs output {}" .format(name, action.eventId, diff_amt, debugged, expect)) outputs = outputs + 1 rdtest.log.success( 'Successfully debugged vertex in {} cycles, {}/{} outputs match'. format(cycles, outputs, len(refl.outputSignature))) self.controller.FreeTrace(trace) history = self.controller.PixelHistory( pipe.GetOutputTargets()[0].resourceId, 200, 150, rd.Subresource(0, 0, 0), rd.CompType.Typeless) # should be a clear then a draw self.check(len(history) == 2) self.check( self.find_action('', history[0].eventId).flags & rd.ActionFlags.Clear) self.check( self.find_action('', history[1].eventId).eventId == action.eventId) self.check(history[1].Passed()) if not rdtest.value_compare(history[1].shaderOut.col.floatValue, (0.0, 1.0, 0.0, 1.0)): raise rdtest.TestFailureException( "History for drawcall output is wrong: {}".format( history[1].shaderOut.col.floatValue)) trace = self.controller.DebugPixel(200, 150, 0, 0) refl: rd.ShaderReflection = pipe.GetShaderReflection( rd.ShaderStage.Pixel) self.check(len(refl.readOnlyResources) == 1) if trace.debugger is None: raise rdtest.TestFailureException("No pixel debug result") cycles, variables = self.process_trace(trace) output_sourcevar = self.find_output_source_var( trace, rd.ShaderBuiltin.ColorOutput, 0) if output_sourcevar is None: raise rdtest.TestFailureException( "Couldn't get colour output value") debugged = self.evaluate_source_var(output_sourcevar, variables) self.controller.FreeTrace(trace) debuggedValue = list(debugged.value.f32v[0:4]) is_eq, diff_amt = rdtest.value_compare_diff( history[1].shaderOut.col.floatValue, debuggedValue, eps=5.0E-06) if not is_eq: rdtest.log.error( "Debugged pixel value {}: {} difference. {} doesn't exactly match history shader output {}" .format(debugged.name, diff_amt, debuggedValue, history[1].shaderOut.col.floatValue)) rdtest.log.success( 'Successfully debugged pixel in {} cycles, result matches'.format( cycles)) out = self.controller.CreateOutput( rd.CreateHeadlessWindowingData(100, 100), rd.ReplayOutputType.Texture) tex = rd.TextureDisplay() tex.resourceId = pipe.GetOutputTargets()[0].resourceId tex.overlay = rd.DebugOverlay.TriangleSizeDraw out.SetTextureDisplay(tex) out.Display() overlay_id = out.GetDebugOverlayTexID() self.check_pixel_value(overlay_id, 200, 150, [14992.0, 14992.0, 14992.0, 1.0]) rdtest.log.success("Triangle size overlay gave correct output") out.Shutdown()