Path: blob/21.2-virgl/src/intel/tools/aubinator_viewer_decoder.cpp
4547 views
/*1* Copyright © 2017 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <string.h>2425#include "util/macros.h"2627#include "aubinator_viewer.h"2829void30aub_viewer_decode_ctx_init(struct aub_viewer_decode_ctx *ctx,31struct aub_viewer_cfg *cfg,32struct aub_viewer_decode_cfg *decode_cfg,33const struct intel_device_info *devinfo,34struct intel_spec *spec,35struct intel_batch_decode_bo (*get_bo)(void *, bool, uint64_t),36unsigned (*get_state_size)(void *, uint32_t),37void *user_data)38{39memset(ctx, 0, sizeof(*ctx));4041ctx->get_bo = get_bo;42ctx->get_state_size = get_state_size;43ctx->user_data = user_data;44ctx->devinfo = devinfo;45ctx->engine = I915_ENGINE_CLASS_RENDER;4647ctx->cfg = cfg;48ctx->decode_cfg = decode_cfg;49ctx->spec = spec;50}5152static void53aub_viewer_print_group(struct aub_viewer_decode_ctx *ctx,54struct intel_group *group,55uint64_t address, const void *map)56{57struct intel_field_iterator iter;58int last_dword = -1;59const uint32_t *p = (const uint32_t *) map;6061intel_field_iterator_init(&iter, group, p, 0, false);62while (intel_field_iterator_next(&iter)) {63if (ctx->decode_cfg->show_dwords) {64int iter_dword = iter.end_bit / 32;65if (last_dword != iter_dword) {66for (int i = last_dword + 1; i <= iter_dword; i++) {67ImGui::TextColored(ctx->cfg->dwords_color,68"0x%012" PRIx64 ": 0x%012x : Dword %d",69address + 4 * i, iter.p[i], i);70}71last_dword = iter_dword;72}73}74if (!intel_field_is_header(iter.field)) {75if (ctx->decode_cfg->field_filter.PassFilter(iter.name)) {76if (iter.field->type.kind == intel_type::INTEL_TYPE_BOOL && iter.raw_value) {77ImGui::Text("%s: ", iter.name); ImGui::SameLine();78ImGui::TextColored(ctx->cfg->boolean_color, "true");79} else {80ImGui::Text("%s: %s", iter.name, iter.value);81}82if (iter.struct_desc) {83int struct_dword = iter.start_bit / 32;84uint64_t struct_address = address + 4 * struct_dword;85aub_viewer_print_group(ctx, iter.struct_desc, struct_address,86&p[struct_dword]);87}88}89}90}91}9293static struct intel_batch_decode_bo94ctx_get_bo(struct aub_viewer_decode_ctx *ctx, bool ppgtt, uint64_t addr)95{96if (intel_spec_get_gen(ctx->spec) >= intel_make_gen(8,0)) {97/* On Broadwell and above, we have 48-bit addresses which consume two98* dwords. Some packets require that these get stored in a "canonical99* form" which means that bit 47 is sign-extended through the upper100* bits. In order to correctly handle those aub dumps, we need to mask101* off the top 16 bits.102*/103addr &= (~0ull >> 16);104}105106struct intel_batch_decode_bo bo = ctx->get_bo(ctx->user_data, ppgtt, addr);107108if (intel_spec_get_gen(ctx->spec) >= intel_make_gen(8,0))109bo.addr &= (~0ull >> 16);110111/* We may actually have an offset into the bo */112if (bo.map != NULL) {113assert(bo.addr <= addr);114uint64_t offset = addr - bo.addr;115bo.map = (const uint8_t *)bo.map + offset;116bo.addr += offset;117bo.size -= offset;118}119120return bo;121}122123static int124update_count(struct aub_viewer_decode_ctx *ctx,125uint32_t offset_from_dsba,126unsigned element_dwords,127unsigned guess)128{129unsigned size = 0;130131if (ctx->get_state_size)132size = ctx->get_state_size(ctx->user_data, offset_from_dsba);133134if (size > 0)135return size / (sizeof(uint32_t) * element_dwords);136137/* In the absence of any information, just guess arbitrarily. */138return guess;139}140141static void142ctx_disassemble_program(struct aub_viewer_decode_ctx *ctx,143uint32_t ksp, const char *type)144{145uint64_t addr = ctx->instruction_base + ksp;146struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, addr);147if (!bo.map) {148ImGui::TextColored(ctx->cfg->missing_color,149"Shader unavailable addr=0x%012" PRIx64, addr);150return;151}152153ImGui::PushID(addr);154if (ImGui::Button(type) && ctx->display_shader)155ctx->display_shader(ctx->user_data, type, addr);156ImGui::PopID();157}158159static void160handle_state_base_address(struct aub_viewer_decode_ctx *ctx,161struct intel_group *inst,162const uint32_t *p)163{164struct intel_field_iterator iter;165intel_field_iterator_init(&iter, inst, p, 0, false);166167uint64_t surface_base = 0, dynamic_base = 0, instruction_base = 0;168bool surface_modify = 0, dynamic_modify = 0, instruction_modify = 0;169170while (intel_field_iterator_next(&iter)) {171if (strcmp(iter.name, "Surface State Base Address") == 0) {172surface_base = iter.raw_value;173} else if (strcmp(iter.name, "Dynamic State Base Address") == 0) {174dynamic_base = iter.raw_value;175} else if (strcmp(iter.name, "Instruction Base Address") == 0) {176instruction_base = iter.raw_value;177} else if (strcmp(iter.name, "Surface State Base Address Modify Enable") == 0) {178surface_modify = iter.raw_value;179} else if (strcmp(iter.name, "Dynamic State Base Address Modify Enable") == 0) {180dynamic_modify = iter.raw_value;181} else if (strcmp(iter.name, "Instruction Base Address Modify Enable") == 0) {182instruction_modify = iter.raw_value;183}184}185186if (dynamic_modify)187ctx->dynamic_base = dynamic_base;188189if (surface_modify)190ctx->surface_base = surface_base;191192if (instruction_modify)193ctx->instruction_base = instruction_base;194}195196static void197dump_binding_table(struct aub_viewer_decode_ctx *ctx, uint32_t offset, int count)198{199struct intel_group *strct =200intel_spec_find_struct(ctx->spec, "RENDER_SURFACE_STATE");201if (strct == NULL) {202ImGui::TextColored(ctx->cfg->missing_color, "did not find RENDER_SURFACE_STATE info");203return;204}205206if (count < 0)207count = update_count(ctx, offset, 1, 8);208209if (offset % 32 != 0 || offset >= UINT16_MAX) {210ImGui::TextColored(ctx->cfg->missing_color, "invalid binding table pointer");211return;212}213214struct intel_batch_decode_bo bind_bo =215ctx_get_bo(ctx, true, ctx->surface_base + offset);216217if (bind_bo.map == NULL) {218ImGui::TextColored(ctx->cfg->missing_color,219"binding table unavailable addr=0x%012" PRIx64,220ctx->surface_base + offset);221return;222}223224const uint32_t *pointers = (const uint32_t *) bind_bo.map;225for (int i = 0; i < count; i++) {226if (pointers[i] == 0)227continue;228229uint64_t addr = ctx->surface_base + pointers[i];230struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, addr);231uint32_t size = strct->dw_length * 4;232233if (pointers[i] % 32 != 0 ||234addr < bo.addr || addr + size >= bo.addr + bo.size) {235ImGui::TextColored(ctx->cfg->missing_color,236"pointer %u: %012x <not valid>", i, pointers[i]);237continue;238}239240const uint8_t *state = (const uint8_t *) bo.map + (addr - bo.addr);241if (ImGui::TreeNodeEx(&pointers[i], ImGuiTreeNodeFlags_Framed,242"pointer %u: %012x", i, pointers[i])) {243aub_viewer_print_group(ctx, strct, addr, state);244ImGui::TreePop();245}246}247}248249static void250dump_samplers(struct aub_viewer_decode_ctx *ctx, uint32_t offset, int count)251{252struct intel_group *strct = intel_spec_find_struct(ctx->spec, "SAMPLER_STATE");253254uint64_t state_addr = ctx->dynamic_base + offset;255struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, state_addr);256const uint8_t *state_map = (const uint8_t *) bo.map;257258if (state_map == NULL) {259ImGui::TextColored(ctx->cfg->missing_color,260"samplers unavailable addr=0x%012" PRIx64, state_addr);261return;262}263264if (offset % 32 != 0) {265ImGui::TextColored(ctx->cfg->missing_color, "invalid sampler state pointer");266return;267}268269const unsigned sampler_state_size = strct->dw_length * 4;270271if (count * sampler_state_size >= bo.size) {272ImGui::TextColored(ctx->cfg->missing_color, "sampler state ends after bo ends");273return;274}275276for (int i = 0; i < count; i++) {277if (ImGui::TreeNodeEx(state_map, ImGuiTreeNodeFlags_Framed,278"sampler state %d", i)) {279aub_viewer_print_group(ctx, strct, state_addr, state_map);280ImGui::TreePop();281}282state_addr += sampler_state_size;283state_map += sampler_state_size;284}285}286287static void288handle_media_interface_descriptor_load(struct aub_viewer_decode_ctx *ctx,289struct intel_group *inst,290const uint32_t *p)291{292struct intel_group *desc =293intel_spec_find_struct(ctx->spec, "INTERFACE_DESCRIPTOR_DATA");294295struct intel_field_iterator iter;296intel_field_iterator_init(&iter, inst, p, 0, false);297uint32_t descriptor_offset = 0;298int descriptor_count = 0;299while (intel_field_iterator_next(&iter)) {300if (strcmp(iter.name, "Interface Descriptor Data Start Address") == 0) {301descriptor_offset = strtol(iter.value, NULL, 16);302} else if (strcmp(iter.name, "Interface Descriptor Total Length") == 0) {303descriptor_count =304strtol(iter.value, NULL, 16) / (desc->dw_length * 4);305}306}307308uint64_t desc_addr = ctx->dynamic_base + descriptor_offset;309struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, desc_addr);310const uint32_t *desc_map = (const uint32_t *) bo.map;311312if (desc_map == NULL) {313ImGui::TextColored(ctx->cfg->missing_color,314"interface descriptors unavailable addr=0x%012" PRIx64, desc_addr);315return;316}317318for (int i = 0; i < descriptor_count; i++) {319ImGui::Text("descriptor %d: %012x", i, descriptor_offset);320321aub_viewer_print_group(ctx, desc, desc_addr, desc_map);322323intel_field_iterator_init(&iter, desc, desc_map, 0, false);324uint64_t ksp = 0;325uint32_t sampler_offset = 0, sampler_count = 0;326uint32_t binding_table_offset = 0, binding_entry_count = 0;327while (intel_field_iterator_next(&iter)) {328if (strcmp(iter.name, "Kernel Start Pointer") == 0) {329ksp = strtoll(iter.value, NULL, 16);330} else if (strcmp(iter.name, "Sampler State Pointer") == 0) {331sampler_offset = strtol(iter.value, NULL, 16);332} else if (strcmp(iter.name, "Sampler Count") == 0) {333sampler_count = strtol(iter.value, NULL, 10);334} else if (strcmp(iter.name, "Binding Table Pointer") == 0) {335binding_table_offset = strtol(iter.value, NULL, 16);336} else if (strcmp(iter.name, "Binding Table Entry Count") == 0) {337binding_entry_count = strtol(iter.value, NULL, 10);338}339}340341ctx_disassemble_program(ctx, ksp, "compute shader");342343dump_samplers(ctx, sampler_offset, sampler_count);344dump_binding_table(ctx, binding_table_offset, binding_entry_count);345346desc_map += desc->dw_length;347desc_addr += desc->dw_length * 4;348}349}350351static void352handle_3dstate_vertex_buffers(struct aub_viewer_decode_ctx *ctx,353struct intel_group *inst,354const uint32_t *p)355{356struct intel_group *vbs = intel_spec_find_struct(ctx->spec, "VERTEX_BUFFER_STATE");357358struct intel_batch_decode_bo vb = {};359uint32_t vb_size = 0;360int index = -1;361int pitch = -1;362bool ready = false;363364struct intel_field_iterator iter;365intel_field_iterator_init(&iter, inst, p, 0, false);366while (intel_field_iterator_next(&iter)) {367if (iter.struct_desc != vbs)368continue;369370uint64_t buffer_addr = 0;371372struct intel_field_iterator vbs_iter;373intel_field_iterator_init(&vbs_iter, vbs, &iter.p[iter.start_bit / 32], 0, false);374while (intel_field_iterator_next(&vbs_iter)) {375if (strcmp(vbs_iter.name, "Vertex Buffer Index") == 0) {376index = vbs_iter.raw_value;377} else if (strcmp(vbs_iter.name, "Buffer Pitch") == 0) {378pitch = vbs_iter.raw_value;379} else if (strcmp(vbs_iter.name, "Buffer Starting Address") == 0) {380buffer_addr = vbs_iter.raw_value;381vb = ctx_get_bo(ctx, true, buffer_addr);382} else if (strcmp(vbs_iter.name, "Buffer Size") == 0) {383vb_size = vbs_iter.raw_value;384ready = true;385} else if (strcmp(vbs_iter.name, "End Address") == 0) {386if (vb.map && vbs_iter.raw_value >= vb.addr)387vb_size = vbs_iter.raw_value - vb.addr;388else389vb_size = 0;390ready = true;391}392393if (!ready)394continue;395396ImGui::Text("vertex buffer %d, size %d, pitch %d", index, vb_size, pitch);397398if (vb.map == NULL) {399ImGui::TextColored(ctx->cfg->missing_color,400"buffer contents unavailable addr=0x%012" PRIx64, buffer_addr);401continue;402}403404if (vb.map == 0 || vb_size == 0)405continue;406407vb.map = NULL;408vb_size = 0;409index = -1;410pitch = -1;411ready = false;412}413}414}415416static void417handle_3dstate_index_buffer(struct aub_viewer_decode_ctx *ctx,418struct intel_group *inst,419const uint32_t *p)420{421struct intel_batch_decode_bo ib = {};422uint64_t buffer_addr = 0;423uint32_t ib_size = 0;424uint32_t format = 0;425426struct intel_field_iterator iter;427intel_field_iterator_init(&iter, inst, p, 0, false);428while (intel_field_iterator_next(&iter)) {429if (strcmp(iter.name, "Index Format") == 0) {430format = iter.raw_value;431} else if (strcmp(iter.name, "Buffer Starting Address") == 0) {432buffer_addr = iter.raw_value;433ib = ctx_get_bo(ctx, true, buffer_addr);434} else if (strcmp(iter.name, "Buffer Size") == 0) {435ib_size = iter.raw_value;436}437}438439if (ib.map == NULL) {440ImGui::TextColored(ctx->cfg->missing_color,441"buffer contents unavailable addr=0x%012" PRIx64,442buffer_addr);443return;444}445446const uint8_t *m = (const uint8_t *) ib.map;447const uint8_t *ib_end = m + MIN2(ib.size, ib_size);448for (int i = 0; m < ib_end && i < 10; i++) {449switch (format) {450case 0:451m += 1;452break;453case 1:454m += 2;455break;456case 2:457m += 4;458break;459}460}461}462463static void464decode_single_ksp(struct aub_viewer_decode_ctx *ctx,465struct intel_group *inst,466const uint32_t *p)467{468uint64_t ksp = 0;469bool is_simd8 = false; /* vertex shaders on Gfx8+ only */470bool is_enabled = true;471472struct intel_field_iterator iter;473intel_field_iterator_init(&iter, inst, p, 0, false);474while (intel_field_iterator_next(&iter)) {475if (strcmp(iter.name, "Kernel Start Pointer") == 0) {476ksp = iter.raw_value;477} else if (strcmp(iter.name, "SIMD8 Dispatch Enable") == 0) {478is_simd8 = iter.raw_value;479} else if (strcmp(iter.name, "Dispatch Mode") == 0) {480is_simd8 = strcmp(iter.value, "SIMD8") == 0;481} else if (strcmp(iter.name, "Dispatch Enable") == 0) {482is_simd8 = strcmp(iter.value, "SIMD8") == 0;483} else if (strcmp(iter.name, "Enable") == 0) {484is_enabled = iter.raw_value;485}486}487488const char *type =489strcmp(inst->name, "VS_STATE") == 0 ? "vertex shader" :490strcmp(inst->name, "GS_STATE") == 0 ? "geometry shader" :491strcmp(inst->name, "SF_STATE") == 0 ? "strips and fans shader" :492strcmp(inst->name, "CLIP_STATE") == 0 ? "clip shader" :493strcmp(inst->name, "3DSTATE_DS") == 0 ? "tessellation evaluation shader" :494strcmp(inst->name, "3DSTATE_HS") == 0 ? "tessellation control shader" :495strcmp(inst->name, "3DSTATE_VS") == 0 ? (is_simd8 ? "SIMD8 vertex shader" : "vec4 vertex shader") :496strcmp(inst->name, "3DSTATE_GS") == 0 ? (is_simd8 ? "SIMD8 geometry shader" : "vec4 geometry shader") :497NULL;498499if (is_enabled)500ctx_disassemble_program(ctx, ksp, type);501}502503static void504decode_ps_kernels(struct aub_viewer_decode_ctx *ctx,505struct intel_group *inst,506const uint32_t *p)507{508uint64_t ksp[3] = {0, 0, 0};509bool enabled[3] = {false, false, false};510511struct intel_field_iterator iter;512intel_field_iterator_init(&iter, inst, p, 0, false);513while (intel_field_iterator_next(&iter)) {514if (strncmp(iter.name, "Kernel Start Pointer ",515strlen("Kernel Start Pointer ")) == 0) {516int idx = iter.name[strlen("Kernel Start Pointer ")] - '0';517ksp[idx] = strtol(iter.value, NULL, 16);518} else if (strcmp(iter.name, "8 Pixel Dispatch Enable") == 0) {519enabled[0] = strcmp(iter.value, "true") == 0;520} else if (strcmp(iter.name, "16 Pixel Dispatch Enable") == 0) {521enabled[1] = strcmp(iter.value, "true") == 0;522} else if (strcmp(iter.name, "32 Pixel Dispatch Enable") == 0) {523enabled[2] = strcmp(iter.value, "true") == 0;524}525}526527/* Reorder KSPs to be [8, 16, 32] instead of the hardware order. */528if (enabled[0] + enabled[1] + enabled[2] == 1) {529if (enabled[1]) {530ksp[1] = ksp[0];531ksp[0] = 0;532} else if (enabled[2]) {533ksp[2] = ksp[0];534ksp[0] = 0;535}536} else {537uint64_t tmp = ksp[1];538ksp[1] = ksp[2];539ksp[2] = tmp;540}541542if (enabled[0])543ctx_disassemble_program(ctx, ksp[0], "SIMD8 fragment shader");544if (enabled[1])545ctx_disassemble_program(ctx, ksp[1], "SIMD16 fragment shader");546if (enabled[2])547ctx_disassemble_program(ctx, ksp[2], "SIMD32 fragment shader");548}549550static void551decode_3dstate_constant(struct aub_viewer_decode_ctx *ctx,552struct intel_group *inst,553const uint32_t *p)554{555struct intel_group *body =556intel_spec_find_struct(ctx->spec, "3DSTATE_CONSTANT_BODY");557558uint32_t read_length[4] = {0};559uint64_t read_addr[4];560561struct intel_field_iterator outer;562intel_field_iterator_init(&outer, inst, p, 0, false);563while (intel_field_iterator_next(&outer)) {564if (outer.struct_desc != body)565continue;566567struct intel_field_iterator iter;568intel_field_iterator_init(&iter, body, &outer.p[outer.start_bit / 32],5690, false);570571while (intel_field_iterator_next(&iter)) {572int idx;573if (sscanf(iter.name, "Read Length[%d]", &idx) == 1) {574read_length[idx] = iter.raw_value;575} else if (sscanf(iter.name, "Buffer[%d]", &idx) == 1) {576read_addr[idx] = iter.raw_value;577}578}579580for (int i = 0; i < 4; i++) {581if (read_length[i] == 0)582continue;583584struct intel_batch_decode_bo buffer = ctx_get_bo(ctx, true, read_addr[i]);585if (!buffer.map) {586ImGui::TextColored(ctx->cfg->missing_color,587"constant buffer %d unavailable addr=0x%012" PRIx64,588i, read_addr[i]);589continue;590}591592unsigned size = read_length[i] * 32;593ImGui::Text("constant buffer %d, size %u", i, size);594595if (ctx->edit_address) {596if (ImGui::Button("Show/Edit buffer"))597ctx->edit_address(ctx->user_data, read_addr[i], size);598}599}600}601}602603static void604decode_3dstate_binding_table_pointers(struct aub_viewer_decode_ctx *ctx,605struct intel_group *inst,606const uint32_t *p)607{608dump_binding_table(ctx, p[1], -1);609}610611static void612decode_3dstate_sampler_state_pointers(struct aub_viewer_decode_ctx *ctx,613struct intel_group *inst,614const uint32_t *p)615{616dump_samplers(ctx, p[1], 1);617}618619static void620decode_3dstate_sampler_state_pointers_gfx6(struct aub_viewer_decode_ctx *ctx,621struct intel_group *inst,622const uint32_t *p)623{624dump_samplers(ctx, p[1], 1);625dump_samplers(ctx, p[2], 1);626dump_samplers(ctx, p[3], 1);627}628629static bool630str_ends_with(const char *str, const char *end)631{632int offset = strlen(str) - strlen(end);633if (offset < 0)634return false;635636return strcmp(str + offset, end) == 0;637}638639static void640decode_dynamic_state_pointers(struct aub_viewer_decode_ctx *ctx,641struct intel_group *inst, const uint32_t *p,642const char *struct_type, int count)643{644uint32_t state_offset = 0;645646struct intel_field_iterator iter;647intel_field_iterator_init(&iter, inst, p, 0, false);648while (intel_field_iterator_next(&iter)) {649if (str_ends_with(iter.name, "Pointer")) {650state_offset = iter.raw_value;651break;652}653}654655uint64_t state_addr = ctx->dynamic_base + state_offset;656struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, state_addr);657const uint8_t *state_map = (const uint8_t *) bo.map;658659if (state_map == NULL) {660ImGui::TextColored(ctx->cfg->missing_color,661"dynamic %s state unavailable addr=0x%012" PRIx64,662struct_type, state_addr);663return;664}665666struct intel_group *state = intel_spec_find_struct(ctx->spec, struct_type);667if (strcmp(struct_type, "BLEND_STATE") == 0) {668/* Blend states are different from the others because they have a header669* struct called BLEND_STATE which is followed by a variable number of670* BLEND_STATE_ENTRY structs.671*/672ImGui::Text("%s", struct_type);673aub_viewer_print_group(ctx, state, state_addr, state_map);674675state_addr += state->dw_length * 4;676state_map += state->dw_length * 4;677678struct_type = "BLEND_STATE_ENTRY";679state = intel_spec_find_struct(ctx->spec, struct_type);680}681682for (int i = 0; i < count; i++) {683ImGui::Text("%s %d", struct_type, i);684aub_viewer_print_group(ctx, state, state_addr, state_map);685686state_addr += state->dw_length * 4;687state_map += state->dw_length * 4;688}689}690691static void692decode_3dstate_viewport_state_pointers_cc(struct aub_viewer_decode_ctx *ctx,693struct intel_group *inst,694const uint32_t *p)695{696decode_dynamic_state_pointers(ctx, inst, p, "CC_VIEWPORT", 4);697}698699static void700decode_3dstate_viewport_state_pointers_sf_clip(struct aub_viewer_decode_ctx *ctx,701struct intel_group *inst,702const uint32_t *p)703{704decode_dynamic_state_pointers(ctx, inst, p, "SF_CLIP_VIEWPORT", 4);705}706707static void708decode_3dstate_blend_state_pointers(struct aub_viewer_decode_ctx *ctx,709struct intel_group *inst,710const uint32_t *p)711{712decode_dynamic_state_pointers(ctx, inst, p, "BLEND_STATE", 1);713}714715static void716decode_3dstate_cc_state_pointers(struct aub_viewer_decode_ctx *ctx,717struct intel_group *inst,718const uint32_t *p)719{720decode_dynamic_state_pointers(ctx, inst, p, "COLOR_CALC_STATE", 1);721}722723static void724decode_3dstate_scissor_state_pointers(struct aub_viewer_decode_ctx *ctx,725struct intel_group *inst,726const uint32_t *p)727{728decode_dynamic_state_pointers(ctx, inst, p, "SCISSOR_RECT", 1);729}730731static void732decode_load_register_imm(struct aub_viewer_decode_ctx *ctx,733struct intel_group *inst,734const uint32_t *p)735{736struct intel_group *reg = intel_spec_find_register(ctx->spec, p[1]);737738if (reg != NULL &&739ImGui::TreeNodeEx(&p[1], ImGuiTreeNodeFlags_Framed,740"%s (0x%x) = 0x%x",741reg->name, reg->register_offset, p[2])) {742aub_viewer_print_group(ctx, reg, reg->register_offset, &p[2]);743ImGui::TreePop();744}745}746747static void748decode_3dprimitive(struct aub_viewer_decode_ctx *ctx,749struct intel_group *inst,750const uint32_t *p)751{752if (ctx->display_urb) {753if (ImGui::Button("Show URB"))754ctx->display_urb(ctx->user_data, ctx->urb_stages);755}756}757758static void759handle_urb(struct aub_viewer_decode_ctx *ctx,760struct intel_group *inst,761const uint32_t *p)762{763struct intel_field_iterator iter;764intel_field_iterator_init(&iter, inst, p, 0, false);765while (intel_field_iterator_next(&iter)) {766if (strstr(iter.name, "URB Starting Address")) {767ctx->urb_stages[ctx->stage].start = iter.raw_value * 8192;768} else if (strstr(iter.name, "URB Entry Allocation Size")) {769ctx->urb_stages[ctx->stage].size = (iter.raw_value + 1) * 64;770} else if (strstr(iter.name, "Number of URB Entries")) {771ctx->urb_stages[ctx->stage].n_entries = iter.raw_value;772}773}774775ctx->end_urb_offset = MAX2(ctx->urb_stages[ctx->stage].start +776ctx->urb_stages[ctx->stage].n_entries *777ctx->urb_stages[ctx->stage].size,778ctx->end_urb_offset);779}780781static void782handle_urb_read(struct aub_viewer_decode_ctx *ctx,783struct intel_group *inst,784const uint32_t *p)785{786struct intel_field_iterator iter;787intel_field_iterator_init(&iter, inst, p, 0, false);788while (intel_field_iterator_next(&iter)) {789/* Workaround the "Force * URB Entry Read Length" fields */790if (iter.end_bit - iter.start_bit < 2)791continue;792793if (strstr(iter.name, "URB Entry Read Offset")) {794ctx->urb_stages[ctx->stage].rd_offset = iter.raw_value * 32;795} else if (strstr(iter.name, "URB Entry Read Length")) {796ctx->urb_stages[ctx->stage].rd_length = iter.raw_value * 32;797} else if (strstr(iter.name, "URB Entry Output Read Offset")) {798ctx->urb_stages[ctx->stage].wr_offset = iter.raw_value * 32;799} else if (strstr(iter.name, "URB Entry Output Length")) {800ctx->urb_stages[ctx->stage].wr_length = iter.raw_value * 32;801}802}803}804805static void806handle_urb_constant(struct aub_viewer_decode_ctx *ctx,807struct intel_group *inst,808const uint32_t *p)809{810struct intel_group *body =811intel_spec_find_struct(ctx->spec, "3DSTATE_CONSTANT_BODY");812813struct intel_field_iterator outer;814intel_field_iterator_init(&outer, inst, p, 0, false);815while (intel_field_iterator_next(&outer)) {816if (outer.struct_desc != body)817continue;818819struct intel_field_iterator iter;820intel_field_iterator_init(&iter, body, &outer.p[outer.start_bit / 32],8210, false);822823ctx->urb_stages[ctx->stage].const_rd_length = 0;824while (intel_field_iterator_next(&iter)) {825int idx;826if (sscanf(iter.name, "Read Length[%d]", &idx) == 1) {827ctx->urb_stages[ctx->stage].const_rd_length += iter.raw_value * 32;828}829}830}831}832833struct custom_decoder {834const char *cmd_name;835void (*decode)(struct aub_viewer_decode_ctx *ctx,836struct intel_group *inst,837const uint32_t *p);838enum aub_decode_stage stage;839} display_decoders[] = {840{ "STATE_BASE_ADDRESS", handle_state_base_address },841{ "MEDIA_INTERFACE_DESCRIPTOR_LOAD", handle_media_interface_descriptor_load },842{ "3DSTATE_VERTEX_BUFFERS", handle_3dstate_vertex_buffers },843{ "3DSTATE_INDEX_BUFFER", handle_3dstate_index_buffer },844{ "3DSTATE_VS", decode_single_ksp, AUB_DECODE_STAGE_VS, },845{ "3DSTATE_GS", decode_single_ksp, AUB_DECODE_STAGE_GS, },846{ "3DSTATE_DS", decode_single_ksp, AUB_DECODE_STAGE_DS, },847{ "3DSTATE_HS", decode_single_ksp, AUB_DECODE_STAGE_HS, },848{ "3DSTATE_PS", decode_ps_kernels, AUB_DECODE_STAGE_PS, },849{ "3DSTATE_CONSTANT_VS", decode_3dstate_constant, AUB_DECODE_STAGE_VS, },850{ "3DSTATE_CONSTANT_GS", decode_3dstate_constant, AUB_DECODE_STAGE_GS, },851{ "3DSTATE_CONSTANT_DS", decode_3dstate_constant, AUB_DECODE_STAGE_DS, },852{ "3DSTATE_CONSTANT_HS", decode_3dstate_constant, AUB_DECODE_STAGE_HS, },853{ "3DSTATE_CONSTANT_PS", decode_3dstate_constant, AUB_DECODE_STAGE_PS, },854855{ "3DSTATE_BINDING_TABLE_POINTERS_VS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_VS, },856{ "3DSTATE_BINDING_TABLE_POINTERS_GS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_GS, },857{ "3DSTATE_BINDING_TABLE_POINTERS_HS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_HS, },858{ "3DSTATE_BINDING_TABLE_POINTERS_DS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_DS, },859{ "3DSTATE_BINDING_TABLE_POINTERS_PS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_PS, },860861{ "3DSTATE_SAMPLER_STATE_POINTERS_VS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_VS, },862{ "3DSTATE_SAMPLER_STATE_POINTERS_GS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_GS, },863{ "3DSTATE_SAMPLER_STATE_POINTERS_DS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_DS, },864{ "3DSTATE_SAMPLER_STATE_POINTERS_HS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_HS, },865{ "3DSTATE_SAMPLER_STATE_POINTERS_PS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_PS, },866{ "3DSTATE_SAMPLER_STATE_POINTERS", decode_3dstate_sampler_state_pointers_gfx6 },867868{ "3DSTATE_VIEWPORT_STATE_POINTERS_CC", decode_3dstate_viewport_state_pointers_cc },869{ "3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP", decode_3dstate_viewport_state_pointers_sf_clip },870{ "3DSTATE_BLEND_STATE_POINTERS", decode_3dstate_blend_state_pointers },871{ "3DSTATE_CC_STATE_POINTERS", decode_3dstate_cc_state_pointers },872{ "3DSTATE_SCISSOR_STATE_POINTERS", decode_3dstate_scissor_state_pointers },873{ "MI_LOAD_REGISTER_IMM", decode_load_register_imm },874{ "3DPRIMITIVE", decode_3dprimitive },875};876877struct custom_decoder info_decoders[] = {878{ "STATE_BASE_ADDRESS", handle_state_base_address },879{ "3DSTATE_URB_VS", handle_urb, AUB_DECODE_STAGE_VS, },880{ "3DSTATE_URB_GS", handle_urb, AUB_DECODE_STAGE_GS, },881{ "3DSTATE_URB_DS", handle_urb, AUB_DECODE_STAGE_DS, },882{ "3DSTATE_URB_HS", handle_urb, AUB_DECODE_STAGE_HS, },883{ "3DSTATE_VS", handle_urb_read, AUB_DECODE_STAGE_VS, },884{ "3DSTATE_GS", handle_urb_read, AUB_DECODE_STAGE_GS, },885{ "3DSTATE_DS", handle_urb_read, AUB_DECODE_STAGE_DS, },886{ "3DSTATE_HS", handle_urb_read, AUB_DECODE_STAGE_HS, },887{ "3DSTATE_PS", handle_urb_read, AUB_DECODE_STAGE_PS, },888{ "3DSTATE_CONSTANT_VS", handle_urb_constant, AUB_DECODE_STAGE_VS, },889{ "3DSTATE_CONSTANT_GS", handle_urb_constant, AUB_DECODE_STAGE_GS, },890{ "3DSTATE_CONSTANT_DS", handle_urb_constant, AUB_DECODE_STAGE_DS, },891{ "3DSTATE_CONSTANT_HS", handle_urb_constant, AUB_DECODE_STAGE_HS, },892{ "3DSTATE_CONSTANT_PS", handle_urb_constant, AUB_DECODE_STAGE_PS, },893};894895void896aub_viewer_render_batch(struct aub_viewer_decode_ctx *ctx,897const void *_batch, uint32_t batch_size,898uint64_t batch_addr, bool from_ring)899{900struct intel_group *inst;901const uint32_t *p, *batch = (const uint32_t *) _batch, *end = batch + batch_size / sizeof(uint32_t);902int length;903904if (ctx->n_batch_buffer_start >= 100) {905ImGui::TextColored(ctx->cfg->error_color,906"0x%08" PRIx64 ": Max batch buffer jumps exceeded", batch_addr);907return;908}909910ctx->n_batch_buffer_start++;911912for (p = batch; p < end; p += length) {913inst = intel_spec_find_instruction(ctx->spec, ctx->engine, p);914length = intel_group_get_length(inst, p);915assert(inst == NULL || length > 0);916length = MAX2(1, length);917918uint64_t offset = batch_addr + ((char *)p - (char *)batch);919920if (inst == NULL) {921ImGui::TextColored(ctx->cfg->error_color,922"0x%012" PRIx64 ": unknown instruction %012x",923offset, p[0]);924continue;925}926927const char *inst_name = intel_group_get_name(inst);928929for (unsigned i = 0; i < ARRAY_SIZE(info_decoders); i++) {930if (strcmp(inst_name, info_decoders[i].cmd_name) == 0) {931ctx->stage = info_decoders[i].stage;932info_decoders[i].decode(ctx, inst, p);933break;934}935}936937if (ctx->decode_cfg->command_filter.PassFilter(inst->name) &&938ImGui::TreeNodeEx(p,939ImGuiTreeNodeFlags_Framed,940"0x%012" PRIx64 ": %s",941offset, inst->name)) {942aub_viewer_print_group(ctx, inst, offset, p);943944for (unsigned i = 0; i < ARRAY_SIZE(display_decoders); i++) {945if (strcmp(inst_name, display_decoders[i].cmd_name) == 0) {946ctx->stage = display_decoders[i].stage;947display_decoders[i].decode(ctx, inst, p);948break;949}950}951952if (ctx->edit_address) {953if (ImGui::Button("Edit instruction"))954ctx->edit_address(ctx->user_data, offset, length * 4);955}956957ImGui::TreePop();958}959960if (strcmp(inst_name, "MI_BATCH_BUFFER_START") == 0) {961uint64_t next_batch_addr = 0xd0d0d0d0;962bool ppgtt = false;963bool second_level = false;964struct intel_field_iterator iter;965intel_field_iterator_init(&iter, inst, p, 0, false);966while (intel_field_iterator_next(&iter)) {967if (strcmp(iter.name, "Batch Buffer Start Address") == 0) {968next_batch_addr = iter.raw_value;969} else if (strcmp(iter.name, "Second Level Batch Buffer") == 0) {970second_level = iter.raw_value;971} else if (strcmp(iter.name, "Address Space Indicator") == 0) {972ppgtt = iter.raw_value;973}974}975976struct intel_batch_decode_bo next_batch = ctx_get_bo(ctx, ppgtt, next_batch_addr);977978if (next_batch.map == NULL) {979ImGui::TextColored(ctx->cfg->missing_color,980"Secondary batch at 0x%012" PRIx64 " unavailable",981next_batch_addr);982} else {983aub_viewer_render_batch(ctx, next_batch.map, next_batch.size,984next_batch.addr, false);985}986if (second_level) {987/* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts988* like a subroutine call. Commands that come afterwards get989* processed once the 2nd level batch buffer returns with990* MI_BATCH_BUFFER_END.991*/992continue;993} else if (!from_ring) {994/* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts995* like a goto. Nothing after it will ever get processed. In996* order to prevent the recursion from growing, we just reset the997* loop and continue;998*/999break;1000}1001} else if (strcmp(inst_name, "MI_BATCH_BUFFER_END") == 0) {1002break;1003}1004}10051006ctx->n_batch_buffer_start--;1007}100810091010