Repository navigation
Expand file tree
/
Copy pathsound_load.cpp
More file actions
206 lines (180 loc) · 5.33 KB
/
Copy pathsound_load.cpp
File metadata and controls
206 lines (180 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* ================================================================================================
* File: sound_load.cpp
* Brief: Background sound loads. See sound_load.h.
*
* A small table of reads in flight, each with the sfx it is for, its staging buffer (64-byte
* aligned, the file rounded up, tag Aio) and the file's location, which a loose file keeps open
* until the read is done. Everything here runs on the main thread; the loader only ever sees the
* staging buffer and the descriptor.
*
* This source code is released under the GNU GPL v2 license.
* ================================================================================================ */
#include "ps2/common.h"
#include "ps2/audio/sound_load.h"
#include "ps2/fs/async_io.h"
#include "ps2/system/heap.h"
#include "ps2/debug/profile.h" // ReadCycles
#include "ps2/renderer/profile.h" // FrameLogNoteReady
#include <cstdio>
#include <cstring>
namespace ps2::audio {
namespace {
constexpr int kMaxPending = 8;
struct PendingSound
{
sfx_s * sfx = nullptr; // null: the entry is free
aio::Ticket ticket;
fs_location_t loc;
u8 * staging = nullptr;
u32 stagingBytes = 0;
int length = 0;
char name[MAX_QPATH];
};
static PendingSound s_pending[kMaxPending];
static int s_numPending = 0;
// The read is over, one way or the other: builds the cache from the staging buffer, or reports,
// and frees the entry.
void Finish(PendingSound & p)
{
const aio::State state = aio::Poll(p.ticket);
const int result = aio::Result(p.ticket);
if (state == aio::State::Done && result == p.length) [[likely]]
{
PS2_SoundFinishLoad(p.sfx, p.staging, p.length); // reports a bad WAV itself
}
else
{
Com_Printf("WARNING: %s: background read failed (%d); the sound stays silent.\n", p.name, result);
}
aio::Release(p.ticket);
heap::Free(p.staging, p.stagingBytes, heap::MemTag::Aio);
FS_ReleaseLocation(&p.loc);
debug::FrameLogNoteReady(p.name);
p.sfx = nullptr;
p.staging = nullptr;
PS2_Assert(s_numPending > 0);
--s_numPending;
}
} // namespace
int PumpLoads(const u32 budgetCycles)
{
if (s_numPending == 0)
{
return 0;
}
const CpuCycles start = ReadCycles();
int finished = 0;
for (PendingSound & p : s_pending)
{
if (p.sfx == nullptr)
{
continue;
}
const aio::State state = aio::Poll(p.ticket);
if (state != aio::State::Done && state != aio::State::Failed)
{
continue;
}
Finish(p);
++finished;
if (ReadCycles() - start > budgetCycles)
{
break;
}
}
return finished;
}
} // namespace ps2::audio
// ------------------------------------------------------------------------------------------------
// C API
// ------------------------------------------------------------------------------------------------
using namespace ps2;
using namespace ps2::audio;
extern "C" int PS2_SoundLoadAsync(sfx_s * const sfx, const char * const path)
{
if (!aio::Enabled() || s_numPending == kMaxPending)
{
return 0;
}
PendingSound * entry = nullptr;
for (PendingSound & p : s_pending)
{
if (p.sfx == nullptr)
{
entry = &p;
break;
}
}
if (entry == nullptr)
{
return 0;
}
fs_location_t loc;
const int length = FS_LocateFile(path, &loc);
if (length <= 0 || loc.fd < 0)
{
if (length >= 0)
{
FS_ReleaseLocation(&loc);
}
return 0; // missing, empty or unlocatable: the synchronous path answers as it always did
}
const u32 stagingBytes = aio::RoundUp64(static_cast<u32>(length));
u8 * const staging = static_cast<u8 *>(heap::TryAllocAligned(heap::MemAlign(64), stagingBytes, heap::MemTag::Aio));
if (staging == nullptr)
{
FS_ReleaseLocation(&loc);
return 0;
}
aio::Request request;
request.kind = (loc.kind == FS_LOC_PKZ_DEFLATED) ? aio::Kind::InflateWhole : aio::Kind::Read;
request.prio = aio::Priority::Sound;
request.fd = loc.fd;
request.offset = loc.offset;
request.bytes = loc.bytes;
request.dest = staging;
request.destBytes = stagingBytes;
request.rawBytes = loc.rawBytes;
const aio::Ticket ticket = aio::Submit(request);
if (!ticket.IsValid())
{
heap::Free(staging, stagingBytes, heap::MemTag::Aio);
FS_ReleaseLocation(&loc);
return 0;
}
entry->sfx = sfx;
entry->ticket = ticket;
entry->loc = loc;
entry->staging = staging;
entry->stagingBytes = stagingBytes;
entry->length = length;
std::snprintf(entry->name, sizeof(entry->name), "%s", path);
++s_numPending;
return 1;
}
extern "C" int PS2_SoundLoadPending(const sfx_s * const sfx)
{
if (s_numPending == 0)
{
return 0;
}
for (const PendingSound & p : s_pending)
{
if (p.sfx == sfx)
{
return 1;
}
}
return 0;
}
extern "C" void PS2_SoundLoadsDrain(void)
{
for (PendingSound & p : s_pending)
{
if (p.sfx != nullptr)
{
aio::Wait(p.ticket);
Finish(p);
}
}
}