Line data Source code
1 : /**
2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #include "XLSnnInputTest.h"
24 : #include "XLCoreFrameQueue.h"
25 : #include "XLCoreFrameRequest.h"
26 : #include "XLVkLoop.h"
27 : #include "XLApplication.h"
28 : #include "SPBitmap.h"
29 :
30 : namespace stappler::xenolith::vk::shadernn {
31 :
32 0 : InputQueue::~InputQueue() { }
33 :
34 0 : bool InputQueue::init() {
35 : using namespace core;
36 0 : Queue::Builder builder("Input");
37 :
38 0 : auto imageAttachment = builder.addAttachemnt("ImageAttachment",
39 0 : [&] (AttachmentBuilder &attachmentBuilder) -> Rc<Attachment> {
40 0 : return Rc<vk::ImageAttachment>::create(attachmentBuilder,
41 0 : ImageInfo(Extent2(1024, 1024),
42 0 : ImageUsage::Storage | ImageUsage::TransferSrc,
43 0 : ImageTiling::Optimal, ImageFormat::R8G8B8A8_UNORM, PassType::Compute),
44 0 : ImageAttachment::AttachmentInfo{
45 : .initialLayout = AttachmentLayout::Ignored,
46 : .finalLayout = AttachmentLayout::Ignored,
47 : .clearOnLoad = true,
48 : .clearColor = Color4F(0.0f, 0.0f, 0.0f, 0.0f)}
49 0 : );
50 0 : });
51 :
52 0 : auto outputAttachment = builder.addAttachemnt("OutputAttachment",
53 0 : [&] (AttachmentBuilder &attachmentBuilder) -> Rc<Attachment> {
54 0 : attachmentBuilder.defineAsOutput();
55 0 : return Rc<vk::ImageAttachment>::create(attachmentBuilder,
56 0 : ImageInfo(Extent3(1024, 1024, 1), ImageType::Image3D,
57 0 : ImageUsage::Storage | ImageUsage::TransferSrc,
58 0 : ImageTiling::Optimal, ImageFormat::R16G16B16A16_SFLOAT, PassType::Compute),
59 0 : ImageAttachment::AttachmentInfo{
60 : .initialLayout = AttachmentLayout::Ignored,
61 : .finalLayout = AttachmentLayout::Ignored,
62 : .clearOnLoad = true,
63 : .clearColor = Color4F(0.0f, 0.0f, 0.0f, 0.0f)}
64 0 : );
65 0 : });
66 :
67 0 : _inputLayer = builder.addPass("InputLayer", PassType::Compute, RenderOrdering(0),
68 0 : [&] (QueuePassBuilder &passBuilder) -> Rc<core::QueuePass> {
69 0 : return Rc<InputLayer>::create(builder, passBuilder, imageAttachment, outputAttachment);
70 : });
71 :
72 0 : if (core::Queue::init(move(builder))) {
73 0 : _imageAttachment = imageAttachment;
74 0 : _outputAttachment = outputAttachment;
75 0 : return true;
76 : }
77 0 : return false;
78 0 : }
79 :
80 0 : const core::AttachmentData *InputQueue::getDataAttachment() const {
81 0 : return static_cast<InputLayer *>(_inputLayer->pass.get())->getDataAttachment();
82 : }
83 :
84 0 : void InputQueue::run(Application *app, StringView image) {
85 0 : Extent3 frameExtent(1, 1, 1);
86 0 : if (!bitmap::getImageSize(image, frameExtent.width, frameExtent.height)) {
87 0 : log::error("InputQueue", "fail to read image: ", image);
88 0 : return;
89 : }
90 :
91 0 : auto req = Rc<core::FrameRequest>::create(Rc<core::Queue>(this), core::FrameContraints{frameExtent});
92 :
93 0 : auto inputData = Rc<InputDataInput>::alloc();
94 0 : inputData->norm = NormData{
95 : Vec4(-0.5f, -0.5f, -0.5f, -0.5f), // to -0.5 - 0.5
96 : Vec4(2.0f, 2.0f, 2.0f, 2.0f) // to -1.0 - 1.0
97 : };
98 0 : inputData->image.extent = frameExtent;
99 0 : inputData->image.stdCallback = [path = image.str<Interface>()] (uint8_t *ptr, uint64_t size, const core::ImageData::DataCallback &dcb) {
100 0 : core::Resource::loadImageFileData(ptr, size, path, core::ImageFormat::R8G8B8A8_UNORM, dcb);
101 0 : };
102 :
103 0 : req->addInput(getDataAttachment(), move(inputData));
104 :
105 0 : req->setOutput(getOutputAttachment(), [app] (core::FrameAttachmentData &data, bool success, Ref *) {
106 0 : app->getGlLoop()->captureImage([app] (core::ImageInfoData info, BytesView view) {
107 0 : if (!view.empty()) {
108 0 : Bitmap bmp;
109 0 : bmp.alloc(info.extent.width, info.extent.height, bitmap::PixelFormat::RGBA8888, bitmap::AlphaFormat::Premultiplied);
110 :
111 0 : std::cout << view.size() << "\n";
112 0 : auto data = bmp.dataPtr();
113 0 : for (size_t i = 0; i < info.extent.width; ++ i) {
114 0 : for (size_t j = 0; j < info.extent.height; ++ j) {
115 0 : for (size_t k = 0; k < info.extent.depth; ++ k) {
116 0 : for (size_t f = 0; f < 4; ++ f) {
117 0 : *(data ++) = uint8_t((view.readFloat16() + 1.0f) * 127.5f);
118 : }
119 : }
120 : }
121 : }
122 :
123 : // bmp.save(toString(Time::now().toMicros(), ".png"));
124 0 : }
125 0 : app->end();
126 0 : }, data.image->getImage(), core::AttachmentLayout::General);
127 0 : return true;
128 : });
129 :
130 0 : app->getGlLoop()->runRenderQueue(move(req), 0);
131 0 : }
132 :
133 : }
|