/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* pixel_put.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/31 15:14:50 by maldavid #+# #+# */ /* Updated: 2023/04/01 15:32:23 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include #include #include namespace mlx { struct PixelPutPipeline::_Pimpl { Texture texture; Buffer buffer; void* map = nullptr; uint32_t width = 0; uint32_t height = 0; }; PixelPutPipeline::PixelPutPipeline() : _impl(std::make_unique<_Pimpl>()) {} void PixelPutPipeline::init(uint32_t width, uint32_t height, Renderer& renderer) noexcept { _impl->texture.create(nullptr, width, height, VK_FORMAT_R8G8B8A8_UNORM); _impl->texture.setDescriptor(renderer.getFragDescriptorSet().duplicate()); _impl->buffer.create(Buffer::kind::dynamic, sizeof(uint32_t) * (width * height), VK_BUFFER_USAGE_TRANSFER_SRC_BIT); _impl->width = width; _impl->height = height; } VkDescriptorSet PixelPutPipeline::getDescriptorSet() noexcept { return _impl->texture.getSet(); } void PixelPutPipeline::setPixel(uint32_t x, uint32_t y, int color) noexcept { if(x < 0 || y < 0 || x > _impl->width || y > _impl->height) return; if(!_impl->buffer.isMapped()) _impl->buffer.mapMem(&_impl->map); unsigned char* mem = static_cast(_impl->map) + (y * _impl->width * sizeof(uint32_t)) + (x * sizeof(uint32_t)); *reinterpret_cast(mem) = color; } void PixelPutPipeline::present() noexcept { if(_impl->buffer.isMapped()) _impl->buffer.unmapMem(); _impl->texture.copyBuffer(_impl->buffer); _impl->texture.updateSet(0); } void PixelPutPipeline::render(Renderer& renderer) noexcept { _impl->texture.render(renderer, 0, 0); } void PixelPutPipeline::destroy() noexcept { _impl->buffer.destroy(); _impl->texture.destroy(); } PixelPutPipeline::~PixelPutPipeline() {} }