static void writeBytes(Bytes& dst, gsl::span<const gsl::byte> src) { size_t start = dst.size(); size_t size = src.size(); dst.resize(start + size); memcpy(dst.data() + start, src.data(), size); }
void subdata(GLintptr offset, gsl::span<const data_type> cont) const noexcept { bind(); glBufferSubData(target, offset * sizeof(data_type), cont.size() * sizeof(data_type), cont.data()); }
void AudioSDL::doQueueAudio(gsl::span<const gsl::byte> data) { std::vector<unsigned char> tmp(data.size_bytes()); memcpy(tmp.data(), data.data(), data.size_bytes()); std::unique_lock<std::mutex> lock(mutex); queuedSize += data.size_bytes(); audioQueue.push_back(std::move(tmp)); }
static gsl::span<std::uint8_t> ReceiveSpan(std::uint16_t frameSize, gsl::span<std::uint8_t> buffer) { if (buffer.size() <= frameSize + 6) { return buffer; } else { return buffer.subspan(0, frameSize + 6); } }
I2CResult I2CLowLevelBus::Write(const I2CAddress address, gsl::span<const uint8_t> inData) { I2C_TransferSeq_TypeDef seq; seq.addr = address; seq.flags = I2C_FLAG_WRITE; seq.buf[0].len = inData.length(); seq.buf[0].data = const_cast<uint8_t*>(inData.data()); seq.buf[1].len = 0; seq.buf[1].data = nullptr; return ExecuteTransfer(&seq); }
std::vector<float> AudioImporter::resampleChannel(int from, int to, gsl::span<const float> src) { AudioResampler resampler(from, to, 1, 1.0f); std::vector<float> dst(resampler.numOutputSamples(src.size()) + 1024); auto result = resampler.resampleInterleaved(src, dst); if (result.nRead != src.size()) { throw Exception("Only read " + toString(result.nRead) + " samples, expected " + toString(src.size())); } if (result.nWritten == dst.size()) { throw Exception("Resample dst buffer overflow."); } dst.resize(result.nWritten); return dst; }
VertexBufferPtr RenderingDevice::CreateVertexBufferRaw(gsl::span<const uint8_t> data) { CComPtr<IDirect3DVertexBuffer9> result; D3DLOG(mDevice->CreateVertexBuffer(data.size(), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &result, nullptr)); void* dataOut; D3DLOG(result->Lock(0, data.size(), &dataOut, D3DLOCK_DISCARD)); memcpy(dataOut, &data[0], data.size()); D3DLOG(result->Unlock()); return std::make_shared<VertexBuffer>(result, data.size()); }
bool CommObject::ReceiveFrame(gsl::span<std::uint8_t> buffer, CommFrame& frame) { if (buffer.size() < 2) { return false; } bool status = this->SendCommandWithResponse(CommReceiver, ReceiverGetFrame, buffer.subspan(0, 2)); if (!status) { return status; } Reader reader(buffer.subspan(0, 2)); auto size = reader.ReadWordLE(); buffer = ReceiveSpan(size, buffer); status = this->SendCommandWithResponse(CommReceiver, ReceiverGetFrame, buffer); if (!status) { return status; } reader.Initialize(buffer); const auto fullSize = reader.ReadWordLE(); const auto doppler = reader.ReadWordLE(); const auto rssi = reader.ReadWordLE(); gsl::span<std::uint8_t> frameContent; if (!reader.Status()) { LOG(LOG_LEVEL_ERROR, "[comm] Failed to receive frame"); } else { LOGF(LOG_LEVEL_DEBUG, "[comm] Received frame %d bytes", static_cast<int>(fullSize)); auto span = reader.ReadArray(reader.RemainingSize()); frameContent = gsl::span<std::uint8_t>(const_cast<std::uint8_t*>(span.data()), span.size()); } frame = CommFrame(doppler, rssi, fullSize, std::move(frameContent)); // if (fullSize == 0 || (doppler & 0xf000) != 0 || (rssi & 0xf000) != 0) // { // LOGF(LOG_LEVEL_ERROR, "[comm] Received invalid frame. Size: %d, Doppler: 0x%X, RSSI: 0x%X. ", fullSize, doppler, rssi); // return false; // } return status; }
std::string disassemble(const gsl::span<const uint8_t> &binary, bool isSubroutine) { disassembler::State state; state.binary = binary; state.cfPC = 0; state.groupPC = 0; for (auto i = 0; i < binary.size(); i += sizeof(ControlFlowInst)) { auto cf = *reinterpret_cast<const ControlFlowInst *>(binary.data() + i); auto id = cf.word1.CF_INST(); auto type = cf.word1.CF_INST_TYPE(); switch (type) { case SQ_CF_INST_TYPE_NORMAL: disassembler::disassembleNormal(state, cf); break; case SQ_CF_INST_TYPE_EXPORT: disassembler::disassembleExport(state, cf); break; case SQ_CF_INST_TYPE_ALU: case SQ_CF_INST_TYPE_ALU_EXTENDED: disassembler::disassembleControlFlowALU(state, cf); break; default: decaf_abort(fmt::format("Invalid top level instruction type {}", type)); } if (cf.word1.CF_INST() == SQ_CF_INST_RETURN && isSubroutine) { break; } if (cf.word1.CF_INST_TYPE() == SQ_CF_INST_TYPE_NORMAL || cf.word1.CF_INST_TYPE() == SQ_CF_INST_TYPE_EXPORT) { if (cf.word1.END_OF_PROGRAM()) { break; } } state.cfPC++; state.out << "\n"; } return state.out.str(); }
IndexBufferPtr RenderingDevice::CreateIndexBuffer(gsl::span<const uint16_t> data) { CComPtr<IDirect3DIndexBuffer9> result; D3DLOG(mDevice->CreateIndexBuffer(data.size_bytes(), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &result, nullptr)); void* dataOut; D3DLOG(result->Lock(0, data.size(), &dataOut, D3DLOCK_DISCARD)); memcpy(dataOut, &data[0], data.size() * sizeof(uint16_t)); D3DLOG(result->Unlock()); return std::make_shared<IndexBuffer>(result, data.size()); }
/** Ported more or less directly from SanOS. */ int Virtio::Queue::enqueue(gsl::span<Token> buffers){ debug ("Enqueuing %i tokens \n", buffers.size()); uint16_t last = _free_head; uint16_t first = _free_head; // Place each buffer in a token for( auto buf : buffers ) { debug (" buf @ %p \n", buffers.data()); // Set read / write flags _queue.desc[_free_head].flags = buf.direction() ? VIRTQ_DESC_F_NEXT : VIRTQ_DESC_F_NEXT | VIRTQ_DESC_F_WRITE; // Assign raw buffer _queue.desc[_free_head].addr = (uint64_t) buf.data(); _queue.desc[_free_head].len = buf.size(); last = _free_head; _free_head = _queue.desc[_free_head].next; } _desc_in_flight += buffers.size(); Ensures(_desc_in_flight <= size()); // No continue on last buffer _queue.desc[last].flags &= ~VIRTQ_DESC_F_NEXT; // Place the head of this current chain in the avail ring uint16_t avail_index = (_queue.avail->idx + _num_added) % _size; // we added a token _num_added++; _queue.avail->ring[avail_index] = first; debug("<Q %i> avail_index: %i size: %i, _free_head %i \n", _pci_index, avail_index, size(), _free_head ); debug ("Free tokens: %i \n", num_free()); return buffers.size(); }
void GLBuffer::setData(gsl::span<const gsl::byte> data) { /* if (size < size_t(data.size_bytes())) { glBufferData(target, data.size_bytes(), data.data(), GL_STREAM_DRAW); size = data.size_bytes(); } else { glBufferData(target, size, nullptr, GL_STREAM_DRAW); glBufferSubData(target, 0, data.size_bytes(), data.data()); } */ if (size < size_t(data.size_bytes())) { size = nextPowerOf2(data.size_bytes()); } glBufferData(target, size, nullptr, GL_STREAM_DRAW); glBufferSubData(target, 0, data.size_bytes(), data.data()); glCheckError(); }
void AudioSDL::queueAudio(gsl::span<const AudioSamplePack> data) { Expects(device); const size_t numSamples = data.size() * 16; // Float if (outputFormat.format == AudioSampleFormat::Float) { doQueueAudio(gsl::as_bytes(data)); } // Int16 else if (outputFormat.format == AudioSampleFormat::Int16) { if (tmpShort.size() < numSamples) { tmpShort.resize(numSamples); } for (ptrdiff_t i = 0; i < data.size(); ++i) { for (size_t j = 0; j < 16; ++j) { tmpShort[i * 16 + j] = static_cast<short>(data[i].samples[j] * 32768.0f); } } doQueueAudio(gsl::as_bytes(gsl::span<short>(tmpShort))); } // Int32 else if (outputFormat.format == AudioSampleFormat::Int32) { if (tmpInt.size() < numSamples) { tmpInt.resize(numSamples); } for (ptrdiff_t i = 0; i < data.size(); ++i) { for (size_t j = 0; j < 16; ++j) { tmpInt[i * 16 + j] = static_cast<int>(data[i].samples[j] * 2147483648.0f); } } doQueueAudio(gsl::as_bytes(gsl::span<int>(tmpInt))); } }
void BufferObject::set_data(gsl::span<const gsl::byte> data, Usage usage) { bind(); GL(glBufferData(GLenum(target_), data.size(), data.data(), GLenum(usage))); }
void AasRenderer::RenderShadowMapShadow(gsl::span<gfx::AnimatedModel*> models, gsl::span<const gfx::AnimatedModelParams*> modelParams, const XMFLOAT3 ¢er, float radius, float height, const XMFLOAT4 &lightDir, float alpha, bool softShadows) { Expects(models.size() == modelParams.size()); float shadowMapWorldX, shadowMapWorldWidth, shadowMapWorldZ, shadowMapWorldHeight; if (lightDir.x < 0.0) { shadowMapWorldX = center.x - 2 * radius + lightDir.x * height; shadowMapWorldWidth = 4 * radius - lightDir.x * height; } else { shadowMapWorldX = center.x - 2 * radius; shadowMapWorldWidth = lightDir.x * height + 4 * radius; } if (lightDir.z < 0.0) { shadowMapWorldZ = center.z - 2 * radius + lightDir.z * height; shadowMapWorldHeight = 4 * radius - lightDir.z * height; } else { shadowMapWorldZ = center.z - 2 * radius; shadowMapWorldHeight = lightDir.z + height + 4 * radius; } CComPtr<IDirect3DSurface9> currentTarget; D3DLOG(mDevice.GetDevice()->GetRenderTarget(0, ¤tTarget)); D3DLOG(mDevice.GetDevice()->SetRenderTarget(0, mShadowTarget->GetSurface())); CComPtr<IDirect3DSurface9> depthStencil; mDevice.GetDevice()->GetDepthStencilSurface(&depthStencil); mDevice.GetDevice()->SetDepthStencilSurface(nullptr); mDevice.SetMaterial(mShadowMapMaterial); // Set shader params XMFLOAT4 floats{ shadowMapWorldX, shadowMapWorldZ, shadowMapWorldWidth, shadowMapWorldHeight }; mDevice.GetDevice()->SetVertexShaderConstantF(0, &floats.x, 1); mDevice.GetDevice()->SetVertexShaderConstantF(1, &lightDir.x, 1); floats.x = center.y; mDevice.GetDevice()->SetVertexShaderConstantF(2, &floats.x, 1); XMCOLOR color(0, 0, 0, 0.5f); XMStoreFloat4(&floats, PackedVector::XMLoadColor(&color)); mDevice.GetDevice()->SetVertexShaderConstantF(4, &floats.x, 1); D3DLOG(mDevice.GetDevice()->Clear(0, nullptr, D3DCLEAR_TARGET, 0, 0, 0)); for (size_t i = 0; i < models.size(); ++i) { RenderWithoutMaterial(models[i], *modelParams[i]); } if (softShadows) { mDevice.SetMaterial(mGaussBlurHor); D3DLOG(mDevice.GetDevice()->SetRenderTarget(0, mShadowTargetTmp->GetSurface())); mShapeRenderer2d.DrawFullScreenQuad(); mDevice.SetMaterial(mGaussBlurVer); D3DLOG(mDevice.GetDevice()->SetRenderTarget(0, mShadowTarget->GetSurface())); mShapeRenderer2d.DrawFullScreenQuad(); } D3DLOG(mDevice.GetDevice()->SetRenderTarget(0, currentTarget)); D3DLOG(mDevice.GetDevice()->SetDepthStencilSurface(depthStencil)); auto shadowMapWorldBottom = shadowMapWorldZ + shadowMapWorldHeight; auto shadowMapWorldRight = shadowMapWorldX + shadowMapWorldWidth; std::array<gfx::ShapeVertex3d, 4> corners; corners[0].pos = { shadowMapWorldX, center.y, shadowMapWorldZ }; corners[1].pos = { shadowMapWorldX, center.y, shadowMapWorldBottom }; corners[2].pos = { shadowMapWorldRight, center.y, shadowMapWorldBottom }; corners[3].pos = { shadowMapWorldRight, center.y, shadowMapWorldZ }; corners[0].uv = { 0, 0 }; corners[1].uv = { 0, 1 }; corners[2].uv = { 1, 1 }; corners[3].uv = { 1, 0 }; mShapeRenderer3d.DrawQuad(corners, 0xFFFFFFFF, mShadowTarget); }
void data(gsl::span<const data_type> cont) noexcept { size_ = cont.size(); bind(); glBufferData(target, size_bytes(), cont.data(), usage); }
void NetworkService::receivePacket(gsl::span<gsl::byte> received, std::string* error) { if (error) { std::cout << "Error receiving packet: " << (*error) << std::endl; // Find the owner of this remote endpoint for (auto& conn : pimpl->activeConnections) { if (conn.second->matchesEndpoint(pimpl->remoteEndpoint)) { conn.second->setError(*error); conn.second->close(); } } return; } if (received.size_bytes() == 0) { return; } // Read connection id short id = -1; std::array<unsigned char, 2> bytes; auto dst = gsl::as_writeable_bytes(gsl::span<unsigned char, 2>(bytes)); dst[0] = received[0]; if (bytes[0] & 0x80) { if (received.size_bytes() < 2) { // Invalid header std::cout << "Invalid header\n"; return; } dst[1] = received[1]; received = received.subspan(2); id = short(bytes[0] & 0x7F) | short(bytes[1]); } else { received = received.subspan(1); id = short(bytes[0]); } // No connection id, check if it's a connection request if (id == 0 && isValidConnectionRequest(received)) { auto& pending = pimpl->pendingIncomingConnections; if (std::find(pending.begin(), pending.end(), pimpl->remoteEndpoint) == pending.end()) { pending.push_back(pimpl->remoteEndpoint); } // Pending connection is valid return; } // Find the owner of this remote endpoint auto conn = pimpl->activeConnections.find(id); if (conn == pimpl->activeConnections.end()) { // Connection doesn't exist, but check the pending slot conn = pimpl->activeConnections.find(0); if (conn == pimpl->activeConnections.end()) { // Nope, give up return; } } // Validate that this connection is who it claims to be if (conn->second->matchesEndpoint(pimpl->remoteEndpoint)) { auto connection = conn->second; if (error) { // Close the connection if there was an error connection->setError(*error); connection->close(); } else { try { connection->onReceive(received); if (conn->first == 0) { // Hold on, we're still on 0, re-bind to the id short newId = connection->getConnectionId(); if (newId != 0) { pimpl->activeConnections[newId] = connection; pimpl->activeConnections.erase(conn); } } } catch (std::exception& e) { connection->setError(e.what()); connection->close(); } catch (...) { connection->setError("Unknown error receiving packet."); connection->close(); } } } }
bool NetworkService::isValidConnectionRequest(gsl::span<const gsl::byte> data) { HandshakeOpen open; return acceptingConnections && data.size() == sizeof(open) && memcmp(data.data(), &open, sizeof(open.handshake)) == 0; }