From 656de800cfe221119ec494b2da45b19e18741f49 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sun, 25 Sep 2022 17:18:04 -0500 Subject: [PATCH] renderSamples optimization only render sample types which are used stability remains to be tested --- src/engine/engine.cpp | 10 ++- src/engine/engine.h | 4 +- src/engine/sample.cpp | 22 +++--- src/engine/sample.h | 2 +- src/engine/sysDef.cpp | 171 +++++++++++++++++++++-------------------- src/gui/sampleEdit.cpp | 6 ++ 6 files changed, 119 insertions(+), 96 deletions(-) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index a2783c39..a7eac1b6 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -1015,9 +1015,17 @@ void DivEngine::renderSamples() { sPreview.pos=0; sPreview.dir=false; + // step 0: make sample format mask + unsigned int formatMask=1U<<16; // 16-bit is always on + for (int i=0; isampleFormatMask; + } + // step 1: render samples for (int i=0; irender(); + song.sample[i]->render(formatMask); } // step 2: render samples to dispatch diff --git a/src/engine/engine.h b/src/engine/engine.h index ff01d2e1..0e56b12f 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -227,6 +227,7 @@ struct DivSysDef { int channels; bool isFM, isSTD, isCompound; unsigned int vgmVersion; + unsigned int sampleFormatMask; const char* chanNames[DIV_MAX_CHANS]; const char* chanShortNames[DIV_MAX_CHANS]; int chanTypes[DIV_MAX_CHANS]; @@ -237,7 +238,7 @@ struct DivSysDef { const EffectHandlerMap postEffectHandlers; DivSysDef( const char* sysName, const char* sysNameJ, unsigned char fileID, unsigned char fileID_DMF, int chans, - bool isFMChip, bool isSTDChip, unsigned int vgmVer, bool compound, const char* desc, + bool isFMChip, bool isSTDChip, unsigned int vgmVer, bool compound, unsigned int formatMask, const char* desc, std::initializer_list chNames, std::initializer_list chShortNames, std::initializer_list chTypes, @@ -255,6 +256,7 @@ struct DivSysDef { isSTD(isSTDChip), isCompound(compound), vgmVersion(vgmVer), + sampleFormatMask(formatMask), effectHandlers(fxHandlers_), postEffectHandlers(postFxHandlers_) { memset(chanNames,0,DIV_MAX_CHANS*sizeof(void*)); diff --git a/src/engine/sample.cpp b/src/engine/sample.cpp index 6839a370..f4a0d98b 100644 --- a/src/engine/sample.cpp +++ b/src/engine/sample.cpp @@ -814,7 +814,9 @@ bool DivSample::resample(double r, int filter) { return false; } -void DivSample::render() { +#define NOT_IN_FORMAT(x) (depth!=x && formatMask&(1U<<(unsigned int)x)) + +void DivSample::render(unsigned int formatMask) { // step 1: convert to 16-bit if needed if (depth!=DIV_SAMPLE_DEPTH_16BIT) { if (!initInternal(DIV_SAMPLE_DEPTH_16BIT,samples)) return; @@ -863,7 +865,7 @@ void DivSample::render() { } // step 2: render to other formats - if (depth!=DIV_SAMPLE_DEPTH_1BIT) { // 1-bit + if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_1BIT)) { // 1-bit if (!initInternal(DIV_SAMPLE_DEPTH_1BIT,samples)) return; for (unsigned int i=0; i0) { @@ -871,7 +873,7 @@ void DivSample::render() { } } } - if (depth!=DIV_SAMPLE_DEPTH_1BIT_DPCM) { // DPCM + if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_1BIT_DPCM)) { // DPCM if (!initInternal(DIV_SAMPLE_DEPTH_1BIT_DPCM,samples)) return; int accum=63; for (unsigned int i=0; i127) accum=127; } } - if (depth!=DIV_SAMPLE_DEPTH_YMZ_ADPCM) { // YMZ ADPCM + if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_YMZ_ADPCM)) { // YMZ ADPCM if (!initInternal(DIV_SAMPLE_DEPTH_YMZ_ADPCM,samples)) return; ymz_encode(data16,dataZ,(samples+7)&(~0x7)); } - if (depth!=DIV_SAMPLE_DEPTH_QSOUND_ADPCM) { // QSound ADPCM + if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_QSOUND_ADPCM)) { // QSound ADPCM if (!initInternal(DIV_SAMPLE_DEPTH_QSOUND_ADPCM,samples)) return; bs_encode(data16,dataQSoundA,samples); } // TODO: pad to 256. - if (depth!=DIV_SAMPLE_DEPTH_ADPCM_A) { // ADPCM-A + if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_ADPCM_A)) { // ADPCM-A if (!initInternal(DIV_SAMPLE_DEPTH_ADPCM_A,samples)) return; yma_encode(data16,dataA,(samples+511)&(~0x1ff)); } - if (depth!=DIV_SAMPLE_DEPTH_ADPCM_B) { // ADPCM-B + if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_ADPCM_B)) { // ADPCM-B if (!initInternal(DIV_SAMPLE_DEPTH_ADPCM_B,samples)) return; ymb_encode(data16,dataB,(samples+511)&(~0x1ff)); } - if (depth!=DIV_SAMPLE_DEPTH_8BIT) { // 8-bit PCM + if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_8BIT)) { // 8-bit PCM if (!initInternal(DIV_SAMPLE_DEPTH_8BIT,samples)) return; for (unsigned int i=0; i>8; } } - if (depth!=DIV_SAMPLE_DEPTH_BRR) { // BRR + if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_BRR)) { // BRR if (!initInternal(DIV_SAMPLE_DEPTH_BRR,samples)) return; brrEncode(data16,dataBRR,(samples+15)&(~15),loop?loopStart:-1); } - if (depth!=DIV_SAMPLE_DEPTH_VOX) { // VOX + if (NOT_IN_FORMAT(DIV_SAMPLE_DEPTH_VOX)) { // VOX if (!initInternal(DIV_SAMPLE_DEPTH_VOX,samples)) return; oki_encode(data16,dataVOX,samples); } diff --git a/src/engine/sample.h b/src/engine/sample.h index 61f024ca..17ffb6fa 100644 --- a/src/engine/sample.h +++ b/src/engine/sample.h @@ -248,7 +248,7 @@ struct DivSample { /** * initialize the rest of sample formats for this sample. */ - void render(); + void render(unsigned int formatMask=0xffffffff); /** * get the sample data for the current depth. diff --git a/src/engine/sysDef.cpp b/src/engine/sysDef.cpp index 1196cf00..02f91a00 100644 --- a/src/engine/sysDef.cpp +++ b/src/engine/sysDef.cpp @@ -586,8 +586,9 @@ void DivEngine::registerSystems() { // SysDefs + // this chip uses YMZ ADPCM, but the emulator uses ADPCM-B because I got it wrong back then. sysDefs[DIV_SYSTEM_YMU759]=new DivSysDef( - "Yamaha YMU759 (MA-2)", NULL, 0x01, 0x01, 17, true, false, 0, false, + "Yamaha YMU759 (MA-2)", NULL, 0x01, 0x01, 17, true, false, 0, false, (1U<", {}, {}, {}, {} ); sysDefs[DIV_SYSTEM_GENESIS_EXT]=new DivSysDef( - "Sega Genesis Extended Channel 3", NULL, 0x42, 0x42, 13, true, true, 0, true, + "Sega Genesis Extended Channel 3", NULL, 0x42, 0x42, 13, true, true, 0, true, 0, "", {}, {}, {}, {} ); sysDefs[DIV_SYSTEM_SMS]=new DivSysDef( - "TI SN76489", NULL, 0x03, 0x03, 4, false, true, 0x150, false, + "TI SN76489", NULL, 0x03, 0x03, 4, false, true, 0x150, false, 0, "a square/noise sound chip found on the Sega Master System, ColecoVision, Tandy, TI's own 99/4A and a few other places.", {"Square 1", "Square 2", "Square 3", "Noise"}, {"S1", "S2", "S3", "NO"}, @@ -621,13 +622,13 @@ void DivEngine::registerSystems() { ); sysDefs[DIV_SYSTEM_SMS_OPLL]=new DivSysDef( - "Sega Master System + FM Expansion", NULL, 0x43, 0x43, 13, true, true, 0, true, + "Sega Master System + FM Expansion", NULL, 0x43, 0x43, 13, true, true, 0, true, 0, "", {}, {}, {}, {} ); sysDefs[DIV_SYSTEM_GB]=new DivSysDef( - "Game Boy", NULL, 0x04, 0x04, 4, false, true, 0x161, false, + "Game Boy", NULL, 0x04, 0x04, 4, false, true, 0x161, false, 0, "the most popular portable game console of the era.", {"Pulse 1", "Pulse 2", "Wavetable", "Noise"}, {"S1", "S2", "WA", "NO"}, @@ -644,7 +645,7 @@ void DivEngine::registerSystems() { ); sysDefs[DIV_SYSTEM_PCE]=new DivSysDef( - "PC Engine/TurboGrafx-16", NULL, 0x05, 0x05, 6, false, true, 0x161, false, + "PC Engine/TurboGrafx-16", NULL, 0x05, 0x05, 6, false, true, 0x161, false, 1U<", {}, {}, {}, {} ); sysDefs[DIV_SYSTEM_NES_FDS]=new DivSysDef( - "Famicom Disk System", NULL, 0, 0x86, 6, false, true, 0, true, + "Famicom Disk System", NULL, 0, 0x86, 6, false, true, 0, true, 0, "", {}, {}, {}, {} ); sysDefs[DIV_SYSTEM_C64_6581]=new DivSysDef( - "Commodore 64 (6581)", NULL, 0x47, 0x47, 3, false, true, 0, false, + "Commodore 64 (6581)", NULL, 0x47, 0x47, 3, false, true, 0, false, 0, "this computer is powered by the SID chip, which had synthesizer features like a filter and ADSR.", {"Channel 1", "Channel 2", "Channel 3"}, {"CH1", "CH2", "CH3"}, @@ -702,7 +703,7 @@ void DivEngine::registerSystems() { ); sysDefs[DIV_SYSTEM_C64_8580]=new DivSysDef( - "Commodore 64 (8580)", NULL, 0x07, 0x07, 3, false, true, 0, false, + "Commodore 64 (8580)", NULL, 0x07, 0x07, 3, false, true, 0, false, 0, "this computer is powered by the SID chip, which had synthesizer features like a filter and ADSR.\nthis is the newer revision of the chip.", {"Channel 1", "Channel 2", "Channel 3"}, {"CH1", "CH2", "CH3"}, @@ -714,13 +715,13 @@ void DivEngine::registerSystems() { ); sysDefs[DIV_SYSTEM_ARCADE]=new DivSysDef( - "DefleCade", NULL, 0x08, 0x08, 13, true, false, 0, true, + "DefleCade", NULL, 0x08, 0x08, 13, true, false, 0, true, 0, "", {}, {}, {}, {} ); sysDefs[DIV_SYSTEM_YM2610]=new DivSysDef( - "Neo Geo CD", NULL, 0x09, 0x09, 13, true, true, 0x151, false, + "Neo Geo CD", NULL, 0x09, 0x09, 13, true, true, 0x151, false, (1U<prepareUndo(true); + e->lockEngine([sample]() { + sample->render(); + }); sample->depth=(DivSampleDepth)i; e->renderSamplesP(); updateSampleTex=true; @@ -630,6 +633,9 @@ void FurnaceGUI::drawSampleEdit() { if (sampleDepths[i]==NULL) continue; if (ImGui::Selectable(sampleDepths[i])) { sample->prepareUndo(true); + e->lockEngine([sample]() { + sample->render(); + }); sample->depth=(DivSampleDepth)i; e->renderSamplesP(); updateSampleTex=true;