Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed3f5d1d76 | ||
|
|
87351097ad | ||
|
|
694766682f | ||
|
|
c946ebd1e6 | ||
|
|
25a8291101 | ||
|
|
60c689688d | ||
|
|
763bc1f34a | ||
|
|
ef07638eef | ||
|
|
0aa0efe159 | ||
|
|
ef7bcd1e19 | ||
|
|
702ceda82a | ||
|
|
66b78074de | ||
|
|
8d440cea17 | ||
|
|
3ab8e4d725 | ||
|
|
97f20afdd7 | ||
|
|
42989e76d7 |
@@ -7,3 +7,10 @@ for next branch cut* header.
|
||||
appropriate header in [RELEASE_NOTES.md](./RELEASE_NOTES.md).
|
||||
|
||||
## Release notes for next branch cut
|
||||
|
||||
- Fix possible NPE when updating fog options from Java/Kotlin
|
||||
- The `emissive` property was not applied properly to `MASKED` materials, and could cause
|
||||
dark fringes to appear (recompile materials)
|
||||
- Allow glTF materials with transmission/volume extensions to choose their alpha mode
|
||||
instead of forcing `MASKED`
|
||||
- Add support for 2 additional material variables [⚠️ **Recompile materials**]
|
||||
|
||||
+3
-1
@@ -102,7 +102,9 @@ public class MaterialBuilder {
|
||||
CUSTOM0,
|
||||
CUSTOM1,
|
||||
CUSTOM2,
|
||||
CUSTOM3
|
||||
CUSTOM3,
|
||||
CUSTOM4,
|
||||
CUSTOM5
|
||||
}
|
||||
|
||||
public enum VertexAttribute {
|
||||
|
||||
@@ -246,17 +246,21 @@ Java_com_google_android_filament_MaterialInstance_nSetFloatParameterArray(JNIEnv
|
||||
env->ReleaseStringUTFChars(name_, name);
|
||||
}
|
||||
|
||||
// defined in TextureSampler.cpp
|
||||
namespace filament::JniUtils {
|
||||
TextureSampler from_long(jlong params) noexcept;
|
||||
} // TextureSamplerJniUtils
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_google_android_filament_MaterialInstance_nSetParameterTexture(
|
||||
JNIEnv *env, jclass, jlong nativeMaterialInstance, jstring name_,
|
||||
jlong nativeTexture, jint sampler_) {
|
||||
jlong nativeTexture, jlong sampler_) {
|
||||
MaterialInstance* instance = (MaterialInstance*) nativeMaterialInstance;
|
||||
Texture* texture = (Texture*) nativeTexture;
|
||||
TextureSampler& sampler = reinterpret_cast<TextureSampler&>(sampler_);
|
||||
|
||||
const char *name = env->GetStringUTFChars(name_, 0);
|
||||
instance->setParameter(name, texture, sampler);
|
||||
instance->setParameter(name, texture, JniUtils::from_long(sampler_));
|
||||
env->ReleaseStringUTFChars(name_, name);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,11 +27,10 @@ extern "C" JNIEXPORT void JNICALL
|
||||
Java_com_google_android_filament_SwapChain_nSetFrameCompletedCallback(JNIEnv* env, jclass,
|
||||
jlong nativeSwapChain, jobject handler, jobject runnable) {
|
||||
SwapChain* swapChain = (SwapChain*) nativeSwapChain;
|
||||
auto *callback = JniCallback::make(env, handler, runnable);
|
||||
swapChain->setFrameCompletedCallback([](void* user) {
|
||||
JniCallback* callback = (JniCallback*)user;
|
||||
auto* callback = JniCallback::make(env, handler, runnable);
|
||||
swapChain->setFrameCompletedCallback(nullptr, [callback](SwapChain* swapChain) {
|
||||
JniCallback::postToJavaAndDestroy(callback);
|
||||
}, callback);
|
||||
});
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jboolean JNICALL
|
||||
|
||||
@@ -18,142 +18,139 @@
|
||||
|
||||
#include <filament/TextureSampler.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
|
||||
using namespace filament;
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nCreateSampler(JNIEnv *env, jclass type, jint min,
|
||||
namespace filament::JniUtils {
|
||||
|
||||
jlong to_long(TextureSampler const& sampler) noexcept {
|
||||
return jlong(utils::bit_cast<uint32_t>(sampler.getSamplerParams()));
|
||||
}
|
||||
|
||||
TextureSampler from_long(jlong params) noexcept {
|
||||
return TextureSampler{
|
||||
utils::bit_cast<backend::SamplerParams>(
|
||||
static_cast<uint32_t>(params))};
|
||||
}
|
||||
|
||||
} // namespace filament::JniUtils
|
||||
|
||||
using namespace JniUtils;
|
||||
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nCreateSampler(JNIEnv *, jclass, jint min,
|
||||
jint max, jint s, jint t, jint r) {
|
||||
return TextureSampler(static_cast<TextureSampler::MinFilter>(min),
|
||||
static_cast<TextureSampler::MagFilter>(max), static_cast<TextureSampler::WrapMode>(s),
|
||||
static_cast<TextureSampler::WrapMode>(t),
|
||||
static_cast<TextureSampler::WrapMode>(r)).getSamplerParams().u;
|
||||
TextureSampler sampler(static_cast<TextureSampler::MinFilter>(min),
|
||||
static_cast<TextureSampler::MagFilter>(max),
|
||||
static_cast<TextureSampler::WrapMode>(s),
|
||||
static_cast<TextureSampler::WrapMode>(t),
|
||||
static_cast<TextureSampler::WrapMode>(r));
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nCreateCompareSampler(JNIEnv *env, jclass type,
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nCreateCompareSampler(JNIEnv *, jclass,
|
||||
jint mode, jint function) {
|
||||
return TextureSampler(static_cast<TextureSampler::CompareMode>(mode),
|
||||
static_cast<TextureSampler::CompareFunc>(function)).getSamplerParams().u;
|
||||
TextureSampler sampler(static_cast<TextureSampler::CompareMode>(mode),
|
||||
static_cast<TextureSampler::CompareFunc>(function));
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nGetMinFilter(JNIEnv *env, jclass type,
|
||||
jint sampler_) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
return static_cast<jint>(sampler.getMinFilter());
|
||||
Java_com_google_android_filament_TextureSampler_nGetMinFilter(JNIEnv *, jclass, jlong sampler) {
|
||||
return static_cast<jint>(from_long(sampler).getMinFilter());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetMinFilter(JNIEnv *env, jclass type,
|
||||
jint sampler_, jint filter) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetMinFilter(JNIEnv *, jclass, jlong sampler_, jint filter) {
|
||||
TextureSampler sampler{from_long(sampler_)};
|
||||
sampler.setMinFilter(static_cast<TextureSampler::MinFilter>(filter));
|
||||
return sampler.getSamplerParams().u;
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nGetMagFilter(JNIEnv *env, jclass type,
|
||||
jint sampler_) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
return static_cast<jint>(sampler.getMagFilter());
|
||||
Java_com_google_android_filament_TextureSampler_nGetMagFilter(JNIEnv *, jclass, jlong sampler) {
|
||||
return static_cast<jint>(from_long(sampler).getMagFilter());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetMagFilter(JNIEnv *env, jclass type,
|
||||
jint sampler_, jint filter) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetMagFilter(JNIEnv *, jclass, jlong sampler_, jint filter) {
|
||||
TextureSampler sampler{from_long(sampler_)};
|
||||
sampler.setMagFilter(static_cast<TextureSampler::MagFilter>(filter));
|
||||
return sampler.getSamplerParams().u;
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nGetWrapModeS(JNIEnv *env, jclass type,
|
||||
jint sampler_) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
return static_cast<jint>(sampler.getWrapModeS());
|
||||
Java_com_google_android_filament_TextureSampler_nGetWrapModeS(JNIEnv *, jclass, jlong sampler) {
|
||||
return static_cast<jint>(from_long(sampler).getWrapModeS());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetWrapModeS(JNIEnv *env, jclass type,
|
||||
jint sampler_, jint mode) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetWrapModeS(JNIEnv *, jclass, jlong sampler_, jint mode) {
|
||||
TextureSampler sampler{from_long(sampler_)};
|
||||
sampler.setWrapModeS(static_cast<TextureSampler::WrapMode>(mode));
|
||||
return sampler.getSamplerParams().u;
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nGetWrapModeT(JNIEnv *env, jclass type,
|
||||
jint sampler_) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
return static_cast<jint>(sampler.getWrapModeT());
|
||||
Java_com_google_android_filament_TextureSampler_nGetWrapModeT(JNIEnv *, jclass, jlong sampler) {
|
||||
return static_cast<jint>(from_long(sampler).getWrapModeT());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetWrapModeT(JNIEnv *env, jclass type,
|
||||
jint sampler_, jint mode) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetWrapModeT(JNIEnv *, jclass, jlong sampler_, jint mode) {
|
||||
TextureSampler sampler{from_long(sampler_)};
|
||||
sampler.setWrapModeT(static_cast<TextureSampler::WrapMode>(mode));
|
||||
return sampler.getSamplerParams().u;
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nGetWrapModeR(JNIEnv *env, jclass type,
|
||||
jint sampler_) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
return static_cast<jint>(sampler.getWrapModeR());
|
||||
Java_com_google_android_filament_TextureSampler_nGetWrapModeR(JNIEnv *, jclass, jlong sampler) {
|
||||
return static_cast<jint>(from_long(sampler).getWrapModeR());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetWrapModeR(JNIEnv *env, jclass type,
|
||||
jint sampler_, jint mode) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetWrapModeR(JNIEnv *, jclass, jlong sampler_, jint mode) {
|
||||
TextureSampler sampler{from_long(sampler_)};
|
||||
sampler.setWrapModeR(static_cast<TextureSampler::WrapMode>(mode));
|
||||
return sampler.getSamplerParams().u;
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nGetCompareMode(JNIEnv *env, jclass type,
|
||||
jint sampler_) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
return static_cast<jint>(sampler.getCompareMode());
|
||||
Java_com_google_android_filament_TextureSampler_nGetCompareMode(JNIEnv *, jclass, jlong sampler) {
|
||||
return static_cast<jint>(from_long(sampler).getCompareMode());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetCompareMode(JNIEnv *env, jclass type,
|
||||
jint sampler_, jint mode) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetCompareMode(JNIEnv *, jclass, jlong sampler_, jint mode) {
|
||||
TextureSampler sampler{from_long(sampler_)};
|
||||
sampler.setCompareMode(static_cast<TextureSampler::CompareMode>(mode),
|
||||
sampler.getCompareFunc());
|
||||
return sampler.getSamplerParams().u;
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nGetCompareFunction(JNIEnv *env, jclass type,
|
||||
jint sampler_) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
return static_cast<jint>(sampler.getCompareFunc());
|
||||
Java_com_google_android_filament_TextureSampler_nGetCompareFunction(JNIEnv *, jclass, jlong sampler) {
|
||||
return static_cast<jint>(from_long(sampler).getCompareFunc());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetCompareFunction(JNIEnv *env, jclass type,
|
||||
jint sampler_, jint function) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetCompareFunction(JNIEnv *, jclass, jlong sampler_, jint function) {
|
||||
TextureSampler sampler{from_long(sampler_)};
|
||||
sampler.setCompareMode(sampler.getCompareMode(),
|
||||
static_cast<TextureSampler::CompareFunc>(function));
|
||||
return sampler.getSamplerParams().u;
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jfloat JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nGetAnisotropy(JNIEnv *env, jclass type,
|
||||
jint sampler_) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
return sampler.getAnisotropy();
|
||||
Java_com_google_android_filament_TextureSampler_nGetAnisotropy(JNIEnv *, jclass, jlong sampler) {
|
||||
return from_long(sampler).getAnisotropy();
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetAnisotropy(JNIEnv *env, jclass type,
|
||||
jint sampler_, jfloat anisotropy) {
|
||||
TextureSampler &sampler = reinterpret_cast<TextureSampler &>(sampler_);
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_google_android_filament_TextureSampler_nSetAnisotropy(JNIEnv *, jclass, jlong sampler_, jfloat anisotropy) {
|
||||
TextureSampler sampler{from_long(sampler_)};
|
||||
sampler.setAnisotropy(anisotropy);
|
||||
return sampler.getSamplerParams().u;
|
||||
return to_long(sampler);
|
||||
}
|
||||
|
||||
+1
-1
@@ -900,7 +900,7 @@ public class MaterialInstance {
|
||||
@IntRange(from = 0) int offset, @IntRange(from = 1) int count);
|
||||
|
||||
private static native void nSetParameterTexture(long nativeMaterialInstance,
|
||||
@NonNull String name, long nativeTexture, int sampler);
|
||||
@NonNull String name, long nativeTexture, long sampler);
|
||||
|
||||
private static native void nSetScissor(long nativeMaterialInstance,
|
||||
@IntRange(from = 0) int left, @IntRange(from = 0) int bottom,
|
||||
|
||||
@@ -137,10 +137,6 @@ public class SwapChain {
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* The FrameCompletedCallback is guaranteed to be called on the main Filament thread.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Warning: Only Filament's Metal backend supports frame callbacks. Other backends ignore the
|
||||
* callback (which will never be called) and proceed normally.
|
||||
* </p>
|
||||
|
||||
+19
-19
@@ -126,7 +126,7 @@ public class TextureSampler {
|
||||
NEVER
|
||||
}
|
||||
|
||||
int mSampler = 0; // bit field used by native
|
||||
long mSampler = 0; // bit field used by native
|
||||
|
||||
/**
|
||||
* Initializes the <code>TextureSampler</code> with default values.
|
||||
@@ -342,26 +342,26 @@ public class TextureSampler {
|
||||
}
|
||||
}
|
||||
|
||||
private static native int nCreateSampler(int min, int max, int s, int t, int r);
|
||||
private static native int nCreateCompareSampler(int mode, int function);
|
||||
private static native long nCreateSampler(int min, int max, int s, int t, int r);
|
||||
private static native long nCreateCompareSampler(int mode, int function);
|
||||
|
||||
private static native int nGetMinFilter(int sampler);
|
||||
private static native int nSetMinFilter(int sampler, int filter);
|
||||
private static native int nGetMagFilter(int sampler);
|
||||
private static native int nSetMagFilter(int sampler, int filter);
|
||||
private static native int nGetMinFilter(long sampler);
|
||||
private static native long nSetMinFilter(long sampler, int filter);
|
||||
private static native int nGetMagFilter(long sampler);
|
||||
private static native long nSetMagFilter(long sampler, int filter);
|
||||
|
||||
private static native int nGetWrapModeS(int sampler);
|
||||
private static native int nSetWrapModeS(int sampler, int mode);
|
||||
private static native int nGetWrapModeT(int sampler);
|
||||
private static native int nSetWrapModeT(int sampler, int mode);
|
||||
private static native int nGetWrapModeR(int sampler);
|
||||
private static native int nSetWrapModeR(int sampler, int mode);
|
||||
private static native int nGetWrapModeS(long sampler);
|
||||
private static native long nSetWrapModeS(long sampler, int mode);
|
||||
private static native int nGetWrapModeT(long sampler);
|
||||
private static native long nSetWrapModeT(long sampler, int mode);
|
||||
private static native int nGetWrapModeR(long sampler);
|
||||
private static native long nSetWrapModeR(long sampler, int mode);
|
||||
|
||||
private static native int nGetCompareMode(int sampler);
|
||||
private static native int nSetCompareMode(int sampler, int mode);
|
||||
private static native int nGetCompareFunction(int sampler);
|
||||
private static native int nSetCompareFunction(int sampler, int function);
|
||||
private static native int nGetCompareMode(long sampler);
|
||||
private static native long nSetCompareMode(long sampler, int mode);
|
||||
private static native int nGetCompareFunction(long sampler);
|
||||
private static native long nSetCompareFunction(long sampler, int function);
|
||||
|
||||
private static native float nGetAnisotropy(int sampler);
|
||||
private static native int nSetAnisotropy(int sampler, float anisotropy);
|
||||
private static native float nGetAnisotropy(long sampler);
|
||||
private static native long nSetAnisotropy(long sampler, float anisotropy);
|
||||
}
|
||||
|
||||
@@ -967,7 +967,8 @@ public class View {
|
||||
options.heightFalloff, options.cutOffDistance,
|
||||
options.color[0], options.color[1], options.color[2],
|
||||
options.density, options.inScatteringStart, options.inScatteringSize,
|
||||
options.fogColorFromIbl, options.skyColor.getNativeObject(),
|
||||
options.fogColorFromIbl,
|
||||
options.skyColor == null ? 0 : options.skyColor.getNativeObject(),
|
||||
options.enabled);
|
||||
}
|
||||
|
||||
@@ -1391,13 +1392,13 @@ public class View {
|
||||
/**
|
||||
* resolution of vertical axis (2^levels to 2048)
|
||||
*/
|
||||
public int resolution = 360;
|
||||
public int resolution = 384;
|
||||
/**
|
||||
* bloom x/y aspect-ratio (1/32 to 32)
|
||||
*/
|
||||
public float anamorphism = 1.0f;
|
||||
/**
|
||||
* number of blur levels (3 to 11)
|
||||
* number of blur levels (1 to 11)
|
||||
*/
|
||||
public int levels = 6;
|
||||
/**
|
||||
@@ -1985,4 +1986,11 @@ public class View {
|
||||
*/
|
||||
public float penumbraRatioScale = 1.0f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for stereoscopic (multi-eye) rendering.
|
||||
*/
|
||||
public static class StereoscopicOptions {
|
||||
public boolean enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1299,7 +1299,7 @@ Type
|
||||
: array of `string`
|
||||
|
||||
Value
|
||||
: Up to 4 strings, each must be a valid GLSL identifier.
|
||||
: Up to 6 strings, each must be a valid GLSL identifier.
|
||||
|
||||
Description
|
||||
: Defines custom interpolants (or variables) that are output by the material's vertex shader.
|
||||
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -27,7 +27,6 @@ set(SRCS
|
||||
src/BackendUtils.cpp
|
||||
src/BlobCacheKey.cpp
|
||||
src/Callable.cpp
|
||||
src/CallbackHandler.cpp
|
||||
src/CircularBuffer.cpp
|
||||
src/CommandBufferQueue.cpp
|
||||
src/CommandStream.cpp
|
||||
@@ -178,8 +177,6 @@ if (FILAMENT_SUPPORTS_VULKAN)
|
||||
src/vulkan/VulkanConstants.h
|
||||
src/vulkan/VulkanContext.cpp
|
||||
src/vulkan/VulkanContext.h
|
||||
src/vulkan/VulkanDisposer.cpp
|
||||
src/vulkan/VulkanDisposer.h
|
||||
src/vulkan/VulkanDriver.cpp
|
||||
src/vulkan/VulkanDriver.h
|
||||
src/vulkan/VulkanDriverFactory.h
|
||||
@@ -201,6 +198,9 @@ if (FILAMENT_SUPPORTS_VULKAN)
|
||||
src/vulkan/VulkanSwapChain.h
|
||||
src/vulkan/VulkanReadPixels.cpp
|
||||
src/vulkan/VulkanReadPixels.h
|
||||
src/vulkan/VulkanResourceAllocator.h
|
||||
src/vulkan/VulkanResources.cpp
|
||||
src/vulkan/VulkanResources.h
|
||||
src/vulkan/VulkanTexture.cpp
|
||||
src/vulkan/VulkanTexture.h
|
||||
src/vulkan/VulkanUtility.cpp
|
||||
|
||||
@@ -66,7 +66,7 @@ public:
|
||||
virtual void post(void* user, Callback callback) = 0;
|
||||
|
||||
protected:
|
||||
virtual ~CallbackHandler();
|
||||
virtual ~CallbackHandler() = default;
|
||||
};
|
||||
|
||||
} // namespace filament::backend
|
||||
|
||||
@@ -796,32 +796,53 @@ enum class SamplerCompareFunc : uint8_t {
|
||||
|
||||
//! Sampler parameters
|
||||
struct SamplerParams { // NOLINT
|
||||
union {
|
||||
struct {
|
||||
SamplerMagFilter filterMag : 1; //!< magnification filter (NEAREST)
|
||||
SamplerMinFilter filterMin : 3; //!< minification filter (NEAREST)
|
||||
SamplerWrapMode wrapS : 2; //!< s-coordinate wrap mode (CLAMP_TO_EDGE)
|
||||
SamplerWrapMode wrapT : 2; //!< t-coordinate wrap mode (CLAMP_TO_EDGE)
|
||||
SamplerMagFilter filterMag : 1; //!< magnification filter (NEAREST)
|
||||
SamplerMinFilter filterMin : 3; //!< minification filter (NEAREST)
|
||||
SamplerWrapMode wrapS : 2; //!< s-coordinate wrap mode (CLAMP_TO_EDGE)
|
||||
SamplerWrapMode wrapT : 2; //!< t-coordinate wrap mode (CLAMP_TO_EDGE)
|
||||
|
||||
SamplerWrapMode wrapR : 2; //!< r-coordinate wrap mode (CLAMP_TO_EDGE)
|
||||
uint8_t anisotropyLog2 : 3; //!< anisotropy level (0)
|
||||
SamplerCompareMode compareMode : 1; //!< sampler compare mode (NONE)
|
||||
uint8_t padding0 : 2; //!< reserved. must be 0.
|
||||
SamplerWrapMode wrapR : 2; //!< r-coordinate wrap mode (CLAMP_TO_EDGE)
|
||||
uint8_t anisotropyLog2 : 3; //!< anisotropy level (0)
|
||||
SamplerCompareMode compareMode : 1; //!< sampler compare mode (NONE)
|
||||
uint8_t padding0 : 2; //!< reserved. must be 0.
|
||||
|
||||
SamplerCompareFunc compareFunc : 3; //!< sampler comparison function (LE)
|
||||
uint8_t padding1 : 5; //!< reserved. must be 0.
|
||||
SamplerCompareFunc compareFunc : 3; //!< sampler comparison function (LE)
|
||||
uint8_t padding1 : 5; //!< reserved. must be 0.
|
||||
uint8_t padding2 : 8; //!< reserved. must be 0.
|
||||
|
||||
uint8_t padding2 : 8; //!< reserved. must be 0.
|
||||
};
|
||||
uint32_t u;
|
||||
struct Hasher {
|
||||
size_t operator()(SamplerParams p) const noexcept {
|
||||
// we don't use std::hash<> here, so we don't have to include <functional>
|
||||
return *reinterpret_cast<uint64_t const*>(reinterpret_cast<char const*>(&p));
|
||||
}
|
||||
};
|
||||
|
||||
struct EqualTo {
|
||||
bool operator()(SamplerParams lhs, SamplerParams rhs) const noexcept {
|
||||
auto* pLhs = reinterpret_cast<uint64_t const*>(reinterpret_cast<char const*>(&lhs));
|
||||
auto* pRhs = reinterpret_cast<uint64_t const*>(reinterpret_cast<char const*>(&rhs));
|
||||
return *pLhs == *pRhs;
|
||||
}
|
||||
};
|
||||
|
||||
struct LessThan {
|
||||
bool operator()(SamplerParams lhs, SamplerParams rhs) const noexcept {
|
||||
auto* pLhs = reinterpret_cast<uint64_t const*>(reinterpret_cast<char const*>(&lhs));
|
||||
auto* pRhs = reinterpret_cast<uint64_t const*>(reinterpret_cast<char const*>(&rhs));
|
||||
return *pLhs == *pRhs;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
friend inline bool operator < (SamplerParams lhs, SamplerParams rhs) {
|
||||
return lhs.u < rhs.u;
|
||||
friend inline bool operator < (SamplerParams lhs, SamplerParams rhs) noexcept {
|
||||
return SamplerParams::LessThan{}(lhs, rhs);
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(SamplerParams) == sizeof(uint32_t), "SamplerParams must be 32 bits");
|
||||
// The limitation to 64-bits max comes from how we store a SamplerParams in our JNI code
|
||||
// see android/.../TextureSampler.cpp
|
||||
static_assert(sizeof(SamplerParams) <= sizeof(uint64_t),
|
||||
"SamplerParams must be no more than 64 bits");
|
||||
|
||||
//! blending equation function
|
||||
enum class BlendEquation : uint8_t {
|
||||
@@ -1126,8 +1147,6 @@ static_assert(sizeof(StencilState) == 12u,
|
||||
|
||||
using FrameScheduledCallback = void(*)(PresentCallable callable, void* user);
|
||||
|
||||
using FrameCompletedCallback = void(*)(void* user);
|
||||
|
||||
enum class Workaround : uint16_t {
|
||||
// The EASU pass must split because shader compiler flattens early-exit branch
|
||||
SPLIT_EASU,
|
||||
|
||||
@@ -142,7 +142,8 @@ DECL_DRIVER_API_N(setFrameScheduledCallback,
|
||||
|
||||
DECL_DRIVER_API_N(setFrameCompletedCallback,
|
||||
backend::SwapChainHandle, sch,
|
||||
backend::FrameCompletedCallback, callback,
|
||||
backend::CallbackHandler*, handler,
|
||||
backend::CallbackHandler::Callback, callback,
|
||||
void*, user)
|
||||
|
||||
DECL_DRIVER_API_N(setPresentationTime,
|
||||
@@ -273,6 +274,7 @@ DECL_DRIVER_API_N(destroyRenderTarget, backend::RenderTargetHandle, rth)
|
||||
DECL_DRIVER_API_N(destroySwapChain, backend::SwapChainHandle, sch)
|
||||
DECL_DRIVER_API_N(destroyStream, backend::StreamHandle, sh)
|
||||
DECL_DRIVER_API_N(destroyTimerQuery, backend::TimerQueryHandle, sh)
|
||||
DECL_DRIVER_API_N(destroyFence, backend::FenceHandle, fh)
|
||||
|
||||
/*
|
||||
* Synchronous APIs
|
||||
@@ -286,7 +288,6 @@ DECL_DRIVER_API_SYNCHRONOUS_N(void, setAcquiredImage, backend::StreamHandle, str
|
||||
DECL_DRIVER_API_SYNCHRONOUS_N(void, setStreamDimensions, backend::StreamHandle, stream, uint32_t, width, uint32_t, height)
|
||||
DECL_DRIVER_API_SYNCHRONOUS_N(int64_t, getStreamTimestamp, backend::StreamHandle, stream)
|
||||
DECL_DRIVER_API_SYNCHRONOUS_N(void, updateStreams, backend::DriverApi*, driver)
|
||||
DECL_DRIVER_API_SYNCHRONOUS_N(void, destroyFence, backend::FenceHandle, fh)
|
||||
DECL_DRIVER_API_SYNCHRONOUS_N(backend::FenceStatus, getFenceStatus, backend::FenceHandle, fh)
|
||||
DECL_DRIVER_API_SYNCHRONOUS_N(bool, isTextureFormatSupported, backend::TextureFormat, format)
|
||||
DECL_DRIVER_API_SYNCHRONOUS_0(bool, isTextureSwizzleSupported)
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <backend/CallbackHandler.h>
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
CallbackHandler::~CallbackHandler() = default;
|
||||
|
||||
} // namespace filament::backend
|
||||
@@ -176,9 +176,9 @@ void MetalDriver::setFrameScheduledCallback(Handle<HwSwapChain> sch,
|
||||
}
|
||||
|
||||
void MetalDriver::setFrameCompletedCallback(Handle<HwSwapChain> sch,
|
||||
FrameCompletedCallback callback, void* user) {
|
||||
CallbackHandler* handler, CallbackHandler::Callback callback, void* user) {
|
||||
auto* swapChain = handle_cast<MetalSwapChain>(sch);
|
||||
swapChain->setFrameCompletedCallback(callback, user);
|
||||
swapChain->setFrameCompletedCallback(handler, callback, user);
|
||||
}
|
||||
|
||||
void MetalDriver::execute(std::function<void(void)> const& fn) noexcept {
|
||||
|
||||
@@ -70,7 +70,8 @@ public:
|
||||
void releaseDrawable();
|
||||
|
||||
void setFrameScheduledCallback(FrameScheduledCallback callback, void* user);
|
||||
void setFrameCompletedCallback(FrameCompletedCallback callback, void* user);
|
||||
void setFrameCompletedCallback(CallbackHandler* handler,
|
||||
CallbackHandler::Callback callback, void* user);
|
||||
|
||||
// For CAMetalLayer-backed SwapChains, presents the drawable or schedules a
|
||||
// FrameScheduledCallback.
|
||||
@@ -112,8 +113,11 @@ private:
|
||||
FrameScheduledCallback frameScheduledCallback = nullptr;
|
||||
void* frameScheduledUserData = nullptr;
|
||||
|
||||
FrameCompletedCallback frameCompletedCallback = nullptr;
|
||||
void* frameCompletedUserData = nullptr;
|
||||
struct {
|
||||
CallbackHandler* handler = nullptr;
|
||||
CallbackHandler::Callback callback = {};
|
||||
void* user = nullptr;
|
||||
} frameCompleted;
|
||||
};
|
||||
|
||||
class MetalBufferObject : public HwBufferObject {
|
||||
|
||||
@@ -194,13 +194,15 @@ void MetalSwapChain::setFrameScheduledCallback(FrameScheduledCallback callback,
|
||||
frameScheduledUserData = user;
|
||||
}
|
||||
|
||||
void MetalSwapChain::setFrameCompletedCallback(FrameCompletedCallback callback, void* user) {
|
||||
frameCompletedCallback = callback;
|
||||
frameCompletedUserData = user;
|
||||
void MetalSwapChain::setFrameCompletedCallback(CallbackHandler* handler,
|
||||
CallbackHandler::Callback callback, void* user) {
|
||||
frameCompleted.handler = handler;
|
||||
frameCompleted.callback = callback;
|
||||
frameCompleted.user = user;
|
||||
}
|
||||
|
||||
void MetalSwapChain::present() {
|
||||
if (frameCompletedCallback) {
|
||||
if (frameCompleted.callback) {
|
||||
scheduleFrameCompletedCallback();
|
||||
}
|
||||
if (drawable) {
|
||||
@@ -244,30 +246,17 @@ void MetalSwapChain::scheduleFrameScheduledCallback() {
|
||||
}
|
||||
|
||||
void MetalSwapChain::scheduleFrameCompletedCallback() {
|
||||
if (!frameCompletedCallback) {
|
||||
if (!frameCompleted.callback) {
|
||||
return;
|
||||
}
|
||||
|
||||
FrameCompletedCallback callback = frameCompletedCallback;
|
||||
void* userData = frameCompletedUserData;
|
||||
[getPendingCommandBuffer(&context) addCompletedHandler:^(id<MTLCommandBuffer> cb) {
|
||||
struct CallbackData {
|
||||
void* userData;
|
||||
FrameCompletedCallback callback;
|
||||
};
|
||||
CallbackData* data = new CallbackData();
|
||||
data->userData = userData;
|
||||
data->callback = callback;
|
||||
CallbackHandler* handler = frameCompleted.handler;
|
||||
void* user = frameCompleted.user;
|
||||
CallbackHandler::Callback callback = frameCompleted.callback;
|
||||
|
||||
// Instantiate a BufferDescriptor with a callback for the sole purpose of passing it to
|
||||
// scheduleDestroy. This forces the BufferDescriptor callback (and thus the
|
||||
// FrameCompletedCallback) to be called on the user thread.
|
||||
BufferDescriptor b(nullptr, 0u, [](void* buffer, size_t size, void* user) {
|
||||
CallbackData* data = (CallbackData*) user;
|
||||
data->callback(data->userData);
|
||||
free(data);
|
||||
}, data);
|
||||
context.driver->scheduleDestroy(std::move(b));
|
||||
MetalDriver* driver = context.driver;
|
||||
[getPendingCommandBuffer(&context) addCompletedHandler:^(id<MTLCommandBuffer> cb) {
|
||||
driver->scheduleCallback(handler, user, callback);
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace filament {
|
||||
namespace backend {
|
||||
|
||||
inline bool operator==(const SamplerParams& lhs, const SamplerParams& rhs) {
|
||||
return lhs.u == rhs.u;
|
||||
return SamplerParams::EqualTo{}(lhs, rhs);
|
||||
}
|
||||
|
||||
// Rasterization Bindings
|
||||
|
||||
@@ -58,7 +58,7 @@ void NoopDriver::setFrameScheduledCallback(Handle<HwSwapChain> sch,
|
||||
}
|
||||
|
||||
void NoopDriver::setFrameCompletedCallback(Handle<HwSwapChain> sch,
|
||||
FrameCompletedCallback callback, void* user) {
|
||||
CallbackHandler* handler, CallbackHandler::Callback callback, void* user) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2917,7 +2917,7 @@ void OpenGLDriver::bindSamplers(uint32_t index, Handle<HwSamplerGroup> sbh) {
|
||||
|
||||
#ifndef FILAMENT_SILENCE_NOT_SUPPORTED_BY_ES2
|
||||
GLuint OpenGLDriver::getSamplerSlow(SamplerParams params) const noexcept {
|
||||
assert_invariant(mSamplerMap.find(params.u) == mSamplerMap.end());
|
||||
assert_invariant(mSamplerMap.find(params) == mSamplerMap.end());
|
||||
|
||||
GLuint s;
|
||||
glGenSamplers(1, &s);
|
||||
@@ -2939,7 +2939,7 @@ GLuint OpenGLDriver::getSamplerSlow(SamplerParams params) const noexcept {
|
||||
}
|
||||
#endif
|
||||
CHECK_GL_ERROR(utils::slog.e)
|
||||
mSamplerMap[params.u] = s;
|
||||
mSamplerMap[params] = s;
|
||||
return s;
|
||||
}
|
||||
#endif
|
||||
@@ -3270,7 +3270,7 @@ void OpenGLDriver::setFrameScheduledCallback(Handle<HwSwapChain> sch,
|
||||
}
|
||||
|
||||
void OpenGLDriver::setFrameCompletedCallback(Handle<HwSwapChain> sch,
|
||||
FrameCompletedCallback callback, void* user) {
|
||||
CallbackHandler* handler, CallbackHandler::Callback callback, void* user) {
|
||||
DEBUG_MARKER()
|
||||
}
|
||||
|
||||
|
||||
@@ -334,7 +334,7 @@ private:
|
||||
assert_invariant(!sp.padding1);
|
||||
assert_invariant(!sp.padding2);
|
||||
auto& samplerMap = mSamplerMap;
|
||||
auto pos = samplerMap.find(sp.u);
|
||||
auto pos = samplerMap.find(sp);
|
||||
if (UTILS_UNLIKELY(pos == samplerMap.end())) {
|
||||
return getSamplerSlow(sp);
|
||||
}
|
||||
@@ -368,7 +368,8 @@ private:
|
||||
// sampler buffer binding points (nullptr if not used)
|
||||
std::array<GLSamplerGroup*, Program::SAMPLER_BINDING_COUNT> mSamplerBindings = {}; // 4 pointers
|
||||
|
||||
mutable tsl::robin_map<uint32_t, GLuint> mSamplerMap;
|
||||
mutable tsl::robin_map<SamplerParams, GLuint,
|
||||
SamplerParams::Hasher, SamplerParams::EqualTo> mSamplerMap;
|
||||
|
||||
// this must be accessed from the driver thread only
|
||||
std::vector<GLTexture*> mTexturesWithStreamsAttached;
|
||||
|
||||
@@ -129,11 +129,11 @@ void TimerQueryNative::endTimeElapsedQuery(GLTimerQuery* tq) {
|
||||
// we need to try this one again later
|
||||
return false;
|
||||
}
|
||||
GLuint64 elapsedTime = 0;
|
||||
// we won't end-up here if we're on ES and don't have GL_EXT_disjoint_timer_query
|
||||
context.procs.getQueryObjectui64v(state->gl.query, GL_QUERY_RESULT, &elapsedTime);
|
||||
state->elapsed.store((int64_t)elapsedTime, std::memory_order_relaxed);
|
||||
}
|
||||
GLuint64 elapsedTime = 0;
|
||||
// we won't end-up here if we're on ES and don't have GL_EXT_disjoint_timer_query
|
||||
context.procs.getQueryObjectui64v(state->gl.query, GL_QUERY_RESULT, &elapsedTime);
|
||||
state->elapsed.store((int64_t)elapsedTime, std::memory_order_relaxed);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "VulkanFboCache.h"
|
||||
#include "VulkanHandles.h"
|
||||
#include "VulkanSamplerCache.h"
|
||||
#include "VulkanTexture.h"
|
||||
|
||||
#include <utils/FixedCapacityVector.h>
|
||||
#include <utils/Panic.h>
|
||||
@@ -136,16 +137,20 @@ void VulkanBlitter::blitColor(BlitArgs args) {
|
||||
VkFormatProperties info;
|
||||
vkGetPhysicalDeviceFormatProperties(gpu, src.getFormat(), &info);
|
||||
if (!ASSERT_POSTCONDITION_NON_FATAL(info.optimalTilingFeatures & VK_FORMAT_FEATURE_BLIT_SRC_BIT,
|
||||
"Source format is not blittable")) {
|
||||
"Source format is not blittable %d", src.getFormat())) {
|
||||
return;
|
||||
}
|
||||
vkGetPhysicalDeviceFormatProperties(gpu, dst.getFormat(), &info);
|
||||
if (!ASSERT_POSTCONDITION_NON_FATAL(info.optimalTilingFeatures & VK_FORMAT_FEATURE_BLIT_DST_BIT,
|
||||
"Destination format is not blittable")) {
|
||||
"Destination format is not blittable %d", dst.getFormat())) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
VkCommandBuffer const cmdbuffer = mCommands->get().cmdbuffer;
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
VkCommandBuffer const cmdbuffer = commands.cmdbuffer;
|
||||
commands.acquire(src.texture);
|
||||
commands.acquire(dst.texture);
|
||||
|
||||
blitFast(cmdbuffer, aspect, args.filter, args.srcTarget->getExtent(), src, dst,
|
||||
args.srcRectPair, args.dstRectPair);
|
||||
}
|
||||
@@ -160,12 +165,12 @@ void VulkanBlitter::blitDepth(BlitArgs args) {
|
||||
VkFormatProperties info;
|
||||
vkGetPhysicalDeviceFormatProperties(gpu, src.getFormat(), &info);
|
||||
if (!ASSERT_POSTCONDITION_NON_FATAL(info.optimalTilingFeatures & VK_FORMAT_FEATURE_BLIT_SRC_BIT,
|
||||
"Depth format is not blittable")) {
|
||||
"Depth src format is not blittable %d", src.getFormat())) {
|
||||
return;
|
||||
}
|
||||
vkGetPhysicalDeviceFormatProperties(gpu, dst.getFormat(), &info);
|
||||
if (!ASSERT_POSTCONDITION_NON_FATAL(info.optimalTilingFeatures & VK_FORMAT_FEATURE_BLIT_DST_BIT,
|
||||
"Depth format is not blittable")) {
|
||||
"Depth dst format is not blittable %d", dst.getFormat())) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
@@ -177,7 +182,11 @@ void VulkanBlitter::blitDepth(BlitArgs args) {
|
||||
args.dstRectPair);
|
||||
return;
|
||||
}
|
||||
VkCommandBuffer const cmdbuffer = mCommands->get().cmdbuffer;
|
||||
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
VkCommandBuffer const cmdbuffer = commands.cmdbuffer;
|
||||
commands.acquire(src.texture);
|
||||
commands.acquire(dst.texture);
|
||||
blitFast(cmdbuffer, aspect, args.filter, args.srcTarget->getExtent(), src, dst, args.srcRectPair,
|
||||
args.dstRectPair);
|
||||
}
|
||||
@@ -247,13 +256,16 @@ void VulkanBlitter::lazyInit() noexcept {
|
||||
+1.0f, +1.0f,
|
||||
};
|
||||
|
||||
mTriangleBuffer = new VulkanBuffer(mAllocator, mCommands, mStagePool,
|
||||
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, sizeof(kTriangleVertices));
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
VkCommandBuffer const cmdbuffer = commands.cmdbuffer;
|
||||
|
||||
mTriangleBuffer->loadFromCpu(kTriangleVertices, 0, sizeof(kTriangleVertices));
|
||||
mTriangleBuffer = new VulkanBuffer(mAllocator, mStagePool, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||
sizeof(kTriangleVertices));
|
||||
|
||||
mParamsBuffer = new VulkanBuffer(mAllocator, mCommands, mStagePool,
|
||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, sizeof(BlitterUniforms));
|
||||
mTriangleBuffer->loadFromCpu(cmdbuffer, kTriangleVertices, 0, sizeof(kTriangleVertices));
|
||||
|
||||
mParamsBuffer = new VulkanBuffer(mAllocator, mStagePool, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
sizeof(BlitterUniforms));
|
||||
}
|
||||
|
||||
// At a high level, the procedure for resolving depth looks like this:
|
||||
@@ -265,11 +277,16 @@ void VulkanBlitter::blitSlowDepth(VkFilter filter, const VkExtent2D srcExtent, V
|
||||
VulkanAttachment dst, const VkOffset3D srcRect[2], const VkOffset3D dstRect[2]) {
|
||||
lazyInit();
|
||||
|
||||
VulkanCommandBuffer* commands = &mCommands->get();
|
||||
VkCommandBuffer const cmdbuffer = commands->cmdbuffer;
|
||||
commands->acquire(src.texture);
|
||||
commands->acquire(dst.texture);
|
||||
|
||||
BlitterUniforms const uniforms = {
|
||||
.sampleCount = src.texture->samples,
|
||||
.inverseSampleCount = 1.0f / float(src.texture->samples),
|
||||
.sampleCount = src.texture->samples,
|
||||
.inverseSampleCount = 1.0f / float(src.texture->samples),
|
||||
};
|
||||
mParamsBuffer->loadFromCpu(&uniforms, 0, sizeof(uniforms));
|
||||
mParamsBuffer->loadFromCpu(cmdbuffer, &uniforms, 0, sizeof(uniforms));
|
||||
|
||||
VkImageAspectFlags const aspect = VK_IMAGE_ASPECT_DEPTH_BIT;
|
||||
|
||||
@@ -316,8 +333,6 @@ void VulkanBlitter::blitSlowDepth(VkFilter filter, const VkExtent2D srcExtent, V
|
||||
renderPassInfo.renderArea.extent.width = dstRect[1].x - dstRect[0].x;
|
||||
renderPassInfo.renderArea.extent.height = dstRect[1].y - dstRect[0].y;
|
||||
|
||||
const VkCommandBuffer cmdbuffer = mCommands->get().cmdbuffer;
|
||||
|
||||
// We need to transition the source into a sampler since it'll be sampled in the shader.
|
||||
const VkImageSubresourceRange srcRange = {
|
||||
.aspectMask = aspect,
|
||||
@@ -378,6 +393,7 @@ void VulkanBlitter::blitSlowDepth(VkFilter filter, const VkExtent2D srcExtent, V
|
||||
VkSampler vksampler = mSamplerCache.getSampler({});
|
||||
|
||||
VkDescriptorImageInfo samplers[VulkanPipelineCache::SAMPLER_BINDING_COUNT];
|
||||
VulkanTexture* textures[VulkanPipelineCache::SAMPLER_BINDING_COUNT] = {nullptr};
|
||||
for (auto& sampler : samplers) {
|
||||
sampler = {
|
||||
.sampler = vksampler,
|
||||
@@ -391,8 +407,9 @@ void VulkanBlitter::blitSlowDepth(VkFilter filter, const VkExtent2D srcExtent, V
|
||||
.imageView = src.getImageView(VK_IMAGE_ASPECT_DEPTH_BIT),
|
||||
.imageLayout = ImgUtil::getVkLayout(samplerLayout),
|
||||
};
|
||||
textures[0] = src.texture;
|
||||
|
||||
mPipelineCache.bindSamplers(samplers,
|
||||
mPipelineCache.bindSamplers(samplers, textures,
|
||||
VulkanPipelineCache::getUsageFlags(0, ShaderStageFlags::FRAGMENT));
|
||||
|
||||
auto previousUbo = mPipelineCache.getUniformBufferBinding(0);
|
||||
@@ -409,7 +426,7 @@ void VulkanBlitter::blitSlowDepth(VkFilter filter, const VkExtent2D srcExtent, V
|
||||
|
||||
mPipelineCache.bindScissor(cmdbuffer, scissor);
|
||||
|
||||
if (!mPipelineCache.bindPipeline(cmdbuffer)) {
|
||||
if (!mPipelineCache.bindPipeline(commands)) {
|
||||
assert_invariant(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,9 +23,11 @@ using namespace bluevk;
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
VulkanBuffer::VulkanBuffer(VmaAllocator allocator, VulkanCommands* commands,
|
||||
VulkanStagePool& stagePool, VkBufferUsageFlags usage, uint32_t numBytes)
|
||||
: mAllocator(allocator), mCommands(commands), mStagePool(stagePool), mUsage(usage) {
|
||||
VulkanBuffer::VulkanBuffer(VmaAllocator allocator, VulkanStagePool& stagePool,
|
||||
VkBufferUsageFlags usage, uint32_t numBytes)
|
||||
: mAllocator(allocator),
|
||||
mStagePool(stagePool),
|
||||
mUsage(usage) {
|
||||
|
||||
// for now make sure that only 1 bit is set in usage
|
||||
// (because loadFromCpu() assumes that somewhat)
|
||||
@@ -53,7 +55,8 @@ void VulkanBuffer::terminate() {
|
||||
mGpuBuffer = VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
void VulkanBuffer::loadFromCpu(const void* cpuData, uint32_t byteOffset, uint32_t numBytes) const {
|
||||
void VulkanBuffer::loadFromCpu(VkCommandBuffer cmdbuf, const void* cpuData, uint32_t byteOffset,
|
||||
uint32_t numBytes) const {
|
||||
assert_invariant(byteOffset == 0);
|
||||
VulkanStage const* stage = mStagePool.acquireStage(numBytes);
|
||||
void* mapped;
|
||||
@@ -62,10 +65,8 @@ void VulkanBuffer::loadFromCpu(const void* cpuData, uint32_t byteOffset, uint32_
|
||||
vmaUnmapMemory(mAllocator, stage->memory);
|
||||
vmaFlushAllocation(mAllocator, stage->memory, byteOffset, numBytes);
|
||||
|
||||
VkCommandBuffer const cmdbuffer = mCommands->get(true).cmdbuffer;
|
||||
|
||||
VkBufferCopy region{ .size = numBytes };
|
||||
vkCmdCopyBuffer(cmdbuffer, stage->buffer, mGpuBuffer, 1, ®ion);
|
||||
vkCmdCopyBuffer(cmdbuf, stage->buffer, mGpuBuffer, 1, ®ion);
|
||||
|
||||
// Firstly, ensure that the copy finishes before the next draw call.
|
||||
// Secondly, in case the user decides to upload another chunk (without ever using the first one)
|
||||
@@ -99,7 +100,7 @@ void VulkanBuffer::loadFromCpu(const void* cpuData, uint32_t byteOffset, uint32_
|
||||
.size = VK_WHOLE_SIZE,
|
||||
};
|
||||
|
||||
vkCmdPipelineBarrier(cmdbuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, dstStageMask, 0, 0, nullptr, 1,
|
||||
vkCmdPipelineBarrier(cmdbuf, VK_PIPELINE_STAGE_TRANSFER_BIT, dstStageMask, 0, 0, nullptr, 1,
|
||||
&barrier, 0, nullptr);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,15 +25,18 @@ namespace filament::backend {
|
||||
// Encapsulates a Vulkan buffer, its attached DeviceMemory and a staging area.
|
||||
class VulkanBuffer {
|
||||
public:
|
||||
VulkanBuffer(VmaAllocator allocator, VulkanCommands* commands, VulkanStagePool& stagePool,
|
||||
VkBufferUsageFlags usage, uint32_t numBytes);
|
||||
VulkanBuffer(VmaAllocator allocator, VulkanStagePool& stagePool, VkBufferUsageFlags usage,
|
||||
uint32_t numBytes);
|
||||
~VulkanBuffer();
|
||||
void terminate();
|
||||
void loadFromCpu(const void* cpuData, uint32_t byteOffset, uint32_t numBytes) const;
|
||||
VkBuffer getGpuBuffer() const { return mGpuBuffer; }
|
||||
void loadFromCpu(VkCommandBuffer cmdbuf, const void* cpuData, uint32_t byteOffset,
|
||||
uint32_t numBytes) const;
|
||||
VkBuffer getGpuBuffer() const {
|
||||
return mGpuBuffer;
|
||||
}
|
||||
|
||||
private:
|
||||
VmaAllocator mAllocator;
|
||||
VulkanCommands* mCommands;
|
||||
VulkanStagePool& mStagePool;
|
||||
|
||||
VmaAllocation mGpuMemory = VK_NULL_HANDLE;
|
||||
|
||||
@@ -68,22 +68,35 @@ static VkCommandPool createPool(VkDevice device, uint32_t queueFamilyIndex) {
|
||||
}
|
||||
|
||||
void VulkanGroupMarkers::push(std::string const& marker, Timestamp start) noexcept {
|
||||
mMarkers.push(marker);
|
||||
mMarkers.push_back(marker);
|
||||
#if FILAMENT_VULKAN_VERBOSE
|
||||
mTimestamps.push(start.time_since_epoch().count() > 0.0
|
||||
mTimestamps.push_back(start.time_since_epoch().count() > 0.0
|
||||
? start
|
||||
: std::chrono::high_resolution_clock::now());
|
||||
#endif
|
||||
}
|
||||
|
||||
std::pair<std::string, Timestamp> VulkanGroupMarkers::pop() noexcept {
|
||||
auto const marker = mMarkers.top();
|
||||
mMarkers.pop();
|
||||
auto const marker = mMarkers.back();
|
||||
mMarkers.pop_back();
|
||||
|
||||
#if FILAMENT_VULKAN_VERBOSE
|
||||
auto const topTimestamp = mTimestamps.top();
|
||||
mTimestamps.pop();
|
||||
return std::make_pair(marker, topTimestamp);
|
||||
auto const timestamp = mTimestamps.back();
|
||||
mTimestamps.pop_back();
|
||||
return std::make_pair(marker, timestamp);
|
||||
#else
|
||||
return std::make_pair(marker, Timestamp{});
|
||||
#endif
|
||||
}
|
||||
|
||||
std::pair<std::string, Timestamp> VulkanGroupMarkers::pop_bottom() noexcept {
|
||||
auto const marker = mMarkers.front();
|
||||
mMarkers.pop_front();
|
||||
|
||||
#if FILAMENT_VULKAN_VERBOSE
|
||||
auto const timestamp = mTimestamps.front();
|
||||
mTimestamps.pop_front();
|
||||
return std::make_pair(marker, timestamp);
|
||||
#else
|
||||
return std::make_pair(marker, Timestamp{});
|
||||
#endif
|
||||
@@ -91,7 +104,7 @@ std::pair<std::string, Timestamp> VulkanGroupMarkers::pop() noexcept {
|
||||
|
||||
std::pair<std::string, Timestamp> VulkanGroupMarkers::top() const {
|
||||
assert_invariant(!empty());
|
||||
auto const marker = mMarkers.top();
|
||||
auto const marker = mMarkers.back();
|
||||
#if FILAMENT_VULKAN_VERBOSE
|
||||
auto const topTimestamp = mTimestamps.top();
|
||||
return std::make_pair(marker, topTimestamp);
|
||||
@@ -105,15 +118,20 @@ bool VulkanGroupMarkers::empty() const noexcept {
|
||||
}
|
||||
|
||||
VulkanCommands::VulkanCommands(VkDevice device, VkQueue queue, uint32_t queueFamilyIndex,
|
||||
VulkanContext* context)
|
||||
VulkanContext* context, VulkanResourceAllocator* allocator)
|
||||
: mDevice(device),
|
||||
mQueue(queue),
|
||||
mPool(createPool(mDevice, queueFamilyIndex)),
|
||||
mContext(context) {
|
||||
mContext(context),
|
||||
mStorage(CAPACITY) {
|
||||
VkSemaphoreCreateInfo sci{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO};
|
||||
for (auto& semaphore: mSubmissionSignals) {
|
||||
vkCreateSemaphore(mDevice, &sci, nullptr, &semaphore);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < CAPACITY; ++i) {
|
||||
mStorage[i] = std::make_unique<VulkanCommandBuffer>(allocator);
|
||||
}
|
||||
}
|
||||
|
||||
VulkanCommands::~VulkanCommands() {
|
||||
@@ -125,10 +143,9 @@ VulkanCommands::~VulkanCommands() {
|
||||
}
|
||||
}
|
||||
|
||||
VulkanCommandBuffer const& VulkanCommands::get(bool blockOnGC) {
|
||||
if (mCurrent) {
|
||||
mCurrent->blockOnGC = mCurrent->blockOnGC || blockOnGC;
|
||||
return *mCurrent;
|
||||
VulkanCommandBuffer& VulkanCommands::get() {
|
||||
if (mCurrentCommandBufferIndex >= 0) {
|
||||
return *mStorage[mCurrentCommandBufferIndex].get();
|
||||
}
|
||||
|
||||
// If we ran out of available command buffers, stall until one finishes. This is very rare.
|
||||
@@ -144,15 +161,18 @@ VulkanCommandBuffer const& VulkanCommands::get(bool blockOnGC) {
|
||||
gc();
|
||||
}
|
||||
|
||||
VulkanCommandBuffer* currentbuf = nullptr;
|
||||
// Find an available slot.
|
||||
for (VulkanCommandBuffer& wrapper : mStorage) {
|
||||
if (wrapper.cmdbuffer == VK_NULL_HANDLE) {
|
||||
mCurrent = &wrapper;
|
||||
for (size_t i = 0; i < CAPACITY; ++i) {
|
||||
auto wrapper = mStorage[i].get();
|
||||
if (wrapper->cmdbuffer == VK_NULL_HANDLE) {
|
||||
mCurrentCommandBufferIndex = static_cast<int8_t>(i);
|
||||
currentbuf = wrapper;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert_invariant(mCurrent);
|
||||
assert_invariant(currentbuf);
|
||||
--mAvailableCount;
|
||||
|
||||
// Create the low-level command buffer.
|
||||
@@ -162,47 +182,46 @@ VulkanCommandBuffer const& VulkanCommands::get(bool blockOnGC) {
|
||||
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
||||
.commandBufferCount = 1
|
||||
};
|
||||
vkAllocateCommandBuffers(mDevice, &allocateInfo, &mCurrent->cmdbuffer);
|
||||
|
||||
mCurrent->blockOnGC = blockOnGC;
|
||||
vkAllocateCommandBuffers(mDevice, &allocateInfo, ¤tbuf->cmdbuffer);
|
||||
|
||||
// Note that the fence wrapper uses shared_ptr because a DriverAPI fence can also have ownership
|
||||
// over it. The destruction of the low-level fence occurs either in VulkanCommands::gc(), or in
|
||||
// VulkanDriver::destroyFence(), both of which are safe spots.
|
||||
mCurrent->fence = std::make_shared<VulkanCmdFence>(mDevice);
|
||||
currentbuf->fence = std::make_shared<VulkanCmdFence>(mDevice);
|
||||
|
||||
// Begin writing into the command buffer.
|
||||
const VkCommandBufferBeginInfo binfo {
|
||||
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
||||
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
|
||||
};
|
||||
vkBeginCommandBuffer(mCurrent->cmdbuffer, &binfo);
|
||||
vkBeginCommandBuffer(currentbuf->cmdbuffer, &binfo);
|
||||
|
||||
// Notify the observer that a new command buffer has been activated.
|
||||
if (mObserver) {
|
||||
mObserver->onCommandBuffer(*mCurrent);
|
||||
mObserver->onCommandBuffer(*currentbuf);
|
||||
}
|
||||
|
||||
// We push the current markers onto a temporary stack. This must be placed after mCurrent is set
|
||||
// to the new command buffer since pushGroupMarker also calls get().
|
||||
// We push the current markers onto a temporary stack. This must be placed after currentbuf is
|
||||
// set to the new command buffer since pushGroupMarker also calls get().
|
||||
while (mCarriedOverMarkers && !mCarriedOverMarkers->empty()) {
|
||||
auto [marker, time] = mCarriedOverMarkers->pop();
|
||||
pushGroupMarker(marker.c_str(), time);
|
||||
}
|
||||
|
||||
return *mCurrent;
|
||||
return *currentbuf;
|
||||
}
|
||||
|
||||
bool VulkanCommands::flush() {
|
||||
// It's perfectly fine to call flush when no commands have been written.
|
||||
if (mCurrent == nullptr) {
|
||||
if (mCurrentCommandBufferIndex < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int64_t index = mCurrent - &mStorage[0];
|
||||
VkSemaphore renderingFinished = mSubmissionSignals[index];
|
||||
int8_t const index = mCurrentCommandBufferIndex;
|
||||
VulkanCommandBuffer const* currentbuf = mStorage[index].get();
|
||||
VkSemaphore const renderingFinished = mSubmissionSignals[index];
|
||||
|
||||
vkEndCommandBuffer(mCurrent->cmdbuffer);
|
||||
vkEndCommandBuffer(currentbuf->cmdbuffer);
|
||||
|
||||
// If the injected semaphore is an "image available" semaphore that has not yet been signaled,
|
||||
// it is sometimes fine to start executing commands anyway, as along as we stall the GPU at the
|
||||
@@ -226,7 +245,7 @@ bool VulkanCommands::flush() {
|
||||
.pWaitSemaphores = signals,
|
||||
.pWaitDstStageMask = waitDestStageMasks,
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &mCurrent->cmdbuffer,
|
||||
.pCommandBuffers = ¤tbuf->cmdbuffer,
|
||||
.signalSemaphoreCount = 1u,
|
||||
.pSignalSemaphores = &renderingFinished,
|
||||
};
|
||||
@@ -239,13 +258,18 @@ bool VulkanCommands::flush() {
|
||||
signals[submitInfo.waitSemaphoreCount++] = mInjectedSignal;
|
||||
}
|
||||
|
||||
if (FILAMENT_VULKAN_VERBOSE) {
|
||||
slog.i << "Submitting cmdbuffer=" << mCurrent->cmdbuffer
|
||||
<< " wait=(" << signals[0] << ", " << signals[1] << ") "
|
||||
<< " signal=" << renderingFinished
|
||||
<< io::endl;
|
||||
// To address a validation warning.
|
||||
if (submitInfo.waitSemaphoreCount == 0) {
|
||||
submitInfo.pWaitSemaphores = VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
#if FILAMENT_VULKAN_VERBOSE
|
||||
slog.i << "Submitting cmdbuffer=" << currentbuf->cmdbuffer
|
||||
<< " wait=(" << signals[0] << ", " << signals[1] << ") "
|
||||
<< " signal=" << renderingFinished
|
||||
<< io::endl;
|
||||
#endif
|
||||
|
||||
// Before actually submitting, we need to pop any leftover group markers.
|
||||
while (mGroupMarkers && !mGroupMarkers->empty()) {
|
||||
if (!mCarriedOverMarkers) {
|
||||
@@ -257,44 +281,51 @@ bool VulkanCommands::flush() {
|
||||
popGroupMarker();
|
||||
}
|
||||
|
||||
auto& cmdfence = mCurrent->fence;
|
||||
auto& cmdfence = currentbuf->fence;
|
||||
std::unique_lock<utils::Mutex> lock(cmdfence->mutex);
|
||||
cmdfence->status.store(VK_NOT_READY);
|
||||
UTILS_UNUSED_IN_RELEASE VkResult result = vkQueueSubmit(mQueue, 1, &submitInfo, cmdfence->fence);
|
||||
cmdfence->condition.notify_all();
|
||||
lock.unlock();
|
||||
|
||||
#if FILAMENT_VULKAN_VERBOSE
|
||||
if (result != VK_SUCCESS) {
|
||||
utils::slog.d <<"Failed command buffer submission result: " << result << utils::io::endl;
|
||||
}
|
||||
#endif
|
||||
assert_invariant(result == VK_SUCCESS);
|
||||
|
||||
mSubmissionSignal = renderingFinished;
|
||||
mInjectedSignal = VK_NULL_HANDLE;
|
||||
mCurrent = nullptr;
|
||||
mCurrentCommandBufferIndex = -1;
|
||||
return true;
|
||||
}
|
||||
|
||||
VkSemaphore VulkanCommands::acquireFinishedSignal() {
|
||||
VkSemaphore semaphore = mSubmissionSignal;
|
||||
mSubmissionSignal = VK_NULL_HANDLE;
|
||||
if (FILAMENT_VULKAN_VERBOSE) {
|
||||
slog.i << "Acquiring " << semaphore << " (e.g. for vkQueuePresentKHR)" << io::endl;
|
||||
}
|
||||
#if FILAMENT_VULKAN_VERBOSE
|
||||
slog.i << "Acquiring " << semaphore << " (e.g. for vkQueuePresentKHR)" << io::endl;
|
||||
#endif
|
||||
return semaphore;
|
||||
}
|
||||
|
||||
void VulkanCommands::injectDependency(VkSemaphore next) {
|
||||
assert_invariant(mInjectedSignal == VK_NULL_HANDLE);
|
||||
mInjectedSignal = next;
|
||||
if (FILAMENT_VULKAN_VERBOSE) {
|
||||
slog.i << "Injecting " << next << " (e.g. due to vkAcquireNextImageKHR)" << io::endl;
|
||||
}
|
||||
#if FILAMENT_VULKAN_VERBOSE
|
||||
slog.i << "Injecting " << next << " (e.g. due to vkAcquireNextImageKHR)" << io::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
void VulkanCommands::wait() {
|
||||
VkFence fences[CAPACITY];
|
||||
uint32_t count = 0;
|
||||
for (auto& wrapper : mStorage) {
|
||||
if (wrapper.cmdbuffer != VK_NULL_HANDLE && mCurrent != &wrapper) {
|
||||
fences[count++] = wrapper.fence->fence;
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < CAPACITY; i++) {
|
||||
auto wrapper = mStorage[i].get();
|
||||
if (wrapper->cmdbuffer != VK_NULL_HANDLE
|
||||
&& mCurrentCommandBufferIndex != static_cast<int8_t>(i)) {
|
||||
fences[count++] = wrapper->fence->fence;
|
||||
}
|
||||
}
|
||||
if (count > 0) {
|
||||
@@ -303,26 +334,34 @@ void VulkanCommands::wait() {
|
||||
}
|
||||
|
||||
void VulkanCommands::gc() {
|
||||
for (auto& wrapper : mStorage) {
|
||||
if (wrapper.cmdbuffer != VK_NULL_HANDLE) {
|
||||
uint64_t const timeout = wrapper.blockOnGC ? UINT64_MAX : 0;
|
||||
VkResult const result
|
||||
= vkWaitForFences(mDevice, 1, &wrapper.fence->fence, VK_TRUE, timeout);
|
||||
if (result == VK_SUCCESS) {
|
||||
vkFreeCommandBuffers(mDevice, mPool, 1, &wrapper.cmdbuffer);
|
||||
wrapper.cmdbuffer = VK_NULL_HANDLE;
|
||||
wrapper.fence->status.store(VK_SUCCESS);
|
||||
wrapper.fence.reset();
|
||||
++mAvailableCount;
|
||||
}
|
||||
VkCommandBuffer buffers[CAPACITY];
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < CAPACITY; i++) {
|
||||
auto wrapper = mStorage[i].get();
|
||||
if (wrapper->cmdbuffer == VK_NULL_HANDLE) {
|
||||
continue;
|
||||
}
|
||||
VkResult const result = vkWaitForFences(mDevice, 1, &wrapper->fence->fence, VK_TRUE, 0);
|
||||
if (result != VK_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
buffers[count++] = wrapper->cmdbuffer;
|
||||
wrapper->cmdbuffer = VK_NULL_HANDLE;
|
||||
wrapper->fence->status.store(VK_SUCCESS);
|
||||
wrapper->fence.reset();
|
||||
wrapper->clearResources();
|
||||
++mAvailableCount;
|
||||
}
|
||||
if (count > 0) {
|
||||
vkFreeCommandBuffers(mDevice, mPool, count, buffers);
|
||||
}
|
||||
}
|
||||
|
||||
void VulkanCommands::updateFences() {
|
||||
for (auto& wrapper : mStorage) {
|
||||
if (wrapper.cmdbuffer != VK_NULL_HANDLE) {
|
||||
VulkanCmdFence* fence = wrapper.fence.get();
|
||||
for (size_t i = 0; i < CAPACITY; i++) {
|
||||
auto wrapper = mStorage[i].get();
|
||||
if (wrapper->cmdbuffer != VK_NULL_HANDLE) {
|
||||
VulkanCmdFence* fence = wrapper->fence.get();
|
||||
if (fence) {
|
||||
VkResult status = vkGetFenceStatus(mDevice, fence->fence);
|
||||
// This is either VK_SUCCESS, VK_NOT_READY, or VK_ERROR_DEVICE_LOST.
|
||||
@@ -388,8 +427,9 @@ void VulkanCommands::popGroupMarker() {
|
||||
}
|
||||
} else if (mCarriedOverMarkers && !mCarriedOverMarkers->empty()) {
|
||||
// It could be that pop is called between flush() and get() (new command buffer), in which
|
||||
// case the marker is in "carried over" state. We'd just remove that
|
||||
mCarriedOverMarkers->pop();
|
||||
// case the marker is in "carried over" state, we'd just remove that. Since the
|
||||
// mCarriedOverMarkers is in the opposite order, we pop the bottom instead of the top.
|
||||
mCarriedOverMarkers->pop_bottom();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,14 +22,16 @@
|
||||
#include "DriverBase.h"
|
||||
|
||||
#include "VulkanConstants.h"
|
||||
#include "VulkanResources.h"
|
||||
|
||||
#include <utils/Condition.h>
|
||||
#include <utils/FixedCapacityVector.h>
|
||||
#include <utils/Mutex.h>
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include <chrono>
|
||||
#include <stack>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
@@ -43,13 +45,14 @@ public:
|
||||
|
||||
void push(std::string const& marker, Timestamp start = {}) noexcept;
|
||||
std::pair<std::string, Timestamp> pop() noexcept;
|
||||
std::pair<std::string, Timestamp> pop_bottom() noexcept;
|
||||
std::pair<std::string, Timestamp> top() const;
|
||||
bool empty() const noexcept;
|
||||
|
||||
private:
|
||||
std::stack<std::string> mMarkers;
|
||||
std::list<std::string> mMarkers;
|
||||
#if FILAMENT_VULKAN_VERBOSE
|
||||
std::stack<Timestamp> mTimestamps;
|
||||
std::list<Timestamp> mTimestamps;
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -68,12 +71,28 @@ struct VulkanCmdFence {
|
||||
// DriverApi fence object and should not be destroyed until both the DriverApi object is freed and
|
||||
// we're done waiting on the most recent submission of the given command buffer.
|
||||
struct VulkanCommandBuffer {
|
||||
VulkanCommandBuffer() {}
|
||||
VulkanCommandBuffer(VulkanResourceAllocator* allocator)
|
||||
: mResourceManager(allocator) {}
|
||||
|
||||
VulkanCommandBuffer(VulkanCommandBuffer const&) = delete;
|
||||
VulkanCommandBuffer& operator=(VulkanCommandBuffer const&) = delete;
|
||||
VkCommandBuffer cmdbuffer = VK_NULL_HANDLE;
|
||||
std::shared_ptr<VulkanCmdFence> fence;
|
||||
bool blockOnGC = false;
|
||||
|
||||
inline void acquire(VulkanResource* resource) {
|
||||
mResourceManager.acquire(resource);
|
||||
}
|
||||
|
||||
inline void acquire(VulkanAcquireOnlyResourceManager* srcResources) {
|
||||
mResourceManager.acquire(srcResources);
|
||||
}
|
||||
|
||||
inline void clearResources() {
|
||||
mResourceManager.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
VulkanAcquireOnlyResourceManager mResourceManager;
|
||||
};
|
||||
|
||||
// Allows classes to be notified after a new command buffer has been activated.
|
||||
@@ -112,14 +131,11 @@ public:
|
||||
class VulkanCommands {
|
||||
public:
|
||||
VulkanCommands(VkDevice device, VkQueue queue, uint32_t queueFamilyIndex,
|
||||
VulkanContext* context);
|
||||
VulkanContext* context, VulkanResourceAllocator* allocator);
|
||||
~VulkanCommands();
|
||||
|
||||
// Creates a "current" command buffer if none exists, otherwise returns the current one.
|
||||
// `blockOnGC` guarrantees that this buffer will be waited on when gc() is called on it so
|
||||
// that dependent resources can be gc'd safetly after the buffer is sumbitted, completed,
|
||||
// and gc'd.
|
||||
VulkanCommandBuffer const& get(bool blockOnGC = false);
|
||||
VulkanCommandBuffer& get();
|
||||
|
||||
// Submits the current command buffer if it exists, then sets "current" to null.
|
||||
// If there are no outstanding commands then nothing happens and this returns false.
|
||||
@@ -162,10 +178,12 @@ class VulkanCommands {
|
||||
VkCommandPool const mPool;
|
||||
VulkanContext const* mContext;
|
||||
|
||||
VulkanCommandBuffer* mCurrent = nullptr;
|
||||
// int8 only goes up to 127, therefore capacity must be less than that.
|
||||
static_assert(CAPACITY < 128);
|
||||
int8_t mCurrentCommandBufferIndex = -1;
|
||||
VkSemaphore mSubmissionSignal = {};
|
||||
VkSemaphore mInjectedSignal = {};
|
||||
VulkanCommandBuffer mStorage[CAPACITY] = {};
|
||||
utils::FixedCapacityVector<std::unique_ptr<VulkanCommandBuffer>> mStorage;
|
||||
VkSemaphore mSubmissionSignals[CAPACITY] = {};
|
||||
size_t mAvailableCount = CAPACITY;
|
||||
CommandBufferObserver* mObserver = nullptr;
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "VulkanDisposer.h"
|
||||
#include "VulkanConstants.h"
|
||||
|
||||
#include <utils/debug.h>
|
||||
#include <utils/Log.h>
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
// Always wait at least 3 frames after a DriverAPI-level resource has been destroyed for safe
|
||||
// destruction, due to potential usage by outstanding command buffers and triple buffering.
|
||||
static constexpr uint32_t FRAMES_BEFORE_EVICTION = VK_MAX_COMMAND_BUFFERS;
|
||||
|
||||
void VulkanDisposer::createDisposable(Key resource, std::function<void()> destructor) noexcept {
|
||||
mDisposables[resource].destructor = destructor;
|
||||
}
|
||||
|
||||
void VulkanDisposer::removeReference(Key resource) noexcept {
|
||||
// Null can be passed in as a no-op, this is not an error.
|
||||
if (resource == nullptr) {
|
||||
return;
|
||||
}
|
||||
assert_invariant(mDisposables[resource].refcount > 0);
|
||||
--mDisposables[resource].refcount;
|
||||
}
|
||||
|
||||
void VulkanDisposer::acquire(Key resource) noexcept {
|
||||
// It's fine to "acquire" a non-managed resource, it's just a no-op.
|
||||
if (resource == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto iter = mDisposables.find(resource);
|
||||
if (iter == mDisposables.end()) {
|
||||
return;
|
||||
}
|
||||
Disposable& disposable = iter.value();
|
||||
assert_invariant(disposable.refcount > 0 && disposable.refcount < 65535);
|
||||
|
||||
// If an auto-decrement is already in place, do not increase the ref count.
|
||||
if (disposable.remainingFrames == 0) {
|
||||
++disposable.refcount;
|
||||
}
|
||||
|
||||
disposable.remainingFrames = FRAMES_BEFORE_EVICTION;
|
||||
}
|
||||
|
||||
void VulkanDisposer::gc() noexcept {
|
||||
// First decrement the frame count of all resources that were held by a command buffer.
|
||||
// If any of these reaches zero, decrement its reference count.
|
||||
for (auto iter = mDisposables.begin(); iter != mDisposables.end(); ++iter) {
|
||||
Disposable& disposable = iter.value();
|
||||
if (disposable.refcount > 0 && disposable.remainingFrames > 0) {
|
||||
if (--disposable.remainingFrames == 0) {
|
||||
removeReference(iter.key());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Next, destroy all resources with a zero refcount.
|
||||
decltype(mDisposables) disposables;
|
||||
for (auto iter : mDisposables) {
|
||||
Disposable& disposable = iter.second;
|
||||
if (disposable.refcount == 0) {
|
||||
disposable.destructor();
|
||||
} else {
|
||||
disposables.insert({iter.first, disposable});
|
||||
}
|
||||
}
|
||||
disposables.swap(mDisposables);
|
||||
}
|
||||
|
||||
void VulkanDisposer::terminate() noexcept {
|
||||
#ifndef NDEBUG
|
||||
utils::slog.i << mDisposables.size() << " disposables are outstanding." << utils::io::endl;
|
||||
#endif
|
||||
for (auto iter : mDisposables) {
|
||||
iter.second.destructor();
|
||||
}
|
||||
mDisposables.clear();
|
||||
}
|
||||
|
||||
} // namespace filament::backend
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TNT_FILAMENT_BACKEND_VULKANDISPOSER_H
|
||||
#define TNT_FILAMENT_BACKEND_VULKANDISPOSER_H
|
||||
|
||||
#include <tsl/robin_map.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
// VulkanDisposer tracks resources (such as textures or vertex buffers) that need deferred
|
||||
// destruction due to potential use by a Vulkan command buffer. Resources are represented with void*
|
||||
// to allow callers to use any type of handle.
|
||||
class VulkanDisposer {
|
||||
public:
|
||||
using Key = const void*;
|
||||
|
||||
// Adds the given resource to the disposer and sets its reference count to 1.
|
||||
void createDisposable(Key resource, std::function<void()> destructor) noexcept;
|
||||
|
||||
// Decrements the reference count.
|
||||
void removeReference(Key resource) noexcept;
|
||||
|
||||
// Increments the reference count and auto-decrements it after FRAMES_BEFORE_EVICTION frames.
|
||||
// This is used to indicate that the current command buffer has a reference to the resource.
|
||||
void acquire(Key resource) noexcept;
|
||||
|
||||
// Invokes the destructor function for each disposable with a 0 refcount.
|
||||
void gc() noexcept;
|
||||
|
||||
// Invokes the destructor function for all disposables, regardless of reference count.
|
||||
void terminate() noexcept;
|
||||
|
||||
private:
|
||||
struct Disposable {
|
||||
uint16_t refcount = 1;
|
||||
uint16_t remainingFrames = 0;
|
||||
std::function<void()> destructor = []() {};
|
||||
};
|
||||
tsl::robin_map<Key, Disposable> mDisposables;
|
||||
};
|
||||
|
||||
} // namespace filament::backend
|
||||
|
||||
#endif // TNT_FILAMENT_BACKEND_VULKANDISPOSER_H
|
||||
@@ -94,7 +94,7 @@ VulkanTexture* createEmptyTexture(VkDevice device, VkPhysicalDevice physicalDevi
|
||||
VulkanTexture* emptyTexture = new VulkanTexture(device, physicalDevice, context, allocator,
|
||||
commands, SamplerType::SAMPLER_2D, 1, TextureFormat::RGBA8, 1, 1, 1, 1,
|
||||
TextureUsage::DEFAULT | TextureUsage::COLOR_ATTACHMENT | TextureUsage::SUBPASS_INPUT,
|
||||
stagePool);
|
||||
stagePool, true /* heap allocated */);
|
||||
uint32_t black = 0;
|
||||
PixelBufferDescriptor pbd(&black, 4, PixelDataFormat::RGBA, PixelDataType::UBYTE);
|
||||
emptyTexture->updateImage(pbd, 1, 1, 1, 0, 0, 0, 0);
|
||||
@@ -108,6 +108,10 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debugReportCallback(VkDebugReportFlagsEXT flags,
|
||||
if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
|
||||
utils::slog.e << "VULKAN ERROR: (" << pLayerPrefix << ") " << pMessage << utils::io::endl;
|
||||
} else {
|
||||
// TODO: emit best practices warnings about aggressive pipeline barriers.
|
||||
if (strstr(pMessage, "ALL_GRAPHICS_BIT") || strstr(pMessage, "ALL_COMMANDS_BIT")) {
|
||||
return VK_FALSE;
|
||||
}
|
||||
utils::slog.w << "VULKAN WARNING: (" << pLayerPrefix << ") " << pMessage << utils::io::endl;
|
||||
}
|
||||
utils::slog.e << utils::io::endl;
|
||||
@@ -122,8 +126,8 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debugUtilsCallback(VkDebugUtilsMessageSeverityFla
|
||||
<< utils::io::endl;
|
||||
} else {
|
||||
// TODO: emit best practices warnings about aggressive pipeline barriers.
|
||||
if (strstr(cbdata->pMessage, "ALL_GRAPHICS_BIT") ||
|
||||
strstr(cbdata->pMessage, "ALL_COMMANDS_BIT")) {
|
||||
if (strstr(cbdata->pMessage, "ALL_GRAPHICS_BIT")
|
||||
|| strstr(cbdata->pMessage, "ALL_COMMANDS_BIT")) {
|
||||
return VK_FALSE;
|
||||
}
|
||||
utils::slog.w << "VULKAN WARNING: (" << cbdata->pMessageIdName << ") " << cbdata->pMessage
|
||||
@@ -148,7 +152,10 @@ VulkanDriver::VulkanDriver(VulkanPlatform* platform, VulkanContext const& contex
|
||||
mAllocator(createAllocator(mPlatform->getInstance(), mPlatform->getPhysicalDevice(),
|
||||
mPlatform->getDevice())),
|
||||
mContext(context),
|
||||
mHandleAllocator(driverConfig.handleArenaSize),
|
||||
mResourceAllocator(driverConfig.handleArenaSize),
|
||||
mResourceManager(&mResourceAllocator),
|
||||
mThreadSafeResourceManager(&mResourceAllocator),
|
||||
mPipelineCache(&mResourceAllocator),
|
||||
mBlitter(mStagePool, mPipelineCache, mFramebufferCache, mSamplerCache),
|
||||
mReadPixels(mPlatform->getDevice()) {
|
||||
|
||||
@@ -183,7 +190,8 @@ VulkanDriver::VulkanDriver(VulkanPlatform* platform, VulkanContext const& contex
|
||||
#endif
|
||||
mTimestamps = std::make_unique<VulkanTimestamps>(mPlatform->getDevice());
|
||||
mCommands = std::make_unique<VulkanCommands>(mPlatform->getDevice(),
|
||||
mPlatform->getGraphicsQueue(), mPlatform->getGraphicsQueueFamilyIndex(), &mContext);
|
||||
mPlatform->getGraphicsQueue(), mPlatform->getGraphicsQueueFamilyIndex(), &mContext,
|
||||
&mResourceAllocator);
|
||||
mCommands->setObserver(&mPipelineCache);
|
||||
mPipelineCache.setDevice(mPlatform->getDevice(), mAllocator);
|
||||
|
||||
@@ -230,9 +238,8 @@ void VulkanDriver::terminate() {
|
||||
mBlitter.terminate();
|
||||
mReadPixels.terminate();
|
||||
|
||||
// Allow the stage pool and disposer to clean up.
|
||||
// Allow the stage pool to clean up.
|
||||
mStagePool.gc();
|
||||
mDisposer.terminate();
|
||||
|
||||
mStagePool.terminate();
|
||||
mPipelineCache.terminate();
|
||||
@@ -261,10 +268,20 @@ void VulkanDriver::tick(int) {
|
||||
void VulkanDriver::collectGarbage() {
|
||||
// Command buffers need to be submitted and completed before other resources can be gc'd. And
|
||||
// its gc() function carrys out the *wait*.
|
||||
SYSTRACE_CONTEXT();
|
||||
SYSTRACE_NAME_BEGIN("gc");
|
||||
|
||||
mCommands->gc();
|
||||
|
||||
SYSTRACE_NAME_BEGIN("stagepool-gc");
|
||||
mStagePool.gc();
|
||||
SYSTRACE_NAME_END();
|
||||
|
||||
SYSTRACE_NAME_BEGIN("framebuffercache-gc");
|
||||
mFramebufferCache.gc();
|
||||
mDisposer.gc();
|
||||
SYSTRACE_NAME_END();
|
||||
|
||||
SYSTRACE_NAME_END(); // "gc"
|
||||
}
|
||||
|
||||
void VulkanDriver::beginFrame(int64_t monotonic_clock_ns, uint32_t frameId) {
|
||||
@@ -276,7 +293,7 @@ void VulkanDriver::setFrameScheduledCallback(Handle<HwSwapChain> sch,
|
||||
}
|
||||
|
||||
void VulkanDriver::setFrameCompletedCallback(Handle<HwSwapChain> sch,
|
||||
FrameCompletedCallback callback, void* user) {
|
||||
CallbackHandler* handler, CallbackHandler::Callback callback, void* user) {
|
||||
}
|
||||
|
||||
void VulkanDriver::setPresentationTime(int64_t monotonic_clock_ns) {
|
||||
@@ -300,86 +317,84 @@ void VulkanDriver::finish(int dummy) {
|
||||
}
|
||||
|
||||
void VulkanDriver::createSamplerGroupR(Handle<HwSamplerGroup> sbh, uint32_t count) {
|
||||
mHandleAllocator.construct<VulkanSamplerGroup>(sbh, count);
|
||||
auto sg = mResourceAllocator.construct<VulkanSamplerGroup>(sbh, count);
|
||||
mResourceManager.acquire(sg);
|
||||
}
|
||||
|
||||
void VulkanDriver::createRenderPrimitiveR(Handle<HwRenderPrimitive> rph,
|
||||
Handle<HwVertexBuffer> vbh, Handle<HwIndexBuffer> ibh,
|
||||
PrimitiveType pt, uint32_t offset,
|
||||
uint32_t minIndex, uint32_t maxIndex, uint32_t count) {
|
||||
mHandleAllocator.construct<VulkanRenderPrimitive>(rph);
|
||||
auto rp = mResourceAllocator.construct<VulkanRenderPrimitive>(rph, &mResourceAllocator);
|
||||
mResourceManager.acquire(rp);
|
||||
VulkanDriver::setRenderPrimitiveBuffer(rph, vbh, ibh);
|
||||
VulkanDriver::setRenderPrimitiveRange(rph, pt, offset, minIndex, maxIndex, count);
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyRenderPrimitive(Handle<HwRenderPrimitive> rph) {
|
||||
if (rph) {
|
||||
mHandleAllocator.destruct<VulkanRenderPrimitive>(rph);
|
||||
if (!rph) {
|
||||
return;
|
||||
}
|
||||
auto rp = mResourceAllocator.handle_cast<VulkanRenderPrimitive*>(rph);
|
||||
mResourceManager.release(rp);
|
||||
}
|
||||
|
||||
void VulkanDriver::createVertexBufferR(Handle<HwVertexBuffer> vbh, uint8_t bufferCount,
|
||||
uint8_t attributeCount, uint32_t elementCount, AttributeArray attributes) {
|
||||
auto vertexBuffer = mHandleAllocator.construct<VulkanVertexBuffer>(vbh, mContext, mStagePool,
|
||||
bufferCount, attributeCount, elementCount, attributes);
|
||||
mDisposer.createDisposable(vertexBuffer, [this, vbh]() {
|
||||
mHandleAllocator.destruct<VulkanVertexBuffer>(vbh);
|
||||
});
|
||||
auto vertexBuffer = mResourceAllocator.construct<VulkanVertexBuffer>(vbh, mContext, mStagePool,
|
||||
&mResourceAllocator, bufferCount, attributeCount, elementCount, attributes);
|
||||
mResourceManager.acquire(vertexBuffer);
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyVertexBuffer(Handle<HwVertexBuffer> vbh) {
|
||||
if (vbh) {
|
||||
auto vertexBuffer = mHandleAllocator.handle_cast<VulkanVertexBuffer*>(vbh);
|
||||
mDisposer.removeReference(vertexBuffer);
|
||||
if (!vbh) {
|
||||
return;
|
||||
}
|
||||
auto vertexBuffer = mResourceAllocator.handle_cast<VulkanVertexBuffer*>(vbh);
|
||||
mResourceManager.release(vertexBuffer);
|
||||
}
|
||||
|
||||
void VulkanDriver::createIndexBufferR(Handle<HwIndexBuffer> ibh, ElementType elementType,
|
||||
uint32_t indexCount, BufferUsage usage) {
|
||||
auto elementSize = (uint8_t) getElementTypeSize(elementType);
|
||||
auto indexBuffer = mHandleAllocator.construct<VulkanIndexBuffer>(ibh, mAllocator,
|
||||
mCommands.get(), mStagePool, elementSize, indexCount);
|
||||
mDisposer.createDisposable(indexBuffer,
|
||||
[this, ibh]() { mHandleAllocator.destruct<VulkanIndexBuffer>(ibh); });
|
||||
auto indexBuffer = mResourceAllocator.construct<VulkanIndexBuffer>(ibh, mAllocator, mStagePool,
|
||||
elementSize, indexCount);
|
||||
mResourceManager.acquire(indexBuffer);
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyIndexBuffer(Handle<HwIndexBuffer> ibh) {
|
||||
if (ibh) {
|
||||
auto indexBuffer = mHandleAllocator.handle_cast<VulkanIndexBuffer*>(ibh);
|
||||
mDisposer.removeReference(indexBuffer);
|
||||
if (!ibh) {
|
||||
return;
|
||||
}
|
||||
auto indexBuffer = mResourceAllocator.handle_cast<VulkanIndexBuffer*>(ibh);
|
||||
mResourceManager.release(indexBuffer);
|
||||
}
|
||||
|
||||
void VulkanDriver::createBufferObjectR(Handle<HwBufferObject> boh, uint32_t byteCount,
|
||||
BufferObjectBinding bindingType, BufferUsage usage) {
|
||||
auto bufferObject = mHandleAllocator.construct<VulkanBufferObject>(boh, mAllocator,
|
||||
mCommands.get(), mStagePool, byteCount, bindingType, usage);
|
||||
mDisposer.createDisposable(bufferObject,
|
||||
[this, boh]() { mHandleAllocator.destruct<VulkanBufferObject>(boh); });
|
||||
auto bufferObject = mResourceAllocator.construct<VulkanBufferObject>(boh, mAllocator,
|
||||
mStagePool, byteCount, bindingType, usage);
|
||||
mResourceManager.acquire(bufferObject);
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyBufferObject(Handle<HwBufferObject> boh) {
|
||||
if (boh) {
|
||||
auto bufferObject = mHandleAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
if (bufferObject->bindingType == BufferObjectBinding::UNIFORM) {
|
||||
mPipelineCache.unbindUniformBuffer(bufferObject->buffer.getGpuBuffer());
|
||||
// Decrement the refcount of the uniform buffer, but schedule it for destruction a few
|
||||
// frames in the future. To be safe, we need to assume that the current command buffer is
|
||||
// still using it somewhere.
|
||||
mDisposer.acquire(bufferObject);
|
||||
}
|
||||
mDisposer.removeReference(bufferObject);
|
||||
if (!boh) {
|
||||
return;
|
||||
}
|
||||
auto bufferObject = mResourceAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
if (bufferObject->bindingType == BufferObjectBinding::UNIFORM) {
|
||||
mPipelineCache.unbindUniformBuffer(bufferObject->buffer.getGpuBuffer());
|
||||
}
|
||||
mResourceManager.release(bufferObject);
|
||||
}
|
||||
|
||||
void VulkanDriver::createTextureR(Handle<HwTexture> th, SamplerType target, uint8_t levels,
|
||||
TextureFormat format, uint8_t samples, uint32_t w, uint32_t h, uint32_t depth,
|
||||
TextureUsage usage) {
|
||||
auto vktexture = mHandleAllocator.construct<VulkanTexture>(th, mPlatform->getDevice(),
|
||||
auto vktexture = mResourceAllocator.construct<VulkanTexture>(th, mPlatform->getDevice(),
|
||||
mPlatform->getPhysicalDevice(), mContext, mAllocator, mCommands.get(), target, levels,
|
||||
format, samples, w, h, depth, usage, mStagePool);
|
||||
mDisposer.createDisposable(vktexture,
|
||||
[this, th]() { mHandleAllocator.destruct<VulkanTexture>(th); });
|
||||
mResourceManager.acquire(vktexture);
|
||||
}
|
||||
|
||||
void VulkanDriver::createTextureSwizzledR(Handle<HwTexture> th, SamplerType target, uint8_t levels,
|
||||
@@ -388,12 +403,10 @@ void VulkanDriver::createTextureSwizzledR(Handle<HwTexture> th, SamplerType targ
|
||||
TextureSwizzle r, TextureSwizzle g, TextureSwizzle b, TextureSwizzle a) {
|
||||
TextureSwizzle swizzleArray[] = {r, g, b, a};
|
||||
const VkComponentMapping swizzleMap = getSwizzleMap(swizzleArray);
|
||||
auto vktexture = mHandleAllocator.construct<VulkanTexture>(th, mPlatform->getDevice(),
|
||||
auto vktexture = mResourceAllocator.construct<VulkanTexture>(th, mPlatform->getDevice(),
|
||||
mPlatform->getPhysicalDevice(), mContext, mAllocator, mCommands.get(), target, levels,
|
||||
format, samples, w, h, depth, usage, mStagePool, swizzleMap);
|
||||
mDisposer.createDisposable(vktexture, [this, th]() {
|
||||
mHandleAllocator.destruct<VulkanTexture>(th);
|
||||
});
|
||||
format, samples, w, h, depth, usage, mStagePool, false /*heap allocated */, swizzleMap);
|
||||
mResourceManager.acquire(vktexture);
|
||||
}
|
||||
|
||||
void VulkanDriver::importTextureR(Handle<HwTexture> th, intptr_t id,
|
||||
@@ -404,33 +417,31 @@ void VulkanDriver::importTextureR(Handle<HwTexture> th, intptr_t id,
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyTexture(Handle<HwTexture> th) {
|
||||
if (th) {
|
||||
auto texture = mHandleAllocator.handle_cast<VulkanTexture*>(th);
|
||||
mPipelineCache.unbindImageView(texture->getPrimaryImageView());
|
||||
mDisposer.removeReference(texture);
|
||||
if (!th) {
|
||||
return;
|
||||
}
|
||||
auto texture = mResourceAllocator.handle_cast<VulkanTexture*>(th);
|
||||
mResourceManager.release(texture);
|
||||
}
|
||||
|
||||
void VulkanDriver::createProgramR(Handle<HwProgram> ph, Program&& program) {
|
||||
auto vkprogram = mHandleAllocator.construct<VulkanProgram>(ph, mPlatform->getDevice(), program);
|
||||
mDisposer.createDisposable(vkprogram, [this, ph] () {
|
||||
mHandleAllocator.destruct<VulkanProgram>(ph);
|
||||
});
|
||||
auto vkprogram = mResourceAllocator.construct<VulkanProgram>(ph, mPlatform->getDevice(), program);
|
||||
mResourceManager.acquire(vkprogram);
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyProgram(Handle<HwProgram> ph) {
|
||||
if (ph) {
|
||||
mDisposer.removeReference(mHandleAllocator.handle_cast<VulkanProgram*>(ph));
|
||||
if (!ph) {
|
||||
return;
|
||||
}
|
||||
auto vkprogram = mResourceAllocator.handle_cast<VulkanProgram*>(ph);
|
||||
mResourceManager.release(vkprogram);
|
||||
}
|
||||
|
||||
void VulkanDriver::createDefaultRenderTargetR(Handle<HwRenderTarget> rth, int) {
|
||||
assert_invariant(mDefaultRenderTarget == nullptr);
|
||||
VulkanRenderTarget* renderTarget = mHandleAllocator.construct<VulkanRenderTarget>(rth);
|
||||
VulkanRenderTarget* renderTarget = mResourceAllocator.construct<VulkanRenderTarget>(rth);
|
||||
mDefaultRenderTarget = renderTarget;
|
||||
mDisposer.createDisposable(renderTarget, [this, rth] () {
|
||||
mHandleAllocator.destruct<VulkanRenderTarget>(rth);
|
||||
});
|
||||
mResourceManager.acquire(renderTarget);
|
||||
}
|
||||
|
||||
void VulkanDriver::createRenderTargetR(Handle<HwRenderTarget> rth,
|
||||
@@ -444,7 +455,7 @@ void VulkanDriver::createRenderTargetR(Handle<HwRenderTarget> rth,
|
||||
for (int i = 0; i < MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT; i++) {
|
||||
if (color[i].handle) {
|
||||
colorTargets[i] = {
|
||||
.texture = mHandleAllocator.handle_cast<VulkanTexture*>(color[i].handle),
|
||||
.texture = mResourceAllocator.handle_cast<VulkanTexture*>(color[i].handle),
|
||||
.level = color[i].level,
|
||||
.layer = color[i].layer,
|
||||
};
|
||||
@@ -458,7 +469,7 @@ void VulkanDriver::createRenderTargetR(Handle<HwRenderTarget> rth,
|
||||
VulkanAttachment depthStencil[2] = {};
|
||||
if (depth.handle) {
|
||||
depthStencil[0] = {
|
||||
.texture = mHandleAllocator.handle_cast<VulkanTexture*>(depth.handle),
|
||||
.texture = mResourceAllocator.handle_cast<VulkanTexture*>(depth.handle),
|
||||
.level = depth.level,
|
||||
.layer = depth.layer,
|
||||
};
|
||||
@@ -470,7 +481,7 @@ void VulkanDriver::createRenderTargetR(Handle<HwRenderTarget> rth,
|
||||
|
||||
if (stencil.handle) {
|
||||
depthStencil[1] = {
|
||||
.texture = mHandleAllocator.handle_cast<VulkanTexture*>(stencil.handle),
|
||||
.texture = mResourceAllocator.handle_cast<VulkanTexture*>(stencil.handle),
|
||||
.level = stencil.level,
|
||||
.layer = stencil.layer,
|
||||
};
|
||||
@@ -486,26 +497,27 @@ void VulkanDriver::createRenderTargetR(Handle<HwRenderTarget> rth,
|
||||
assert_invariant(tmin == tmax);
|
||||
assert_invariant(tmin.x >= width && tmin.y >= height);
|
||||
|
||||
auto renderTarget = mHandleAllocator.construct<VulkanRenderTarget>(rth, mPlatform->getDevice(),
|
||||
auto renderTarget = mResourceAllocator.construct<VulkanRenderTarget>(rth, mPlatform->getDevice(),
|
||||
mPlatform->getPhysicalDevice(), mContext, mAllocator, mCommands.get(), width, height,
|
||||
samples, colorTargets, depthStencil, mStagePool);
|
||||
mDisposer.createDisposable(renderTarget,
|
||||
[this, rth]() { mHandleAllocator.destruct<VulkanRenderTarget>(rth); });
|
||||
mResourceManager.acquire(renderTarget);
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyRenderTarget(Handle<HwRenderTarget> rth) {
|
||||
if (rth) {
|
||||
VulkanRenderTarget* rt = mHandleAllocator.handle_cast<VulkanRenderTarget*>(rth);
|
||||
if (UTILS_UNLIKELY(rt == mDefaultRenderTarget)) {
|
||||
mDefaultRenderTarget = nullptr;
|
||||
}
|
||||
mDisposer.removeReference(rt);
|
||||
if (!rth) {
|
||||
return;
|
||||
}
|
||||
|
||||
VulkanRenderTarget* rt = mResourceAllocator.handle_cast<VulkanRenderTarget*>(rth);
|
||||
if (UTILS_UNLIKELY(rt == mDefaultRenderTarget)) {
|
||||
mDefaultRenderTarget = nullptr;
|
||||
}
|
||||
mResourceManager.release(rt);
|
||||
}
|
||||
|
||||
void VulkanDriver::createFenceR(Handle<HwFence> fh, int) {
|
||||
VulkanCommandBuffer const& commandBuffer = mCommands->get();
|
||||
mHandleAllocator.construct<VulkanFence>(fh, commandBuffer.fence);
|
||||
mResourceAllocator.construct<VulkanFence>(fh, commandBuffer.fence);
|
||||
}
|
||||
|
||||
void VulkanDriver::createSwapChainR(Handle<HwSwapChain> sch, void* nativeWindow, uint64_t flags) {
|
||||
@@ -514,7 +526,7 @@ void VulkanDriver::createSwapChainR(Handle<HwSwapChain> sch, void* nativeWindow,
|
||||
<< utils::io::endl;
|
||||
flags = flags | ~(backend::SWAP_CHAIN_CONFIG_SRGB_COLORSPACE);
|
||||
}
|
||||
mHandleAllocator.construct<VulkanSwapChain>(sch, mPlatform, mContext, mAllocator,
|
||||
mResourceAllocator.construct<VulkanSwapChain>(sch, mPlatform, mContext, mAllocator,
|
||||
mCommands.get(), mStagePool, nativeWindow, flags);
|
||||
}
|
||||
|
||||
@@ -527,7 +539,7 @@ void VulkanDriver::createSwapChainHeadlessR(Handle<HwSwapChain> sch, uint32_t wi
|
||||
flags = flags | ~(backend::SWAP_CHAIN_CONFIG_SRGB_COLORSPACE);
|
||||
}
|
||||
assert_invariant(width > 0 && height > 0 && "Vulkan requires non-zero swap chain dimensions.");
|
||||
mHandleAllocator.construct<VulkanSwapChain>(sch, mPlatform, mContext, mAllocator,
|
||||
mResourceAllocator.construct<VulkanSwapChain>(sch, mPlatform, mContext, mAllocator,
|
||||
mCommands.get(), mStagePool, nullptr, flags, VkExtent2D{width, height});
|
||||
}
|
||||
|
||||
@@ -536,106 +548,108 @@ void VulkanDriver::createTimerQueryR(Handle<HwTimerQuery> tqh, int) {
|
||||
}
|
||||
|
||||
Handle<HwVertexBuffer> VulkanDriver::createVertexBufferS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanVertexBuffer>();
|
||||
return mResourceAllocator.allocHandle<VulkanVertexBuffer>();
|
||||
}
|
||||
|
||||
Handle<HwIndexBuffer> VulkanDriver::createIndexBufferS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanIndexBuffer>();
|
||||
return mResourceAllocator.allocHandle<VulkanIndexBuffer>();
|
||||
}
|
||||
|
||||
Handle<HwBufferObject> VulkanDriver::createBufferObjectS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanBufferObject>();
|
||||
return mResourceAllocator.allocHandle<VulkanBufferObject>();
|
||||
}
|
||||
|
||||
Handle<HwTexture> VulkanDriver::createTextureS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanTexture>();
|
||||
return mResourceAllocator.allocHandle<VulkanTexture>();
|
||||
}
|
||||
|
||||
Handle<HwTexture> VulkanDriver::createTextureSwizzledS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanTexture>();
|
||||
return mResourceAllocator.allocHandle<VulkanTexture>();
|
||||
}
|
||||
|
||||
Handle<HwTexture> VulkanDriver::importTextureS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanTexture>();
|
||||
return mResourceAllocator.allocHandle<VulkanTexture>();
|
||||
}
|
||||
|
||||
Handle<HwSamplerGroup> VulkanDriver::createSamplerGroupS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanSamplerGroup>();
|
||||
return mResourceAllocator.allocHandle<VulkanSamplerGroup>();
|
||||
}
|
||||
|
||||
Handle<HwRenderPrimitive> VulkanDriver::createRenderPrimitiveS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanRenderPrimitive>();
|
||||
return mResourceAllocator.allocHandle<VulkanRenderPrimitive>();
|
||||
}
|
||||
|
||||
Handle<HwProgram> VulkanDriver::createProgramS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanProgram>();
|
||||
return mResourceAllocator.allocHandle<VulkanProgram>();
|
||||
}
|
||||
|
||||
Handle<HwRenderTarget> VulkanDriver::createDefaultRenderTargetS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanRenderTarget>();
|
||||
return mResourceAllocator.allocHandle<VulkanRenderTarget>();
|
||||
}
|
||||
|
||||
Handle<HwRenderTarget> VulkanDriver::createRenderTargetS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanRenderTarget>();
|
||||
return mResourceAllocator.allocHandle<VulkanRenderTarget>();
|
||||
}
|
||||
|
||||
Handle<HwFence> VulkanDriver::createFenceS() noexcept {
|
||||
return mHandleAllocator.initHandle<VulkanFence>();
|
||||
return mResourceAllocator.initHandle<VulkanFence>();
|
||||
}
|
||||
|
||||
Handle<HwSwapChain> VulkanDriver::createSwapChainS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanSwapChain>();
|
||||
return mResourceAllocator.allocHandle<VulkanSwapChain>();
|
||||
}
|
||||
|
||||
Handle<HwSwapChain> VulkanDriver::createSwapChainHeadlessS() noexcept {
|
||||
return mHandleAllocator.allocHandle<VulkanSwapChain>();
|
||||
return mResourceAllocator.allocHandle<VulkanSwapChain>();
|
||||
}
|
||||
|
||||
Handle<HwTimerQuery> VulkanDriver::createTimerQueryS() noexcept {
|
||||
// The handle must be constructed here, as a synchronous call to getTimerQueryValue might happen
|
||||
// before createTimerQueryR is executed.
|
||||
Handle<HwTimerQuery> tqh
|
||||
= mHandleAllocator.initHandle<VulkanTimerQuery>(mTimestamps->getNextQuery());
|
||||
auto query = mHandleAllocator.handle_cast<VulkanTimerQuery*>(tqh);
|
||||
mDisposer.createDisposable(query, [this, tqh] () {
|
||||
mHandleAllocator.destruct<VulkanTimerQuery>(tqh);
|
||||
});
|
||||
= mResourceAllocator.initHandle<VulkanTimerQuery>(mTimestamps->getNextQuery());
|
||||
auto query = mResourceAllocator.handle_cast<VulkanTimerQuery*>(tqh);
|
||||
mThreadSafeResourceManager.acquire(query);
|
||||
return tqh;
|
||||
}
|
||||
|
||||
void VulkanDriver::destroySamplerGroup(Handle<HwSamplerGroup> sbh) {
|
||||
if (sbh) {
|
||||
// Unlike most of the other "Hw" handles, the sampler buffer is an abstract concept and does
|
||||
// not map to any Vulkan objects. To handle destruction, the only thing we need to do is
|
||||
// ensure that the next draw call doesn't try to access a zombie sampler buffer. Therefore,
|
||||
// simply replace all weak references with null.
|
||||
auto* hwsb = mHandleAllocator.handle_cast<VulkanSamplerGroup*>(sbh);
|
||||
for (auto& binding : mSamplerBindings) {
|
||||
if (binding == hwsb) {
|
||||
binding = nullptr;
|
||||
}
|
||||
}
|
||||
mHandleAllocator.destruct<VulkanSamplerGroup>(sbh);
|
||||
if (!sbh) {
|
||||
return;
|
||||
}
|
||||
// Unlike most of the other "Hw" handles, the sampler buffer is an abstract concept and does
|
||||
// not map to any Vulkan objects. To handle destruction, the only thing we need to do is
|
||||
// ensure that the next draw call doesn't try to access a zombie sampler buffer. Therefore,
|
||||
// simply replace all weak references with null.
|
||||
auto* hwsb = mResourceAllocator.handle_cast<VulkanSamplerGroup*>(sbh);
|
||||
for (auto& binding : mSamplerBindings) {
|
||||
if (binding == hwsb) {
|
||||
binding = nullptr;
|
||||
}
|
||||
}
|
||||
mResourceManager.release(hwsb);
|
||||
}
|
||||
|
||||
void VulkanDriver::destroySwapChain(Handle<HwSwapChain> sch) {
|
||||
if (sch) {
|
||||
VulkanSwapChain& swapChain = *mHandleAllocator.handle_cast<VulkanSwapChain*>(sch);
|
||||
if (mCurrentSwapChain == &swapChain) {
|
||||
mCurrentSwapChain = nullptr;
|
||||
}
|
||||
mHandleAllocator.destruct<VulkanSwapChain>(sch);
|
||||
if (!sch) {
|
||||
return;
|
||||
}
|
||||
VulkanSwapChain* swapChain = mResourceAllocator.handle_cast<VulkanSwapChain*>(sch);
|
||||
if (mCurrentSwapChain == swapChain) {
|
||||
mCurrentSwapChain = nullptr;
|
||||
}
|
||||
mResourceManager.release(swapChain);
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyStream(Handle<HwStream> sh) {
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyTimerQuery(Handle<HwTimerQuery> tqh) {
|
||||
if (tqh) {
|
||||
mDisposer.removeReference(mHandleAllocator.handle_cast<VulkanTimerQuery*>(tqh));
|
||||
if (!tqh) {
|
||||
return;
|
||||
}
|
||||
auto vtq = mResourceAllocator.handle_cast<VulkanTimerQuery*>(tqh);
|
||||
mThreadSafeResourceManager.release(vtq);
|
||||
}
|
||||
|
||||
Handle<HwStream> VulkanDriver::createStreamNative(void* nativeStream) {
|
||||
@@ -661,11 +675,11 @@ void VulkanDriver::updateStreams(CommandStream* driver) {
|
||||
}
|
||||
|
||||
void VulkanDriver::destroyFence(Handle<HwFence> fh) {
|
||||
mHandleAllocator.destruct<VulkanFence>(fh);
|
||||
mResourceAllocator.destruct<VulkanFence>(fh);
|
||||
}
|
||||
|
||||
FenceStatus VulkanDriver::getFenceStatus(Handle<HwFence> fh) {
|
||||
auto& cmdfence = mHandleAllocator.handle_cast<VulkanFence*>(fh)->fence;
|
||||
auto& cmdfence = mResourceAllocator.handle_cast<VulkanFence*>(fh)->fence;
|
||||
if (!cmdfence) {
|
||||
// If wait is called before a fence actually exists, we return timeout. This matches the
|
||||
// current behavior in OpenGLDriver, but we should eventually reconsider a different error
|
||||
@@ -846,34 +860,42 @@ size_t VulkanDriver::getMaxUniformBufferSize() {
|
||||
|
||||
void VulkanDriver::setVertexBufferObject(Handle<HwVertexBuffer> vbh, uint32_t index,
|
||||
Handle<HwBufferObject> boh) {
|
||||
auto& vb = *mHandleAllocator.handle_cast<VulkanVertexBuffer*>(vbh);
|
||||
auto& bo = *mHandleAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
assert_invariant(bo.bindingType == BufferObjectBinding::VERTEX);
|
||||
vb.buffers[index] = &bo.buffer;
|
||||
auto vb = mResourceAllocator.handle_cast<VulkanVertexBuffer*>(vbh);
|
||||
auto bo = mResourceAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
assert_invariant(bo->bindingType == BufferObjectBinding::VERTEX);
|
||||
|
||||
vb->setBuffer(bo, index);
|
||||
}
|
||||
|
||||
void VulkanDriver::updateIndexBuffer(Handle<HwIndexBuffer> ibh, BufferDescriptor&& p,
|
||||
uint32_t byteOffset) {
|
||||
auto ib = mHandleAllocator.handle_cast<VulkanIndexBuffer*>(ibh);
|
||||
ib->buffer.loadFromCpu(p.buffer, byteOffset, p.size);
|
||||
mDisposer.acquire(ib);
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
auto ib = mResourceAllocator.handle_cast<VulkanIndexBuffer*>(ibh);
|
||||
commands.acquire(ib);
|
||||
ib->buffer.loadFromCpu(commands.cmdbuffer, p.buffer, byteOffset, p.size);
|
||||
|
||||
scheduleDestroy(std::move(p));
|
||||
}
|
||||
|
||||
void VulkanDriver::updateBufferObject(Handle<HwBufferObject> boh, BufferDescriptor&& bd,
|
||||
uint32_t byteOffset) {
|
||||
auto bo = mHandleAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
bo->buffer.loadFromCpu(bd.buffer, byteOffset, bd.size);
|
||||
mDisposer.acquire(bo);
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
|
||||
auto bo = mResourceAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
commands.acquire(bo);
|
||||
bo->buffer.loadFromCpu(commands.cmdbuffer, bd.buffer, byteOffset, bd.size);
|
||||
|
||||
scheduleDestroy(std::move(bd));
|
||||
}
|
||||
|
||||
void VulkanDriver::updateBufferObjectUnsynchronized(Handle<HwBufferObject> boh,
|
||||
BufferDescriptor&& bd, uint32_t byteOffset) {
|
||||
auto bo = mHandleAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
auto bo = mResourceAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
commands.acquire(bo);
|
||||
// TODO: implement unsynchronized version
|
||||
bo->buffer.loadFromCpu(bd.buffer, byteOffset, bd.size);
|
||||
mDisposer.acquire(bo);
|
||||
bo->buffer.loadFromCpu(commands.cmdbuffer, bd.buffer, byteOffset, bd.size);
|
||||
mResourceManager.acquire(bo);
|
||||
scheduleDestroy(std::move(bd));
|
||||
}
|
||||
|
||||
@@ -887,13 +909,13 @@ void VulkanDriver::resetBufferObject(Handle<HwBufferObject> boh) {
|
||||
}
|
||||
|
||||
void VulkanDriver::setMinMaxLevels(Handle<HwTexture> th, uint32_t minLevel, uint32_t maxLevel) {
|
||||
mHandleAllocator.handle_cast<VulkanTexture*>(th)->setPrimaryRange(minLevel, maxLevel);
|
||||
mResourceAllocator.handle_cast<VulkanTexture*>(th)->setPrimaryRange(minLevel, maxLevel);
|
||||
}
|
||||
|
||||
void VulkanDriver::update3DImage(Handle<HwTexture> th, uint32_t level, uint32_t xoffset,
|
||||
uint32_t yoffset, uint32_t zoffset, uint32_t width, uint32_t height, uint32_t depth,
|
||||
PixelBufferDescriptor&& data) {
|
||||
mHandleAllocator.handle_cast<VulkanTexture*>(th)->updateImage(data, width, height, depth,
|
||||
mResourceAllocator.handle_cast<VulkanTexture*>(th)->updateImage(data, width, height, depth,
|
||||
xoffset, yoffset, zoffset, level);
|
||||
scheduleDestroy(std::move(data));
|
||||
}
|
||||
@@ -902,7 +924,7 @@ void VulkanDriver::setupExternalImage(void* image) {
|
||||
}
|
||||
|
||||
bool VulkanDriver::getTimerQueryValue(Handle<HwTimerQuery> tqh, uint64_t* elapsedTime) {
|
||||
VulkanTimerQuery* vtq = mHandleAllocator.handle_cast<VulkanTimerQuery*>(tqh);
|
||||
VulkanTimerQuery* vtq = mResourceAllocator.handle_cast<VulkanTimerQuery*>(tqh);
|
||||
if (!vtq->isCompleted()) {
|
||||
return false;
|
||||
}
|
||||
@@ -946,7 +968,7 @@ bool VulkanDriver::canGenerateMipmaps() {
|
||||
|
||||
void VulkanDriver::updateSamplerGroup(Handle<HwSamplerGroup> sbh,
|
||||
BufferDescriptor&& data) {
|
||||
auto* sb = mHandleAllocator.handle_cast<VulkanSamplerGroup*>(sbh);
|
||||
auto* sb = mResourceAllocator.handle_cast<VulkanSamplerGroup*>(sbh);
|
||||
|
||||
// FIXME: we shouldn't be using SamplerGroup here, instead the backend should create
|
||||
// a descriptor or any internal data-structure that represents the textures/samplers.
|
||||
@@ -968,7 +990,7 @@ void VulkanDriver::compilePrograms(CompilerPriorityQueue priority,
|
||||
}
|
||||
|
||||
void VulkanDriver::beginRenderPass(Handle<HwRenderTarget> rth, const RenderPassParams& params) {
|
||||
VulkanRenderTarget* const rt = mHandleAllocator.handle_cast<VulkanRenderTarget*>(rth);
|
||||
VulkanRenderTarget* const rt = mResourceAllocator.handle_cast<VulkanRenderTarget*>(rth);
|
||||
const VkExtent2D extent = rt->getExtent();
|
||||
assert_invariant(extent.width > 0 && extent.height > 0);
|
||||
|
||||
@@ -997,7 +1019,8 @@ void VulkanDriver::beginRenderPass(Handle<HwRenderTarget> rth, const RenderPassP
|
||||
// more general layout. Otherwise, we prefer the DEPTH_ATTACHMENT layout, which is optimal for
|
||||
// the non-sampling case.
|
||||
bool samplingDepthAttachment = false;
|
||||
VkCommandBuffer const cmdbuffer = mCommands->get().cmdbuffer;
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
VkCommandBuffer const cmdbuffer = commands.cmdbuffer;
|
||||
|
||||
UTILS_NOUNROLL
|
||||
for (uint8_t samplerGroupIdx = 0; samplerGroupIdx < Program::SAMPLER_BINDING_COUNT;
|
||||
@@ -1011,7 +1034,7 @@ void VulkanDriver::beginRenderPass(Handle<HwRenderTarget> rth, const RenderPassP
|
||||
SamplerDescriptor const* boundSampler = sb->data() + i;
|
||||
if (UTILS_LIKELY(boundSampler->t)) {
|
||||
VulkanTexture* texture
|
||||
= mHandleAllocator.handle_cast<VulkanTexture*>(boundSampler->t);
|
||||
= mResourceAllocator.handle_cast<VulkanTexture*>(boundSampler->t);
|
||||
if (!any(texture->usage & TextureUsage::DEPTH_ATTACHMENT)) {
|
||||
continue;
|
||||
}
|
||||
@@ -1027,6 +1050,7 @@ void VulkanDriver::beginRenderPass(Handle<HwRenderTarget> rth, const RenderPassP
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = texture->depth,
|
||||
};
|
||||
commands.acquire(texture);
|
||||
texture->transitionLayout(cmdbuffer, subresources, VulkanLayout::DEPTH_SAMPLER);
|
||||
break;
|
||||
}
|
||||
@@ -1145,10 +1169,14 @@ void VulkanDriver::beginRenderPass(Handle<HwRenderTarget> rth, const RenderPassP
|
||||
|
||||
// The current command buffer now owns a reference to the render target and its attachments.
|
||||
// Note that we must acquire parent textures, not sidecars.
|
||||
mDisposer.acquire(rt);
|
||||
mDisposer.acquire((VulkanTexture const*) rt->getDepth().texture);
|
||||
commands.acquire(rt);
|
||||
if (depth.texture) {
|
||||
commands.acquire((VulkanTexture*) depth.texture);
|
||||
}
|
||||
for (int i = 0; i < MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT; i++) {
|
||||
mDisposer.acquire((VulkanTexture const*) rt->getColor(i).texture);
|
||||
if (rt->getColor(i).texture) {
|
||||
commands.acquire(rt->getColor(i).texture);
|
||||
}
|
||||
}
|
||||
|
||||
// Populate the structures required for vkCmdBeginRenderPass.
|
||||
@@ -1217,7 +1245,8 @@ void VulkanDriver::beginRenderPass(Handle<HwRenderTarget> rth, const RenderPassP
|
||||
}
|
||||
|
||||
void VulkanDriver::endRenderPass(int) {
|
||||
VkCommandBuffer cmdbuffer = mCommands->get().cmdbuffer;
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
VkCommandBuffer cmdbuffer = commands.cmdbuffer;
|
||||
vkCmdEndRenderPass(cmdbuffer);
|
||||
|
||||
VulkanRenderTarget* rt = mCurrentRenderPass.renderTarget;
|
||||
@@ -1288,15 +1317,15 @@ void VulkanDriver::nextSubpass(int) {
|
||||
|
||||
void VulkanDriver::setRenderPrimitiveBuffer(Handle<HwRenderPrimitive> rph,
|
||||
Handle<HwVertexBuffer> vbh, Handle<HwIndexBuffer> ibh) {
|
||||
auto primitive = mHandleAllocator.handle_cast<VulkanRenderPrimitive*>(rph);
|
||||
primitive->setBuffers(mHandleAllocator.handle_cast<VulkanVertexBuffer*>(vbh),
|
||||
mHandleAllocator.handle_cast<VulkanIndexBuffer*>(ibh));
|
||||
auto primitive = mResourceAllocator.handle_cast<VulkanRenderPrimitive*>(rph);
|
||||
primitive->setBuffers(mResourceAllocator.handle_cast<VulkanVertexBuffer*>(vbh),
|
||||
mResourceAllocator.handle_cast<VulkanIndexBuffer*>(ibh));
|
||||
}
|
||||
|
||||
void VulkanDriver::setRenderPrimitiveRange(Handle<HwRenderPrimitive> rph,
|
||||
PrimitiveType pt, uint32_t offset,
|
||||
uint32_t minIndex, uint32_t maxIndex, uint32_t count) {
|
||||
auto& primitive = *mHandleAllocator.handle_cast<VulkanRenderPrimitive*>(rph);
|
||||
auto& primitive = *mResourceAllocator.handle_cast<VulkanRenderPrimitive*>(rph);
|
||||
primitive.setPrimitiveType(pt);
|
||||
primitive.offset = offset * primitive.indexBuffer->elementSize;
|
||||
primitive.count = count;
|
||||
@@ -1306,8 +1335,9 @@ void VulkanDriver::setRenderPrimitiveRange(Handle<HwRenderPrimitive> rph,
|
||||
|
||||
void VulkanDriver::makeCurrent(Handle<HwSwapChain> drawSch, Handle<HwSwapChain> readSch) {
|
||||
ASSERT_PRECONDITION_NON_FATAL(drawSch == readSch,
|
||||
"Vulkan driver does not support distinct draw/read swap chains.");
|
||||
VulkanSwapChain* swapChain = mCurrentSwapChain = mHandleAllocator.handle_cast<VulkanSwapChain*>(drawSch);
|
||||
"Vulkan driver does not support distinct draw/read swap chains.");
|
||||
VulkanSwapChain* swapChain = mCurrentSwapChain
|
||||
= mResourceAllocator.handle_cast<VulkanSwapChain*>(drawSch);
|
||||
|
||||
bool resized = false;
|
||||
swapChain->acquire(resized);
|
||||
@@ -1322,7 +1352,7 @@ void VulkanDriver::makeCurrent(Handle<HwSwapChain> drawSch, Handle<HwSwapChain>
|
||||
}
|
||||
|
||||
void VulkanDriver::commit(Handle<HwSwapChain> sch) {
|
||||
VulkanSwapChain* swapChain = mHandleAllocator.handle_cast<VulkanSwapChain*>(sch);
|
||||
VulkanSwapChain* swapChain = mResourceAllocator.handle_cast<VulkanSwapChain*>(sch);
|
||||
|
||||
if (mCommands->flush()) {
|
||||
collectGarbage();
|
||||
@@ -1333,10 +1363,10 @@ void VulkanDriver::commit(Handle<HwSwapChain> sch) {
|
||||
}
|
||||
|
||||
void VulkanDriver::bindUniformBuffer(uint32_t index, Handle<HwBufferObject> boh) {
|
||||
auto* bo = mHandleAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
auto* bo = mResourceAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
const VkDeviceSize offset = 0;
|
||||
const VkDeviceSize size = VK_WHOLE_SIZE;
|
||||
mPipelineCache.bindUniformBuffer((uint32_t) index, bo->buffer.getGpuBuffer(), offset, size);
|
||||
mPipelineCache.bindUniformBufferObject((uint32_t) index, bo, offset, size);
|
||||
}
|
||||
|
||||
void VulkanDriver::bindBufferRange(BufferObjectBinding bindingType, uint32_t index,
|
||||
@@ -1347,8 +1377,8 @@ void VulkanDriver::bindBufferRange(BufferObjectBinding bindingType, uint32_t ind
|
||||
|
||||
// TODO: implement BufferObjectBinding::SHADER_STORAGE case
|
||||
|
||||
auto* bo = mHandleAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
mPipelineCache.bindUniformBuffer((uint32_t)index, bo->buffer.getGpuBuffer(), offset, size);
|
||||
auto* bo = mResourceAllocator.handle_cast<VulkanBufferObject*>(boh);
|
||||
mPipelineCache.bindUniformBufferObject((uint32_t)index, bo, offset, size);
|
||||
}
|
||||
|
||||
void VulkanDriver::unbindBuffer(BufferObjectBinding bindingType, uint32_t index) {
|
||||
@@ -1356,7 +1386,7 @@ void VulkanDriver::unbindBuffer(BufferObjectBinding bindingType, uint32_t index)
|
||||
}
|
||||
|
||||
void VulkanDriver::bindSamplers(uint32_t index, Handle<HwSamplerGroup> sbh) {
|
||||
auto* hwsb = mHandleAllocator.handle_cast<VulkanSamplerGroup*>(sbh);
|
||||
auto* hwsb = mResourceAllocator.handle_cast<VulkanSamplerGroup*>(sbh);
|
||||
mSamplerBindings[index] = hwsb;
|
||||
}
|
||||
|
||||
@@ -1387,12 +1417,13 @@ void VulkanDriver::stopCapture(int) {}
|
||||
|
||||
void VulkanDriver::readPixels(Handle<HwRenderTarget> src, uint32_t x, uint32_t y, uint32_t width,
|
||||
uint32_t height, PixelBufferDescriptor&& pbd) {
|
||||
VulkanRenderTarget* srcTarget = mHandleAllocator.handle_cast<VulkanRenderTarget*>(src);
|
||||
VulkanRenderTarget* srcTarget = mResourceAllocator.handle_cast<VulkanRenderTarget*>(src);
|
||||
mCommands->flush();
|
||||
mReadPixels.run(
|
||||
srcTarget, x, y, width, height, mPlatform->getGraphicsQueueFamilyIndex(),
|
||||
std::move(pbd),
|
||||
[&context = mContext](uint32_t reqs, VkFlags flags) {
|
||||
utils::slog.d <<"read pixels: reqs=" << reqs <<" flags=" << flags << utils::io::endl;
|
||||
return context.selectMemoryType(reqs, flags);
|
||||
},
|
||||
[this](PixelBufferDescriptor&& pbd) {
|
||||
@@ -1419,8 +1450,8 @@ void VulkanDriver::blit(TargetBufferFlags buffers, Handle<HwRenderTarget> dst, V
|
||||
return;
|
||||
}
|
||||
|
||||
VulkanRenderTarget* dstTarget = mHandleAllocator.handle_cast<VulkanRenderTarget*>(dst);
|
||||
VulkanRenderTarget* srcTarget = mHandleAllocator.handle_cast<VulkanRenderTarget*>(src);
|
||||
VulkanRenderTarget* dstTarget = mResourceAllocator.handle_cast<VulkanRenderTarget*>(dst);
|
||||
VulkanRenderTarget* srcTarget = mResourceAllocator.handle_cast<VulkanRenderTarget*>(src);
|
||||
|
||||
VkFilter vkfilter = filter == SamplerMagFilter::NEAREST ? VK_FILTER_NEAREST : VK_FILTER_LINEAR;
|
||||
|
||||
@@ -1451,19 +1482,19 @@ void VulkanDriver::blit(TargetBufferFlags buffers, Handle<HwRenderTarget> dst, V
|
||||
|
||||
void VulkanDriver::draw(PipelineState pipelineState, Handle<HwRenderPrimitive> rph,
|
||||
const uint32_t instanceCount) {
|
||||
VulkanCommandBuffer const* commands = &mCommands->get();
|
||||
VulkanCommandBuffer* commands = &mCommands->get();
|
||||
VkCommandBuffer cmdbuffer = commands->cmdbuffer;
|
||||
const VulkanRenderPrimitive& prim = *mHandleAllocator.handle_cast<VulkanRenderPrimitive*>(rph);
|
||||
const VulkanRenderPrimitive& prim = *mResourceAllocator.handle_cast<VulkanRenderPrimitive*>(rph);
|
||||
|
||||
Handle<HwProgram> programHandle = pipelineState.program;
|
||||
RasterState rasterState = pipelineState.rasterState;
|
||||
PolygonOffset depthOffset = pipelineState.polygonOffset;
|
||||
const Viewport& scissorBox = pipelineState.scissor;
|
||||
Viewport const& scissorBox = pipelineState.scissor;
|
||||
|
||||
auto* program = mHandleAllocator.handle_cast<VulkanProgram*>(programHandle);
|
||||
mDisposer.acquire(program);
|
||||
mDisposer.acquire(prim.indexBuffer);
|
||||
mDisposer.acquire(prim.vertexBuffer);
|
||||
auto* program = mResourceAllocator.handle_cast<VulkanProgram*>(programHandle);
|
||||
commands->acquire(program);
|
||||
commands->acquire(prim.indexBuffer);
|
||||
commands->acquire(prim.vertexBuffer);
|
||||
|
||||
// If this is a debug build, validate the current shader.
|
||||
#if !defined(NDEBUG)
|
||||
@@ -1553,6 +1584,8 @@ void VulkanDriver::draw(PipelineState pipelineState, Handle<HwRenderPrimitive> r
|
||||
// Filament concept used to form groups of samplers.
|
||||
|
||||
VkDescriptorImageInfo samplerInfo[VulkanPipelineCache::SAMPLER_BINDING_COUNT] = {};
|
||||
VulkanTexture* samplerTextures[VulkanPipelineCache::SAMPLER_BINDING_COUNT] = {nullptr};
|
||||
|
||||
VulkanPipelineCache::UsageFlags usage;
|
||||
|
||||
UTILS_NOUNROLL
|
||||
@@ -1574,8 +1607,7 @@ void VulkanDriver::draw(PipelineState pipelineState, Handle<HwRenderPrimitive> r
|
||||
samplerIdx++;
|
||||
|
||||
if (UTILS_LIKELY(boundSampler->t)) {
|
||||
VulkanTexture* texture = mHandleAllocator.handle_cast<VulkanTexture*>(boundSampler->t);
|
||||
mDisposer.acquire(texture);
|
||||
VulkanTexture* texture = mResourceAllocator.handle_cast<VulkanTexture*>(boundSampler->t);
|
||||
|
||||
// TODO: can this uninitialized check be checked in a higher layer?
|
||||
// This fallback path is very flaky because the dummy texture might not have
|
||||
@@ -1586,7 +1618,6 @@ void VulkanDriver::draw(PipelineState pipelineState, Handle<HwRenderPrimitive> r
|
||||
utils::slog.w << " in material '" << program->name.c_str() << "'";
|
||||
utils::slog.w << " at binding point " << +sampler.binding << utils::io::endl;
|
||||
#endif
|
||||
// Calling get() won't leak here since `texture` is local.
|
||||
texture = mEmptyTexture.get();
|
||||
}
|
||||
|
||||
@@ -1600,11 +1631,12 @@ void VulkanDriver::draw(PipelineState pipelineState, Handle<HwRenderPrimitive> r
|
||||
.imageView = texture->getPrimaryImageView(),
|
||||
.imageLayout = ImgUtil::getVkLayout(texture->getPrimaryImageLayout())
|
||||
};
|
||||
samplerTextures[sampler.binding] = texture;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mPipelineCache.bindSamplers(samplerInfo, usage);
|
||||
mPipelineCache.bindSamplers(samplerInfo, samplerTextures, usage);
|
||||
|
||||
// Bind new descriptor sets if they need to change.
|
||||
// If descriptor set allocation failed, skip the draw call and bail. No need to emit an error
|
||||
@@ -1637,7 +1669,7 @@ void VulkanDriver::draw(PipelineState pipelineState, Handle<HwRenderPrimitive> r
|
||||
// Bind a new pipeline if the pipeline state changed.
|
||||
// If allocation failed, skip the draw call and bail. We do not emit an error since the
|
||||
// validation layer will already do so.
|
||||
if (!mPipelineCache.bindPipeline(cmdbuffer)) {
|
||||
if (!mPipelineCache.bindPipeline(commands)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1653,6 +1685,7 @@ void VulkanDriver::draw(PipelineState pipelineState, Handle<HwRenderPrimitive> r
|
||||
const uint32_t firstIndex = prim.offset / prim.indexBuffer->elementSize;
|
||||
const int32_t vertexOffset = 0;
|
||||
const uint32_t firstInstId = 0;
|
||||
|
||||
vkCmdDrawIndexed(cmdbuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstId);
|
||||
}
|
||||
|
||||
@@ -1661,12 +1694,12 @@ void VulkanDriver::dispatchCompute(Handle<HwProgram> program, math::uint3 workGr
|
||||
}
|
||||
|
||||
void VulkanDriver::beginTimerQuery(Handle<HwTimerQuery> tqh) {
|
||||
VulkanTimerQuery* vtq = mHandleAllocator.handle_cast<VulkanTimerQuery*>(tqh);
|
||||
VulkanTimerQuery* vtq = mResourceAllocator.handle_cast<VulkanTimerQuery*>(tqh);
|
||||
mTimestamps->beginQuery(&(mCommands->get()), vtq);
|
||||
}
|
||||
|
||||
void VulkanDriver::endTimerQuery(Handle<HwTimerQuery> tqh) {
|
||||
VulkanTimerQuery* vtq = mHandleAllocator.handle_cast<VulkanTimerQuery*>(tqh);
|
||||
VulkanTimerQuery* vtq = mResourceAllocator.handle_cast<VulkanTimerQuery*>(tqh);
|
||||
mTimestamps->endQuery(&(mCommands->get()), vtq);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,18 +20,17 @@
|
||||
#include "VulkanBlitter.h"
|
||||
#include "VulkanConstants.h"
|
||||
#include "VulkanContext.h"
|
||||
#include "VulkanDisposer.h"
|
||||
#include "VulkanFboCache.h"
|
||||
#include "VulkanHandles.h"
|
||||
#include "VulkanPipelineCache.h"
|
||||
#include "VulkanReadPixels.h"
|
||||
#include "VulkanResourceAllocator.h"
|
||||
#include "VulkanSamplerCache.h"
|
||||
#include "VulkanStagePool.h"
|
||||
#include "VulkanUtility.h"
|
||||
|
||||
#include "DriverBase.h"
|
||||
#include "private/backend/Driver.h"
|
||||
#include "private/backend/HandleAllocator.h"
|
||||
|
||||
#include <utils/Allocator.h>
|
||||
#include <utils/compiler.h>
|
||||
@@ -41,60 +40,6 @@ namespace filament::backend {
|
||||
class VulkanPlatform;
|
||||
struct VulkanSamplerGroup;
|
||||
|
||||
class VulkanHandleAllocator {
|
||||
public:
|
||||
VulkanHandleAllocator(size_t arenaSize)
|
||||
: mHandleAllocatorImpl("Handles", arenaSize) {}
|
||||
|
||||
template<typename D, typename... ARGS>
|
||||
inline Handle<D> initHandle(ARGS&&... args) noexcept {
|
||||
return mHandleAllocatorImpl.allocateAndConstruct<D>(std::forward<ARGS>(args)...);
|
||||
}
|
||||
|
||||
template<typename D>
|
||||
inline Handle<D> allocHandle() noexcept {
|
||||
return mHandleAllocatorImpl.allocate<D>();
|
||||
}
|
||||
|
||||
template<typename D, typename B, typename... ARGS>
|
||||
inline typename std::enable_if<std::is_base_of<B, D>::value, D>::type* construct(
|
||||
Handle<B> const& handle, ARGS&&... args) noexcept {
|
||||
return mHandleAllocatorImpl.construct<D, B>(handle, std::forward<ARGS>(args)...);
|
||||
}
|
||||
|
||||
template<typename B, typename D,
|
||||
typename = typename std::enable_if<std::is_base_of<B, D>::value, D>::type>
|
||||
inline void destruct(Handle<B> handle, D const* p) noexcept {
|
||||
return mHandleAllocatorImpl.deallocate(handle, p);
|
||||
}
|
||||
|
||||
template<typename Dp, typename B>
|
||||
inline typename std::enable_if_t<
|
||||
std::is_pointer_v<Dp> && std::is_base_of_v<B, typename std::remove_pointer_t<Dp>>, Dp>
|
||||
handle_cast(Handle<B>& handle) noexcept {
|
||||
return mHandleAllocatorImpl.handle_cast<Dp, B>(handle);
|
||||
}
|
||||
|
||||
template<typename Dp, typename B>
|
||||
inline typename std::enable_if_t<
|
||||
std::is_pointer_v<Dp> && std::is_base_of_v<B, typename std::remove_pointer_t<Dp>>, Dp>
|
||||
handle_cast(Handle<B> const& handle) noexcept {
|
||||
return mHandleAllocatorImpl.handle_cast<Dp, B>(handle);
|
||||
}
|
||||
|
||||
template<typename D, typename B>
|
||||
inline void destruct(Handle<B> handle) noexcept {
|
||||
if constexpr (std::is_base_of_v<VulkanIndexBuffer, D>
|
||||
|| std::is_base_of_v<VulkanBufferObject, D>) {
|
||||
auto ptr = handle_cast<D*>(handle);
|
||||
ptr->terminate();
|
||||
}
|
||||
destruct(handle, handle_cast<D const*>(handle));
|
||||
}
|
||||
|
||||
HandleAllocatorVK mHandleAllocatorImpl;
|
||||
};
|
||||
|
||||
class VulkanDriver final : public DriverBase {
|
||||
public:
|
||||
static Driver* create(VulkanPlatform* platform, VulkanContext const& context,
|
||||
@@ -153,9 +98,14 @@ private:
|
||||
VkDebugUtilsMessengerEXT mDebugMessenger = VK_NULL_HANDLE;
|
||||
|
||||
VulkanContext mContext = {};
|
||||
VulkanHandleAllocator mHandleAllocator;
|
||||
VulkanResourceAllocator mResourceAllocator;
|
||||
VulkanResourceManager mResourceManager;
|
||||
|
||||
// Used for resources that are created synchronously and used and destroyed on the backend
|
||||
// thread.
|
||||
VulkanThreadSafeResourceManager mThreadSafeResourceManager;
|
||||
|
||||
VulkanPipelineCache mPipelineCache;
|
||||
VulkanDisposer mDisposer;
|
||||
VulkanStagePool mStagePool;
|
||||
VulkanFboCache mFramebufferCache;
|
||||
VulkanSamplerCache mSamplerCache;
|
||||
|
||||
@@ -46,10 +46,12 @@ static void clampToFramebuffer(VkRect2D* rect, uint32_t fbWidth, uint32_t fbHeig
|
||||
rect->extent.height = std::max(top - y, 0);
|
||||
}
|
||||
|
||||
VulkanProgram::VulkanProgram(VkDevice device, const Program& builder) noexcept :
|
||||
HwProgram(builder.getName()), mDevice(device) {
|
||||
VulkanProgram::VulkanProgram(VkDevice device, const Program& builder) noexcept
|
||||
: HwProgram(builder.getName()),
|
||||
VulkanResource(VulkanResourceType::PROGRAM),
|
||||
mDevice(device) {
|
||||
auto const& blobs = builder.getShadersSource();
|
||||
VkShaderModule* modules[2] = { &bundle.vertex, &bundle.fragment };
|
||||
VkShaderModule* modules[2] = {&bundle.vertex, &bundle.fragment};
|
||||
// TODO: handle compute shaders.
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
const auto& blob = blobs[i];
|
||||
@@ -113,8 +115,9 @@ VulkanProgram::VulkanProgram(VkDevice device, const Program& builder) noexcept :
|
||||
}
|
||||
}
|
||||
|
||||
VulkanProgram::VulkanProgram(VkDevice device, VkShaderModule vs, VkShaderModule fs) noexcept :
|
||||
mDevice(device) {
|
||||
VulkanProgram::VulkanProgram(VkDevice device, VkShaderModule vs, VkShaderModule fs) noexcept
|
||||
: VulkanResource(VulkanResourceType::PROGRAM),
|
||||
mDevice(device) {
|
||||
bundle.vertex = vs;
|
||||
bundle.fragment = fs;
|
||||
}
|
||||
@@ -126,7 +129,10 @@ VulkanProgram::~VulkanProgram() {
|
||||
}
|
||||
|
||||
// Creates a special "default" render target (i.e. associated with the swap chain)
|
||||
VulkanRenderTarget::VulkanRenderTarget() : HwRenderTarget(0, 0), mOffscreen(false), mSamples(1) {}
|
||||
VulkanRenderTarget::VulkanRenderTarget() :
|
||||
HwRenderTarget(0, 0),
|
||||
VulkanResource(VulkanResourceType::RENDER_TARGET),
|
||||
mOffscreen(false), mSamples(1) {}
|
||||
|
||||
void VulkanRenderTarget::bindToSwapChain(VulkanSwapChain& swapChain) {
|
||||
assert_invariant(!mOffscreen);
|
||||
@@ -138,11 +144,14 @@ void VulkanRenderTarget::bindToSwapChain(VulkanSwapChain& swapChain) {
|
||||
}
|
||||
|
||||
VulkanRenderTarget::VulkanRenderTarget(VkDevice device, VkPhysicalDevice physicalDevice,
|
||||
VulkanContext const& context, VmaAllocator allocator,
|
||||
VulkanCommands* commands, uint32_t width, uint32_t height, uint8_t samples,
|
||||
VulkanContext const& context, VmaAllocator allocator, VulkanCommands* commands,
|
||||
uint32_t width, uint32_t height, uint8_t samples,
|
||||
VulkanAttachment color[MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT],
|
||||
VulkanAttachment depthStencil[2], VulkanStagePool& stagePool)
|
||||
: HwRenderTarget(width, height), mOffscreen(true), mSamples(samples) {
|
||||
: HwRenderTarget(width, height),
|
||||
VulkanResource(VulkanResourceType::RENDER_TARGET),
|
||||
mOffscreen(true),
|
||||
mSamples(samples) {
|
||||
for (int index = 0; index < MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT; index++) {
|
||||
mColor[index] = color[index];
|
||||
}
|
||||
@@ -166,10 +175,11 @@ VulkanRenderTarget::VulkanRenderTarget(VkDevice device, VkPhysicalDevice physica
|
||||
if (texture && texture->samples == 1) {
|
||||
auto msTexture = texture->getSidecar();
|
||||
if (UTILS_UNLIKELY(!msTexture)) {
|
||||
msTexture = new VulkanTexture(device, physicalDevice, context,
|
||||
allocator, commands, texture->target,
|
||||
((VulkanTexture const*) texture)->levels, texture->format, samples,
|
||||
texture->width, texture->height, texture->depth, texture->usage, stagePool);
|
||||
// TODO: This should be allocated with the ResourceAllocator.
|
||||
msTexture = new VulkanTexture(device, physicalDevice, context, allocator, commands,
|
||||
texture->target, ((VulkanTexture const*) texture)->levels, texture->format,
|
||||
samples, texture->width, texture->height, texture->depth, texture->usage,
|
||||
stagePool, true /* heap allocated */);
|
||||
texture->setSidecar(msTexture);
|
||||
}
|
||||
mMsaaAttachments[index] = {.texture = msTexture};
|
||||
@@ -198,7 +208,7 @@ VulkanRenderTarget::VulkanRenderTarget(VkDevice device, VkPhysicalDevice physica
|
||||
msTexture = new VulkanTexture(device, physicalDevice, context, allocator,
|
||||
commands, depthTexture->target, msLevel, depthTexture->format, samples,
|
||||
depthTexture->width, depthTexture->height, depthTexture->depth, depthTexture->usage,
|
||||
stagePool);
|
||||
stagePool, true /* heap allocated */);
|
||||
depthTexture->setSidecar(msTexture);
|
||||
}
|
||||
|
||||
@@ -257,16 +267,23 @@ uint8_t VulkanRenderTarget::getColorTargetCount(const VulkanRenderPass& pass) co
|
||||
}
|
||||
|
||||
VulkanVertexBuffer::VulkanVertexBuffer(VulkanContext& context, VulkanStagePool& stagePool,
|
||||
uint8_t bufferCount, uint8_t attributeCount,
|
||||
uint32_t elementCount, AttributeArray const& attribs) :
|
||||
HwVertexBuffer(bufferCount, attributeCount, elementCount, attribs),
|
||||
buffers(bufferCount, nullptr) {}
|
||||
VulkanResourceAllocator* allocator, uint8_t bufferCount, uint8_t attributeCount,
|
||||
uint32_t elementCount, AttributeArray const& attribs)
|
||||
: HwVertexBuffer(bufferCount, attributeCount, elementCount, attribs),
|
||||
VulkanResource(VulkanResourceType::VERTEX_BUFFER),
|
||||
buffers(bufferCount, nullptr),
|
||||
mResources(allocator) {}
|
||||
|
||||
VulkanBufferObject::VulkanBufferObject(VmaAllocator allocator,
|
||||
VulkanCommands* commands, VulkanStagePool& stagePool, uint32_t byteCount,
|
||||
BufferObjectBinding bindingType, BufferUsage usage)
|
||||
void VulkanVertexBuffer::setBuffer(VulkanBufferObject* bufferObject, uint32_t index) {
|
||||
buffers[index] = &bufferObject->buffer;
|
||||
mResources.acquire(bufferObject);
|
||||
}
|
||||
|
||||
VulkanBufferObject::VulkanBufferObject(VmaAllocator allocator, VulkanStagePool& stagePool,
|
||||
uint32_t byteCount, BufferObjectBinding bindingType, BufferUsage usage)
|
||||
: HwBufferObject(byteCount),
|
||||
buffer(allocator, commands, stagePool, getBufferObjectUsage(bindingType), byteCount),
|
||||
VulkanResource(VulkanResourceType::BUFFER_OBJECT),
|
||||
buffer(allocator, stagePool, getBufferObjectUsage(bindingType), byteCount),
|
||||
bindingType(bindingType) {}
|
||||
|
||||
void VulkanRenderPrimitive::setPrimitiveType(PrimitiveType pt) {
|
||||
@@ -294,10 +311,14 @@ void VulkanRenderPrimitive::setBuffers(VulkanVertexBuffer* vertexBuffer,
|
||||
VulkanIndexBuffer* indexBuffer) {
|
||||
this->vertexBuffer = vertexBuffer;
|
||||
this->indexBuffer = indexBuffer;
|
||||
mResources.acquire(vertexBuffer);
|
||||
mResources.acquire(indexBuffer);
|
||||
}
|
||||
|
||||
VulkanTimerQuery::VulkanTimerQuery(std::tuple<uint32_t, uint32_t> indices)
|
||||
: mStartingQueryIndex(std::get<0>(indices)), mStoppingQueryIndex(std::get<1>(indices)) {}
|
||||
: VulkanThreadSafeResource(VulkanResourceType::TIMER_QUERY),
|
||||
mStartingQueryIndex(std::get<0>(indices)),
|
||||
mStoppingQueryIndex(std::get<1>(indices)) {}
|
||||
|
||||
void VulkanTimerQuery::setFence(std::shared_ptr<VulkanCmdFence> fence) noexcept {
|
||||
std::unique_lock<utils::Mutex> lock(mFenceMutex);
|
||||
|
||||
@@ -20,8 +20,9 @@
|
||||
// This needs to be at the top
|
||||
#include "DriverBase.h"
|
||||
|
||||
#include "VulkanPipelineCache.h"
|
||||
#include "VulkanBuffer.h"
|
||||
#include "VulkanPipelineCache.h"
|
||||
#include "VulkanResources.h"
|
||||
#include "VulkanSwapChain.h"
|
||||
#include "VulkanTexture.h"
|
||||
#include "VulkanUtility.h"
|
||||
@@ -34,7 +35,7 @@ namespace filament::backend {
|
||||
|
||||
class VulkanTimestamps;
|
||||
|
||||
struct VulkanProgram : public HwProgram {
|
||||
struct VulkanProgram : public HwProgram, VulkanResource {
|
||||
VulkanProgram(VkDevice device, const Program& builder) noexcept;
|
||||
VulkanProgram(VkDevice device, VkShaderModule vs, VkShaderModule fs) noexcept;
|
||||
~VulkanProgram();
|
||||
@@ -53,7 +54,7 @@ private:
|
||||
//
|
||||
// We use private inheritance to shield clients from the width / height fields in HwRenderTarget,
|
||||
// which are not representative when this is the default render target.
|
||||
struct VulkanRenderTarget : private HwRenderTarget {
|
||||
struct VulkanRenderTarget : private HwRenderTarget, VulkanResource {
|
||||
// Creates an offscreen render target.
|
||||
VulkanRenderTarget(VkDevice device, VkPhysicalDevice physicalDevice,
|
||||
VulkanContext const& context, VmaAllocator allocator,
|
||||
@@ -86,29 +87,43 @@ private:
|
||||
uint8_t mSamples : 7;
|
||||
};
|
||||
|
||||
struct VulkanVertexBuffer : public HwVertexBuffer {
|
||||
struct VulkanBufferObject;
|
||||
|
||||
struct VulkanVertexBuffer : public HwVertexBuffer, VulkanResource {
|
||||
VulkanVertexBuffer(VulkanContext& context, VulkanStagePool& stagePool,
|
||||
uint8_t bufferCount, uint8_t attributeCount, uint32_t elementCount,
|
||||
AttributeArray const& attributes);
|
||||
VulkanResourceAllocator* allocator, uint8_t bufferCount, uint8_t attributeCount,
|
||||
uint32_t elementCount, AttributeArray const& attributes);
|
||||
|
||||
void setBuffer(VulkanBufferObject* bufferObject, uint32_t index);
|
||||
|
||||
inline void terminate() {
|
||||
mResources.clear();
|
||||
}
|
||||
|
||||
utils::FixedCapacityVector<VulkanBuffer const*> buffers;
|
||||
|
||||
private:
|
||||
FixedSizeVulkanResourceManager mResources;
|
||||
};
|
||||
|
||||
struct VulkanIndexBuffer : public HwIndexBuffer {
|
||||
VulkanIndexBuffer(VmaAllocator allocator, VulkanCommands* commands,
|
||||
VulkanStagePool& stagePool, uint8_t elementSize, uint32_t indexCount)
|
||||
struct VulkanIndexBuffer : public HwIndexBuffer, VulkanResource {
|
||||
VulkanIndexBuffer(VmaAllocator allocator, VulkanStagePool& stagePool, uint8_t elementSize,
|
||||
uint32_t indexCount)
|
||||
: HwIndexBuffer(elementSize, indexCount),
|
||||
buffer(allocator, commands, stagePool, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
||||
elementSize * indexCount),
|
||||
VulkanResource(VulkanResourceType::INDEX_BUFFER),
|
||||
buffer(allocator, stagePool, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, elementSize * indexCount),
|
||||
indexType(elementSize == 2 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32) {}
|
||||
void terminate() { buffer.terminate(); }
|
||||
|
||||
void terminate() {
|
||||
buffer.terminate();
|
||||
}
|
||||
VulkanBuffer buffer;
|
||||
const VkIndexType indexType;
|
||||
};
|
||||
|
||||
struct VulkanBufferObject : public HwBufferObject {
|
||||
VulkanBufferObject(VmaAllocator allocator, VulkanCommands* commands,
|
||||
VulkanStagePool& stagePool, uint32_t byteCount, BufferObjectBinding bindingType,
|
||||
BufferUsage usage);
|
||||
struct VulkanBufferObject : public HwBufferObject, VulkanResource {
|
||||
VulkanBufferObject(VmaAllocator allocator, VulkanStagePool& stagePool, uint32_t byteCount,
|
||||
BufferObjectBinding bindingType, BufferUsage usage);
|
||||
void terminate() {
|
||||
buffer.terminate();
|
||||
}
|
||||
@@ -116,28 +131,45 @@ struct VulkanBufferObject : public HwBufferObject {
|
||||
const BufferObjectBinding bindingType;
|
||||
};
|
||||
|
||||
struct VulkanSamplerGroup : public HwSamplerGroup {
|
||||
struct VulkanSamplerGroup : public HwSamplerGroup, VulkanResource {
|
||||
// NOTE: we have to use out-of-line allocation here because the size of a Handle<> is limited
|
||||
std::unique_ptr<SamplerGroup> sb; // FIXME: this shouldn't depend on filament::SamplerGroup
|
||||
explicit VulkanSamplerGroup(size_t size) noexcept : sb(new SamplerGroup(size)) { }
|
||||
std::unique_ptr<SamplerGroup> sb;// FIXME: this shouldn't depend on filament::SamplerGroup
|
||||
explicit VulkanSamplerGroup(size_t size) noexcept
|
||||
: VulkanResource(VulkanResourceType::SAMPLER_GROUP),
|
||||
sb(new SamplerGroup(size)) {}
|
||||
};
|
||||
|
||||
struct VulkanRenderPrimitive : public HwRenderPrimitive {
|
||||
struct VulkanRenderPrimitive : public HwRenderPrimitive, VulkanResource {
|
||||
VulkanRenderPrimitive(VulkanResourceAllocator* allocator)
|
||||
: VulkanResource(VulkanResourceType::RENDER_PRIMITIVE),
|
||||
mResources(allocator) {}
|
||||
|
||||
~VulkanRenderPrimitive() {
|
||||
mResources.clear();
|
||||
}
|
||||
|
||||
void setPrimitiveType(PrimitiveType pt);
|
||||
void setBuffers(VulkanVertexBuffer* vertexBuffer, VulkanIndexBuffer* indexBuffer);
|
||||
VulkanVertexBuffer* vertexBuffer = nullptr;
|
||||
VulkanIndexBuffer* indexBuffer = nullptr;
|
||||
VkPrimitiveTopology primitiveTopology;
|
||||
|
||||
private:
|
||||
FixedSizeVulkanResourceManager mResources;
|
||||
};
|
||||
|
||||
struct VulkanFence : public HwFence {
|
||||
VulkanFence() = default;
|
||||
explicit VulkanFence(std::shared_ptr<VulkanCmdFence> fence) : fence(fence) {}
|
||||
struct VulkanFence : public HwFence, VulkanResource {
|
||||
VulkanFence()
|
||||
: VulkanResource(VulkanResourceType::FENCE) {}
|
||||
|
||||
explicit VulkanFence(std::shared_ptr<VulkanCmdFence> fence)
|
||||
: VulkanResource(VulkanResourceType::FENCE),
|
||||
fence(fence) {}
|
||||
|
||||
std::shared_ptr<VulkanCmdFence> fence;
|
||||
};
|
||||
|
||||
struct VulkanTimerQuery : public HwTimerQuery {
|
||||
struct VulkanTimerQuery : public HwTimerQuery, VulkanThreadSafeResource {
|
||||
explicit VulkanTimerQuery(std::tuple<uint32_t, uint32_t> indices);
|
||||
~VulkanTimerQuery();
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
#include "VulkanConstants.h"
|
||||
#include "VulkanHandles.h"
|
||||
#include "VulkanTexture.h"
|
||||
#include "VulkanUtility.h"
|
||||
|
||||
// Vulkan functions often immediately dereference pointers, so it's fine to pass in a pointer
|
||||
@@ -64,7 +65,10 @@ VulkanPipelineCache::getUsageFlags(uint16_t binding, ShaderStageFlags flags, Usa
|
||||
return src;
|
||||
}
|
||||
|
||||
VulkanPipelineCache::VulkanPipelineCache() : mCurrentRasterState(createDefaultRasterState()) {
|
||||
VulkanPipelineCache::VulkanPipelineCache(VulkanResourceAllocator* allocator)
|
||||
: mCurrentRasterState(createDefaultRasterState()),
|
||||
mResourceAllocator(allocator),
|
||||
mPipelineBoundResources(allocator) {
|
||||
mDummyBufferWriteInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
mDummyBufferWriteInfo.pNext = nullptr;
|
||||
mDummyBufferWriteInfo.dstArrayElement = 0;
|
||||
@@ -144,6 +148,15 @@ bool VulkanPipelineCache::bindDescriptors(VkCommandBuffer cmdbuffer) noexcept {
|
||||
|
||||
cacheEntry->lastUsed = mCurrentTime;
|
||||
mBoundDescriptor = mDescriptorRequirements;
|
||||
// This passes the currently "bound" uniform buffer objects to pipeline that will be used in the
|
||||
// draw call.
|
||||
auto resourceEntry = mDescriptorResources.find(cacheEntry->id);
|
||||
if (resourceEntry == mDescriptorResources.end()) {
|
||||
mDescriptorResources[cacheEntry->id]
|
||||
= std::make_unique<VulkanAcquireOnlyResourceManager>(mResourceAllocator);
|
||||
resourceEntry = mDescriptorResources.find(cacheEntry->id);
|
||||
}
|
||||
resourceEntry->second->acquire(&mPipelineBoundResources);
|
||||
|
||||
vkCmdBindDescriptorSets(cmdbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
getOrCreatePipelineLayout()->handle, 0, VulkanPipelineCache::DESCRIPTOR_TYPE_COUNT,
|
||||
@@ -152,7 +165,9 @@ bool VulkanPipelineCache::bindDescriptors(VkCommandBuffer cmdbuffer) noexcept {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VulkanPipelineCache::bindPipeline(VkCommandBuffer cmdbuffer) noexcept {
|
||||
bool VulkanPipelineCache::bindPipeline(VulkanCommandBuffer* commands) noexcept {
|
||||
VkCommandBuffer const cmdbuffer = commands->cmdbuffer;
|
||||
|
||||
PipelineMap::iterator pipelineIter = mPipelines.find(mPipelineRequirements);
|
||||
|
||||
// Check if the required pipeline is already bound.
|
||||
@@ -191,7 +206,10 @@ void VulkanPipelineCache::bindScissor(VkCommandBuffer cmdbuffer, VkRect2D scisso
|
||||
VulkanPipelineCache::DescriptorCacheEntry* VulkanPipelineCache::createDescriptorSets() noexcept {
|
||||
PipelineLayoutCacheEntry* layoutCacheEntry = getOrCreatePipelineLayout();
|
||||
|
||||
DescriptorCacheEntry descriptorCacheEntry = { .pipelineLayout = mPipelineRequirements.layout };
|
||||
DescriptorCacheEntry descriptorCacheEntry = {
|
||||
.pipelineLayout = mPipelineRequirements.layout,
|
||||
.id = mDescriptorCacheEntryCount++,
|
||||
};
|
||||
|
||||
// Each of the arenas for this particular layout are guaranteed to have the same size. Check
|
||||
// the first arena to see if any descriptor sets are available that can be re-claimed. If not,
|
||||
@@ -602,13 +620,19 @@ void VulkanPipelineCache::unbindImageView(VkImageView imageView) noexcept {
|
||||
}
|
||||
}
|
||||
|
||||
void VulkanPipelineCache::bindUniformBuffer(uint32_t bindingIndex, VkBuffer uniformBuffer,
|
||||
void VulkanPipelineCache::bindUniformBufferObject(uint32_t bindingIndex,
|
||||
VulkanBufferObject* bufferObject, VkDeviceSize offset, VkDeviceSize size) noexcept {
|
||||
bindUniformBuffer(bindingIndex, bufferObject->buffer.getGpuBuffer(), offset, size);
|
||||
mPipelineBoundResources.acquire(bufferObject);
|
||||
}
|
||||
|
||||
void VulkanPipelineCache::bindUniformBuffer(uint32_t bindingIndex, VkBuffer buffer,
|
||||
VkDeviceSize offset, VkDeviceSize size) noexcept {
|
||||
ASSERT_POSTCONDITION(bindingIndex < UBUFFER_BINDING_COUNT,
|
||||
"Uniform bindings overflow: index = %d, capacity = %d.",
|
||||
bindingIndex, UBUFFER_BINDING_COUNT);
|
||||
"Uniform bindings overflow: index = %d, capacity = %d.", bindingIndex,
|
||||
UBUFFER_BINDING_COUNT);
|
||||
auto& key = mDescriptorRequirements;
|
||||
key.uniformBuffers[bindingIndex] = uniformBuffer;
|
||||
key.uniformBuffers[bindingIndex] = buffer;
|
||||
|
||||
if (size == VK_WHOLE_SIZE) {
|
||||
size = WHOLE_SIZE;
|
||||
@@ -622,9 +646,12 @@ void VulkanPipelineCache::bindUniformBuffer(uint32_t bindingIndex, VkBuffer unif
|
||||
}
|
||||
|
||||
void VulkanPipelineCache::bindSamplers(VkDescriptorImageInfo samplers[SAMPLER_BINDING_COUNT],
|
||||
UsageFlags flags) noexcept {
|
||||
VulkanTexture* textures[SAMPLER_BINDING_COUNT], UsageFlags flags) noexcept {
|
||||
for (uint32_t bindingIndex = 0; bindingIndex < SAMPLER_BINDING_COUNT; bindingIndex++) {
|
||||
mDescriptorRequirements.samplers[bindingIndex] = samplers[bindingIndex];
|
||||
if (textures[bindingIndex]) {
|
||||
mPipelineBoundResources.acquire(textures[bindingIndex]);
|
||||
}
|
||||
}
|
||||
mPipelineRequirements.layout = flags;
|
||||
}
|
||||
@@ -643,6 +670,7 @@ void VulkanPipelineCache::terminate() noexcept {
|
||||
for (auto& iter : mPipelines) {
|
||||
vkDestroyPipeline(mDevice, iter.second.handle, VKALLOC);
|
||||
}
|
||||
mPipelineBoundResources.clear();
|
||||
mPipelines.clear();
|
||||
mBoundPipeline = {};
|
||||
vmaDestroyBuffer(mAllocator, mDummyBuffer, mDummyMemory);
|
||||
@@ -676,6 +704,7 @@ void VulkanPipelineCache::onCommandBuffer(const VulkanCommandBuffer& cmdbuffer)
|
||||
arenas[i].push_back(cacheEntry.handles[i]);
|
||||
}
|
||||
++mDescriptorArenasCount;
|
||||
mDescriptorResources.erase(cacheEntry.id);
|
||||
iter = mDescriptorSets.erase(iter);
|
||||
} else {
|
||||
++iter;
|
||||
@@ -736,6 +765,10 @@ void VulkanPipelineCache::onCommandBuffer(const VulkanCommandBuffer& cmdbuffer)
|
||||
vkDestroyDescriptorPool(mDevice, pool, VKALLOC);
|
||||
}
|
||||
mExtinctDescriptorPools.clear();
|
||||
|
||||
for (auto const& entry : mExtinctDescriptorBundles) {
|
||||
mDescriptorResources.erase(entry.id);
|
||||
}
|
||||
mExtinctDescriptorBundles.clear();
|
||||
}
|
||||
}
|
||||
@@ -798,6 +831,10 @@ void VulkanPipelineCache::destroyLayoutsAndDescriptors() noexcept {
|
||||
mExtinctDescriptorPools.clear();
|
||||
mExtinctDescriptorBundles.clear();
|
||||
|
||||
// Both mDescriptorSets and mExtinctDescriptorBundles have been cleared, so it's safe to call
|
||||
// clear() on mDescriptorResources.
|
||||
mDescriptorResources.clear();
|
||||
|
||||
mBoundDescriptor = {};
|
||||
}
|
||||
|
||||
|
||||
@@ -28,9 +28,11 @@
|
||||
#include <utils/compiler.h>
|
||||
#include <utils/Hash.h>
|
||||
|
||||
#include <list>
|
||||
#include <tsl/robin_map.h>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "VulkanCommands.h"
|
||||
|
||||
@@ -41,6 +43,9 @@ VK_DEFINE_HANDLE(VmaPool)
|
||||
namespace filament::backend {
|
||||
|
||||
struct VulkanProgram;
|
||||
struct VulkanBufferObject;
|
||||
struct VulkanTexture;
|
||||
class VulkanResourceAllocator;
|
||||
|
||||
// VulkanPipelineCache manages a cache of descriptor sets and pipelines.
|
||||
//
|
||||
@@ -131,7 +136,7 @@ public:
|
||||
|
||||
// Upon construction, the pipeCache initializes some internal state but does not make any Vulkan
|
||||
// calls. On destruction it will free any cached Vulkan objects that haven't already been freed.
|
||||
VulkanPipelineCache();
|
||||
VulkanPipelineCache(VulkanResourceAllocator* allocator);
|
||||
~VulkanPipelineCache();
|
||||
void setDevice(VkDevice device, VmaAllocator allocator);
|
||||
|
||||
@@ -141,7 +146,7 @@ public:
|
||||
|
||||
// Creates a new pipeline if necessary and binds it using vkCmdBindPipeline.
|
||||
// Returns false if an error occurred.
|
||||
bool bindPipeline(VkCommandBuffer cmdbuffer) noexcept;
|
||||
bool bindPipeline(VulkanCommandBuffer* commands) noexcept;
|
||||
|
||||
// Sets up a new scissor rectangle if it has been dirtied.
|
||||
void bindScissor(VkCommandBuffer cmdbuffer, VkRect2D scissor) noexcept;
|
||||
@@ -151,9 +156,12 @@ public:
|
||||
void bindRasterState(const RasterState& rasterState) noexcept;
|
||||
void bindRenderPass(VkRenderPass renderPass, int subpassIndex) noexcept;
|
||||
void bindPrimitiveTopology(VkPrimitiveTopology topology) noexcept;
|
||||
void bindUniformBuffer(uint32_t bindingIndex, VkBuffer uniformBuffer,
|
||||
void bindUniformBufferObject(uint32_t bindingIndex, VulkanBufferObject* bufferObject,
|
||||
VkDeviceSize offset = 0, VkDeviceSize size = VK_WHOLE_SIZE) noexcept;
|
||||
void bindSamplers(VkDescriptorImageInfo samplers[SAMPLER_BINDING_COUNT], UsageFlags flags) noexcept;
|
||||
void bindUniformBuffer(uint32_t bindingIndex, VkBuffer buffer,
|
||||
VkDeviceSize offset = 0, VkDeviceSize size = VK_WHOLE_SIZE) noexcept;
|
||||
void bindSamplers(VkDescriptorImageInfo samplers[SAMPLER_BINDING_COUNT],
|
||||
VulkanTexture* textures[SAMPLER_BINDING_COUNT], UsageFlags flags) noexcept;
|
||||
void bindInputAttachment(uint32_t bindingIndex, VkDescriptorImageInfo imageInfo) noexcept;
|
||||
void bindVertexArray(const VertexArray& varray) noexcept;
|
||||
|
||||
@@ -185,17 +193,22 @@ public:
|
||||
mDummyTargetInfo.imageView = imageView;
|
||||
}
|
||||
|
||||
// Acquires a resource to be bound to the current pipeline. The ownership of the resource
|
||||
// will be transferred to the corresponding pipeline when pipeline is bound.
|
||||
void acquireResource(VulkanResource* resource) {
|
||||
mPipelineBoundResources.acquire(resource);
|
||||
}
|
||||
|
||||
inline RasterState getCurrentRasterState() const noexcept {
|
||||
return mCurrentRasterState;
|
||||
return mCurrentRasterState;
|
||||
}
|
||||
|
||||
// We need to update this outside of bindRasterState due to VulkanDriver::draw.
|
||||
inline void setCurrentRasterState(RasterState const& rasterState) noexcept {
|
||||
mCurrentRasterState = rasterState;
|
||||
mCurrentRasterState = rasterState;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// PIPELINE LAYOUT CACHE KEY
|
||||
// -------------------------
|
||||
|
||||
@@ -337,7 +350,10 @@ private:
|
||||
std::array<VkDescriptorSet, DESCRIPTOR_TYPE_COUNT> handles;
|
||||
Timestamp lastUsed;
|
||||
PipelineLayoutKey pipelineLayout;
|
||||
uint32_t id;
|
||||
};
|
||||
uint32_t mDescriptorCacheEntryCount = 0;
|
||||
|
||||
|
||||
struct PipelineCacheEntry {
|
||||
VkPipeline handle;
|
||||
@@ -372,12 +388,15 @@ private:
|
||||
PipelineLayoutKeyHashFn, PipelineLayoutKeyEqual>;
|
||||
using PipelineMap = tsl::robin_map<PipelineKey, PipelineCacheEntry,
|
||||
PipelineHashFn, PipelineEqual>;
|
||||
using DescriptorMap = tsl::robin_map<DescriptorKey, DescriptorCacheEntry,
|
||||
DescHashFn, DescEqual>;
|
||||
using DescriptorMap
|
||||
= tsl::robin_map<DescriptorKey, DescriptorCacheEntry, DescHashFn, DescEqual>;
|
||||
using DescriptorResourceMap
|
||||
= std::unordered_map<uint32_t, std::unique_ptr<VulkanAcquireOnlyResourceManager>>;
|
||||
|
||||
PipelineLayoutMap mPipelineLayouts;
|
||||
PipelineMap mPipelines;
|
||||
DescriptorMap mDescriptorSets;
|
||||
DescriptorResourceMap mDescriptorResources;
|
||||
|
||||
// These helpers all return unstable pointers that should not be stored.
|
||||
DescriptorCacheEntry* createDescriptorSets() noexcept;
|
||||
@@ -425,8 +444,8 @@ private:
|
||||
// After a growth event (i.e. when the VkDescriptorPool is replaced with a bigger version), all
|
||||
// currently used descriptors are moved into the "extinct" sets so that they can be safely
|
||||
// destroyed a few frames later.
|
||||
std::vector<VkDescriptorPool> mExtinctDescriptorPools;
|
||||
std::vector<DescriptorCacheEntry> mExtinctDescriptorBundles;
|
||||
std::list<VkDescriptorPool> mExtinctDescriptorPools;
|
||||
std::list<DescriptorCacheEntry> mExtinctDescriptorBundles;
|
||||
|
||||
VkDescriptorBufferInfo mDummyBufferInfo = {};
|
||||
VkWriteDescriptorSet mDummyBufferWriteInfo = {};
|
||||
@@ -435,6 +454,9 @@ private:
|
||||
|
||||
VkBuffer mDummyBuffer;
|
||||
VmaAllocation mDummyMemory;
|
||||
|
||||
VulkanResourceAllocator* mResourceAllocator;
|
||||
VulkanAcquireOnlyResourceManager mPipelineBoundResources;
|
||||
};
|
||||
|
||||
} // namespace filament::backend
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (C) 2023 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TNT_FILAMENT_BACKEND_VULKANRESOURCEALLOCATOR_H
|
||||
#define TNT_FILAMENT_BACKEND_VULKANRESOURCEALLOCATOR_H
|
||||
|
||||
#include "VulkanHandles.h"
|
||||
|
||||
#include <private/backend/HandleAllocator.h>
|
||||
|
||||
#include <utils/FixedCapacityVector.h>
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <type_traits>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
// RESOURCE_TYPE_COUNT matches the count of enum VulkanResourceType.
|
||||
#define RESOURCE_TYPE_COUNT 12
|
||||
#define DEBUG_RESOURCE_LEAKS 0
|
||||
|
||||
#if DEBUG_RESOURCE_LEAKS
|
||||
#define TRACK_INCREMENT() \
|
||||
if (!IS_MANAGED_TYPE(obj->mType)) { \
|
||||
mDebugOnlyResourceCount[static_cast<size_t>(obj->mType)]++; \
|
||||
}
|
||||
#define TRACK_DECREMENT() \
|
||||
if (!IS_MANAGED_TYPE(obj->mType)) { \
|
||||
mDebugOnlyResourceCount[static_cast<size_t>(obj->mType)]--; \
|
||||
}
|
||||
#else
|
||||
// No-op
|
||||
#define TRACK_INCREMENT()
|
||||
#define TRACK_DECREMENT()
|
||||
#endif
|
||||
|
||||
class VulkanResourceAllocator {
|
||||
|
||||
public:
|
||||
VulkanResourceAllocator(size_t arenaSize)
|
||||
: mHandleAllocatorImpl("Handles", arenaSize)
|
||||
#if DEBUG_RESOURCE_LEAKS
|
||||
, mDebugOnlyResourceCount(RESOURCE_TYPE_COUNT) {
|
||||
std::memset(mDebugOnlyResourceCount.data(), 0, sizeof(size_t) * RESOURCE_TYPE_COUNT);
|
||||
}
|
||||
#else
|
||||
{}
|
||||
#endif
|
||||
|
||||
template<typename D, typename... ARGS>
|
||||
inline Handle<D> initHandle(ARGS&&... args) noexcept {
|
||||
auto handle = mHandleAllocatorImpl.allocateAndConstruct<D>(std::forward<ARGS>(args)...);
|
||||
auto obj = handle_cast<D*>(handle);
|
||||
obj->initResource(handle.getId());
|
||||
TRACK_INCREMENT();
|
||||
return handle;
|
||||
}
|
||||
|
||||
template<typename D>
|
||||
inline Handle<D> allocHandle() noexcept {
|
||||
return mHandleAllocatorImpl.allocate<D>();
|
||||
}
|
||||
|
||||
template<typename D, typename B, typename... ARGS>
|
||||
inline typename std::enable_if<std::is_base_of<B, D>::value, D>::type* construct(
|
||||
Handle<B> const& handle, ARGS&&... args) noexcept {
|
||||
auto obj = mHandleAllocatorImpl.construct<D, B>(handle, std::forward<ARGS>(args)...);
|
||||
obj->initResource(handle.getId());
|
||||
TRACK_INCREMENT();
|
||||
return obj;
|
||||
}
|
||||
|
||||
template<typename Dp, typename B>
|
||||
inline typename std::enable_if_t<
|
||||
std::is_pointer_v<Dp> && std::is_base_of_v<B, typename std::remove_pointer_t<Dp>>, Dp>
|
||||
handle_cast(Handle<B>& handle) noexcept {
|
||||
return mHandleAllocatorImpl.handle_cast<Dp, B>(handle);
|
||||
}
|
||||
|
||||
template<typename Dp, typename B>
|
||||
inline typename std::enable_if_t<
|
||||
std::is_pointer_v<Dp> && std::is_base_of_v<B, typename std::remove_pointer_t<Dp>>, Dp>
|
||||
handle_cast(Handle<B> const& handle) noexcept {
|
||||
return mHandleAllocatorImpl.handle_cast<Dp, B>(handle);
|
||||
}
|
||||
|
||||
template<typename D, typename B>
|
||||
inline void destruct(Handle<B> handle) noexcept {
|
||||
auto obj = handle_cast<D*>(handle);
|
||||
if constexpr (std::is_base_of_v<VulkanIndexBuffer, D>
|
||||
|| std::is_base_of_v<VulkanBufferObject, D>) {
|
||||
obj->terminate();
|
||||
}
|
||||
TRACK_DECREMENT();
|
||||
mHandleAllocatorImpl.deallocate(handle, obj);
|
||||
}
|
||||
|
||||
private:
|
||||
HandleAllocatorVK mHandleAllocatorImpl;
|
||||
|
||||
#if DEBUG_RESOURCE_LEAKS
|
||||
public:
|
||||
void print() {
|
||||
utils::slog.d << "Resource Allocator state (debug only)" << utils::io::endl;
|
||||
for (size_t i = 0; i < RESOURCE_TYPE_COUNT; i++) {
|
||||
utils::slog.d << "[" << i << "]=" << mDebugOnlyResourceCount[i] << utils::io::endl;
|
||||
}
|
||||
utils::slog.d << "+++++++++++++++++++++++++++++++++++++" << utils::io::endl;
|
||||
}
|
||||
private:
|
||||
utils::FixedCapacityVector<size_t> mDebugOnlyResourceCount;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
#undef TRACK_INCREMENT
|
||||
#undef TRACK_DECREMENT
|
||||
#undef DEBUG_RESOURCE_LEAKS
|
||||
|
||||
} // namespace filament::backend
|
||||
|
||||
#endif // TNT_FILAMENT_BACKEND_VULKANRESOURCEALLOCATOR_H
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2023 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
#include "VulkanResources.h"
|
||||
#include "VulkanHandles.h"
|
||||
#include "VulkanResourceAllocator.h"
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
void deallocateResource(VulkanResourceAllocator* allocator, VulkanResourceType type,
|
||||
HandleBase::HandleId id) {
|
||||
|
||||
if (IS_HEAP_ALLOC_TYPE(type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case VulkanResourceType::BUFFER_OBJECT:
|
||||
allocator->destruct<VulkanBufferObject>(Handle<HwBufferObject>(id));
|
||||
break;
|
||||
case VulkanResourceType::INDEX_BUFFER:
|
||||
allocator->destruct<VulkanIndexBuffer>(Handle<HwIndexBuffer>(id));
|
||||
break;
|
||||
case VulkanResourceType::PROGRAM:
|
||||
allocator->destruct<VulkanProgram>(Handle<HwProgram>(id));
|
||||
break;
|
||||
case VulkanResourceType::RENDER_TARGET:
|
||||
allocator->destruct<VulkanRenderTarget>(Handle<HwRenderTarget>(id));
|
||||
break;
|
||||
case VulkanResourceType::SAMPLER_GROUP:
|
||||
allocator->destruct<VulkanSamplerGroup>(Handle<HwSamplerGroup>(id));
|
||||
break;
|
||||
case VulkanResourceType::SWAP_CHAIN:
|
||||
allocator->destruct<VulkanSwapChain>(Handle<HwSwapChain>(id));
|
||||
break;
|
||||
case VulkanResourceType::TEXTURE:
|
||||
allocator->destruct<VulkanTexture>(Handle<HwTexture>(id));
|
||||
break;
|
||||
case VulkanResourceType::TIMER_QUERY:
|
||||
allocator->destruct<VulkanTimerQuery>(Handle<HwTimerQuery>(id));
|
||||
break;
|
||||
case VulkanResourceType::VERTEX_BUFFER:
|
||||
allocator->destruct<VulkanVertexBuffer>(Handle<HwVertexBuffer>(id));
|
||||
break;
|
||||
case VulkanResourceType::RENDER_PRIMITIVE:
|
||||
allocator->destruct<VulkanRenderPrimitive>(Handle<VulkanRenderPrimitive>(id));
|
||||
break;
|
||||
// If the resource is heap allocated, then the resource manager just skip refcounted
|
||||
// destruction.
|
||||
case VulkanResourceType::FENCE:
|
||||
case VulkanResourceType::HEAP_ALLOCATED:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace filament::backend
|
||||
@@ -0,0 +1,337 @@
|
||||
/*
|
||||
* Copyright (C) 2023 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TNT_FILAMENT_BACKEND_VULKANRESOURCES_H
|
||||
#define TNT_FILAMENT_BACKEND_VULKANRESOURCES_H
|
||||
|
||||
#include <backend/DriverEnums.h>// For MAX_VERTEX_BUFFER_COUNT
|
||||
#include <backend/Handle.h>
|
||||
|
||||
#include <tsl/robin_set.h>
|
||||
#include <utils/Mutex.h>
|
||||
#include <utils/Panic.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
class VulkanResourceAllocator;
|
||||
struct VulkanThreadSafeResource;
|
||||
|
||||
// Subclasses of VulkanResource must provide this enum in their construction.
|
||||
enum class VulkanResourceType : uint8_t {
|
||||
BUFFER_OBJECT,
|
||||
INDEX_BUFFER,
|
||||
PROGRAM,
|
||||
RENDER_TARGET,
|
||||
SAMPLER_GROUP,
|
||||
SWAP_CHAIN,
|
||||
RENDER_PRIMITIVE,
|
||||
TEXTURE,
|
||||
TIMER_QUERY,
|
||||
VERTEX_BUFFER,
|
||||
|
||||
// Below are resources that are managed manually (i.e. not ref counted).
|
||||
FENCE,
|
||||
HEAP_ALLOCATED,
|
||||
};
|
||||
|
||||
#define IS_HEAP_ALLOC_TYPE(f) \
|
||||
(f == VulkanResourceType::FENCE || f == VulkanResourceType::HEAP_ALLOCATED)
|
||||
|
||||
|
||||
// This is a ref-counting base class that tracks how many references of this resource exist. This
|
||||
// class is paired with VulkanResourceManagerImpl which is responsible for incrementing or
|
||||
// decrementing the count. Once mRefCount == 0, VulkanResourceManagerImpl will also call the
|
||||
// appropriate destructor. VulkanCommandBuffer, VulkanDriver, and composite structure like
|
||||
// VulkanRenderPrimitive are owners of VulkanResourceManagerImpl instances.
|
||||
struct VulkanResourceBase {
|
||||
protected:
|
||||
explicit VulkanResourceBase(VulkanResourceType type)
|
||||
: mRefCount(IS_HEAP_ALLOC_TYPE(type) ? 1 : 0),
|
||||
mType(type),
|
||||
mHandleId(0) {}
|
||||
|
||||
private:
|
||||
inline VulkanResourceType getType() {
|
||||
return mType;
|
||||
}
|
||||
|
||||
inline HandleBase::HandleId getId() {
|
||||
return mHandleId;
|
||||
}
|
||||
|
||||
inline void initResource(HandleBase::HandleId id) noexcept {
|
||||
mHandleId = id;
|
||||
}
|
||||
|
||||
inline void ref() noexcept {
|
||||
if (IS_HEAP_ALLOC_TYPE(mType)) {
|
||||
return;
|
||||
}
|
||||
++mRefCount;
|
||||
}
|
||||
|
||||
inline void deref() noexcept {
|
||||
if (IS_HEAP_ALLOC_TYPE(mType)) {
|
||||
return;
|
||||
}
|
||||
--mRefCount;
|
||||
}
|
||||
|
||||
inline size_t refcount() noexcept {
|
||||
return mRefCount;
|
||||
}
|
||||
|
||||
size_t mRefCount = 0;
|
||||
VulkanResourceType mType = VulkanResourceType::BUFFER_OBJECT;
|
||||
HandleBase::HandleId mHandleId;
|
||||
|
||||
friend struct VulkanThreadSafeResource;
|
||||
friend class VulkanResourceAllocator;
|
||||
|
||||
template<typename RT, typename ST>
|
||||
friend class VulkanResourceManagerImpl;
|
||||
};
|
||||
|
||||
struct VulkanThreadSafeResource {
|
||||
protected:
|
||||
explicit VulkanThreadSafeResource(VulkanResourceType type)
|
||||
: mImpl(type) {}
|
||||
|
||||
private:
|
||||
inline VulkanResourceType getType() {
|
||||
return mImpl.getType();
|
||||
}
|
||||
|
||||
inline HandleBase::HandleId getId() {
|
||||
return mImpl.getId();
|
||||
}
|
||||
|
||||
inline void initResource(HandleBase::HandleId id) noexcept {
|
||||
std::unique_lock<utils::Mutex> lock(mMutex);
|
||||
mImpl.initResource(id);
|
||||
}
|
||||
|
||||
inline void ref() noexcept {
|
||||
std::unique_lock<utils::Mutex> lock(mMutex);
|
||||
mImpl.ref();
|
||||
}
|
||||
|
||||
inline void deref() noexcept {
|
||||
std::unique_lock<utils::Mutex> lock(mMutex);
|
||||
mImpl.deref();
|
||||
}
|
||||
|
||||
inline size_t refcount() noexcept {
|
||||
std::unique_lock<utils::Mutex> lock(mMutex);
|
||||
return mImpl.refcount();
|
||||
}
|
||||
|
||||
utils::Mutex mMutex;
|
||||
VulkanResourceBase mImpl;
|
||||
|
||||
friend class VulkanResourceAllocator;
|
||||
template<typename RT, typename ST>
|
||||
friend class VulkanResourceManagerImpl;
|
||||
};
|
||||
|
||||
using VulkanResource = VulkanResourceBase;
|
||||
|
||||
namespace {
|
||||
|
||||
// When the size of the resource set is known to be small, (for example for VulkanRenderPrimitive),
|
||||
// we just use a std::array to back the set.
|
||||
class FixedCapacityResourceSet {
|
||||
private:
|
||||
constexpr static size_t const SIZE = MAX_VERTEX_BUFFER_COUNT;
|
||||
using FixedSizeArray = std::array<VulkanResource*, SIZE>;
|
||||
|
||||
public:
|
||||
using const_iterator = FixedSizeArray::const_iterator;
|
||||
|
||||
inline const_iterator begin() {
|
||||
if (mInd == 0) {
|
||||
return mArray.cend();
|
||||
}
|
||||
return mArray.cbegin();
|
||||
}
|
||||
|
||||
inline const_iterator end() {
|
||||
if (mInd == 0) {
|
||||
return mArray.cend();
|
||||
}
|
||||
if (mInd < SIZE) {
|
||||
return mArray.begin() + mInd;
|
||||
}
|
||||
return mArray.cend();
|
||||
}
|
||||
|
||||
inline const_iterator find(VulkanResource* resource) {
|
||||
return std::find(mArray.begin(), mArray.end(), resource);
|
||||
}
|
||||
|
||||
inline void insert(VulkanResource* resource) {
|
||||
assert_invariant(mInd < SIZE);
|
||||
mArray[mInd++] = resource;
|
||||
}
|
||||
|
||||
inline void erase(VulkanResource* resource) {
|
||||
assert_invariant(false && "FixedCapacityResourceSet::erase should not be called");
|
||||
}
|
||||
|
||||
inline void clear() {
|
||||
if (mInd == 0) {
|
||||
return;
|
||||
}
|
||||
mInd = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
FixedSizeArray mArray{nullptr};
|
||||
size_t mInd = 0;
|
||||
};
|
||||
|
||||
// robin_set/map are useful for sets that are acquire only and the set will be iterated when the set
|
||||
// is cleared.
|
||||
using FastIterationResourceSet = tsl::robin_set<VulkanResource*>;
|
||||
|
||||
// unoredered_set is used in the general case where insert/erase can occur at will. This is useful
|
||||
// for the basic object ownership count - i.e. VulkanDriver.
|
||||
using ResourceSet = std::unordered_set<VulkanResource*>;
|
||||
|
||||
using ThreadSafeResourceSet = std::unordered_set<VulkanThreadSafeResource*>;
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
class VulkanResourceAllocator;
|
||||
|
||||
#define LOCK_IF_NEEDED() \
|
||||
if constexpr (std::is_base_of_v<VulkanThreadSafeResource, ResourceType>) { \
|
||||
mMutex->lock(); \
|
||||
}
|
||||
|
||||
#define UNLOCK_IF_NEEDED() \
|
||||
if constexpr (std::is_base_of_v<VulkanThreadSafeResource, ResourceType>) { \
|
||||
mMutex->unlock(); \
|
||||
}
|
||||
|
||||
void deallocateResource(VulkanResourceAllocator* allocator, VulkanResourceType type,
|
||||
HandleBase::HandleId id);
|
||||
|
||||
template<typename ResourceType, typename SetType>
|
||||
class VulkanResourceManagerImpl {
|
||||
public:
|
||||
explicit VulkanResourceManagerImpl(VulkanResourceAllocator* allocator)
|
||||
: mAllocator(allocator) {
|
||||
if constexpr (std::is_base_of_v<VulkanThreadSafeResource, ResourceType>) {
|
||||
mMutex = std::make_unique<utils::Mutex>();
|
||||
}
|
||||
}
|
||||
|
||||
VulkanResourceManagerImpl(const VulkanResourceManagerImpl& other) = delete;
|
||||
void operator=(const VulkanResourceManagerImpl& other) = delete;
|
||||
VulkanResourceManagerImpl(const VulkanResourceManagerImpl&& other) = delete;
|
||||
void operator=(const VulkanResourceManagerImpl&& other) = delete;
|
||||
|
||||
~VulkanResourceManagerImpl() {
|
||||
clear();
|
||||
}
|
||||
|
||||
inline void acquire(ResourceType* resource) {
|
||||
if (IS_HEAP_ALLOC_TYPE(resource->getType())) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOCK_IF_NEEDED();
|
||||
if (mResources.find(resource) != mResources.end()) {
|
||||
UNLOCK_IF_NEEDED();
|
||||
return;
|
||||
}
|
||||
mResources.insert(resource);
|
||||
UNLOCK_IF_NEEDED();
|
||||
resource->ref();
|
||||
}
|
||||
|
||||
// Transfers ownership from one resource set to another
|
||||
inline void acquire(VulkanResourceManagerImpl<ResourceType, SetType>* srcResources) {
|
||||
LOCK_IF_NEEDED();
|
||||
for (auto iter = srcResources->mResources.begin(); iter != srcResources->mResources.end();
|
||||
iter++) {
|
||||
acquire(*iter);
|
||||
}
|
||||
UNLOCK_IF_NEEDED();
|
||||
srcResources->clear();
|
||||
}
|
||||
|
||||
inline void release(ResourceType* resource) {
|
||||
if (IS_HEAP_ALLOC_TYPE(resource->getType())) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOCK_IF_NEEDED();
|
||||
auto resItr = mResources.find(resource);
|
||||
if (resItr == mResources.end()) {
|
||||
UNLOCK_IF_NEEDED();
|
||||
return;
|
||||
}
|
||||
mResources.erase(resItr);
|
||||
UNLOCK_IF_NEEDED();
|
||||
derefImpl(resource);
|
||||
}
|
||||
|
||||
inline void clear() {
|
||||
LOCK_IF_NEEDED();
|
||||
for (auto iter = mResources.begin(); iter != mResources.end(); iter++) {
|
||||
derefImpl(*iter);
|
||||
}
|
||||
mResources.clear();
|
||||
UNLOCK_IF_NEEDED();
|
||||
}
|
||||
|
||||
inline size_t size() {
|
||||
return mResources.size();
|
||||
}
|
||||
|
||||
private:
|
||||
inline void derefImpl(ResourceType* resource) {
|
||||
resource->deref();
|
||||
if (resource->refcount() != 0) {
|
||||
return;
|
||||
}
|
||||
deallocateResource(mAllocator, resource->getType(), resource->getId());
|
||||
}
|
||||
|
||||
VulkanResourceAllocator* mAllocator;
|
||||
SetType mResources;
|
||||
std::unique_ptr<utils::Mutex> mMutex;
|
||||
};
|
||||
|
||||
using VulkanAcquireOnlyResourceManager
|
||||
= VulkanResourceManagerImpl<VulkanResource, FastIterationResourceSet>;
|
||||
using VulkanResourceManager = VulkanResourceManagerImpl<VulkanResource, ResourceSet>;
|
||||
using FixedSizeVulkanResourceManager
|
||||
= VulkanResourceManagerImpl<VulkanResource, FixedCapacityResourceSet>;
|
||||
using VulkanThreadSafeResourceManager
|
||||
= VulkanResourceManagerImpl<VulkanThreadSafeResource, ThreadSafeResourceSet>;
|
||||
|
||||
#undef LOCK_IF_NEEDED
|
||||
#undef UNLOCK_IF_NEEDED
|
||||
|
||||
} // namespace filament::backend
|
||||
|
||||
#endif // TNT_FILAMENT_BACKEND_VULKANRESOURCES_H
|
||||
@@ -87,8 +87,7 @@ constexpr inline float getMaxLod(SamplerMinFilter filter) noexcept {
|
||||
case SamplerMinFilter::LINEAR_MIPMAP_NEAREST:
|
||||
case SamplerMinFilter::NEAREST_MIPMAP_LINEAR:
|
||||
case SamplerMinFilter::LINEAR_MIPMAP_LINEAR:
|
||||
// Assuming our maximum texture size is 4k, we'll never need more than 12 miplevels.
|
||||
return 12.0f;
|
||||
return VK_LOD_CLAMP_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +98,7 @@ constexpr inline VkBool32 getCompareEnable(SamplerCompareMode mode) noexcept {
|
||||
void VulkanSamplerCache::initialize(VkDevice device) { mDevice = device; }
|
||||
|
||||
VkSampler VulkanSamplerCache::getSampler(SamplerParams params) noexcept {
|
||||
auto iter = mCache.find(params.u);
|
||||
auto iter = mCache.find(params);
|
||||
if (UTILS_LIKELY(iter != mCache.end())) {
|
||||
return iter->second;
|
||||
}
|
||||
@@ -123,7 +122,7 @@ VkSampler VulkanSamplerCache::getSampler(SamplerParams params) noexcept {
|
||||
VkSampler sampler;
|
||||
VkResult error = vkCreateSampler(mDevice, &samplerInfo, VKALLOC, &sampler);
|
||||
ASSERT_POSTCONDITION(!error, "Unable to create sampler.");
|
||||
mCache.insert({params.u, sampler});
|
||||
mCache.insert({params, sampler});
|
||||
return sampler;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ public:
|
||||
void terminate() noexcept;
|
||||
private:
|
||||
VkDevice mDevice;
|
||||
tsl::robin_map<uint32_t, VkSampler> mCache;
|
||||
tsl::robin_map<SamplerParams, VkSampler, SamplerParams::Hasher, SamplerParams::EqualTo> mCache;
|
||||
};
|
||||
|
||||
} // namespace filament::backend
|
||||
|
||||
@@ -28,7 +28,8 @@ namespace filament::backend {
|
||||
VulkanSwapChain::VulkanSwapChain(VulkanPlatform* platform, VulkanContext const& context,
|
||||
VmaAllocator allocator, VulkanCommands* commands, VulkanStagePool& stagePool,
|
||||
void* nativeWindow, uint64_t flags, VkExtent2D extent)
|
||||
: mPlatform(platform),
|
||||
: VulkanResource(VulkanResourceType::SWAP_CHAIN),
|
||||
mPlatform(platform),
|
||||
mCommands(commands),
|
||||
mAllocator(allocator),
|
||||
mStagePool(stagePool),
|
||||
@@ -67,11 +68,11 @@ void VulkanSwapChain::update() {
|
||||
for (auto const color: bundle.colors) {
|
||||
mColors.push_back(std::make_unique<VulkanTexture>(device, mAllocator, mCommands, color,
|
||||
bundle.colorFormat, 1, bundle.extent.width, bundle.extent.height,
|
||||
TextureUsage::COLOR_ATTACHMENT, mStagePool));
|
||||
TextureUsage::COLOR_ATTACHMENT, mStagePool, true /* heap allocated */));
|
||||
}
|
||||
mDepth = std::make_unique<VulkanTexture>(device, mAllocator, mCommands, bundle.depth,
|
||||
bundle.depthFormat, 1, bundle.extent.width, bundle.extent.height,
|
||||
TextureUsage::DEPTH_ATTACHMENT, mStagePool);
|
||||
TextureUsage::DEPTH_ATTACHMENT, mStagePool, true /* heap allocated */);
|
||||
|
||||
mExtent = bundle.extent;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "DriverBase.h"
|
||||
|
||||
#include "VulkanContext.h"
|
||||
#include "VulkanResources.h"
|
||||
|
||||
#include <backend/platforms/VulkanPlatform.h>
|
||||
|
||||
@@ -36,7 +37,7 @@ struct VulkanHeadlessSwapChain;
|
||||
struct VulkanSurfaceSwapChain;
|
||||
|
||||
// A wrapper around the platform implementation of swapchain.
|
||||
struct VulkanSwapChain : public HwSwapChain {
|
||||
struct VulkanSwapChain : public HwSwapChain, VulkanResource {
|
||||
VulkanSwapChain(VulkanPlatform* platform, VulkanContext const& context, VmaAllocator allocator,
|
||||
VulkanCommands* commands, VulkanStagePool& stagePool,
|
||||
void* nativeWindow, uint64_t flags, VkExtent2D extent = {0, 0});
|
||||
|
||||
@@ -29,12 +29,16 @@ using namespace bluevk;
|
||||
namespace filament::backend {
|
||||
|
||||
using ImgUtil = VulkanImageUtility;
|
||||
VulkanTexture::VulkanTexture(VkDevice device, VmaAllocator allocator,
|
||||
VulkanCommands* commands, VkImage image, VkFormat format, uint8_t samples,
|
||||
uint32_t width, uint32_t height, TextureUsage tusage, VulkanStagePool& stagePool)
|
||||
VulkanTexture::VulkanTexture(VkDevice device, VmaAllocator allocator, VulkanCommands* commands,
|
||||
VkImage image, VkFormat format, uint8_t samples, uint32_t width, uint32_t height,
|
||||
TextureUsage tusage, VulkanStagePool& stagePool, bool heapAllocated)
|
||||
: HwTexture(SamplerType::SAMPLER_2D, 1, samples, width, height, 1, TextureFormat::UNUSED,
|
||||
tusage),
|
||||
mVkFormat(format), mViewType(ImgUtil::getViewType(target)), mSwizzle({}),
|
||||
tusage),
|
||||
VulkanResource(
|
||||
heapAllocated ? VulkanResourceType::HEAP_ALLOCATED : VulkanResourceType::TEXTURE),
|
||||
mVkFormat(format),
|
||||
mViewType(ImgUtil::getViewType(target)),
|
||||
mSwizzle({}),
|
||||
mTextureImage(image),
|
||||
mPrimaryViewRange{
|
||||
.aspectMask = getImageAspect(),
|
||||
@@ -43,19 +47,28 @@ VulkanTexture::VulkanTexture(VkDevice device, VmaAllocator allocator,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
mStagePool(stagePool), mDevice(device), mAllocator(allocator), mCommands(commands) {}
|
||||
mStagePool(stagePool),
|
||||
mDevice(device),
|
||||
mAllocator(allocator),
|
||||
mCommands(commands) {}
|
||||
|
||||
VulkanTexture::VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
||||
VulkanContext const& context, VmaAllocator allocator,
|
||||
VulkanCommands* commands, SamplerType target, uint8_t levels,
|
||||
TextureFormat tformat, uint8_t samples, uint32_t w, uint32_t h, uint32_t depth,
|
||||
TextureUsage tusage, VulkanStagePool& stagePool, VkComponentMapping swizzle)
|
||||
VulkanContext const& context, VmaAllocator allocator, VulkanCommands* commands,
|
||||
SamplerType target, uint8_t levels, TextureFormat tformat, uint8_t samples, uint32_t w,
|
||||
uint32_t h, uint32_t depth, TextureUsage tusage, VulkanStagePool& stagePool,
|
||||
bool heapAllocated, VkComponentMapping swizzle)
|
||||
: HwTexture(target, levels, samples, w, h, depth, tformat, tusage),
|
||||
VulkanResource(
|
||||
heapAllocated ? VulkanResourceType::HEAP_ALLOCATED : VulkanResourceType::TEXTURE),
|
||||
// Vulkan does not support 24-bit depth, use the official fallback format.
|
||||
mVkFormat(tformat == TextureFormat::DEPTH24 ? context.getDepthFormat()
|
||||
: backend::getVkFormat(tformat)),
|
||||
mViewType(ImgUtil::getViewType(target)), mSwizzle(swizzle), mStagePool(stagePool),
|
||||
mDevice(device), mAllocator(allocator), mCommands(commands) {
|
||||
mViewType(ImgUtil::getViewType(target)),
|
||||
mSwizzle(swizzle),
|
||||
mStagePool(stagePool),
|
||||
mDevice(device),
|
||||
mAllocator(allocator),
|
||||
mCommands(commands) {
|
||||
|
||||
// Create an appropriately-sized device-only VkImage, but do not fill it yet.
|
||||
VkImageCreateInfo imageInfo{.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
||||
@@ -154,7 +167,7 @@ VulkanTexture::VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
||||
<< "handle = " << utils::io::hex << mTextureImage << utils::io::dec << ", "
|
||||
<< "extent = " << w << "x" << h << "x"<< depth << ", "
|
||||
<< "mipLevels = " << int(levels) << ", "
|
||||
<< "TextureUsage = " << static_cast<int>(usage) << ", "
|
||||
<< "TextureUsage = " << static_cast<int>(usage) << ", "
|
||||
<< "usage = " << imageInfo.usage << ", "
|
||||
<< "samples = " << imageInfo.samples << ", "
|
||||
<< "type = " << imageInfo.imageType << ", "
|
||||
@@ -212,7 +225,11 @@ VulkanTexture::VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
||||
| VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
|
||||
const uint32_t layers = mPrimaryViewRange.layerCount;
|
||||
VkImageSubresourceRange range = { getImageAspect(), 0, levels, 0, layers };
|
||||
VkCommandBuffer cmdbuf = mCommands->get().cmdbuffer;
|
||||
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
VkCommandBuffer const cmdbuf = commands.cmdbuffer;
|
||||
commands.acquire(this);
|
||||
|
||||
transitionLayout(cmdbuf, range, ImgUtil::getDefaultLayout(imageInfo.usage));
|
||||
}
|
||||
}
|
||||
@@ -262,7 +279,9 @@ void VulkanTexture::updateImage(const PixelBufferDescriptor& data, uint32_t widt
|
||||
vmaUnmapMemory(mAllocator, stage->memory);
|
||||
vmaFlushAllocation(mAllocator, stage->memory, 0, hostData->size);
|
||||
|
||||
const VkCommandBuffer cmdbuf = mCommands->get(true).cmdbuffer;
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
VkCommandBuffer const cmdbuf = commands.cmdbuffer;
|
||||
commands.acquire(this);
|
||||
|
||||
VkBufferImageCopy copyRegion = {
|
||||
.bufferOffset = {},
|
||||
@@ -323,7 +342,9 @@ void VulkanTexture::updateImageWithBlit(const PixelBufferDescriptor& hostData, u
|
||||
vmaUnmapMemory(mAllocator, stage->memory);
|
||||
vmaFlushAllocation(mAllocator, stage->memory, 0, hostData.size);
|
||||
|
||||
const VkCommandBuffer cmdbuf = mCommands->get().cmdbuffer;
|
||||
VulkanCommandBuffer& commands = mCommands->get();
|
||||
VkCommandBuffer const cmdbuf = commands.cmdbuffer;
|
||||
commands.acquire(this);
|
||||
|
||||
// TODO: support blit-based format conversion for 3D images and cubemaps.
|
||||
const int layer = 0;
|
||||
|
||||
@@ -20,24 +20,26 @@
|
||||
#include "DriverBase.h"
|
||||
|
||||
#include "VulkanBuffer.h"
|
||||
#include "VulkanResources.h"
|
||||
#include "VulkanImageUtility.h"
|
||||
|
||||
#include <utils/RangeMap.h>
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
struct VulkanTexture : public HwTexture {
|
||||
struct VulkanTexture : public HwTexture, VulkanResource {
|
||||
// Standard constructor for user-facing textures.
|
||||
VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice, VulkanContext const& context,
|
||||
VmaAllocator allocator, VulkanCommands* commands, SamplerType target, uint8_t levels,
|
||||
TextureFormat tformat, uint8_t samples, uint32_t w, uint32_t h, uint32_t depth,
|
||||
TextureUsage tusage, VulkanStagePool& stagePool, VkComponentMapping swizzle = {});
|
||||
TextureUsage tusage, VulkanStagePool& stagePool, bool heapAllocated = false,
|
||||
VkComponentMapping swizzle = {});
|
||||
|
||||
// Specialized constructor for internally created textures (e.g. from a swap chain)
|
||||
// The texture will never destroy the given VkImage, but it does manages its subresources.
|
||||
VulkanTexture(VkDevice device, VmaAllocator allocator, VulkanCommands* commands, VkImage image,
|
||||
VkFormat format, uint8_t samples, uint32_t width, uint32_t height, TextureUsage tusage,
|
||||
VulkanStagePool& stagePool);
|
||||
VulkanStagePool& stagePool, bool heapAllocated = false);
|
||||
|
||||
~VulkanTexture();
|
||||
|
||||
|
||||
@@ -133,9 +133,9 @@ struct BloomOptions {
|
||||
Texture* dirt = nullptr; //!< user provided dirt texture %codegen_skip_json% %codegen_skip_javascript%
|
||||
float dirtStrength = 0.2f; //!< strength of the dirt texture %codegen_skip_json% %codegen_skip_javascript%
|
||||
float strength = 0.10f; //!< bloom's strength between 0.0 and 1.0
|
||||
uint32_t resolution = 360; //!< resolution of vertical axis (2^levels to 2048)
|
||||
uint32_t resolution = 384; //!< resolution of vertical axis (2^levels to 2048)
|
||||
float anamorphism = 1.0f; //!< bloom x/y aspect-ratio (1/32 to 32)
|
||||
uint8_t levels = 6; //!< number of blur levels (3 to 11)
|
||||
uint8_t levels = 6; //!< number of blur levels (1 to 11)
|
||||
BlendMode blendMode = BlendMode::ADD; //!< how the bloom effect is applied
|
||||
bool threshold = true; //!< whether to threshold the source
|
||||
bool enabled = false; //!< enable or disable bloom
|
||||
|
||||
@@ -18,10 +18,13 @@
|
||||
#define TNT_FILAMENT_SWAPCHAIN_H
|
||||
|
||||
#include <filament/FilamentAPI.h>
|
||||
|
||||
#include <backend/CallbackHandler.h>
|
||||
#include <backend/DriverEnums.h>
|
||||
#include <backend/PresentCallable.h>
|
||||
|
||||
#include <utils/compiler.h>
|
||||
#include <utils/Invocable.h>
|
||||
|
||||
namespace filament {
|
||||
|
||||
@@ -148,7 +151,7 @@ class Engine;
|
||||
class UTILS_PUBLIC SwapChain : public FilamentAPI {
|
||||
public:
|
||||
using FrameScheduledCallback = backend::FrameScheduledCallback;
|
||||
using FrameCompletedCallback = backend::FrameCompletedCallback;
|
||||
using FrameCompletedCallback = utils::Invocable<void(SwapChain*)>;
|
||||
|
||||
/**
|
||||
* Requests a SwapChain with an alpha channel.
|
||||
@@ -241,17 +244,23 @@ public:
|
||||
* contents have completed rendering on the GPU.
|
||||
*
|
||||
* Use SwapChain::setFrameCompletedCallback to set a callback on an individual SwapChain. Each
|
||||
* time a frame completes GPU rendering, the callback will be called with optional user data.
|
||||
* time a frame completes GPU rendering, the callback will be called.
|
||||
*
|
||||
* The FrameCompletedCallback is guaranteed to be called on the main Filament thread.
|
||||
* If handler is nullptr, the callback is guaranteed to be called on the main Filament thread.
|
||||
*
|
||||
* @param callback A callback, or nullptr to unset.
|
||||
* @param user An optional pointer to user data passed to the callback function.
|
||||
* Use \c setFrameCompletedCallback() (with default arguments) to unset the callback.
|
||||
*
|
||||
* @param handler Handler to dispatch the callback or nullptr for the default handler.
|
||||
* @param callback Callback called when each frame completes.
|
||||
*
|
||||
* @remark Only Filament's Metal backend supports frame callbacks. Other backends ignore the
|
||||
* callback (which will never be called) and proceed normally.
|
||||
*
|
||||
* @see CallbackHandler
|
||||
*/
|
||||
void setFrameCompletedCallback(FrameCompletedCallback callback, void* user = nullptr);
|
||||
void setFrameCompletedCallback(backend::CallbackHandler* handler = nullptr,
|
||||
FrameCompletedCallback&& callback = {}) noexcept;
|
||||
|
||||
};
|
||||
|
||||
} // namespace filament
|
||||
|
||||
@@ -52,6 +52,8 @@ public:
|
||||
*/
|
||||
TextureSampler() noexcept = default;
|
||||
|
||||
explicit TextureSampler(backend::SamplerParams params) noexcept : mSamplerParams(params) { }
|
||||
|
||||
TextureSampler(const TextureSampler& rhs) noexcept = default;
|
||||
TextureSampler& operator=(const TextureSampler& rhs) noexcept = default;
|
||||
|
||||
|
||||
@@ -2132,7 +2132,7 @@ FrameGraphId<FrameGraphTexture> PostProcessManager::flarePass(FrameGraph& fg,
|
||||
.filterMin = SamplerMinFilter::LINEAR_MIPMAP_NEAREST
|
||||
});
|
||||
|
||||
mi->setParameter("level", 1.0f); // adjust with resolution
|
||||
mi->setParameter("level", 0.0f); // adjust with resolution
|
||||
mi->setParameter("aspectRatio", float2{ aspectRatio, 1.0f / aspectRatio });
|
||||
mi->setParameter("threshold",
|
||||
float2{ bloomOptions.ghostThreshold, bloomOptions.haloThreshold });
|
||||
|
||||
@@ -28,8 +28,9 @@ void SwapChain::setFrameScheduledCallback(FrameScheduledCallback callback, void*
|
||||
return downcast(this)->setFrameScheduledCallback(callback, user);
|
||||
}
|
||||
|
||||
void SwapChain::setFrameCompletedCallback(FrameCompletedCallback callback, void* user) {
|
||||
return downcast(this)->setFrameCompletedCallback(callback, user);
|
||||
void SwapChain::setFrameCompletedCallback(backend::CallbackHandler* handler,
|
||||
utils::Invocable<void(SwapChain*)>&& callback) noexcept {
|
||||
return downcast(this)->setFrameCompletedCallback(handler, std::move(callback));
|
||||
}
|
||||
|
||||
bool SwapChain::isSRGBSwapChainSupported(Engine& engine) noexcept {
|
||||
|
||||
@@ -38,8 +38,24 @@ void FSwapChain::setFrameScheduledCallback(FrameScheduledCallback callback, void
|
||||
mEngine.getDriverApi().setFrameScheduledCallback(mSwapChain, callback, user);
|
||||
}
|
||||
|
||||
void FSwapChain::setFrameCompletedCallback(FrameCompletedCallback callback, void* user) {
|
||||
mEngine.getDriverApi().setFrameCompletedCallback(mSwapChain, callback, user);
|
||||
void FSwapChain::setFrameCompletedCallback(backend::CallbackHandler* handler,
|
||||
utils::Invocable<void(SwapChain*)>&& callback) noexcept {
|
||||
struct Callback {
|
||||
utils::Invocable<void(SwapChain*)> f;
|
||||
SwapChain* s;
|
||||
static void func(void* user) {
|
||||
auto* const c = reinterpret_cast<Callback*>(user);
|
||||
c->f(c->s);
|
||||
delete c;
|
||||
}
|
||||
};
|
||||
if (callback) {
|
||||
auto* const user = new(std::nothrow) Callback{ std::move(callback), this };
|
||||
mEngine.getDriverApi().setFrameCompletedCallback(
|
||||
mSwapChain, handler, &Callback::func, static_cast<void*>(user));
|
||||
} else {
|
||||
mEngine.getDriverApi().setFrameCompletedCallback(mSwapChain, nullptr, nullptr, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
bool FSwapChain::isSRGBSwapChainSupported(FEngine& engine) noexcept {
|
||||
|
||||
@@ -23,6 +23,9 @@
|
||||
|
||||
#include <filament/SwapChain.h>
|
||||
|
||||
#include <backend/CallbackHandler.h>
|
||||
|
||||
#include <utils/Invocable.h>
|
||||
#include <utils/compiler.h>
|
||||
|
||||
namespace filament {
|
||||
@@ -61,7 +64,8 @@ public:
|
||||
|
||||
void setFrameScheduledCallback(FrameScheduledCallback callback, void* user);
|
||||
|
||||
void setFrameCompletedCallback(FrameCompletedCallback callback, void* user);
|
||||
void setFrameCompletedCallback(backend::CallbackHandler* handler,
|
||||
utils::Invocable<void(SwapChain*)>&& callback) noexcept;
|
||||
|
||||
static bool isSRGBSwapChainSupported(FEngine& engine) noexcept;
|
||||
|
||||
|
||||
@@ -1080,7 +1080,7 @@ void FView::setSoftShadowOptions(SoftShadowOptions options) noexcept {
|
||||
|
||||
void FView::setBloomOptions(BloomOptions options) noexcept {
|
||||
options.dirtStrength = math::saturate(options.dirtStrength);
|
||||
options.levels = math::clamp(options.levels, uint8_t(3), uint8_t(11));
|
||||
options.levels = math::clamp(options.levels, uint8_t(1), uint8_t(11));
|
||||
options.resolution = math::clamp(options.resolution, 1u << options.levels, 2048u);
|
||||
options.anamorphism = math::clamp(options.anamorphism, 1.0f/32.0f, 32.0f);
|
||||
options.highlight = std::max(10.0f, options.highlight);
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
// This substitute for the built-in "mix" function exists to work around #732,
|
||||
// seen with Vulkan on the Pixel 3 + Android P.
|
||||
vec4 lerp(vec4 x, vec4 y, float a) {
|
||||
vec4 lerp(const vec4 x, const vec4 y, float a) {
|
||||
return x * (1.0 - a) + y * a;
|
||||
}
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ fragment {
|
||||
#define PREVENT_FLICKERING 0 // FIXME: thin lines disapear
|
||||
|
||||
|
||||
float luma(vec3 color) {
|
||||
float luma(const vec3 color) {
|
||||
#if USE_YCoCg
|
||||
return color.x;
|
||||
#else
|
||||
@@ -103,14 +103,14 @@ float luma(vec3 color) {
|
||||
#endif
|
||||
}
|
||||
|
||||
vec3 RGB_YCoCg(vec3 c) {
|
||||
vec3 RGB_YCoCg(const vec3 c) {
|
||||
float Y = dot(c.rgb, vec3( 1, 2, 1) * 0.25);
|
||||
float Co = dot(c.rgb, vec3( 2, 0, -2) * 0.25);
|
||||
float Cg = dot(c.rgb, vec3(-1, 2, -1) * 0.25);
|
||||
return vec3(Y, Co, Cg);
|
||||
}
|
||||
|
||||
vec3 YCoCg_RGB(vec3 c) {
|
||||
vec3 YCoCg_RGB(const vec3 c) {
|
||||
float Y = c.x;
|
||||
float Co = c.y;
|
||||
float Cg = c.z;
|
||||
@@ -121,8 +121,8 @@ vec3 YCoCg_RGB(vec3 c) {
|
||||
}
|
||||
|
||||
// clip the (c, h) segment to a box
|
||||
vec4 clipToBox(int quality,
|
||||
vec3 boxmin, vec3 boxmax, vec4 c, vec4 h) {
|
||||
vec4 clipToBox(const int quality,
|
||||
const vec3 boxmin, const vec3 boxmax, const vec4 c, const vec4 h) {
|
||||
const float epsilon = 0.0001;
|
||||
|
||||
if (quality == BOX_CLIPPING_ACCURATE) {
|
||||
@@ -144,7 +144,7 @@ vec4 clipToBox(int quality,
|
||||
// http://vec3.ca/bicubic-filtering-in-fewer-taps/ for more details
|
||||
// Optimized to 5 taps by removing the corner samples
|
||||
// And modified for mediump support
|
||||
vec4 sampleTextureCatmullRom(sampler2D tex, highp vec2 uv, highp vec2 texSize) {
|
||||
vec4 sampleTextureCatmullRom(const sampler2D tex, const highp vec2 uv, const highp vec2 texSize) {
|
||||
// We're going to sample a a 4x4 grid of texels surrounding the target UV coordinate. We'll do this by rounding
|
||||
// down the sample location to get the exact center of our "starting" texel. The starting texel will be at
|
||||
// location [1, 1] in the grid, where [0, 0] is the top left corner.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
vec3 colorGrade(mediump sampler3D lut, vec3 x) {
|
||||
vec3 colorGrade(mediump sampler3D lut, const vec3 x) {
|
||||
// Alexa LogC EI 1000
|
||||
const float a = 5.555556;
|
||||
const float b = 0.047996;
|
||||
|
||||
@@ -89,7 +89,7 @@ fragment {
|
||||
|
||||
void dummy(){}
|
||||
|
||||
float starburst(vec2 uv) {
|
||||
float starburst(const vec2 uv) {
|
||||
// get an offset that continuously moves with the camera
|
||||
vec3 forward = getViewFromWorldMatrix()[2].xyz;
|
||||
float offset = forward.x + forward.y + forward.z;
|
||||
@@ -104,7 +104,7 @@ float starburst(vec2 uv) {
|
||||
return saturate(mask + (1.0 - smoothstep(0.0, 0.3, d)));
|
||||
}
|
||||
|
||||
vec3 bloom(highp vec2 uv, vec3 color) {
|
||||
vec3 bloom(highp vec2 uv, const vec3 color) {
|
||||
vec3 result = vec3(0.0);
|
||||
|
||||
if (materialParams.bloom.x > 0.0) {
|
||||
@@ -128,7 +128,7 @@ vec3 bloom(highp vec2 uv, vec3 color) {
|
||||
return result;
|
||||
}
|
||||
|
||||
vec4 resolveFragment(highp vec2 uv) {
|
||||
vec4 resolveFragment(const highp vec2 uv) {
|
||||
#if POST_PROCESS_OPAQUE
|
||||
return vec4(textureLod(materialParams_colorBuffer, uv, 0.0).rgb, 1.0);
|
||||
#else
|
||||
|
||||
@@ -108,16 +108,16 @@ float ringCountFast() {
|
||||
return materialParams.ringCounts[2];
|
||||
}
|
||||
|
||||
float sampleCount(float ringCount) {
|
||||
float sampleCount(const float ringCount) {
|
||||
float s = (ringCount * 2.0 - 1.0);
|
||||
return s * s;
|
||||
}
|
||||
|
||||
float computeNeededRings(float kernelSizeInPixel) {
|
||||
float computeNeededRings(const float kernelSizeInPixel) {
|
||||
return ceil(kernelSizeInPixel + 0.5);
|
||||
}
|
||||
|
||||
float getMipLevel(float ringCount, float kernelSizeInPixels) {
|
||||
float getMipLevel(const float ringCount, const float kernelSizeInPixels) {
|
||||
#if KERNEL_USE_MIPMAP
|
||||
// note: the 0.5 is to convert from highres to our downslampled texture
|
||||
float ringDistanceInTexels = kernelSizeInPixels * rcp(ringCount - 0.5);
|
||||
@@ -140,7 +140,7 @@ float getMipLevel(float ringCount, float kernelSizeInPixels) {
|
||||
#endif
|
||||
}
|
||||
|
||||
float sampleWeight(float coc, float mip) {
|
||||
float sampleWeight(const float coc, const float mip) {
|
||||
// The contribution of sample is inversely proportional to *its* area
|
||||
// (the larger area, the fainter it is).
|
||||
// In theory this factor should be 1 / pi * radius^2, however 1/pi is a constant, and
|
||||
@@ -159,7 +159,7 @@ float sampleWeight(float coc, float mip) {
|
||||
return (MAX_COC_RADIUS * MAX_COC_RADIUS) / (max(coc * coc, pixelRadiusSquared));
|
||||
}
|
||||
|
||||
float intersection(float border, float absCoc, float mip) {
|
||||
float intersection(const float border, const float absCoc, const float mip) {
|
||||
// there is very little visible difference, so use the cheaper version on mobile
|
||||
#if FILAMENT_QUALITY < FILAMENT_QUALITY_HIGH
|
||||
return saturate((absCoc - border) + 0.5);
|
||||
@@ -168,7 +168,7 @@ float intersection(float border, float absCoc, float mip) {
|
||||
#endif
|
||||
}
|
||||
|
||||
highp vec2 diaphragm(highp vec2 center, vec2 offset) {
|
||||
highp vec2 diaphragm(const highp vec2 center, const vec2 offset) {
|
||||
#if DOF_DIAPHRAGM == DOF_DIAPHRAGM_CIRCLE
|
||||
return center + offset;
|
||||
#elif DOF_DIAPHRAGM == DOF_DIAPHRAGM_STRAIGHT_BLADES
|
||||
@@ -217,7 +217,7 @@ void initBucket(out Bucket ring) {
|
||||
ring.coc = 0.0;
|
||||
}
|
||||
|
||||
void initRing(float i, float ringCount, float kernelSize, vec2 noise,
|
||||
void initRing(const float i, const float ringCount, const float kernelSize, const vec2 noise,
|
||||
out float offset, out float count, out mat2 r, out vec2 p) {
|
||||
float radius = (kernelSize / (ringCount - 0.5)) * i;
|
||||
|
||||
@@ -240,7 +240,7 @@ void initRing(float i, float ringCount, float kernelSize, vec2 noise,
|
||||
offset = length(p + noise);
|
||||
}
|
||||
|
||||
void mergeRings(inout Bucket curr, inout Bucket prev, float count) {
|
||||
void mergeRings(inout Bucket curr, inout Bucket prev, const float count) {
|
||||
if (curr.cw >= MEDIUMP_FLT_MIN) {
|
||||
// "Life of a Bokeh", SIGGRAPH 2018 -- slide 32
|
||||
// How much the current ring is occluding the previous ring -- we estimate this based
|
||||
@@ -265,8 +265,8 @@ void mergeRings(inout Bucket curr, inout Bucket prev, float count) {
|
||||
}
|
||||
}
|
||||
|
||||
void accumulate(inout Bucket curr, inout Bucket prev, Sample tap,
|
||||
float radius, float border, float mip, bool first) {
|
||||
void accumulate(inout Bucket curr, inout Bucket prev, const Sample tap,
|
||||
const float radius, const float border, const float mip, const bool first) {
|
||||
float inLayer = tap.inLayer;
|
||||
float coc = abs(tap.coc);
|
||||
float w = inLayer * intersection(radius, coc, mip) * sampleWeight(coc, mip);
|
||||
@@ -299,8 +299,8 @@ void accumulate(inout Bucket curr, inout Bucket prev, Sample tap,
|
||||
}
|
||||
}
|
||||
|
||||
void accumulateBackground(inout Bucket curr, inout Bucket prev, highp vec2 pos,
|
||||
float radius, float border, float mip, bool first) {
|
||||
void accumulateBackground(inout Bucket curr, inout Bucket prev, const highp vec2 pos,
|
||||
const float radius, const float border, const float mip, const bool first) {
|
||||
Sample tap;
|
||||
tap.s = textureLod(materialParams_color, pos, mip);
|
||||
tap.coc = textureLod(materialParams_coc, pos, mip).r;
|
||||
@@ -309,15 +309,15 @@ void accumulateBackground(inout Bucket curr, inout Bucket prev, highp vec2 pos,
|
||||
}
|
||||
|
||||
void accumulateBackgroundMirror(inout Bucket curr, inout Bucket prev,
|
||||
highp vec2 center, vec2 offset,
|
||||
float radius, float border, float mip, bool first) {
|
||||
const highp vec2 center, const vec2 offset,
|
||||
const float radius, const float border, const float mip, const bool first) {
|
||||
accumulateBackground(curr, prev, diaphragm(center, offset), radius, border, mip, first);
|
||||
accumulateBackground(curr, prev, diaphragm(center, -offset), radius, border, mip, first);
|
||||
}
|
||||
|
||||
void accumulateBackgroundCenter(inout Bucket prev,
|
||||
highp vec2 pos, float ringCount,
|
||||
float kernelSize, float noiseRadius, float mip) {
|
||||
const highp vec2 pos, const float ringCount,
|
||||
const float kernelSize, const float noiseRadius, const float mip) {
|
||||
Bucket curr;
|
||||
initBucket(curr);
|
||||
float border = (kernelSize / (ringCount - 0.5)) * 0.5;
|
||||
@@ -327,9 +327,9 @@ void accumulateBackgroundCenter(inout Bucket prev,
|
||||
mergeRings(curr, prev, 1.0);
|
||||
}
|
||||
|
||||
void accumulateRing(inout Bucket prev, float index, float ringCount,
|
||||
float kernelSize, vec2 noise, highp vec2 uvCenter,
|
||||
highp vec2 cocToTexelScale, float mip, bool first) {
|
||||
void accumulateRing(inout Bucket prev, const float index, const float ringCount,
|
||||
const float kernelSize, const vec2 noise, const highp vec2 uvCenter,
|
||||
const highp vec2 cocToTexelScale, const float mip, const bool first) {
|
||||
|
||||
// we accumulate the larger rings first
|
||||
float i = (ringCount - 1.0) - index;
|
||||
@@ -378,12 +378,12 @@ float computeLayer(float coc, vec2 tiles) {
|
||||
return saturate((coc - tiles.r - frontLayerCocSize) * invTransitionSize);
|
||||
}
|
||||
|
||||
float foregroundFadding(float coc) {
|
||||
float foregroundFadding(const float coc) {
|
||||
return saturate(abs(coc) - (MAX_IN_FOCUS_COC - 1.0));
|
||||
}
|
||||
|
||||
void accumulateForeground(inout Layer layer[2], vec2 tiles,
|
||||
highp vec2 pos, float border, float mip) {
|
||||
void accumulateForeground(inout Layer layer[2], const vec2 tiles,
|
||||
const highp vec2 pos, const float border, const float mip) {
|
||||
float coc = textureLod(materialParams_coc, pos, mip).r;
|
||||
vec4 s = textureLod(materialParams_color, pos, mip);
|
||||
|
||||
@@ -399,13 +399,13 @@ void accumulateForeground(inout Layer layer[2], vec2 tiles,
|
||||
layer[1].weight += inBackWeight;
|
||||
}
|
||||
|
||||
void accumulateForegroundCenter(inout Layer layer[2], vec2 tiles,
|
||||
highp vec2 pos, float border, float mip) {
|
||||
void accumulateForegroundCenter(inout Layer layer[2], const vec2 tiles,
|
||||
const highp vec2 pos, const float border, const float mip) {
|
||||
accumulateForeground(layer, tiles, pos, border, mip);
|
||||
}
|
||||
|
||||
void accumulateForegroundMirror(inout Layer layer[2], vec2 tiles,
|
||||
highp vec2 center, vec2 offset, float border, float mip) {
|
||||
void accumulateForegroundMirror(inout Layer layer[2], const vec2 tiles,
|
||||
const highp vec2 center, const vec2 offset, const float border, const float mip) {
|
||||
// The code below is equivalent to:
|
||||
// accumulateForeground(layer, tiles, diaphragm(center, offset), border, mip);
|
||||
// accumulateForeground(layer, tiles, diaphragm(center, -offset), border, mip);
|
||||
@@ -440,7 +440,7 @@ void accumulateForegroundMirror(inout Layer layer[2], vec2 tiles,
|
||||
*/
|
||||
|
||||
void fastTile(inout vec4 color, inout float alpha,
|
||||
highp vec2 uv, vec2 tiles, NoiseState noiseState) {
|
||||
const highp vec2 uv, const vec2 tiles, const NoiseState noiseState) {
|
||||
highp vec2 cocToTexelScale = materialParams.cocToTexelScale;
|
||||
float cocToPixelScale = materialParams.cocToPixelScale;
|
||||
|
||||
@@ -481,7 +481,7 @@ void fastTile(inout vec4 color, inout float alpha,
|
||||
}
|
||||
|
||||
void foregroundTile(inout vec4 foreground, inout float fgOpacity,
|
||||
highp vec2 uv, vec2 tiles, NoiseState noiseState) {
|
||||
const highp vec2 uv, const vec2 tiles, const NoiseState noiseState) {
|
||||
highp vec2 cocToTexelScale = materialParams.cocToTexelScale;
|
||||
float cocToPixelScale = materialParams.cocToPixelScale;
|
||||
|
||||
@@ -536,7 +536,7 @@ void foregroundTile(inout vec4 foreground, inout float fgOpacity,
|
||||
}
|
||||
|
||||
void backgroundTile(inout vec4 background, inout float bgOpacity,
|
||||
highp vec2 uv, vec2 tiles, NoiseState noiseState) {
|
||||
const highp vec2 uv, const vec2 tiles, const NoiseState noiseState) {
|
||||
highp vec2 cocToTexelScale = materialParams.cocToTexelScale;
|
||||
float cocToPixelScale = materialParams.cocToPixelScale;
|
||||
|
||||
|
||||
@@ -30,11 +30,11 @@ void dummy(){}
|
||||
// Size of a tile in full-resolution pixels -- must match tileSize in PostProcessManager.cpp
|
||||
#define TILE_SIZE 16.0
|
||||
|
||||
vec2 tap(vec2 uv, vec2 offset) {
|
||||
vec2 tap(const vec2 uv, const vec2 offset) {
|
||||
return textureLod(materialParams_tiles, uv + offset, 0.0).rg;
|
||||
}
|
||||
|
||||
vec2 dilate(inout vec2 center, vec2 tap) {
|
||||
vec2 dilate(inout vec2 center, const vec2 tap) {
|
||||
// Tiles that can affect us need to transfer their CoC to us (for the foreground it's
|
||||
// min(ourCoc, theirCoc) because CoC are negative (the min is the larger radius).
|
||||
center.r = min(center.r, tap.r);
|
||||
|
||||
@@ -32,12 +32,12 @@ fragment {
|
||||
|
||||
void dummy(){}
|
||||
|
||||
float max4(vec4 f) {
|
||||
float max4(const vec4 f) {
|
||||
vec2 t = max(f.xy, f.zw);
|
||||
return max(t.x, t.y);
|
||||
}
|
||||
|
||||
float min4(vec4 f) {
|
||||
float min4(const vec4 f) {
|
||||
vec2 t = min(f.xy, f.zw);
|
||||
return min(t.x, t.y);
|
||||
}
|
||||
|
||||
@@ -32,12 +32,12 @@ fragment {
|
||||
|
||||
void dummy(){}
|
||||
|
||||
float max4(vec4 f) {
|
||||
float max4(const vec4 f) {
|
||||
vec2 t = max(f.xy, f.zw);
|
||||
return max(t.x, t.y);
|
||||
}
|
||||
|
||||
float min4(vec4 f) {
|
||||
float min4(const vec4 f) {
|
||||
vec2 t = min(f.xy, f.zw);
|
||||
return min(t.x, t.y);
|
||||
}
|
||||
|
||||
@@ -17,68 +17,68 @@
|
||||
// Currently our dilate pass is set-up for 32 max.
|
||||
#define MAX_COC_RADIUS 32.0
|
||||
|
||||
float min2(vec2 v) {
|
||||
float min2(const vec2 v) {
|
||||
return min(v.x, v.y);
|
||||
}
|
||||
|
||||
float max2(vec2 v) {
|
||||
float max2(const vec2 v) {
|
||||
return max(v.x, v.y);
|
||||
}
|
||||
|
||||
float max4(vec4 v) {
|
||||
float max4(const vec4 v) {
|
||||
return max2(max(v.xy, v.zw));
|
||||
}
|
||||
|
||||
float min4(vec4 v) {
|
||||
float min4(const vec4 v) {
|
||||
return min2(min(v.xy, v.zw));
|
||||
}
|
||||
|
||||
float rcp(float x) {
|
||||
float rcp(const float x) {
|
||||
return 1.0 / x;
|
||||
}
|
||||
|
||||
float rcpOrZero(float x) {
|
||||
float rcpOrZero(const float x) {
|
||||
return x > MEDIUMP_FLT_MIN ? (1.0 / x) : 0.0;
|
||||
}
|
||||
|
||||
highp float rcpOrZeroHighp(highp float x) {
|
||||
highp float rcpOrZeroHighp(const highp float x) {
|
||||
return x > MEDIUMP_FLT_MIN ? (1.0 / x) : 0.0;
|
||||
}
|
||||
|
||||
float cocToAlpha(float coc) {
|
||||
float cocToAlpha(const float coc) {
|
||||
// CoC is positive for background field.
|
||||
// CoC is negative for the foreground field.
|
||||
return saturate(abs(coc) - MAX_IN_FOCUS_COC);
|
||||
}
|
||||
|
||||
// returns circle-of-confusion diameter in pixels
|
||||
float getCOC(float depth, vec2 cocParams) {
|
||||
float getCOC(const float depth, const vec2 cocParams) {
|
||||
return depth * cocParams.x + cocParams.y;
|
||||
}
|
||||
|
||||
vec4 getCOC(vec4 depth, vec2 cocParams) {
|
||||
vec4 getCOC(const vec4 depth, const vec2 cocParams) {
|
||||
return depth * cocParams.x + cocParams.y;
|
||||
}
|
||||
|
||||
float isForeground(float coc) {
|
||||
float isForeground(const float coc) {
|
||||
return coc < 0.0 ? 1.0 : 0.0;
|
||||
}
|
||||
|
||||
float isBackground(float coc) {
|
||||
float isBackground(const float coc) {
|
||||
return coc > 0.0 ? 1.0 : 0.0;
|
||||
}
|
||||
|
||||
bool isForegroundTile(vec2 tiles) {
|
||||
bool isForegroundTile(const vec2 tiles) {
|
||||
// A foreground tile is one where the smallest CoC is negative
|
||||
return tiles.r < 0.0;
|
||||
}
|
||||
|
||||
bool isBackgroundTile(vec2 tiles) {
|
||||
bool isBackgroundTile(const vec2 tiles) {
|
||||
// A background tile is one where the largest CoC is positive
|
||||
return tiles.g > 0.0;
|
||||
}
|
||||
|
||||
bool isFastTile(vec2 tiles) {
|
||||
bool isFastTile(const vec2 tiles) {
|
||||
// We use the distance between the min and max CoC and if the relative error is less than
|
||||
// 5% we assume the tile contains a constant CoC.
|
||||
// We could cannot use the absolute value of the min/mac CoC -- which would categorize more
|
||||
@@ -87,19 +87,19 @@ bool isFastTile(vec2 tiles) {
|
||||
return (tiles.g - tiles.r) <= abs(tiles.g) * 0.05;
|
||||
}
|
||||
|
||||
bool isTrivialTile(vec2 tiles) {
|
||||
bool isTrivialTile(const vec2 tiles) {
|
||||
float maxCocRadius = max(abs(tiles.r), abs(tiles.g));
|
||||
return maxCocRadius < MAX_IN_FOCUS_COC;
|
||||
}
|
||||
|
||||
float downsampleCoC(vec4 c) {
|
||||
float downsampleCoC(const vec4 c) {
|
||||
// We need to compute a suitable CoC to represent the 4 pixels that are downsampled.
|
||||
// We pick the min because this always selects the most foreground sample if there is one,
|
||||
// because the foreground can leak on the background, but not the reverse.
|
||||
return min4(c);
|
||||
}
|
||||
|
||||
vec4 downsampleCocWeights(vec4 c, float outCoc, float scale) {
|
||||
vec4 downsampleCocWeights(const vec4 c, const float outCoc, const float scale) {
|
||||
// The bilateral weight is normally computed as saturate(1 - |outCoc - c|) which selects
|
||||
// the sample with the outCoc weight (and does a little bit of cross-fade if other samples
|
||||
// are close). However, this can also cause some aliasing with dithered objects, so by
|
||||
|
||||
@@ -78,15 +78,15 @@ fragment {
|
||||
|
||||
void dummy(){ }
|
||||
|
||||
float smoothstep01(float t) {
|
||||
float smoothstep01(const float t) {
|
||||
return t * t * (3.0 - 2.0 * t);
|
||||
}
|
||||
|
||||
float ring(float x, float c, float r) {
|
||||
float ring(float x, const float c, const float r) {
|
||||
return 1.0 - smoothstep01(min(abs(x - c) / r, 1.0));
|
||||
}
|
||||
|
||||
vec3 sampleColor(vec2 uv) {
|
||||
vec3 sampleColor(const vec2 uv) {
|
||||
// because we work on reduced resolution, we assume mediump uv is enough.
|
||||
float level = materialParams.level;
|
||||
float chromaticAberration = materialParams.chromaticAberration;
|
||||
@@ -99,7 +99,7 @@ vec3 sampleColor(vec2 uv) {
|
||||
);
|
||||
}
|
||||
|
||||
vec3 ghosts(vec2 vertex_uv) {
|
||||
vec3 ghosts(const vec2 vertex_uv) {
|
||||
// because we work on reduced resolution, we assume mediump uv is enough.
|
||||
float ghostSpacing = materialParams.ghostSpacing;
|
||||
float ghostCount = materialParams.ghostCount;
|
||||
@@ -116,7 +116,7 @@ vec3 ghosts(vec2 vertex_uv) {
|
||||
return color;
|
||||
}
|
||||
|
||||
vec3 halo(vec2 uv) {
|
||||
vec3 halo(const vec2 uv) {
|
||||
float haloRadius = materialParams.haloRadius;
|
||||
float haloThickness = materialParams.haloThickness;
|
||||
float threshold = materialParams.threshold.y;
|
||||
|
||||
@@ -72,7 +72,7 @@ fragment {
|
||||
#if defined(FILAMENT_HAS_FEATURE_TEXTURE_GATHER)
|
||||
#define gather textureGather
|
||||
#else
|
||||
vec4 gather(mediump sampler2D color, highp vec2 p, int comp) {
|
||||
vec4 gather(const mediump sampler2D color, highp vec2 p, const int comp) {
|
||||
highp ivec2 i = ivec2(p * materialParams.textureSize - 0.5);
|
||||
vec4 d;
|
||||
d[0] = texelFetchOffset(color, i, 0, ivec2(0, 1))[comp];
|
||||
|
||||
@@ -75,7 +75,7 @@ fragment {
|
||||
#if defined(FILAMENT_HAS_FEATURE_TEXTURE_GATHER)
|
||||
#define gather textureGather
|
||||
#else
|
||||
vec4 gather(mediump sampler2D color, highp vec2 p, int comp) {
|
||||
vec4 gather(const mediump sampler2D color, highp vec2 p, const int comp) {
|
||||
highp ivec2 i = ivec2(p * materialParams.textureSize - 0.5);
|
||||
vec4 d;
|
||||
d[0] = texelFetchOffset(color, i, 0, ivec2(0, 1))[comp];
|
||||
|
||||
@@ -76,7 +76,7 @@ fragment {
|
||||
#if defined(FILAMENT_HAS_FEATURE_TEXTURE_GATHER)
|
||||
#define gather textureGather
|
||||
#else
|
||||
vec4 gather(mediump sampler2D color, highp vec2 p, int comp) {
|
||||
vec4 gather(const mediump sampler2D color, highp vec2 p, const int comp) {
|
||||
highp ivec2 i = ivec2(p * materialParams.textureSize - 0.5);
|
||||
vec4 d;
|
||||
d[0] = texelFetchOffset(color, i, 0, ivec2(0, 1))[comp];
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
float vmax(float v) {
|
||||
float vmax(const float v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
highp vec4 sourceTexLod(highp vec2 p, float m, float l) {
|
||||
highp vec4 sourceTexLod(const highp vec2 p, float m, float l) {
|
||||
// This condition is optimized away at compile-time.
|
||||
if (materialConstants_arraySampler) {
|
||||
return textureLod(materialParams_sourceArray, vec3(p, l), m);
|
||||
@@ -11,12 +11,12 @@ highp vec4 sourceTexLod(highp vec2 p, float m, float l) {
|
||||
}
|
||||
}
|
||||
|
||||
void tap(inout highp vec4 sum, float weight, highp vec2 position) {
|
||||
void tap(inout highp vec4 sum, const float weight, const highp vec2 position) {
|
||||
highp vec4 s = sourceTexLod(position, materialParams.level, materialParams.layer);
|
||||
sum += s * weight;
|
||||
}
|
||||
|
||||
void tapReinhard(inout highp vec4 sum, inout float totalWeight, float weight, highp vec2 position) {
|
||||
void tapReinhard(inout highp vec4 sum, inout float totalWeight, const float weight, const highp vec2 position) {
|
||||
highp vec4 s = sourceTexLod(position, materialParams.level, materialParams.layer);
|
||||
float w = weight / (1.0 + vmax(s));
|
||||
totalWeight += w;
|
||||
|
||||
@@ -48,7 +48,7 @@ fragment {
|
||||
return max(0.0, 1.0 - diff * diff);
|
||||
}
|
||||
|
||||
void tap(highp sampler2DArray saoTexture,
|
||||
void tap(const highp sampler2DArray saoTexture,
|
||||
inout float sum, inout float totalWeight, float weight, float depth, vec2 position) {
|
||||
// ambient occlusion sample
|
||||
vec3 data = textureLod(saoTexture, vec3(position, 0.0), 0.0).rgb;
|
||||
|
||||
@@ -60,14 +60,14 @@ fragment {
|
||||
return max(0.0, 1.0 - diff * diff);
|
||||
}
|
||||
|
||||
void tapAO(highp sampler2DArray saoTexture, highp vec2 uv,
|
||||
void tapAO(const highp sampler2DArray saoTexture, highp vec2 uv,
|
||||
out float ao, out highp float sampleDepth) {
|
||||
vec3 data = textureLod(saoTexture, vec3(uv, 0.0), 0.0).rgb;
|
||||
ao = data.r;
|
||||
sampleDepth = unpack(data.gb);
|
||||
}
|
||||
|
||||
void tapBN(highp sampler2DArray saoTexture, highp vec2 uv,
|
||||
void tapBN(const highp sampler2DArray saoTexture, highp vec2 uv,
|
||||
out vec3 bentNormal) {
|
||||
vec3 data = textureLod(saoTexture, vec3(uv, 1.0), 0.0).xyz;
|
||||
bentNormal = unpackBentNormal(data);
|
||||
|
||||
@@ -34,14 +34,14 @@ const float kLog2LodRate = 3.0;
|
||||
// "The Alchemy Screen-Space Ambient Obscurance Algorithm" by Morgan McGuire
|
||||
// "Scalable Ambient Obscurance" by Morgan McGuire, Michael Mara and David Luebke
|
||||
|
||||
vec3 tapLocation(float i, float noise) {
|
||||
vec3 tapLocation(float i, const float noise) {
|
||||
float offset = ((2.0 * PI) * 2.4) * noise;
|
||||
float angle = ((i * materialParams.sampleCount.y) * materialParams.spiralTurns) * (2.0 * PI) + offset;
|
||||
float radius = (i + noise + 0.5) * materialParams.sampleCount.y;
|
||||
return vec3(cos(angle), sin(angle), radius * radius);
|
||||
}
|
||||
|
||||
highp vec2 startPosition(float noise) {
|
||||
highp vec2 startPosition(const float noise) {
|
||||
float angle = ((2.0 * PI) * 2.4) * noise;
|
||||
return vec2(cos(angle), sin(angle));
|
||||
}
|
||||
@@ -51,15 +51,15 @@ highp mat2 tapAngleStep() {
|
||||
return mat2(t.x, t.y, -t.y, t.x);
|
||||
}
|
||||
|
||||
vec3 tapLocationFast(float i, vec2 p, float noise) {
|
||||
vec3 tapLocationFast(float i, vec2 p, const float noise) {
|
||||
float radius = (i + noise + 0.5) * materialParams.sampleCount.y;
|
||||
return vec3(p, radius * radius);
|
||||
}
|
||||
|
||||
void computeAmbientOcclusionSAO(inout float occlusion, inout vec3 bentNormal,
|
||||
float i, float ssDiskRadius,
|
||||
highp vec2 uv, highp vec3 origin, vec3 normal,
|
||||
vec2 tapPosition, float noise) {
|
||||
const highp vec2 uv, const highp vec3 origin, const vec3 normal,
|
||||
const vec2 tapPosition, const float noise) {
|
||||
|
||||
vec3 tap = tapLocationFast(i, tapPosition, noise);
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ struct ConeTraceSetup {
|
||||
uint sampleCount;
|
||||
};
|
||||
|
||||
highp float getWFromProjectionMatrix(highp mat4 p, vec3 v) {
|
||||
highp float getWFromProjectionMatrix(const highp mat4 p, const vec3 v) {
|
||||
// this essentially returns (p * vec4(v, 1.0)).w, but we make some assumptions
|
||||
// this assumes a perspective projection
|
||||
return -v.z;
|
||||
@@ -60,14 +60,14 @@ highp float getWFromProjectionMatrix(highp mat4 p, vec3 v) {
|
||||
//return p[2][3] * v.z + p[3][3];
|
||||
}
|
||||
|
||||
highp float getViewSpaceZFromW(highp mat4 p, float w) {
|
||||
highp float getViewSpaceZFromW(const highp mat4 p, const float w) {
|
||||
// this assumes a perspective projection
|
||||
return -w;
|
||||
// this assumes a perspective or ortho projection
|
||||
//return (w - p[3][3]) / p[2][3];
|
||||
}
|
||||
|
||||
float coneTraceOcclusion(in ConeTraceSetup setup, highp sampler2D depthTexture) {
|
||||
float coneTraceOcclusion(in ConeTraceSetup setup, const highp sampler2D depthTexture) {
|
||||
// skip fragments that are back-facing trace direction
|
||||
// (avoid overshadowing of translucent surfaces)
|
||||
float NoL = dot(setup.vsNormal, setup.vsConeDirection);
|
||||
@@ -138,7 +138,7 @@ float coneTraceOcclusion(in ConeTraceSetup setup, highp sampler2D depthTexture)
|
||||
}
|
||||
|
||||
float ssctDominantLightShadowing(highp vec2 uv, highp vec3 origin, vec3 normal,
|
||||
highp sampler2D depthTexture, highp vec2 fragCoord,
|
||||
const highp sampler2D depthTexture, const highp vec2 fragCoord,
|
||||
vec2 rayCount, ConeTraceSetup cone) {
|
||||
|
||||
float occlusion = 0.0;
|
||||
|
||||
@@ -28,12 +28,12 @@ highp float linearizeDepth(highp float depth) {
|
||||
return (depth * p[2].z + p[3].z) / max(depth * p[2].w + p[3].w, preventDiv0);
|
||||
}
|
||||
|
||||
highp float sampleDepth(highp sampler2D depthTexture, highp vec2 uv, float lod) {
|
||||
highp float sampleDepth(const highp sampler2D depthTexture, const highp vec2 uv, float lod) {
|
||||
return textureLod(depthTexture, uvToRenderTargetUV(uv), lod).r;
|
||||
}
|
||||
|
||||
highp float sampleDepthLinear(highp sampler2D depthTexture,
|
||||
highp vec2 uv, float lod) {
|
||||
highp float sampleDepthLinear(const highp sampler2D depthTexture,
|
||||
const highp vec2 uv, float lod) {
|
||||
return linearizeDepth(sampleDepth(depthTexture, uv, lod));
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ highp vec3 faceNormal(highp vec3 dpdx, highp vec3 dpdy) {
|
||||
// this creates arifacts around geometry edges.
|
||||
// Note: when using the spirv optimizer, this results in much slower execution time because
|
||||
// this whole expression is inlined in the AO loop below.
|
||||
highp vec3 computeViewSpaceNormalLowQ(highp vec3 position) {
|
||||
highp vec3 computeViewSpaceNormalLowQ(const highp vec3 position) {
|
||||
return faceNormal(dFdx(position), dFdy(position));
|
||||
}
|
||||
|
||||
@@ -52,8 +52,8 @@ highp vec3 computeViewSpaceNormalLowQ(highp vec3 position) {
|
||||
// positionParams : invProjection[0][0] * 2, invProjection[1][1] * 2
|
||||
//
|
||||
highp vec3 computeViewSpaceNormalMediumQ(
|
||||
highp sampler2D depthTexture, highp vec2 uv,
|
||||
highp vec3 position,
|
||||
const highp sampler2D depthTexture, const highp vec2 uv,
|
||||
const highp vec3 position,
|
||||
highp vec2 texel, highp vec2 positionParams) {
|
||||
|
||||
precision highp float;
|
||||
@@ -81,8 +81,8 @@ highp vec3 computeViewSpaceNormalMediumQ(
|
||||
// positionParams : invProjection[0][0] * 2, invProjection[1][1] * 2
|
||||
//
|
||||
highp vec3 computeViewSpaceNormalHighQ(
|
||||
highp sampler2D depthTexture, highp vec2 uv,
|
||||
highp float depth, highp vec3 position,
|
||||
const highp sampler2D depthTexture, const highp vec2 uv,
|
||||
const highp float depth, const highp vec3 position,
|
||||
highp vec2 texel, highp vec2 positionParams) {
|
||||
|
||||
precision highp float;
|
||||
@@ -125,8 +125,8 @@ highp vec3 computeViewSpaceNormalHighQ(
|
||||
// positionParams : invProjection[0][0] * 2, invProjection[1][1] * 2
|
||||
//
|
||||
highp vec3 computeViewSpaceNormal(
|
||||
highp sampler2D depthTexture, highp vec2 uv,
|
||||
highp float depth, highp vec3 position,
|
||||
const highp sampler2D depthTexture, const highp vec2 uv,
|
||||
const highp float depth, const highp vec3 position,
|
||||
highp vec2 texel, highp vec2 positionParams) {
|
||||
// todo: maybe make this a quality parameter
|
||||
#if FILAMENT_QUALITY == FILAMENT_QUALITY_HIGH
|
||||
|
||||
@@ -208,12 +208,14 @@ public:
|
||||
MaterialBuilder(MaterialBuilder&& rhs) noexcept = default;
|
||||
MaterialBuilder& operator=(MaterialBuilder&& rhs) noexcept = default;
|
||||
|
||||
static constexpr size_t MATERIAL_VARIABLES_COUNT = 4;
|
||||
static constexpr size_t MATERIAL_VARIABLES_COUNT = 6;
|
||||
enum class Variable : uint8_t {
|
||||
CUSTOM0,
|
||||
CUSTOM1,
|
||||
CUSTOM2,
|
||||
CUSTOM3
|
||||
CUSTOM3,
|
||||
CUSTOM4,
|
||||
CUSTOM5
|
||||
// when adding more variables, make sure to update MATERIAL_VARIABLES_COUNT
|
||||
};
|
||||
|
||||
|
||||
@@ -205,6 +205,8 @@ MaterialBuilder& MaterialBuilder::variable(Variable v, const char* name) noexcep
|
||||
case Variable::CUSTOM1:
|
||||
case Variable::CUSTOM2:
|
||||
case Variable::CUSTOM3:
|
||||
case Variable::CUSTOM4:
|
||||
case Variable::CUSTOM5:
|
||||
assert(size_t(v) < MATERIAL_VARIABLES_COUNT);
|
||||
mVariables[size_t(v)] = CString(name);
|
||||
break;
|
||||
|
||||
@@ -120,8 +120,12 @@ build_ubershader(specularGlossiness_masked base specularGlossiness ma
|
||||
build_ubershader(unlit_fade base unlit fade)
|
||||
build_ubershader(unlit_opaque base unlit opaque)
|
||||
build_ubershader(unlit_masked base unlit masked)
|
||||
build_ubershader(volume volume _ _)
|
||||
build_ubershader(transmission transmission _ _)
|
||||
build_ubershader(volume_opaque volume lit opaque)
|
||||
build_ubershader(volume_fade volume lit fade)
|
||||
build_ubershader(volume_masked volume lit masked)
|
||||
build_ubershader(transmission_opaque transmission lit opaque)
|
||||
build_ubershader(transmission_fade transmission lit fade)
|
||||
build_ubershader(transmission_masked transmission lit masked)
|
||||
build_ubershader(sheen sheen _ _)
|
||||
|
||||
add_custom_target(uberz_file DEPENDS ${UBERZ_OUTPUT_PATH})
|
||||
|
||||
@@ -2,9 +2,9 @@ material {
|
||||
name : transmission_ubershader,
|
||||
requires : [ uv0, uv1, color ],
|
||||
shadingModel : lit,
|
||||
blending : masked,
|
||||
doubleSided : false,
|
||||
transparency : default,
|
||||
blending : ${BLENDING},
|
||||
doubleSided : ${DOUBLESIDED},
|
||||
transparency : ${TRANSPARENCY},
|
||||
flipUV : false,
|
||||
specularAmbientOcclusion : simple,
|
||||
specularAntiAliasing : true,
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
ShadingModel=lit
|
||||
|
||||
# Even though the transmission material actually has its
|
||||
# blend mode set to "masked", we want the glTF loader to
|
||||
# think that it is "opaque".
|
||||
BlendingMode=opaque
|
||||
# Required Aspects
|
||||
BlendingMode = ${BLENDING}
|
||||
|
||||
Sheen=unsupported
|
||||
Volume=unsupported
|
||||
|
||||
@@ -2,9 +2,9 @@ material {
|
||||
name : volume_ubershader,
|
||||
requires : [ uv0, uv1, color ],
|
||||
shadingModel : lit,
|
||||
blending : masked,
|
||||
doubleSided : false,
|
||||
transparency : default,
|
||||
blending : ${BLENDING},
|
||||
doubleSided : ${DOUBLESIDED},
|
||||
transparency : ${TRANSPARENCY},
|
||||
flipUV : false,
|
||||
specularAmbientOcclusion : simple,
|
||||
specularAntiAliasing : true,
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
ShadingModel=lit
|
||||
|
||||
# NOTE: this intentionally omits the blending mode spec because
|
||||
# Filament requires MASKED blending for KHR_materials_volume.
|
||||
# Required Aspects
|
||||
BlendingMode = ${BLENDING}
|
||||
|
||||
# Core Features
|
||||
VertexColors=optional
|
||||
|
||||
@@ -461,6 +461,22 @@ Material* createMaterial(Engine* engine, const MaterialKey& config, const UvMap&
|
||||
}
|
||||
}
|
||||
|
||||
// BLENDING
|
||||
switch (config.alphaMode) {
|
||||
case AlphaMode::OPAQUE:
|
||||
builder.blending(MaterialBuilder::BlendingMode::OPAQUE);
|
||||
break;
|
||||
case AlphaMode::MASK:
|
||||
builder.blending(MaterialBuilder::BlendingMode::MASKED);
|
||||
break;
|
||||
case AlphaMode::BLEND:
|
||||
builder.blending(MaterialBuilder::BlendingMode::FADE);
|
||||
break;
|
||||
default:
|
||||
// Ignore
|
||||
break;
|
||||
}
|
||||
|
||||
// TRANSMISSION
|
||||
if (config.hasTransmission) {
|
||||
// According to KHR_materials_transmission, the minimum expectation for a compliant renderer
|
||||
@@ -480,24 +496,6 @@ Material* createMaterial(Engine* engine, const MaterialKey& config, const UvMap&
|
||||
MaterialBuilder::ParameterPrecision::HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
builder.blending(MaterialBuilder::BlendingMode::MASKED);
|
||||
} else {
|
||||
// BLENDING
|
||||
switch (config.alphaMode) {
|
||||
case AlphaMode::OPAQUE:
|
||||
builder.blending(MaterialBuilder::BlendingMode::OPAQUE);
|
||||
break;
|
||||
case AlphaMode::MASK:
|
||||
builder.blending(MaterialBuilder::BlendingMode::MASKED);
|
||||
break;
|
||||
case AlphaMode::BLEND:
|
||||
builder.blending(MaterialBuilder::BlendingMode::FADE);
|
||||
break;
|
||||
default:
|
||||
// Ignore
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// VOLUME
|
||||
@@ -517,8 +515,6 @@ Material* createMaterial(Engine* engine, const MaterialKey& config, const UvMap&
|
||||
MaterialBuilder::ParameterPrecision::HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
builder.blending(MaterialBuilder::BlendingMode::MASKED);
|
||||
}
|
||||
|
||||
// IOR
|
||||
|
||||
@@ -199,14 +199,26 @@ RandomAccessIterator partition_point(
|
||||
return first;
|
||||
}
|
||||
|
||||
template <class To, class From>
|
||||
typename std::enable_if_t<
|
||||
(sizeof(To) == sizeof(From)) &&
|
||||
std::is_trivially_copyable<From>::value,
|
||||
To>
|
||||
// constexpr support needs compiler magic
|
||||
bit_cast(const From &src) noexcept {
|
||||
return reinterpret_cast<const To&>(src);
|
||||
template <class Dest, class Source>
|
||||
#if __has_builtin(__builtin_bit_cast)
|
||||
constexpr
|
||||
#else
|
||||
inline
|
||||
#endif
|
||||
Dest bit_cast(const Source& source) noexcept {
|
||||
#if __has_builtin(__builtin_bit_cast)
|
||||
return __builtin_bit_cast(Dest, source);
|
||||
#else
|
||||
static_assert(sizeof(Dest) == sizeof(Source),
|
||||
"bit_cast requires source and destination to be the same size");
|
||||
static_assert(std::is_trivially_copyable_v<Dest>,
|
||||
"bit_cast requires the destination type to be copyable");
|
||||
static_assert(std::is_trivially_copyable_v<Source>,
|
||||
"bit_cast requires the source type to be copyable");
|
||||
Dest dest;
|
||||
memcpy(&dest, &source, sizeof(dest));
|
||||
return dest;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
|
||||
@@ -868,4 +868,30 @@ std::ostream& operator<<(std::ostream& out, const SoftShadowOptions& in) {
|
||||
<< "}";
|
||||
}
|
||||
|
||||
int parse(jsmntok_t const* tokens, int i, const char* jsonChunk, StereoscopicOptions* out) {
|
||||
CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
|
||||
int size = tokens[i++].size;
|
||||
for (int j = 0; j < size; ++j) {
|
||||
const jsmntok_t tok = tokens[i];
|
||||
CHECK_KEY(tok);
|
||||
if (compare(tok, jsonChunk, "enabled") == 0) {
|
||||
i = parse(tokens, i + 1, jsonChunk, &out->enabled);
|
||||
} else {
|
||||
slog.w << "Invalid StereoscopicOptions key: '" << STR(tok, jsonChunk) << "'" << io::endl;
|
||||
i = parse(tokens, i + 1);
|
||||
}
|
||||
if (i < 0) {
|
||||
slog.e << "Invalid StereoscopicOptions value: '" << STR(tok, jsonChunk) << "'" << io::endl;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const StereoscopicOptions& in) {
|
||||
return out << "{\n"
|
||||
<< "\"enabled\": " << to_string(in.enabled) << "\n"
|
||||
<< "}";
|
||||
}
|
||||
|
||||
} // namespace filament::viewer
|
||||
|
||||
@@ -93,4 +93,7 @@ std::ostream& operator<<(std::ostream& out, const VsmShadowOptions& in);
|
||||
int parse(jsmntok_t const* tokens, int i, const char* jsonChunk, SoftShadowOptions* out);
|
||||
std::ostream& operator<<(std::ostream& out, const SoftShadowOptions& in);
|
||||
|
||||
int parse(jsmntok_t const* tokens, int i, const char* jsonChunk, StereoscopicOptions* out);
|
||||
std::ostream& operator<<(std::ostream& out, const StereoscopicOptions& in);
|
||||
|
||||
} // namespace filament::viewer
|
||||
|
||||
@@ -738,8 +738,6 @@ void ViewerGui::updateUserInterface() {
|
||||
ImGui::Checkbox("Dithering", &dither);
|
||||
enableDithering(dither);
|
||||
ImGui::Checkbox("Bloom", &mSettings.view.bloom.enabled);
|
||||
ImGui::Checkbox("Flare", &mSettings.view.bloom.lensFlare);
|
||||
|
||||
ImGui::Checkbox("TAA", &mSettings.view.taa.enabled);
|
||||
// this clutters the UI and isn't that useful (except when working on TAA)
|
||||
//ImGui::Indent();
|
||||
@@ -758,56 +756,74 @@ void ViewerGui::updateUserInterface() {
|
||||
ImGui::Unindent();
|
||||
|
||||
ImGui::Checkbox("SSAO", &mSettings.view.ssao.enabled);
|
||||
if (ImGui::CollapsingHeader("SSAO Options")) {
|
||||
auto& ssao = mSettings.view.ssao;
|
||||
|
||||
int quality = (int) ssao.quality;
|
||||
int lowpass = (int) ssao.lowPassFilter;
|
||||
bool upsampling = ssao.upsampling != View::QualityLevel::LOW;
|
||||
|
||||
bool halfRes = ssao.resolution != 1.0f;
|
||||
ImGui::SliderInt("Quality", &quality, 0, 3);
|
||||
ImGui::SliderInt("Low Pass", &lowpass, 0, 2);
|
||||
ImGui::Checkbox("Bent Normals", &ssao.bentNormals);
|
||||
ImGui::Checkbox("High quality upsampling", &upsampling);
|
||||
ImGui::SliderFloat("Min Horizon angle", &ssao.minHorizonAngleRad, 0.0f, (float)M_PI_4);
|
||||
ImGui::SliderFloat("Bilateral Threshold", &ssao.bilateralThreshold, 0.0f, 0.1f);
|
||||
ImGui::Checkbox("Half resolution", &halfRes);
|
||||
ssao.resolution = halfRes ? 0.5f : 1.0f;
|
||||
|
||||
|
||||
ssao.upsampling = upsampling ? View::QualityLevel::HIGH : View::QualityLevel::LOW;
|
||||
ssao.lowPassFilter = (View::QualityLevel) lowpass;
|
||||
ssao.quality = (View::QualityLevel) quality;
|
||||
|
||||
if (ImGui::CollapsingHeader("Dominant Light Shadows (experimental)")) {
|
||||
int sampleCount = ssao.ssct.sampleCount;
|
||||
ImGui::Checkbox("Enabled##dls", &ssao.ssct.enabled);
|
||||
ImGui::SliderFloat("Cone angle", &ssao.ssct.lightConeRad, 0.0f, (float)M_PI_2);
|
||||
ImGui::SliderFloat("Shadow Distance", &ssao.ssct.shadowDistance, 0.0f, 10.0f);
|
||||
ImGui::SliderFloat("Contact dist max", &ssao.ssct.contactDistanceMax, 0.0f, 100.0f);
|
||||
ImGui::SliderFloat("Intensity##dls", &ssao.ssct.intensity, 0.0f, 10.0f);
|
||||
ImGui::SliderFloat("Depth bias", &ssao.ssct.depthBias, 0.0f, 1.0f);
|
||||
ImGui::SliderFloat("Depth slope bias", &ssao.ssct.depthSlopeBias, 0.0f, 1.0f);
|
||||
ImGui::SliderInt("Sample Count", &sampleCount, 1, 32);
|
||||
ImGuiExt::DirectionWidget("Direction##dls", ssao.ssct.lightDirection.v);
|
||||
ssao.ssct.sampleCount = sampleCount;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::Checkbox("Screen-space reflections", &mSettings.view.screenSpaceReflections.enabled);
|
||||
if (ImGui::CollapsingHeader("Screen-space reflections Options")) {
|
||||
auto& ssrefl = mSettings.view.screenSpaceReflections;
|
||||
ImGui::SliderFloat("Ray thickness", &ssrefl.thickness, 0.001f, 0.2f);
|
||||
ImGui::SliderFloat("Bias", &ssrefl.bias, 0.001f, 0.5f);
|
||||
ImGui::SliderFloat("Max distance", &ssrefl.maxDistance, 0.1, 10.0f);
|
||||
ImGui::SliderFloat("Stride", &ssrefl.stride, 1.0, 10.0f);
|
||||
}
|
||||
ImGui::Unindent();
|
||||
|
||||
ImGui::Checkbox("Screen-space Guard Band", &mSettings.view.guardBand.enabled);
|
||||
}
|
||||
|
||||
if (ImGui::CollapsingHeader("Bloom Options")) {
|
||||
ImGui::SliderFloat("Strength", &mSettings.view.bloom.strength, 0.0f, 1.0f);
|
||||
ImGui::Checkbox("Threshold", &mSettings.view.bloom.threshold);
|
||||
|
||||
float anamorphism = mSettings.view.bloom.anamorphism >= 1.0f ?
|
||||
mSettings.view.bloom.anamorphism - 1.0f : 1.0f - 1.0f / mSettings.view.bloom.anamorphism;
|
||||
ImGui::SliderFloat("Amamorphism", &anamorphism, -32.0f, 32.0f);
|
||||
mSettings.view.bloom.anamorphism = anamorphism >= 0 ?
|
||||
(anamorphism + 1.0f) : 1.0f / (1.0f - anamorphism);
|
||||
|
||||
int levels = mSettings.view.bloom.levels;
|
||||
ImGui::SliderInt("Levels", &levels, 3, 11);
|
||||
mSettings.view.bloom.levels = levels;
|
||||
|
||||
ImGui::Checkbox("Lens Flare", &mSettings.view.bloom.lensFlare);
|
||||
}
|
||||
|
||||
if (ImGui::CollapsingHeader("SSAO Options")) {
|
||||
auto& ssao = mSettings.view.ssao;
|
||||
|
||||
int quality = (int) ssao.quality;
|
||||
int lowpass = (int) ssao.lowPassFilter;
|
||||
bool upsampling = ssao.upsampling != View::QualityLevel::LOW;
|
||||
|
||||
bool halfRes = ssao.resolution != 1.0f;
|
||||
ImGui::SliderInt("Quality", &quality, 0, 3);
|
||||
ImGui::SliderInt("Low Pass", &lowpass, 0, 2);
|
||||
ImGui::Checkbox("Bent Normals", &ssao.bentNormals);
|
||||
ImGui::Checkbox("High quality upsampling", &upsampling);
|
||||
ImGui::SliderFloat("Min Horizon angle", &ssao.minHorizonAngleRad, 0.0f, (float)M_PI_4);
|
||||
ImGui::SliderFloat("Bilateral Threshold", &ssao.bilateralThreshold, 0.0f, 0.1f);
|
||||
ImGui::Checkbox("Half resolution", &halfRes);
|
||||
ssao.resolution = halfRes ? 0.5f : 1.0f;
|
||||
|
||||
ssao.upsampling = upsampling ? View::QualityLevel::HIGH : View::QualityLevel::LOW;
|
||||
ssao.lowPassFilter = (View::QualityLevel) lowpass;
|
||||
ssao.quality = (View::QualityLevel) quality;
|
||||
|
||||
if (ImGui::CollapsingHeader("Dominant Light Shadows (experimental)")) {
|
||||
int sampleCount = ssao.ssct.sampleCount;
|
||||
ImGui::Checkbox("Enabled##dls", &ssao.ssct.enabled);
|
||||
ImGui::SliderFloat("Cone angle", &ssao.ssct.lightConeRad, 0.0f, (float)M_PI_2);
|
||||
ImGui::SliderFloat("Shadow Distance", &ssao.ssct.shadowDistance, 0.0f, 10.0f);
|
||||
ImGui::SliderFloat("Contact dist max", &ssao.ssct.contactDistanceMax, 0.0f, 100.0f);
|
||||
ImGui::SliderFloat("Intensity##dls", &ssao.ssct.intensity, 0.0f, 10.0f);
|
||||
ImGui::SliderFloat("Depth bias", &ssao.ssct.depthBias, 0.0f, 1.0f);
|
||||
ImGui::SliderFloat("Depth slope bias", &ssao.ssct.depthSlopeBias, 0.0f, 1.0f);
|
||||
ImGui::SliderInt("Sample Count", &sampleCount, 1, 32);
|
||||
ImGuiExt::DirectionWidget("Direction##dls", ssao.ssct.lightDirection.v);
|
||||
ssao.ssct.sampleCount = sampleCount;
|
||||
}
|
||||
}
|
||||
|
||||
if (ImGui::CollapsingHeader("Screen-space reflections Options")) {
|
||||
auto& ssrefl = mSettings.view.screenSpaceReflections;
|
||||
ImGui::SliderFloat("Ray thickness", &ssrefl.thickness, 0.001f, 0.2f);
|
||||
ImGui::SliderFloat("Bias", &ssrefl.bias, 0.001f, 0.5f);
|
||||
ImGui::SliderFloat("Max distance", &ssrefl.maxDistance, 0.1, 10.0f);
|
||||
ImGui::SliderFloat("Stride", &ssrefl.stride, 1.0, 10.0f);
|
||||
}
|
||||
|
||||
if (ImGui::CollapsingHeader("Dynamic Resolution")) {
|
||||
auto& dsr = mSettings.view.dsr;
|
||||
int quality = (int)dsr.quality;
|
||||
|
||||
@@ -139,7 +139,7 @@ vec3 unpackBentNormal(vec3 bn) {
|
||||
return bn * 2.0 - 1.0;
|
||||
}
|
||||
|
||||
float specularAO(float NoV, float visibility, float roughness, SSAOInterpolationCache cache) {
|
||||
float specularAO(float NoV, float visibility, float roughness, const in SSAOInterpolationCache cache) {
|
||||
float specularAO = 1.0;
|
||||
|
||||
// SSAO is not applied when blending is enabled
|
||||
@@ -200,7 +200,7 @@ float specularAO(float NoV, float visibility, float roughness, SSAOInterpolation
|
||||
* The albedo term is meant to be the diffuse color or f0 for the diffuse and
|
||||
* specular terms respectively.
|
||||
*/
|
||||
vec3 gtaoMultiBounce(float visibility, vec3 albedo) {
|
||||
vec3 gtaoMultiBounce(float visibility, const vec3 albedo) {
|
||||
// Jimenez et al. 2016, "Practical Realtime Strategies for Accurate Indirect Occlusion"
|
||||
vec3 a = 2.0404 * albedo - 0.3324;
|
||||
vec3 b = -4.7951 * albedo + 0.6417;
|
||||
@@ -210,13 +210,13 @@ vec3 gtaoMultiBounce(float visibility, vec3 albedo) {
|
||||
}
|
||||
#endif
|
||||
|
||||
void multiBounceAO(float visibility, vec3 albedo, inout vec3 color) {
|
||||
void multiBounceAO(float visibility, const vec3 albedo, inout vec3 color) {
|
||||
#if MULTI_BOUNCE_AMBIENT_OCCLUSION == 1
|
||||
color *= gtaoMultiBounce(visibility, albedo);
|
||||
#endif
|
||||
}
|
||||
|
||||
void multiBounceSpecularAO(float visibility, vec3 albedo, inout vec3 color) {
|
||||
void multiBounceSpecularAO(float visibility, const vec3 albedo, inout vec3 color) {
|
||||
#if MULTI_BOUNCE_AMBIENT_OCCLUSION == 1 && SPECULAR_AMBIENT_OCCLUSION != SPECULAR_AO_OFF
|
||||
color *= gtaoMultiBounce(visibility, albedo);
|
||||
#endif
|
||||
|
||||
+6
-6
@@ -51,7 +51,7 @@
|
||||
// Specular BRDF implementations
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
float D_GGX(float roughness, float NoH, vec3 h) {
|
||||
float D_GGX(float roughness, float NoH, const vec3 h) {
|
||||
// Walter et al. 2007, "Microfacet Models for Refraction through Rough Surfaces"
|
||||
|
||||
// In mediump, there are two problems computing 1.0 - NoH^2
|
||||
@@ -138,12 +138,12 @@ float V_Neubelt(float NoV, float NoL) {
|
||||
return saturateMediump(1.0 / (4.0 * (NoL + NoV - NoL * NoV)));
|
||||
}
|
||||
|
||||
vec3 F_Schlick(vec3 f0, float f90, float VoH) {
|
||||
vec3 F_Schlick(const vec3 f0, float f90, float VoH) {
|
||||
// Schlick 1994, "An Inexpensive BRDF Model for Physically-Based Rendering"
|
||||
return f0 + (f90 - f0) * pow5(1.0 - VoH);
|
||||
}
|
||||
|
||||
vec3 F_Schlick(vec3 f0, float VoH) {
|
||||
vec3 F_Schlick(const vec3 f0, float VoH) {
|
||||
float f = pow(1.0 - VoH, 5.0);
|
||||
return f + f0 * (1.0 - f);
|
||||
}
|
||||
@@ -156,7 +156,7 @@ float F_Schlick(float f0, float f90, float VoH) {
|
||||
// Specular BRDF dispatch
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
float distribution(float roughness, float NoH, vec3 h) {
|
||||
float distribution(float roughness, float NoH, const vec3 h) {
|
||||
#if BRDF_SPECULAR_D == SPECULAR_D_GGX
|
||||
return D_GGX(roughness, NoH, h);
|
||||
#endif
|
||||
@@ -170,7 +170,7 @@ float visibility(float roughness, float NoV, float NoL) {
|
||||
#endif
|
||||
}
|
||||
|
||||
vec3 fresnel(vec3 f0, float LoH) {
|
||||
vec3 fresnel(const vec3 f0, float LoH) {
|
||||
#if BRDF_SPECULAR_F == SPECULAR_F_SCHLICK
|
||||
#if FILAMENT_QUALITY == FILAMENT_QUALITY_LOW
|
||||
return F_Schlick(f0, LoH); // f90 = 1.0
|
||||
@@ -196,7 +196,7 @@ float visibilityAnisotropic(float roughness, float at, float ab,
|
||||
#endif
|
||||
}
|
||||
|
||||
float distributionClearCoat(float roughness, float NoH, vec3 h) {
|
||||
float distributionClearCoat(float roughness, float NoH, const vec3 h) {
|
||||
#if BRDF_CLEAR_COAT_D == SPECULAR_D_GGX
|
||||
return D_GGX(roughness, NoH, h);
|
||||
#endif
|
||||
|
||||
@@ -22,3 +22,13 @@
|
||||
|
||||
#define float3x3 mat3
|
||||
#define float4x4 mat4
|
||||
|
||||
// To workaround an adreno crash (#5294), we need ensure that a method with
|
||||
// parameter 'const mat4' does not call another method also with a 'const mat4'
|
||||
// parameter (i.e. mulMat4x4Float3). So we remove the const modifier for
|
||||
// materials compiled for vulkan+mobile.
|
||||
#if defined(TARGET_VULKAN_ENVIRONMENT) && defined(TARGET_MOBILE)
|
||||
#define highp_mat4 highp mat4
|
||||
#else
|
||||
#define highp_mat4 const highp mat4
|
||||
#endif
|
||||
|
||||
@@ -70,7 +70,7 @@ highp float getUserTimeMod(float m) {
|
||||
*
|
||||
* @public-api
|
||||
*/
|
||||
highp vec2 uvToRenderTargetUV(highp vec2 uv) {
|
||||
highp vec2 uvToRenderTargetUV(const highp vec2 uv) {
|
||||
#if defined(TARGET_METAL_ENVIRONMENT) || defined(TARGET_VULKAN_ENVIRONMENT)
|
||||
return vec2(uv.x, 1.0 - uv.y);
|
||||
#else
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @public-api
|
||||
*/
|
||||
float luminance(vec3 linear) {
|
||||
float luminance(const vec3 linear) {
|
||||
return dot(linear, vec3(0.2126, 0.7152, 0.0722));
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ float luminance(vec3 linear) {
|
||||
* Computes the pre-exposed intensity using the specified intensity and exposure.
|
||||
* This function exists to force highp precision on the two parameters
|
||||
*/
|
||||
float computePreExposedIntensity(highp float intensity, highp float exposure) {
|
||||
float computePreExposedIntensity(const highp float intensity, const highp float exposure) {
|
||||
return intensity * exposure;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ vec3 ycbcrToRgb(float luminance, vec2 cbcr) {
|
||||
/*
|
||||
* The input must be in the [0, 1] range.
|
||||
*/
|
||||
vec3 Inverse_Tonemap_Filmic(vec3 x) {
|
||||
vec3 Inverse_Tonemap_Filmic(const vec3 x) {
|
||||
return (0.03 - 0.59 * x - sqrt(0.0009 + 1.3702 * x - 1.0127 * x * x)) / (-5.02 + 4.86 * x);
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ vec3 decodeRGBM(vec4 c) {
|
||||
|
||||
// returns the frag coord in the GL convention with (0, 0) at the bottom-left
|
||||
// resolution : width, height
|
||||
highp vec2 getFragCoord(highp vec2 resolution) {
|
||||
highp vec2 getFragCoord(const highp vec2 resolution) {
|
||||
#if defined(TARGET_METAL_ENVIRONMENT) || defined(TARGET_VULKAN_ENVIRONMENT)
|
||||
return vec2(gl_FragCoord.x, resolution.y - gl_FragCoord.y);
|
||||
#else
|
||||
|
||||
@@ -82,7 +82,7 @@ float computeMicroShadowing(float NoL, float visibility) {
|
||||
* direction to match reference renderings when the roughness increases
|
||||
*/
|
||||
|
||||
vec3 getReflectedVector(PixelParams pixel, vec3 v, vec3 n) {
|
||||
vec3 getReflectedVector(const PixelParams pixel, const vec3 v, const vec3 n) {
|
||||
#if defined(MATERIAL_HAS_ANISOTROPY)
|
||||
vec3 anisotropyDirection = pixel.anisotropy >= 0.0 ? pixel.anisotropicB : pixel.anisotropicT;
|
||||
vec3 anisotropicTangent = cross(anisotropyDirection, v);
|
||||
@@ -97,7 +97,7 @@ vec3 getReflectedVector(PixelParams pixel, vec3 v, vec3 n) {
|
||||
return r;
|
||||
}
|
||||
|
||||
void getAnisotropyPixelParams(MaterialInputs material, inout PixelParams pixel) {
|
||||
void getAnisotropyPixelParams(const MaterialInputs material, inout PixelParams pixel) {
|
||||
#if defined(MATERIAL_HAS_ANISOTROPY)
|
||||
vec3 direction = material.anisotropyDirection;
|
||||
pixel.anisotropy = material.anisotropy;
|
||||
|
||||
@@ -14,11 +14,11 @@ float clampNoV(float NoV) {
|
||||
return max(NoV, MIN_N_DOT_V);
|
||||
}
|
||||
|
||||
vec3 computeDiffuseColor(vec4 baseColor, float metallic) {
|
||||
vec3 computeDiffuseColor(const vec4 baseColor, float metallic) {
|
||||
return baseColor.rgb * (1.0 - metallic);
|
||||
}
|
||||
|
||||
vec3 computeF0(vec4 baseColor, float metallic, float reflectance) {
|
||||
vec3 computeF0(const vec4 baseColor, float metallic, float reflectance) {
|
||||
return baseColor.rgb * metallic + (reflectance * (1.0 - metallic));
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ float computeDielectricF0(float reflectance) {
|
||||
return 0.16 * reflectance * reflectance;
|
||||
}
|
||||
|
||||
float computeMetallicFromSpecularColor(vec3 specularColor) {
|
||||
float computeMetallicFromSpecularColor(const vec3 specularColor) {
|
||||
return max3(specularColor);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ float f0ToIor(float f0) {
|
||||
return (1.0 + r) / (1.0 - r);
|
||||
}
|
||||
|
||||
vec3 f0ClearCoatToSurface(vec3 f0) {
|
||||
vec3 f0ClearCoatToSurface(const vec3 f0) {
|
||||
// Approximation of iorTof0(f0ToIor(f0), 1.5)
|
||||
// This assumes that the clear coat layer has an IOR of 1.5
|
||||
#if FILAMENT_QUALITY == FILAMENT_QUALITY_LOW
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user