140 lines
4.1 KiB
C++
140 lines
4.1 KiB
C++
#include <config.h>
|
|
#include <opts.h>
|
|
#include <algorithm>
|
|
#include <unistd.h>
|
|
#include <user/spMcRingBuffer.h>
|
|
#include <componentThread.h>
|
|
#include "pcloudStimulusBuffer.h"
|
|
|
|
namespace smo {
|
|
namespace stim_buff {
|
|
|
|
extern const SmoCallbacks* smoHooksPtr;
|
|
|
|
// OpenCL kernels are used to collate and produce our StimFrames.
|
|
static SpMcRingBuffer::InputEngineConstraints openClInputConstraints(
|
|
static_cast<size_t>(sysconf(_SC_PAGE_SIZE)), sizeof(void *));
|
|
|
|
PcloudStimulusBuffer::PcloudStimulusBuffer(
|
|
const std::shared_ptr<device::DeviceAttachmentSpec> &deviceAttachmentSpec,
|
|
std::shared_ptr<livoxProto1::Device> &device,
|
|
const PcloudFormatDesc& formatDesc,
|
|
int histbuffMs,
|
|
size_t nDgramsPerStagingBufferFrame)
|
|
: StimulusBuffer(
|
|
deviceAttachmentSpec,
|
|
static_cast<size_t>(histbuffMs / CONFIG_STIMBUFF_FRAME_PERIOD_MS),
|
|
openClInputConstraints,
|
|
device->componentThread->getIoService()),
|
|
device(device),
|
|
formatDesc(formatDesc),
|
|
openClCollatingAndMeshingEngine(*this),
|
|
assemblyBuffer(
|
|
StagingBuffer::IOEngineConstraints::ioUringConstraints,
|
|
StagingBuffer::IOEngineConstraints::openClInputConstraints,
|
|
nDgramsPerStagingBufferFrame),
|
|
ioUringAssemblyEngine(*this),
|
|
collationBuffer(
|
|
StagingBuffer::IOEngineConstraints::openClInputConstraints,
|
|
StagingBuffer::IOEngineConstraints::openClInputConstraints,
|
|
nDgramsPerStagingBufferFrame)
|
|
{
|
|
if (smoHooksPtr->OptionParser_getOptions().verbose)
|
|
{
|
|
std::cout << __func__ << ": assembly buffer : "
|
|
<< assemblyBuffer.stringify()
|
|
<< "\n\tcollation buffer : " << collationBuffer.stringify()
|
|
<< "\n";
|
|
}
|
|
|
|
std::cout << __func__ << ": Device's component thread is "
|
|
<< device->componentThread->name << std::endl;
|
|
|
|
#ifndef CONFIG_WORLD_USE_BODY_THREAD
|
|
if (smoHooksPtr->ComponentThread_getSelf()->id != ComponentThread::WORLD)
|
|
#else
|
|
if (smoHooksPtr->ComponentThread_getSelf()->id != ComponentThread::BODY)
|
|
#endif
|
|
{
|
|
std::string errMsg = std::string(__func__) +
|
|
": PcloudStimulusBuffer constructor called on non-world/body thread " +
|
|
smoHooksPtr->ComponentThread_getSelf()->name;
|
|
|
|
std::cout << errMsg << std::endl;
|
|
// throw std::runtime_error(errMsg);
|
|
}
|
|
}
|
|
|
|
void PcloudStimulusBuffer::start()
|
|
{
|
|
// Call ioUringAssemblyEngine setup() as the final step
|
|
ioUringAssemblyEngine.setup();
|
|
openClCollatingAndMeshingEngine.setup();
|
|
// Call base class start() as the final step
|
|
StimulusBuffer::start();
|
|
}
|
|
|
|
void PcloudStimulusBuffer::stop()
|
|
{
|
|
// Call base class stop() as the first step
|
|
StimulusBuffer::stop();
|
|
// Call ioUringAssemblyEngine stop() as the final step
|
|
openClCollatingAndMeshingEngine.finalize();
|
|
ioUringAssemblyEngine.finalize();
|
|
}
|
|
|
|
void PcloudStimulusBuffer::stimFrameProductionTimesliceInd()
|
|
{
|
|
ioUringAssemblyEngine.assembleFrameReq(
|
|
{nullptr, [this](bool success, AsynchronousLoop loop) {
|
|
if (!success) {
|
|
std::cerr << __func__ << ": Failed to assemble frame" << std::endl;
|
|
} else {
|
|
std::cout << __func__ << ": Successfully assembled frame "
|
|
<< loop.nSucceeded.load() << " slots succeeded "
|
|
<< "out of " << loop.nTotal << " total slots" << std::endl;
|
|
}
|
|
}});
|
|
// Release the spinlock for now
|
|
allowNextStimulusFrame();
|
|
}
|
|
|
|
class PcloudStimulusBuffer::AssembleAndProduceStimulusFrameReq
|
|
: public smo::PostedAsynchronousContinuation<
|
|
assembleAndProduceStimulusFrameReqCbFn>
|
|
{
|
|
private:
|
|
PcloudStimulusBuffer& stimBuff;
|
|
|
|
public:
|
|
AssembleAndProduceStimulusFrameReq(
|
|
PcloudStimulusBuffer& buffer,
|
|
Callback<assembleAndProduceStimulusFrameReqCbFn> callback)
|
|
: PostedAsynchronousContinuation<assembleAndProduceStimulusFrameReqCbFn>(
|
|
buffer.device->componentThread, std::move(callback)),
|
|
stimBuff(buffer)
|
|
{}
|
|
|
|
void callOriginalCallback()
|
|
{
|
|
stimBuff.frameAssemblyRateLimiter.release();
|
|
callOriginalCb(static_cast<SimultaneityStamp>(0));
|
|
}
|
|
|
|
void callOriginalCallbackWithFailure()
|
|
{
|
|
stimBuff.frameAssemblyRateLimiter.release();
|
|
callOriginalCb(static_cast<SimultaneityStamp>(0));
|
|
}
|
|
};
|
|
|
|
void PcloudStimulusBuffer::assembleAndProduceStimulusFrameReq(
|
|
smo::Callback<assembleAndProduceStimulusFrameReqCbFn> callback)
|
|
{
|
|
// Wireframe implementation - do nothing for now
|
|
(void)callback;
|
|
}
|
|
|
|
} // namespace stim_buff
|
|
} // namespace smo
|