/* KallistiOS ##version## pvr_scene.c Copyright (C) 2002,2004 Megan Potter Copyright (C) 2024 Falco Girgis Copyright (C) 2026 Troy Davis */ #include #include #include #include #include #include #include #include #include #include #include "pvr_internal.h" /* FIXME: NDEBUG is a reserved C macro, we shouldn't use it like that... */ #ifdef NDEBUG # define PVR_DEBUG 0 #else # define PVR_DEBUG 1 #endif /* Scene rendering Please see ../../include/dc/pvr.h for more info on this API! */ void *pvr_set_vertbuf(pvr_list_t list, void *buffer, size_t len) { void *oldbuf; // Make sure we have global DMA usage enabled. The DMA can still // be used in other situations, but the user must take care of // that themselves. assert(pvr_state.dma_mode); // Make sure it's an _enabled_ list. assert(pvr_state.lists_enabled & BIT(list)); // Make sure the buffer parameters are valid. assert(__is_aligned(buffer, 32)); assert(!(len & 63)); // Save the old value. oldbuf = pvr_state.dma_buffers[0].base[list]; // Write new values. pvr_state.dma_buffers[0].base[list] = (uint8_t *)buffer; pvr_state.dma_buffers[0].ptr[list] = 0; pvr_state.dma_buffers[0].size[list] = len / 2; pvr_state.dma_buffers[0].ready = 0; pvr_state.dma_buffers[1].base[list] = ((uint8_t *)buffer) + len / 2; pvr_state.dma_buffers[1].ptr[list] = 0; pvr_state.dma_buffers[1].size[list] = len / 2; pvr_state.dma_buffers[1].ready = 0; return oldbuf; } void *pvr_vertbuf_tail(pvr_list_t list) { uint8_t *bufbase; // Check the validity of the request. assert(pvr_state.dma_mode); // Get the buffer base. bufbase = pvr_state.dma_buffers[pvr_state.ram_target].base[list]; assert(bufbase); // Return the current end of the buffer. return bufbase + pvr_state.dma_buffers[pvr_state.ram_target].ptr[list]; } void pvr_vertbuf_written(pvr_list_t list, size_t amt) { uint32_t val; // Check the validity of the request. assert(pvr_state.dma_mode); // Change the current end of the buffer. val = pvr_state.dma_buffers[pvr_state.ram_target].ptr[list]; val += amt; assert(val < pvr_state.dma_buffers[pvr_state.ram_target].size[list]); pvr_state.dma_buffers[pvr_state.ram_target].ptr[list] = val; } static void pvr_start_ta_rendering(void) { // Make sure to wait until the TA is ready to start rendering a new scene if(!pvr_state.ta_checked_ready) { pvr_wait_ready(); // If using a single vertex buffer, we have to wait until the PVR is // done rendering to use the TA again. if(!pvr_state.vbuf_doublebuf) pvr_wait_render_done(); pvr_state.ta_checked_ready = 1; pvr_state.curr_to_texture = pvr_state.next_to_texture; pvr_state.to_txr_rp = pvr_state.next_to_txr_rp; pvr_state.to_txr_w = pvr_state.next_to_txr_w; pvr_state.to_txr_h = pvr_state.next_to_txr_h; pvr_state.to_txr_stride_px = pvr_state.next_to_txr_stride_px; pvr_state.to_txr_addr = pvr_state.next_to_txr_addr; // Starting from that point, we consider that the Tile Accelerator // might be busy. pvr_state.ta_busy = 1; } } /* Begin collecting data for a frame of 3D output to the off-screen frame buffer */ void pvr_scene_begin(void) { int i; pvr_state.next_to_texture = 0; pvr_state.ta_checked_ready = 0; pvr_state.lists_closed = 0; // Get general stuff ready. pvr_state.list_reg_open = PVR_LIST_NONE; // Clear these out in case we're using DMA. if(pvr_state.dma_mode) { for(i = 0; i < PVR_OPB_COUNT; i++) { pvr_state.dma_buffers[pvr_state.ram_target].ptr[i] = 0; } pvr_sync_stats(PVR_SYNC_BUFSTART); } else { // We assume registration is starting immediately pvr_sync_stats(PVR_SYNC_REGSTART); } } void pvr_scene_begin_txr(pvr_ptr_t txr, uint32_t *rx, uint32_t *ry) { (void)ry; (void)pvr_scene_begin_rtt(txr, pvr_state.w, pvr_state.h, *rx); } int pvr_scene_begin_rtt(pvr_ptr_t txr, uint32_t render_w, uint32_t render_h, uint32_t stride_px) { if(!txr || render_w == 0 || render_h == 0 || stride_px < render_w || (stride_px & 3)) return -1; /* The existing render-to-texture path programs a 16-bit render pitch in 64-bit units. Keep that behavior explicit for the sized RTT API. */ pvr_state.next_to_txr_rp = stride_px * 2 / 8; pvr_state.next_to_txr_w = render_w; pvr_state.next_to_txr_h = render_h; pvr_state.next_to_txr_stride_px = stride_px; pvr_state.next_to_txr_addr = (uint32_t)(txr) - PVR_RAM_INT_BASE; pvr_scene_begin(); pvr_state.next_to_texture = 1; return 0; } static bool pvr_list_dma; inline static bool pvr_list_uses_dma(pvr_list_t list) { return pvr_state.dma_mode && pvr_state.dma_buffers[pvr_state.ram_target].base[list]; } /* Begin collecting data for the given list type. Lists do not have to be submitted in any particular order, but all types of a list must be submitted at once. If the given list has already been closed, then an error (-1) is returned. */ int pvr_list_begin(pvr_list_t list) { /* Check to make sure we can do this */ if(PVR_DEBUG && !pvr_state.dma_mode && pvr_state.lists_closed & BIT(list)) { dbglog(DBG_WARNING, "pvr_list_begin: attempt to open already closed list\n"); return -1; } /* If we already had a list open, close it first */ if(pvr_state.list_reg_open != PVR_LIST_NONE && pvr_state.list_reg_open != list) pvr_list_finish(); pvr_list_dma = pvr_list_uses_dma(list); if(!pvr_list_dma) { pvr_start_ta_rendering(); sq_lock((void *)PVR_TA_INPUT); } /* Ok, set the flag */ pvr_state.list_reg_open = list; return 0; } /* End collecting data for the current list type. Lists can never be opened again within a single frame once they have been closed. Thus submitting a primitive that belongs in a closed list is considered an error. Closing a list that is already closed is also an error (-1). Note that if you open a list but do not submit any primitives, this causes a hardware error. For simplicity we just always submit a blank primitive. */ int pvr_list_finish(void) { /* Check to make sure we can do this */ if(PVR_DEBUG && !pvr_state.dma_mode && pvr_state.list_reg_open == PVR_LIST_NONE) { dbglog(DBG_WARNING, "pvr_list_finish: attempt to close unopened list\n"); return -1; } /* Check for immediate submission: A. If we are not in DMA mode, we must be submitting polygons immediately. B. If we are in DMA mode, yet there's no vertex buffer associated with the list type, assume we're doing hybrid drawing and are directly submitting this list type. */ if(!pvr_list_dma) { /* In case we haven't sent anything in this list, send a dummy */ pvr_blank_polyhdr(pvr_state.list_reg_open); sq_unlock(); /* Set the flags */ pvr_state.lists_closed |= BIT(pvr_state.list_reg_open); /* Send an EOL marker */ pvr_sq_set32((void *)0, 0, 32, PVR_DMA_TA); } pvr_state.list_reg_open = PVR_LIST_NONE; return 0; } int pvr_prim(const void *data, size_t size) { /* Check to make sure we can do this */ if(PVR_DEBUG && pvr_state.list_reg_open == PVR_LIST_NONE) { dbglog(DBG_WARNING, "pvr_prim: attempt to submit to unopened list\n"); return -1; } if(!pvr_list_dma) { if(PVR_DEBUG && ((uintptr_t)data & 0x7)) { dbglog(DBG_WARNING, "pvr_prim: attempt to submit data unaligned " "to 8 bytes.\n"); return -1; } /* Immediately send data via SQs. */ sq_fast_cpy(SQ_MASK_DEST(PVR_TA_INPUT), data, size >> 5); } /* Defer data to RAM buffer for DMA-ing later. */ else return pvr_list_prim(pvr_state.list_reg_open, data, size); return 0; } int pvr_list_prim(pvr_list_t list, const void *data, size_t size) { volatile pvr_dma_buffers_t * b; b = pvr_state.dma_buffers + pvr_state.ram_target; /* Ensure we associated a DMA vertex buffer with this list type. */ assert(b->base[list]); /* Ensure data size is multiple of 32-bytes. */ assert(!(size & 31)); /* Ensure at least 4-byte alignment. */ assert(!((uintptr_t)data & 0x3)); /* Ensure we won't overflow the vertex buffer. */ assert(b->ptr[list] + size <= b->size[list]); memcpy(b->base[list] + b->ptr[list], data, size); b->ptr[list] += size; return 0; } int pvr_list_flush(pvr_list_t list) { (void)list; assert_msg(0, "not implemented yet"); return -1; } /* Call this after you have finished submitting all data for a frame; once this has been called, you can not submit any more data until one of the pvr_scene_begin() functions is called again. An error (-1) is returned if you have not started a scene already. */ int pvr_scene_finish(void) { int i, o; volatile pvr_dma_buffers_t *b; // If we're in DMA mode, then this works a little differently... if(pvr_state.dma_mode) { // If any enabled lists are empty, fill them with a blank polyhdr. Also // add a zero-marker to the end of each list. b = pvr_state.dma_buffers + pvr_state.ram_target; for(i = 0; i < PVR_OPB_COUNT; i++) { /* We never enabled the list globally with pvr_init() - skip it */ if(!(pvr_state.lists_enabled & BIT(i))) continue; /* If any lists weren't used in this scene, submit blank ones now */ if(!(pvr_state.lists_closed & BIT(i))) { pvr_list_begin(i); pvr_blank_polyhdr(i); pvr_list_finish(); } /* We never associated an in-RAM DMA vertex buffer with the given list type, because we're using hybrid rendering and submitted that list type directly - skip it */ if(!b->base[i]) continue; // Make sure there's at least one primitive in each. if(b->ptr[i] == 0) { pvr_blank_polyhdr_buf(i, (pvr_poly_hdr_t*)(b->base[i])); b->ptr[i] += 32; } // Put a zero-marker on the end. memset(b->base[i] + b->ptr[i], 0, 32); b->ptr[i] += 32; // Verify that there is no overrun. assert(b->ptr[i] <= b->size[i]); } pvr_start_ta_rendering(); // Flip buffers and mark them complete. o = irq_disable(); pvr_state.dma_buffers[pvr_state.ram_target].ready = 1; pvr_state.ram_target ^= 1; irq_restore(o); pvr_sync_stats(PVR_SYNC_BUFDONE); pvr_start_dma(); } else { /* If a list was open, close it */ if(pvr_state.list_reg_open != PVR_LIST_NONE) pvr_list_finish(); /* If any lists weren't submitted, then submit blank ones now */ for(i = 0; i < PVR_OPB_COUNT; i++) { if((pvr_state.lists_enabled & BIT(i)) && (!(pvr_state.lists_closed & BIT(i)))) { pvr_list_begin(i); pvr_blank_polyhdr(i); pvr_list_finish(); } } } /* Ok, now it's just a matter of waiting for the interrupt... */ return 0; } int pvr_wait_ready(void) { int flags, t = 0; assert(pvr_state.valid); flags = irq_disable(); if(pvr_state.ta_busy) t = genwait_wait((void *)&pvr_state.ta_busy, "PVR wait ready", 100); irq_restore(flags); if(t < 0) { #if 0 dbglog(DBG_WARNING, "pvr_wait_ready: timed out\n"); printf("VERTBUF_ADDR: %08lx\n", PVR_GET(PVR_ISP_VERTBUF_ADDR)); printf("TILEMAT_ADDR: %08lx\n", PVR_GET(PVR_ISP_TILEMAT_ADDR)); printf("OPB_START: %08lx\n", PVR_GET(PVR_TA_OPB_START)); printf("OPB_END: %08lx\n", PVR_GET(PVR_TA_OPB_END)); printf("OPB_POS: %08lx\n", PVR_GET(PVR_TA_OPB_POS)); printf("OPB_INIT: %08lx\n", PVR_GET(PVR_TA_OPB_INIT)); printf("VERTBUF_START: %08lx\n", PVR_GET(PVR_TA_VERTBUF_START)); printf("VERTBUF_END: %08lx\n", PVR_GET(PVR_TA_VERTBUF_END)); printf("VERTBUF_POS: %08lx\n", PVR_GET(PVR_TA_VERTBUF_POS)); #endif return -1; } return 0; } int pvr_check_ready(void) { assert(pvr_state.valid); if(!pvr_state.ta_busy) return 0; else return -1; } int pvr_wait_render_done(void) { int t = 0; irq_disable_scoped(); if(pvr_state.render_busy) t = genwait_wait((void *)&pvr_state.render_busy, "PVR wait render done", 100); return t; }