# SPDX-License-Identifier: GPL-3.0-or-later """Check production texture/image handle accessors with synthetic constant banks.""" import argparse import os from pathlib import Path import subprocess import tempfile RUN_ALL_ARGS = ["--cxx", "{cxx}"] # These production bodies have balanced braces; literals in them contain no braces. def function(text, signature): start = text.find(signature) assert start >= 0, f'Missing function anchor: {signature}' brace = text.index('{', start) depth = 1 end = brace + 1 while depth: depth += (text[end] == '{') - (text[end] == '}') end += 1 return text[start:end] def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument('--source-root', type=Path, default=Path(__file__).resolve().parents[1]) p.add_argument('--cxx', default='c++') a = p.parse_args() shader = (a.source_root / 'gpu/vk/vk_shader.cpp').read_text(encoding='utf-8') draw = (a.source_root / 'gpu/vk/vk_draw.cpp').read_text(encoding='utf-8') extracted = '\n'.join([ function(shader, 'uint32_t stage_cbuf_word('), function(draw, 'template \nuint32_t handle_of('), function(shader, 'uint32_t descriptor_handle(const NvnState& s, const Stage& st, const Shader::TextureDescriptor& d)'), function(shader, 'uint32_t descriptor_handle(const NvnState& s, const Stage& st, const Shader::ImageDescriptor& d)'), ]) prelude = r''' #define _CRT_SECURE_NO_WARNINGS 1 #include #include #include #include #include constexpr int kNvnStages=8; struct nvn_p_bind_buf { uint64_t addr=0, size=0; }; struct NvnState { nvn_p_bind_buf ubo[kNvnStages][16]{}; }; struct Stage { int nvn_stage=2; std::vector c1; }; namespace Shader { struct TextureDescriptor { uint32_t handle_program=~0U,cbuf_index=1,cbuf_offset=0,size_shift=3; bool has_secondary=false; uint32_t shift_left=0,secondary_cbuf_index=3,secondary_cbuf_offset=0,secondary_shift_left=8; }; struct ImageDescriptor { uint32_t handle_program=~0U,cbuf_index=1,cbuf_offset=0,size_shift=3; }; } uint32_t driver_bank_word(const NvnState&,int bank,int stage,uint32_t offset) { return bank==0 || bank==2 ? 0x1000U+uint32_t(bank*256+stage*16)+offset : 0; } uint8_t recorded[32]{},live_bytes[32]{}; int recorded_reads=0,live_reads=0,vm_reads=0; void ub_read(uint64_t addr,void* out,size_t n) { ++recorded_reads; std::memcpy(out,recorded+addr-4096,n); } struct Host { void gpu_read(uint64_t addr,void* out,size_t n) { ++live_reads; std::memcpy(out,live_bytes+addr-4096,n); } } host_object; Host* host=&host_object; uint32_t stage_handle(const NvnState&,const Stage&,uint32_t program,uint32_t element=0) { ++vm_reads; return 0xABC000U+program+0x1000U*element; } ''' checks = r''' int main() { int failures=0; auto eq=[&](uint32_t actual,uint32_t expected,const char* label) { if(actual!=expected) { std::cerr<