1bf215546Sopenharmony_ci-- Parse logs from test-quad-textured-3d.c to exctract layer/level
2bf215546Sopenharmony_ci-- offsets
3bf215546Sopenharmony_ci--
4bf215546Sopenharmony_ci-- We figure out the offsets from blits, but there may be some
5bf215546Sopenharmony_ci-- unrelated blits.  So just save all of them until we find the
6bf215546Sopenharmony_ci-- texture state for the 3d texture.  This gives us the base
7bf215546Sopenharmony_ci-- address, and the miplevel #0 width/height/depth.  Then work
8bf215546Sopenharmony_ci-- backwards from there finding the blits to the same dst buffer
9bf215546Sopenharmony_ci-- and deducing the miplevel from the minified dimensions
10bf215546Sopenharmony_ci
11bf215546Sopenharmony_cilocal posix = require "posix"
12bf215546Sopenharmony_ci
13bf215546Sopenharmony_ciio.write("Analyzing Data...\n")
14bf215546Sopenharmony_ci
15bf215546Sopenharmony_cilocal allblits = {}
16bf215546Sopenharmony_cilocal nallblits = 0
17bf215546Sopenharmony_cilocal r = rnn.init("a630")
18bf215546Sopenharmony_ci
19bf215546Sopenharmony_cifunction minify(val, lvls)
20bf215546Sopenharmony_ci  val = val >> lvls
21bf215546Sopenharmony_ci  if val < 1 then
22bf215546Sopenharmony_ci    return 1
23bf215546Sopenharmony_ci  end
24bf215546Sopenharmony_ci  return val
25bf215546Sopenharmony_ciend
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_cifunction printf(fmt, ...)
28bf215546Sopenharmony_ci  return io.write(string.format(fmt, ...))
29bf215546Sopenharmony_ciend
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cifunction start_cmdstream(name)
32bf215546Sopenharmony_ci  io.write("Parsing " .. name .. "\n")
33bf215546Sopenharmony_ci  allblits = {}
34bf215546Sopenharmony_ci  nallblits = 0
35bf215546Sopenharmony_ciend
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cifunction draw(primtype, nindx)
38bf215546Sopenharmony_ci  if primtype ~= "BLIT_OP_SCALE" then
39bf215546Sopenharmony_ci    return
40bf215546Sopenharmony_ci  end
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci  -- Just in case, filter out anything that isn't starting
43bf215546Sopenharmony_ci  -- at 0,0
44bf215546Sopenharmony_ci  if r.GRAS_2D_DST_TL.X ~= 0 or r.GRAS_2D_DST_TL.Y ~= 0 then
45bf215546Sopenharmony_ci    return
46bf215546Sopenharmony_ci  end
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci  local blit = {}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci  blit.width   = r.GRAS_2D_DST_BR.X + 1
51bf215546Sopenharmony_ci  blit.height  = r.GRAS_2D_DST_BR.Y + 1
52bf215546Sopenharmony_ci  blit.pitch   = r.RB_2D_DST_SIZE.PITCH
53bf215546Sopenharmony_ci  blit.addr    = r.RB_2D_DST_LO | (r.RB_2D_DST_HI << 32)
54bf215546Sopenharmony_ci  blit.base    = bos.base(blit.addr)
55bf215546Sopenharmony_ci  blit.endaddr = 0  -- filled in later
56bf215546Sopenharmony_ci  --printf("Found blit: 0x%x (0x%x)\n", blit.addr, blit.base)
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci  allblits[nallblits] = blit
59bf215546Sopenharmony_ci  nallblits = nallblits + 1
60bf215546Sopenharmony_ciend
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_cifunction A6XX_TEX_CONST(pkt, size)
63bf215546Sopenharmony_ci  -- ignore any texture state w/ DEPTH=1, these aren't the 3d tex state we
64bf215546Sopenharmony_ci  -- are looking for
65bf215546Sopenharmony_ci  if pkt[5].DEPTH <= 1 then
66bf215546Sopenharmony_ci    return
67bf215546Sopenharmony_ci  end
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci  local base = pkt[4].BASE_LO | (pkt[5].BASE_HI << 32)
70bf215546Sopenharmony_ci  local width0  = pkt[1].WIDTH
71bf215546Sopenharmony_ci  local height0 = pkt[1].HEIGHT
72bf215546Sopenharmony_ci  local depth0  = pkt[5].DEPTH
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci  printf("Found texture state: %ux%ux%u (MIN_LAYERSZ=0x%x)\n",
75bf215546Sopenharmony_ci         width0, height0, depth0, pkt[3].MIN_LAYERSZ)
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci  -- Note that in some case the texture has some extra page or so
78bf215546Sopenharmony_ci  -- at the beginning:
79bf215546Sopenharmony_ci  local basebase = bos.base(base)
80bf215546Sopenharmony_ci  printf("base: 0x%x (0x%x)\n", base, basebase)
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci  -- see if we can find the associated blits..  The blob always seems to
83bf215546Sopenharmony_ci  -- start from the lower (larger) mipmap levels and layers, so we don't
84bf215546Sopenharmony_ci  -- need to sort by dst address.  Also, while we are at it, fill in the
85bf215546Sopenharmony_ci  -- end-addr (at least for everything but the last blit)
86bf215546Sopenharmony_ci  local blits = {}
87bf215546Sopenharmony_ci  local nblits = 0
88bf215546Sopenharmony_ci  local lastblit = nil
89bf215546Sopenharmony_ci  for n = 0,nallblits-1 do
90bf215546Sopenharmony_ci    local blit = allblits[n]
91bf215546Sopenharmony_ci    --printf("blit addr: 0x%x (0x%x)\n", blit.addr, blit.base)
92bf215546Sopenharmony_ci    if blit.base == basebase and blit.addr >= base then
93bf215546Sopenharmony_ci      blits[nblits] = blit
94bf215546Sopenharmony_ci      nblits = nblits + 1
95bf215546Sopenharmony_ci      if lastblit then
96bf215546Sopenharmony_ci        lastblit.endaddr = blit.addr
97bf215546Sopenharmony_ci      end
98bf215546Sopenharmony_ci      lastblit = blit
99bf215546Sopenharmony_ci    end
100bf215546Sopenharmony_ci  end
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci  -- now go thru the relevant blits and print out interesting details
103bf215546Sopenharmony_ci  local level = 0
104bf215546Sopenharmony_ci  local layer = 0
105bf215546Sopenharmony_ci  local w = width0   -- track current width/height to detect changing
106bf215546Sopenharmony_ci  local h = height0  -- mipmap level
107bf215546Sopenharmony_ci  for n = 0,nblits-1 do
108bf215546Sopenharmony_ci    local blit = blits[n]
109bf215546Sopenharmony_ci    --printf("%u: %ux%u, addr=%x\n", n, blit.width, blit.height, blit.addr)
110bf215546Sopenharmony_ci    if w ~= blit.width or h ~= blit.height then
111bf215546Sopenharmony_ci      level = level + 1
112bf215546Sopenharmony_ci      layer = 0
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci      if blit.width ~= minify(w, 1) or blit.height ~= minify(h, 1) then
115bf215546Sopenharmony_ci        printf("I am confused! %ux%u vs %ux%u\n", blit.width, blit.height, minify(w, 1), minify(h, 1))
116bf215546Sopenharmony_ci	printf("addr=%x\n", blit.addr)
117bf215546Sopenharmony_ci        --return
118bf215546Sopenharmony_ci      end
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci      w = blit.width
121bf215546Sopenharmony_ci      h = blit.height
122bf215546Sopenharmony_ci    end
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci    printf("level=%u, layer=%u, sz=%ux%u, pitch=%u, offset=0x%x, addr=%x",
125bf215546Sopenharmony_ci           level, layer, w, h, blit.pitch, blit.addr - base, blit.addr)
126bf215546Sopenharmony_ci    if blit.endaddr ~= 0 then
127bf215546Sopenharmony_ci      local layersz = blit.endaddr - blit.addr
128bf215546Sopenharmony_ci      local alignedheight = layersz / blit.pitch
129bf215546Sopenharmony_ci      printf(", layersz=0x%x, alignedheight=%f", layersz, alignedheight)
130bf215546Sopenharmony_ci    end
131bf215546Sopenharmony_ci    printf("\n")
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci    layer = layer + 1
134bf215546Sopenharmony_ci  end
135bf215546Sopenharmony_ci  printf("\n\n")
136bf215546Sopenharmony_ciend
137bf215546Sopenharmony_ci
138