Compare commits

...

1 Commits

Author SHA1 Message Date
Powei Feng
9c2fec6b9e Testing stencilling 2 2024-10-09 16:24:45 -07:00
3 changed files with 115 additions and 35 deletions

View File

@@ -1044,6 +1044,11 @@ void OpenGLContext::resetState() noexcept {
);
glDepthMask(state.raster.depthMask);
glDepthFunc(state.raster.depthFunc);
utils::slog.e << "stencil front=" <<
state.stencil.front.func.func << " " <<
state.stencil.front.func.ref << " " <<
state.stencil.front.func.mask << " " << utils::io::endl;
// state.stencil
glStencilFuncSeparate(

View File

@@ -460,20 +460,20 @@ void OpenGLDriver::setStencilState(StencilState ss) noexcept {
// stencil test / operation
// GL_STENCIL_TEST must be enabled if we're testing OR writing to the stencil buffer.
if (UTILS_LIKELY(
ss.front.stencilFunc == StencilState::StencilFunction::A &&
ss.back.stencilFunc == StencilState::StencilFunction::A &&
ss.front.stencilOpDepthFail == StencilOperation::KEEP &&
ss.back.stencilOpDepthFail == StencilOperation::KEEP &&
ss.front.stencilOpStencilFail == StencilOperation::KEEP &&
ss.back.stencilOpStencilFail == StencilOperation::KEEP &&
ss.front.stencilOpDepthStencilPass == StencilOperation::KEEP &&
ss.back.stencilOpDepthStencilPass == StencilOperation::KEEP)) {
// that's equivalent to having the stencil test disabled
gl.disable(GL_STENCIL_TEST);
} else {
// if (UTILS_LIKELY(
// ss.front.stencilFunc == StencilState::StencilFunction::A &&
// ss.back.stencilFunc == StencilState::StencilFunction::A &&
// ss.front.stencilOpDepthFail == StencilOperation::KEEP &&
// ss.back.stencilOpDepthFail == StencilOperation::KEEP &&
// ss.front.stencilOpStencilFail == StencilOperation::KEEP &&
// ss.back.stencilOpStencilFail == StencilOperation::KEEP &&
// ss.front.stencilOpDepthStencilPass == StencilOperation::KEEP &&
// ss.back.stencilOpDepthStencilPass == StencilOperation::KEEP) && false) {
// // that's equivalent to having the stencil test disabled
// gl.disable(GL_STENCIL_TEST);
// } else {
gl.enable(GL_STENCIL_TEST);
}
// }
// glStencilFuncSeparate() also sets the reference value, which may be used depending
// on the stencilOp, so we always need to call glStencilFuncSeparate().
@@ -481,9 +481,9 @@ void OpenGLDriver::setStencilState(StencilState ss) noexcept {
getStencilFunc(ss.front.stencilFunc), ss.front.ref, ss.front.readMask,
getStencilFunc(ss.back.stencilFunc), ss.back.ref, ss.back.readMask);
if (UTILS_LIKELY(!ss.stencilWrite)) {
gl.stencilMaskSeparate(0x00, 0x00);
} else {
// if (UTILS_LIKELY(!ss.stencilWrite)) {
// gl.stencilMaskSeparate(0x00, 0x00);
// } else {
// Stencil ops are only relevant when stencil write is enabled
gl.stencilOpSeparate(
getStencilOp(ss.front.stencilOpStencilFail),
@@ -493,7 +493,7 @@ void OpenGLDriver::setStencilState(StencilState ss) noexcept {
getStencilOp(ss.back.stencilOpDepthFail),
getStencilOp(ss.back.stencilOpDepthStencilPass));
gl.stencilMaskSeparate(ss.front.writeMask, ss.back.writeMask);
}
// }
}
// ------------------------------------------------------------------------------------------------

View File

@@ -20,6 +20,7 @@
#include <filament/Material.h>
#include <filament/MaterialInstance.h>
#include <filament/RenderableManager.h>
#include <filament/Renderer.h>
#include <filament/Scene.h>
#include <filament/Skybox.h>
#include <filament/TransformManager.h>
@@ -46,23 +47,33 @@ using utils::EntityManager;
struct App {
Config config;
VertexBuffer* vb;
VertexBuffer* vb2;
IndexBuffer* ib;
Material* mat;
Camera* cam;
Entity camera;
Skybox* skybox;
Entity renderable;
Entity r2;
};
struct Vertex {
filament::math::float2 position;
filament::math::float3 position;
uint32_t color;
};
float const z = 5;
static const Vertex TRIANGLE_VERTICES[3] = {
{{1, 0}, 0xffff0000u},
{{cos(M_PI * 2 / 3), sin(M_PI * 2 / 3)}, 0xff00ff00u},
{{cos(M_PI * 4 / 3), sin(M_PI * 4 / 3)}, 0xff0000ffu},
{{1, 0, z}, 0xffff0000u},
{{cos(M_PI * 2 / 3), sin(M_PI * 2 / 3), z}, 0xff00ff00u},
{{cos(M_PI * 4 / 3), sin(M_PI * 4 / 3), z}, 0xff0000ffu},
};
static Vertex T2[3] = {
TRIANGLE_VERTICES[0],
TRIANGLE_VERTICES[1],
TRIANGLE_VERTICES[2],
};
static constexpr uint16_t TRIANGLE_INDICES[3] = { 0, 1, 2 };
@@ -120,25 +131,39 @@ static int handleCommandLineArguments(int argc, char* argv[], App* app) {
}
int main(int argc, char** argv) {
T2[0].position.z = z - 10;
T2[1].position.z = z - 10;
T2[2].position.z = z - 10;
T2[0].color = 0xFF0000FF;
T2[1].color = 0xFF0000FF;
T2[2].color = 0xFF0000FF;
App app{};
app.config.title = "hellotriangle";
app.config.featureLevel = backend::FeatureLevel::FEATURE_LEVEL_0;
handleCommandLineArguments(argc, argv, &app);
auto setup = [&app](Engine* engine, View* view, Scene* scene) {
app.skybox = Skybox::Builder().color({0.1, 0.125, 0.25, 1.0}).build(*engine);
scene->setSkybox(app.skybox);
view->setPostProcessingEnabled(false);
static_assert(sizeof(Vertex) == 12, "Strange vertex size.");
app.vb = VertexBuffer::Builder()
view->setStencilBufferEnabled(true);
// app.skybox = Skybox::Builder().color({0.1, 0.125, 0.25, 1.0}).build(*engine);
// scene->setSkybox(app.skybox);
static_assert(sizeof(Vertex) == 16, "Strange vertex size.");
auto builder = VertexBuffer::Builder()
.vertexCount(3)
.bufferCount(1)
.attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT2, 0, 12)
.attribute(VertexAttribute::COLOR, 0, VertexBuffer::AttributeType::UBYTE4, 8, 12)
.normalized(VertexAttribute::COLOR)
.build(*engine);
.attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3, 0, 16)
.attribute(VertexAttribute::COLOR, 0, VertexBuffer::AttributeType::UBYTE4, 12, 16)
.normalized(VertexAttribute::COLOR);
app.vb = builder.build(*engine);
app.vb2 = builder.build(*engine);
app.vb->setBufferAt(*engine, 0,
VertexBuffer::BufferDescriptor(TRIANGLE_VERTICES, 36, nullptr));
VertexBuffer::BufferDescriptor(TRIANGLE_VERTICES, 48, nullptr));
app.vb2->setBufferAt(*engine, 0,
VertexBuffer::BufferDescriptor(T2, 48, nullptr));
app.ib = IndexBuffer::Builder()
.indexCount(3)
.bufferType(IndexBuffer::IndexType::USHORT)
@@ -149,22 +174,67 @@ int main(int argc, char** argv) {
.package(RESOURCES_BAKEDCOLOR_DATA, RESOURCES_BAKEDCOLOR_SIZE)
.build(*engine);
app.renderable = EntityManager::get().create();
app.r2 = EntityManager::get().create();
auto inst1 = app.mat->createInstance();
inst1->setDepthWrite(false);
inst1->setDepthFunc(MaterialInstance::DepthFunc::A);
inst1->setDepthCulling(false);
inst1->setStencilWrite(true);
inst1->setStencilCompareFunction(MaterialInstance::StencilCompareFunc::E);
inst1->setStencilOpDepthStencilPass(MaterialInstance::StencilOperation::REPLACE);
inst1->setStencilReferenceValue(6);
auto inst2 = app.mat->createInstance();
inst2->setDepthWrite(false);
inst2->setDepthFunc(MaterialInstance::DepthFunc::A);
inst2->setDepthCulling(false);
inst2->setStencilWrite(true);
inst2->setStencilCompareFunction(MaterialInstance::StencilCompareFunc::L);
inst2->setStencilOpDepthStencilPass(MaterialInstance::StencilOperation::REPLACE);
inst2->setStencilReferenceValue(6);
auto& renderableMan = engine->getRenderableManager();
RenderableManager::Builder(1)
.boundingBox({{ -1, -1, -1 }, { 1, 1, 1 }})
.material(0, app.mat->getDefaultInstance())
.material(0, inst1)
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, app.vb, app.ib, 0, 3)
.culling(false)
.receiveShadows(false)
.castShadows(false)
.build(*engine, app.renderable);
RenderableManager::Builder(1)
.boundingBox({{ -1, -1, -1 }, { 1, 1, 1 }})
.material(0, inst2)
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, app.vb2, app.ib, 0, 3)
.culling(false)
.receiveShadows(false)
.castShadows(false)
.build(*engine, app.r2);
scene->addEntity(app.renderable);
auto r1inst = renderableMan.getInstance(app.renderable);
renderableMan.setPriority(r1inst, 7);
scene->addEntity(app.r2);
auto r2inst = renderableMan.getInstance(app.r2);
renderableMan.setPriority(r2inst, 6);
app.camera = utils::EntityManager::get().create();
app.cam = engine->createCamera(app.camera);
view->setCamera(app.cam);
};
auto cleanup = [&app](Engine* engine, View*, Scene*) {
engine->destroy(app.skybox);
// engine->destroy(app.skybox);
engine->destroy(app.renderable);
engine->destroy(app.mat);
engine->destroy(app.vb);
@@ -174,19 +244,24 @@ int main(int argc, char** argv) {
};
FilamentApp::get().animate([&app](Engine* engine, View* view, double now) {
constexpr float ZOOM = 1.5f;
constexpr float ZOOM = 1.5;
const uint32_t w = view->getViewport().width;
const uint32_t h = view->getViewport().height;
const float aspect = (float) w / h;
app.cam->setProjection(Camera::Projection::ORTHO,
-aspect * ZOOM, aspect * ZOOM,
-ZOOM, ZOOM, 0, 1);
-ZOOM, ZOOM, -100, 100);
auto& tcm = engine->getTransformManager();
tcm.setTransform(tcm.getInstance(app.renderable),
filament::math::mat4f::rotation(now, filament::math::float3{ 0, 0, 1 }));
});
FilamentApp::get().run(app.config, setup, cleanup);
auto preRender = [](Engine*, View* view, Scene*, Renderer* renderer) {
renderer->setClearOptions({ .clearStencil = 0u, .clear = true, });
};
FilamentApp::get().run(app.config, setup, cleanup, {}, preRender);
return 0;
}