HoviTron Video Pipeline
Shader.cpp
1/* ----------------------
2* Copyright 2023 Université Libre de Bruxelles(ULB), Universidad Politécnica de Madrid(UPM), CREAL, Deutsches Zentrum für Luft - und Raumfahrt(DLR)
3
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at < http://www.apache.org/licenses/LICENSE-2.0>
7
8* Unless required by applicable law or agreed to in writing, software
9* distributed under the License is distributed on an "AS IS" BASIS,
10* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11* See the License for the specific language governing permissionsand
12* limitations under the License.
13---------------------- */
14
15
16#include "Shader.h"
17
18#ifdef __ANDROID__
19#include <android/log.h>
20#include <android/asset_manager.h>
21#include <android/asset_manager_jni.h>
22//#include "JniUtils.h"
23#include <android/window.h> // for AWINDOW_FLAG_KEEP_SCREEN_ON
24#include <android/native_window_jni.h> // for native window JNI
25#else
26#include <fstream>
27#endif
28
29#if !defined(ALOGV) && __ANDROID__
30#define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "Framework_Vulkan", __VA_ARGS__)
31#endif
32
33#ifdef __ANDROID__
34void ShadersList::readShader(AAssetManager* assetManager, std::string shaderName){
35 ALOGV("Loading %s", shaderName.c_str());
36 Shader shad;
37 std::string filename = "shaders/"+shaderName+".spv";
38 ALOGV("File %s", filename.c_str());
39 AAsset* file = AAssetManager_open(assetManager,filename.c_str(), AASSET_MODE_BUFFER);
40 shad.size = AAsset_getLength(file);
41 ALOGV("Shader File Size: %d", shad.size);
42 shad.source = new char[shad.size];
43 AAsset_read(file,shad.source, shad.size);
44
45 m_shaders.insert(std::pair<std::string, Shader>(shaderName,shad));
46}
47
48void ShadersList::loadShaders(android_app *app) {
49 AAssetManager* assetManager = app->activity->assetManager;
50
51 //synthesis shader
52 readShader(assetManager,"synthesis.vert");
53 readShader(assetManager,"synthesis.geom");
54 readShader(assetManager,"synthesis.frag");
55
56 readShader(assetManager,"shader.vert");
57 readShader(assetManager,"shader.frag");
58
59 readShader(assetManager,"blending.vert");
60 readShader(assetManager,"blending.frag");
61 readShader(assetManager, "blendingFinal.frag");
62 readShader(assetManager, "blendingFirst.frag");
63 readShader(assetManager, "blendingFirstFinal.frag");
64
65}
66
67#else
69{
70
71 for (auto & shaderName : namesList) {
72 readShader(shaderName);
73 }
74}
75void ShadersList::readShader(std::string & shaderName)
76{
77 Shader shad;
78#ifndef NDEBUG
79 shad.name = shaderName;
80#endif // !NDEBUG
81
82 std::string filename = "./shaders/" + shaderName + ".spv";
83 std::ifstream file(filename, std::ios::ate | std::ios::binary);
84
85 if (!file.is_open()) {
86 throw std::runtime_error("failed to open file!: " + filename + "\n");
87 }
88
89 size_t fileSize = (size_t)file.tellg();
90 shad.size = fileSize;
91 shad.source = new char[shad.size];
92 file.seekg(0);
93 file.read(shad.source, shad.size);
94 m_shaders.insert(std::pair<std::string, Shader>(shaderName, shad));
95 file.close();
96}
97#endif
98
100 static ShadersList singleton;
101 return singleton;
102}
103Shader const& ShadersList::operator () (const char* name) const
104{
105 return m_shaders.at(name);
106}
107
108ShadersList::ShadersList() {
109
110}
Contains the class that loads the SPIR-V shaders from files.
Class that represents SPIR-V shader.
Definition: Shader.h:42
char * source
Definition: Shader.h:45
size_t size
Definition: Shader.h:47
std::string name
Definition: Shader.h:50
Class that loads SPIR-V shader from files and make their content available to the rest of the code.
Definition: Shader.h:64
void loadShaders()
Definition: Shader.cpp:68
Shader const & operator()(const char *name) const
Definition: Shader.cpp:103
static ShadersList & getInstance()
Definition: Shader.cpp:99