Merge pull request #5286 from samcake/daft

Migrating the rendering openGL calls from DeferredLightingEffect to gpu::Batch
This commit is contained in:
Brad Hefta-Gaub 2015-07-09 15:23:45 -07:00
commit f0a1bc15a9
32 changed files with 813 additions and 377 deletions

View file

@ -114,6 +114,23 @@ void Batch::clearFramebuffer(Framebuffer::Masks targets, const Vec4& color, floa
_params.push_back(targets); _params.push_back(targets);
} }
void Batch::clearColorFramebuffer(Framebuffer::Masks targets, const Vec4& color) {
clearFramebuffer(targets & Framebuffer::BUFFER_COLORS, color, 1.0f, 0);
}
void Batch::clearDepthFramebuffer(float depth) {
clearFramebuffer(Framebuffer::BUFFER_DEPTH, Vec4(0.0f), depth, 0);
}
void Batch::clearStencilFramebuffer(int stencil) {
clearFramebuffer(Framebuffer::BUFFER_STENCIL, Vec4(0.0f), 1.0f, stencil);
}
void Batch::clearDepthStencilFramebuffer(float depth, int stencil) {
clearFramebuffer(Framebuffer::BUFFER_DEPTHSTENCIL, Vec4(0.0f), depth, stencil);
}
void Batch::setInputFormat(const Stream::FormatPointer& format) { void Batch::setInputFormat(const Stream::FormatPointer& format) {
ADD_COMMAND(setInputFormat); ADD_COMMAND(setInputFormat);
@ -152,6 +169,10 @@ void Batch::setIndexBuffer(Type type, const BufferPointer& buffer, Offset offset
_params.push_back(type); _params.push_back(type);
} }
void Batch::setIndexBuffer(const BufferView& buffer) {
setIndexBuffer(buffer._element.getType(), buffer._buffer, buffer._offset);
}
void Batch::setModelTransform(const Transform& model) { void Batch::setModelTransform(const Transform& model) {
ADD_COMMAND(setModelTransform); ADD_COMMAND(setModelTransform);
@ -170,10 +191,10 @@ void Batch::setProjectionTransform(const Mat4& proj) {
_params.push_back(cacheData(sizeof(Mat4), &proj)); _params.push_back(cacheData(sizeof(Mat4), &proj));
} }
void Batch::setViewportTransform(const Vec4i& viewport) { void Batch::setViewportTransform(const Vec4i& viewport) {
ADD_COMMAND(setViewportTransform); ADD_COMMAND(setViewportTransform);
_params.push_back(cacheData(sizeof(Vec4i), &viewport)); _params.push_back(cacheData(sizeof(Vec4i), &viewport));
} }
void Batch::setPipeline(const PipelinePointer& pipeline) { void Batch::setPipeline(const PipelinePointer& pipeline) {
@ -218,7 +239,7 @@ void Batch::setUniformTexture(uint32 slot, const TextureView& view) {
} }
void Batch::setFramebuffer(const FramebufferPointer& framebuffer) { void Batch::setFramebuffer(const FramebufferPointer& framebuffer) {
ADD_COMMAND(setUniformTexture); ADD_COMMAND(setFramebuffer);
_params.push_back(_framebuffers.cache(framebuffer)); _params.push_back(_framebuffers.cache(framebuffer));

View file

@ -38,19 +38,6 @@
namespace gpu { namespace gpu {
enum Primitive {
POINTS = 0,
LINES,
LINE_STRIP,
TRIANGLES,
TRIANGLE_STRIP,
TRIANGLE_FAN,
QUADS,
QUAD_STRIP,
NUM_PRIMITIVES,
};
enum ReservedSlot { enum ReservedSlot {
/* TRANSFORM_OBJECT_SLOT = 6, /* TRANSFORM_OBJECT_SLOT = 6,
TRANSFORM_CAMERA_SLOT = 7, TRANSFORM_CAMERA_SLOT = 7,
@ -76,7 +63,12 @@ public:
void drawIndexedInstanced(uint32 nbInstances, Primitive primitiveType, uint32 nbIndices, uint32 startIndex = 0, uint32 startInstance = 0); void drawIndexedInstanced(uint32 nbInstances, Primitive primitiveType, uint32 nbIndices, uint32 startIndex = 0, uint32 startInstance = 0);
// Clear framebuffer layers // Clear framebuffer layers
// Targets can be any of the render buffers contained in the Framebuffer
void clearFramebuffer(Framebuffer::Masks targets, const Vec4& color, float depth, int stencil); void clearFramebuffer(Framebuffer::Masks targets, const Vec4& color, float depth, int stencil);
void clearColorFramebuffer(Framebuffer::Masks targets, const Vec4& color); // not a command, just a shortcut for clearFramebuffer, mask out targets to make sure it touches only color targets
void clearDepthFramebuffer(float depth); // not a command, just a shortcut for clearFramebuffer, it touches only depth target
void clearStencilFramebuffer(int stencil); // not a command, just a shortcut for clearFramebuffer, it touches only stencil target
void clearDepthStencilFramebuffer(float depth, int stencil); // not a command, just a shortcut for clearFramebuffer, it touches depth and stencil target
// Input Stage // Input Stage
// InputFormat // InputFormat
@ -89,6 +81,7 @@ public:
void setInputStream(Slot startChannel, const BufferStream& stream); // not a command, just unroll into a loop of setInputBuffer void setInputStream(Slot startChannel, const BufferStream& stream); // not a command, just unroll into a loop of setInputBuffer
void setIndexBuffer(Type type, const BufferPointer& buffer, Offset offset); void setIndexBuffer(Type type, const BufferPointer& buffer, Offset offset);
void setIndexBuffer(const BufferView& buffer); // not a command, just a shortcut from a BufferView
// Transform Stage // Transform Stage
// Vertex position is transformed by ModelTransform from object space to world space // Vertex position is transformed by ModelTransform from object space to world space

View file

@ -42,7 +42,7 @@ public:
Mat4 _projectionViewUntranslated; Mat4 _projectionViewUntranslated;
Mat4 _projection; Mat4 _projection;
Mat4 _projectionInverse; Mat4 _projectionInverse;
Vec4 _viewport; Vec4 _viewport; // Public value is int but float in the shader to stay in floats for all the transform computations.
}; };
template< typename T > template< typename T >

View file

@ -0,0 +1,21 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// Draw texture 0 fetched at texcoord.xy
//
// Created by Sam Gateau on 6/22/2015
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
uniform sampler2D colorMap;
varying vec2 varTexcoord;
void main(void) {
gl_FragColor = texture2D(colorMap, varTexcoord);
}

View file

@ -0,0 +1,36 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// Draw and transform the unit quad [-1,-1 -> 1,1]
// Simply draw a Triangle_strip of 2 triangles, no input buffers or index buffer needed
//
// Created by Sam Gateau on 6/22/2015
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
varying vec2 varTexcoord;
void main(void) {
const vec4 UNIT_QUAD[4] = vec4[4](
vec4(-1.0, -1.0, 0.0, 1.0),
vec4(1.0, -1.0, 0.0, 1.0),
vec4(-1.0, 1.0, 0.0, 1.0),
vec4(1.0, 1.0, 0.0, 1.0)
);
vec4 pos = UNIT_QUAD[gl_VertexID];
// standard transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
<$transformModelToClipPos(cam, obj, pos, gl_Position)$>
varTexcoord = (pos.xy + 1) * 0.5;
}

View file

@ -0,0 +1,38 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// Draw the unit quad [-1,-1 -> 1,1] filling in
// Simply draw a Triangle_strip of 2 triangles, no input buffers or index buffer needed
//
// Created by Sam Gateau on 6/22/2015
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
varying vec2 varTexcoord;
void main(void) {
const vec4 UNIT_QUAD[4] = vec4[4](
vec4(-1.0, -1.0, 0.0, 1.0),
vec4(1.0, -1.0, 0.0, 1.0),
vec4(-1.0, 1.0, 0.0, 1.0),
vec4(1.0, 1.0, 0.0, 1.0)
);
vec4 pos = UNIT_QUAD[gl_VertexID];
// standard transform but applied to the Texcoord
vec4 tc = vec4((pos.xy + 1) * 0.5, pos.zw);
TransformObject obj = getTransformObject();
<$transformModelToWorldPos(obj, tc, tc)$>
gl_Position = pos;
varTexcoord = tc.xy;
}

View file

@ -182,6 +182,9 @@ public:
} }
static const Element COLOR_RGBA_32; static const Element COLOR_RGBA_32;
static const Element VEC3F_XYZ;
static const Element INDEX_UINT16;
static const Element PART_DRAWCALL;
protected: protected:
uint8 _semantic; uint8 _semantic;
@ -203,6 +206,19 @@ enum ComparisonFunction {
NUM_COMPARISON_FUNCS, NUM_COMPARISON_FUNCS,
}; };
enum Primitive {
POINTS = 0,
LINES,
LINE_STRIP,
TRIANGLES,
TRIANGLE_STRIP,
TRIANGLE_FAN,
QUADS,
QUAD_STRIP,
NUM_PRIMITIVES,
};
}; };

View file

@ -233,13 +233,31 @@ void GLBackend::do_clearFramebuffer(Batch& batch, uint32 paramOffset) {
glmask |= GL_DEPTH_BUFFER_BIT; glmask |= GL_DEPTH_BUFFER_BIT;
} }
std::vector<GLenum> drawBuffers;
if (masks & Framebuffer::BUFFER_COLORS) { if (masks & Framebuffer::BUFFER_COLORS) {
glClearColor(color.x, color.y, color.z, color.w); for (int i = 0; i < Framebuffer::MAX_NUM_RENDER_BUFFERS; i++) {
glmask |= GL_COLOR_BUFFER_BIT; if (masks & (1 << i)) {
drawBuffers.push_back(GL_COLOR_ATTACHMENT0 + i);
}
}
if (!drawBuffers.empty()) {
glDrawBuffers(drawBuffers.size(), drawBuffers.data());
glClearColor(color.x, color.y, color.z, color.w);
glmask |= GL_COLOR_BUFFER_BIT;
}
} }
glClear(glmask); glClear(glmask);
// Restore the color draw buffers only if a frmaebuffer is bound
if (_output._framebuffer && !drawBuffers.empty()) {
auto glFramebuffer = syncGPUObject(*_output._framebuffer);
if (glFramebuffer) {
glDrawBuffers(glFramebuffer->_colorBuffers.size(), glFramebuffer->_colorBuffers.data());
}
}
(void) CHECK_GL_ERROR(); (void) CHECK_GL_ERROR();
} }
@ -598,10 +616,11 @@ void GLBackend::do_glUniform4fv(Batch& batch, uint32 paramOffset) {
return; return;
} }
updatePipeline(); updatePipeline();
glUniform4fv(
batch._params[paramOffset + 2]._int, GLint location = batch._params[paramOffset + 2]._int;
batch._params[paramOffset + 1]._uint, GLsizei count = batch._params[paramOffset + 1]._uint;
(const GLfloat*)batch.editData(batch._params[paramOffset + 0]._uint)); const GLfloat* value = (const GLfloat*)batch.editData(batch._params[paramOffset + 0]._uint);
glUniform4fv(location, count, value);
(void) CHECK_GL_ERROR(); (void) CHECK_GL_ERROR();
} }

View file

@ -181,6 +181,7 @@ public:
class GLFramebuffer : public GPUObject { class GLFramebuffer : public GPUObject {
public: public:
GLuint _fbo = 0; GLuint _fbo = 0;
std::vector<GLenum> _colorBuffers;
GLFramebuffer(); GLFramebuffer();
~GLFramebuffer(); ~GLFramebuffer();
@ -198,7 +199,7 @@ public:
void do_setStateFillMode(int32 mode); void do_setStateFillMode(int32 mode);
void do_setStateCullMode(int32 mode); void do_setStateCullMode(int32 mode);
void do_setStateFrontFaceClockwise(bool isClockwise); void do_setStateFrontFaceClockwise(bool isClockwise);
void do_setStateDepthClipEnable(bool enable); void do_setStateDepthClampEnable(bool enable);
void do_setStateScissorEnable(bool enable); void do_setStateScissorEnable(bool enable);
void do_setStateMultisampleEnable(bool enable); void do_setStateMultisampleEnable(bool enable);
void do_setStateAntialiasedLineEnable(bool enable); void do_setStateAntialiasedLineEnable(bool enable);
@ -298,6 +299,7 @@ protected:
_model(), _model(),
_view(), _view(),
_projection(), _projection(),
_viewport(0,0,1,1),
_invalidModel(true), _invalidModel(true),
_invalidView(true), _invalidView(true),
_invalidProj(false), _invalidProj(false),

View file

@ -40,8 +40,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe
glBindFramebuffer(GL_FRAMEBUFFER, fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo);
unsigned int nbColorBuffers = 0; std::vector<GLenum> colorBuffers;
GLenum colorBuffers[16];
if (framebuffer.hasColor()) { if (framebuffer.hasColor()) {
static const GLenum colorAttachments[] = { static const GLenum colorAttachments[] = {
GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT0,
@ -69,8 +68,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe
if (gltexture) { if (gltexture) {
glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[unit], GL_TEXTURE_2D, gltexture->_texture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[unit], GL_TEXTURE_2D, gltexture->_texture, 0);
} }
colorBuffers[nbColorBuffers] = colorAttachments[unit]; colorBuffers.push_back(colorAttachments[unit]);
nbColorBuffers++;
unit++; unit++;
} }
} }
@ -100,8 +98,8 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe
} }
// Last but not least, define where we draw // Last but not least, define where we draw
if (nbColorBuffers > 0) { if (!colorBuffers.empty()) {
glDrawBuffers(nbColorBuffers, colorBuffers); glDrawBuffers(colorBuffers.size(), colorBuffers.data());
} else { } else {
glDrawBuffer( GL_NONE ); glDrawBuffer( GL_NONE );
} }
@ -139,6 +137,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe
// All is green, assign the gpuobject to the Framebuffer // All is green, assign the gpuobject to the Framebuffer
object = new GLFramebuffer(); object = new GLFramebuffer();
object->_fbo = fbo; object->_fbo = fbo;
object->_colorBuffers = colorBuffers;
Backend::setGPUObject(framebuffer, object); Backend::setGPUObject(framebuffer, object);
} }

View file

@ -147,9 +147,9 @@ void GLBackend::updatePipeline() {
#if (GPU_TRANSFORM_PROFILE == GPU_CORE) #if (GPU_TRANSFORM_PROFILE == GPU_CORE)
#else #else
// If shader program needs the model we need to provide it // If shader program needs the model we need to provide it
if (_pipeline._program_transformObject_model >= 0) { if (_pipeline._program_transformObject_model >= 0) {
glUniformMatrix4fv(_pipeline._program_transformObject_model, 1, false, (const GLfloat*) &_transform._transformObject._model); glUniformMatrix4fv(_pipeline._program_transformObject_model, 1, false, (const GLfloat*) &_transform._transformObject._model);
} }
// If shader program needs the inverseView we need to provide it // If shader program needs the inverseView we need to provide it

View file

@ -111,10 +111,10 @@ void makeBindings(GLBackend::GLShader* shader) {
shader->_transformCameraSlot = gpu::TRANSFORM_CAMERA_SLOT; shader->_transformCameraSlot = gpu::TRANSFORM_CAMERA_SLOT;
} }
#else #else
loc = glGetUniformLocation(glprogram, "transformObject_model"); loc = glGetUniformLocation(glprogram, "transformObject_model");
if (loc >= 0) { if (loc >= 0) {
shader->_transformObject_model = loc; shader->_transformObject_model = loc;
} }
loc = glGetUniformLocation(glprogram, "transformCamera_viewInverse"); loc = glGetUniformLocation(glprogram, "transformCamera_viewInverse");
if (loc >= 0) { if (loc >= 0) {

View file

@ -51,7 +51,7 @@ const GLBackend::GLState::Commands makeResetStateCommands() {
CommandPointer(new Command1I(&GLBackend::do_setStateFillMode, DEFAULT.fillMode)), CommandPointer(new Command1I(&GLBackend::do_setStateFillMode, DEFAULT.fillMode)),
CommandPointer(new Command1I(&GLBackend::do_setStateCullMode, DEFAULT.cullMode)), CommandPointer(new Command1I(&GLBackend::do_setStateCullMode, DEFAULT.cullMode)),
CommandPointer(new Command1B(&GLBackend::do_setStateFrontFaceClockwise, DEFAULT.frontFaceClockwise)), CommandPointer(new Command1B(&GLBackend::do_setStateFrontFaceClockwise, DEFAULT.frontFaceClockwise)),
CommandPointer(new Command1B(&GLBackend::do_setStateDepthClipEnable, DEFAULT.depthClipEnable)), CommandPointer(new Command1B(&GLBackend::do_setStateDepthClampEnable, DEFAULT.depthClampEnable)),
CommandPointer(new Command1B(&GLBackend::do_setStateScissorEnable, DEFAULT.scissorEnable)), CommandPointer(new Command1B(&GLBackend::do_setStateScissorEnable, DEFAULT.scissorEnable)),
CommandPointer(new Command1B(&GLBackend::do_setStateMultisampleEnable, DEFAULT.multisampleEnable)), CommandPointer(new Command1B(&GLBackend::do_setStateMultisampleEnable, DEFAULT.multisampleEnable)),
CommandPointer(new Command1B(&GLBackend::do_setStateAntialiasedLineEnable, DEFAULT.antialisedLineEnable)), CommandPointer(new Command1B(&GLBackend::do_setStateAntialiasedLineEnable, DEFAULT.antialisedLineEnable)),
@ -89,8 +89,8 @@ void generateFrontFaceClockwise(GLBackend::GLState::Commands& commands, bool isC
commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateFrontFaceClockwise, isClockwise))); commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateFrontFaceClockwise, isClockwise)));
} }
void generateDepthClipEnable(GLBackend::GLState::Commands& commands, bool enable) { void generateDepthClampEnable(GLBackend::GLState::Commands& commands, bool enable) {
commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateDepthClipEnable, enable))); commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateDepthClampEnable, enable)));
} }
void generateScissorEnable(GLBackend::GLState::Commands& commands, bool enable) { void generateScissorEnable(GLBackend::GLState::Commands& commands, bool enable) {
@ -176,8 +176,8 @@ GLBackend::GLState* GLBackend::syncGPUObject(const State& state) {
generateFrontFaceClockwise(object->_commands, state.isFrontFaceClockwise()); generateFrontFaceClockwise(object->_commands, state.isFrontFaceClockwise());
break; break;
} }
case State::DEPTH_CLIP_ENABLE: { case State::DEPTH_CLAMP_ENABLE: {
generateDepthClipEnable(object->_commands, state.isDepthClipEnable()); generateDepthClampEnable(object->_commands, state.isDepthClampEnable());
break; break;
} }
case State::SCISSOR_ENABLE: { case State::SCISSOR_ENABLE: {
@ -373,7 +373,7 @@ void GLBackend::getCurrentGLState(State::Data& state) {
GLint winding; GLint winding;
glGetIntegerv(GL_FRONT_FACE, &winding); glGetIntegerv(GL_FRONT_FACE, &winding);
state.frontFaceClockwise = (winding == GL_CW); state.frontFaceClockwise = (winding == GL_CW);
state.depthClipEnable = glIsEnabled(GL_DEPTH_CLAMP); state.depthClampEnable = glIsEnabled(GL_DEPTH_CLAMP);
state.scissorEnable = glIsEnabled(GL_SCISSOR_TEST); state.scissorEnable = glIsEnabled(GL_SCISSOR_TEST);
state.multisampleEnable = glIsEnabled(GL_MULTISAMPLE); state.multisampleEnable = glIsEnabled(GL_MULTISAMPLE);
state.antialisedLineEnable = glIsEnabled(GL_LINE_SMOOTH); state.antialisedLineEnable = glIsEnabled(GL_LINE_SMOOTH);
@ -533,8 +533,8 @@ void GLBackend::do_setStateFrontFaceClockwise(bool isClockwise) {
} }
} }
void GLBackend::do_setStateDepthClipEnable(bool enable) { void GLBackend::do_setStateDepthClampEnable(bool enable) {
if (_pipeline._stateCache.depthClipEnable != enable) { if (_pipeline._stateCache.depthClampEnable != enable) {
if (enable) { if (enable) {
glEnable(GL_DEPTH_CLAMP); glEnable(GL_DEPTH_CLAMP);
} else { } else {
@ -542,7 +542,7 @@ void GLBackend::do_setStateDepthClipEnable(bool enable) {
} }
(void) CHECK_GL_ERROR(); (void) CHECK_GL_ERROR();
_pipeline._stateCache.depthClipEnable = enable; _pipeline._stateCache.depthClampEnable = enable;
} }
} }

View file

@ -15,7 +15,6 @@
using namespace gpu; using namespace gpu;
// Transform Stage // Transform Stage
void GLBackend::do_setModelTransform(Batch& batch, uint32 paramOffset) { void GLBackend::do_setModelTransform(Batch& batch, uint32 paramOffset) {
_transform._model = batch._transforms.get(batch._params[paramOffset]._uint); _transform._model = batch._transforms.get(batch._params[paramOffset]._uint);
_transform._invalidModel = true; _transform._invalidModel = true;
@ -31,11 +30,10 @@ void GLBackend::do_setProjectionTransform(Batch& batch, uint32 paramOffset) {
_transform._invalidProj = true; _transform._invalidProj = true;
} }
void GLBackend::do_setViewportTransform(Batch& batch, uint32 paramOffset) { void GLBackend::do_setViewportTransform(Batch& batch, uint32 paramOffset) {
memcpy(&_transform._viewport, batch.editData(batch._params[paramOffset]._uint), sizeof(Vec4i)); memcpy(&_transform._viewport, batch.editData(batch._params[paramOffset]._uint), sizeof(Vec4i));
_transform._invalidViewport = true; _transform._invalidViewport = true;
} }
void GLBackend::initTransform() { void GLBackend::initTransform() {
#if (GPU_TRANSFORM_PROFILE == GPU_CORE) #if (GPU_TRANSFORM_PROFILE == GPU_CORE)
@ -68,7 +66,7 @@ void GLBackend::syncTransformStateCache() {
_transform._invalidView = true; _transform._invalidView = true;
_transform._invalidModel = true; _transform._invalidModel = true;
glGetIntegerv(GL_VIEWPORT, (GLint*) &_transform._viewport); glGetIntegerv(GL_VIEWPORT, (GLint*) &_transform._viewport);
GLint currentMode; GLint currentMode;
glGetIntegerv(GL_MATRIX_MODE, &currentMode); glGetIntegerv(GL_MATRIX_MODE, &currentMode);
@ -87,11 +85,11 @@ void GLBackend::updateTransform() {
GLint originalMatrixMode; GLint originalMatrixMode;
glGetIntegerv(GL_MATRIX_MODE, &originalMatrixMode); glGetIntegerv(GL_MATRIX_MODE, &originalMatrixMode);
// Check all the dirty flags and update the state accordingly // Check all the dirty flags and update the state accordingly
if (_transform._invalidViewport) { if (_transform._invalidViewport) {
_transform._transformCamera._viewport = glm::vec4(_transform._viewport); _transform._transformCamera._viewport = glm::vec4(_transform._viewport);
// Where we assign the GL viewport // Where we assign the GL viewport
glViewport(_transform._viewport.x, _transform._viewport.y, _transform._viewport.z, _transform._viewport.w); glViewport(_transform._viewport.x, _transform._viewport.y, _transform._viewport.z, _transform._viewport.w);
} }
if (_transform._invalidProj) { if (_transform._invalidProj) {
@ -116,13 +114,13 @@ void GLBackend::updateTransform() {
} }
#if (GPU_TRANSFORM_PROFILE == GPU_CORE) #if (GPU_TRANSFORM_PROFILE == GPU_CORE)
if (_transform._invalidView || _transform._invalidProj || _transform._invalidViewport) { if (_transform._invalidView || _transform._invalidProj || _transform._invalidViewport) {
glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_CAMERA_SLOT, 0); glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_CAMERA_SLOT, 0);
glBindBuffer(GL_ARRAY_BUFFER, _transform._transformCameraBuffer); glBindBuffer(GL_ARRAY_BUFFER, _transform._transformCameraBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(_transform._transformCamera), (const void*) &_transform._transformCamera, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(_transform._transformCamera), (const void*) &_transform._transformCamera, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
CHECK_GL_ERROR(); CHECK_GL_ERROR();
} }
if (_transform._invalidModel) { if (_transform._invalidModel) {
glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_OBJECT_SLOT, 0); glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_OBJECT_SLOT, 0);

View file

@ -15,6 +15,9 @@
using namespace gpu; using namespace gpu;
const Element Element::COLOR_RGBA_32 = Element(VEC4, UINT8, RGBA); const Element Element::COLOR_RGBA_32 = Element(VEC4, UINT8, RGBA);
const Element Element::VEC3F_XYZ = Element(VEC3, FLOAT, XYZ);
const Element Element::INDEX_UINT16 = Element(SCALAR, UINT16, INDEX);
const Element Element::PART_DRAWCALL = Element(VEC4, UINT32, PART);
Resource::Size Resource::Sysmem::allocateMemory(Byte** dataAllocated, Size size) { Resource::Size Resource::Sysmem::allocateMemory(Byte** dataAllocated, Size size) {
if ( !dataAllocated ) { if ( !dataAllocated ) {

View file

@ -0,0 +1,44 @@
//
// StandardShaderLib.cpp
// libraries/gpu/src/gpu
//
// Collection of standard shaders that can be used all over the place
//
// Created by Sam Gateau on 6/22/2015.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "StandardShaderLib.h"
#include "DrawTransformUnitQuad_vert.h"
#include "DrawViewportQuadTransformTexcoord_vert.h"
#include "DrawTexture_frag.h"
using namespace gpu;
ShaderPointer StandardShaderLib::_drawTransformUnitQuadVS;
ShaderPointer StandardShaderLib::_drawViewportQuadTransformTexcoordVS;
ShaderPointer StandardShaderLib::_drawTexturePS;
ShaderPointer StandardShaderLib::getDrawTransformUnitQuadVS() {
if (!_drawTransformUnitQuadVS) {
_drawTransformUnitQuadVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(DrawTransformUnitQuad_vert)));
}
return _drawTransformUnitQuadVS;
}
ShaderPointer StandardShaderLib::getDrawViewportQuadTransformTexcoordVS() {
if (!_drawViewportQuadTransformTexcoordVS) {
_drawViewportQuadTransformTexcoordVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(DrawViewportQuadTransformTexcoord_vert)));
}
return _drawViewportQuadTransformTexcoordVS;
}
ShaderPointer StandardShaderLib::getDrawTexturePS() {
if (!_drawTexturePS) {
_drawTexturePS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(DrawTexture_frag)));
}
return _drawTexturePS;
}

View file

@ -0,0 +1,44 @@
//
// StandardShaderLib.h
// libraries/gpu/src/gpu
//
// Collection of standard shaders that can be used all over the place
//
// Created by Sam Gateau on 6/22/2015.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_gpu_StandardShaderLib_h
#define hifi_gpu_StandardShaderLib_h
#include <assert.h>
#include "Shader.h"
namespace gpu {
class StandardShaderLib {
public:
// Shader draw the unit quad objectPos = ([(-1,-1),(1,1)]) and transform it by the full model transform stack (Model, View, Proj).
// A texcoord attribute is also generated texcoord = [(0,0),(1,1)]
static ShaderPointer getDrawTransformUnitQuadVS();
// Shader draws the unit quad in the full viewport clipPos = ([(-1,-1),(1,1)]) and transform the texcoord = [(0,0),(1,1)] by the model transform.
static ShaderPointer getDrawViewportQuadTransformTexcoordVS();
static ShaderPointer getDrawTexturePS();
protected:
static ShaderPointer _drawTransformUnitQuadVS;
static ShaderPointer _drawViewportQuadTransformTexcoordVS;
static ShaderPointer _drawTexturePS;
};
};
#endif

View file

@ -24,20 +24,20 @@ State::~State() {
// Please make sure to go check makeResetStateCommands() before modifying this value // Please make sure to go check makeResetStateCommands() before modifying this value
const State::Data State::DEFAULT = State::Data(); const State::Data State::DEFAULT = State::Data();
State::Signature State::evalSignature(const Data& state) { State::Signature State::evalSignature(const Data& state) {
Signature signature(0); Signature signature(0);
if (state.fillMode != State::DEFAULT.fillMode) { if (state.fillMode != State::DEFAULT.fillMode) {
signature.set(State::FILL_MODE); signature.set(State::FILL_MODE);
} }
if (state.cullMode != State::DEFAULT.cullMode) { if (state.cullMode != State::DEFAULT.cullMode) {
signature.set(State::CULL_MODE); signature.set(State::CULL_MODE);
} }
if (state.frontFaceClockwise != State::DEFAULT.frontFaceClockwise) { if (state.frontFaceClockwise != State::DEFAULT.frontFaceClockwise) {
signature.set(State::FRONT_FACE_CLOCKWISE); signature.set(State::FRONT_FACE_CLOCKWISE);
} }
if (state.depthClipEnable != State::DEFAULT.depthClipEnable) { if (state.depthClampEnable != State::DEFAULT.depthClampEnable) {
signature.set(State::DEPTH_CLIP_ENABLE); signature.set(State::DEPTH_CLAMP_ENABLE);
} }
if (state.scissorEnable != State::DEFAULT.scissorEnable) { if (state.scissorEnable != State::DEFAULT.scissorEnable) {
signature.set(State::SCISSOR_ENABLE); signature.set(State::SCISSOR_ENABLE);
@ -47,16 +47,16 @@ State::Signature State::evalSignature(const Data& state) {
} }
if (state.antialisedLineEnable != State::DEFAULT.antialisedLineEnable) { if (state.antialisedLineEnable != State::DEFAULT.antialisedLineEnable) {
signature.set(State::ANTIALISED_LINE_ENABLE); signature.set(State::ANTIALISED_LINE_ENABLE);
} }
if (state.depthBias != State::DEFAULT.depthBias) { if (state.depthBias != State::DEFAULT.depthBias) {
signature.set(State::DEPTH_BIAS); signature.set(State::DEPTH_BIAS);
} }
if (state.depthBiasSlopeScale != State::DEFAULT.depthBiasSlopeScale) { if (state.depthBiasSlopeScale != State::DEFAULT.depthBiasSlopeScale) {
signature.set(State::DEPTH_BIAS_SLOPE_SCALE); signature.set(State::DEPTH_BIAS_SLOPE_SCALE);
} }
if (state.depthTest != State::DEFAULT.depthTest) { if (state.depthTest != State::DEFAULT.depthTest) {
signature.set(State::DEPTH_TEST); signature.set(State::DEPTH_TEST);
} }
if (state.stencilActivation != State::DEFAULT.stencilActivation) { if (state.stencilActivation != State::DEFAULT.stencilActivation) {
signature.set(State::STENCIL_ACTIVATION); signature.set(State::STENCIL_ACTIVATION);
} }
@ -68,21 +68,21 @@ State::Signature State::evalSignature(const Data& state) {
} }
if (state.sampleMask != State::DEFAULT.sampleMask) { if (state.sampleMask != State::DEFAULT.sampleMask) {
signature.set(State::SAMPLE_MASK); signature.set(State::SAMPLE_MASK);
} }
if (state.alphaToCoverageEnable != State::DEFAULT.alphaToCoverageEnable) { if (state.alphaToCoverageEnable != State::DEFAULT.alphaToCoverageEnable) {
signature.set(State::ALPHA_TO_COVERAGE_ENABLE); signature.set(State::ALPHA_TO_COVERAGE_ENABLE);
} }
if (state.blendFunction != State::DEFAULT.blendFunction) { if (state.blendFunction != State::DEFAULT.blendFunction) {
signature.set(State::BLEND_FUNCTION); signature.set(State::BLEND_FUNCTION);
} }
if (state.colorWriteMask != State::DEFAULT.colorWriteMask) { if (state.colorWriteMask != State::DEFAULT.colorWriteMask) {
signature.set(State::COLOR_WRITE_MASK); signature.set(State::COLOR_WRITE_MASK);
} }
return signature; return signature;
} }
State::State(const Data& values) : State::State(const Data& values) :
_values(values) { _values(values) {
_signature = evalSignature(_values); _signature = evalSignature(_values);
} }

View file

@ -249,7 +249,7 @@ public:
uint8 colorWriteMask = WRITE_ALL; uint8 colorWriteMask = WRITE_ALL;
bool frontFaceClockwise : 1; bool frontFaceClockwise : 1;
bool depthClipEnable : 1; bool depthClampEnable : 1;
bool scissorEnable : 1; bool scissorEnable : 1;
bool multisampleEnable : 1; bool multisampleEnable : 1;
bool antialisedLineEnable : 1; bool antialisedLineEnable : 1;
@ -257,7 +257,7 @@ public:
Data() : Data() :
frontFaceClockwise(false), frontFaceClockwise(false),
depthClipEnable(false), depthClampEnable(false),
scissorEnable(false), scissorEnable(false),
multisampleEnable(false), multisampleEnable(false),
antialisedLineEnable(false), antialisedLineEnable(false),
@ -276,8 +276,8 @@ public:
void setFrontFaceClockwise(bool isClockwise) { SET_FIELD(FRONT_FACE_CLOCKWISE, DEFAULT.frontFaceClockwise, isClockwise, _values.frontFaceClockwise); } void setFrontFaceClockwise(bool isClockwise) { SET_FIELD(FRONT_FACE_CLOCKWISE, DEFAULT.frontFaceClockwise, isClockwise, _values.frontFaceClockwise); }
bool isFrontFaceClockwise() const { return _values.frontFaceClockwise; } bool isFrontFaceClockwise() const { return _values.frontFaceClockwise; }
void setDepthClipEnable(bool enable) { SET_FIELD(DEPTH_CLIP_ENABLE, DEFAULT.depthClipEnable, enable, _values.depthClipEnable); } void setDepthClampEnable(bool enable) { SET_FIELD(DEPTH_CLAMP_ENABLE, DEFAULT.depthClampEnable, enable, _values.depthClampEnable); }
bool isDepthClipEnable() const { return _values.depthClipEnable; } bool isDepthClampEnable() const { return _values.depthClampEnable; }
void setScissorEnable(bool enable) { SET_FIELD(SCISSOR_ENABLE, DEFAULT.scissorEnable, enable, _values.scissorEnable); } void setScissorEnable(bool enable) { SET_FIELD(SCISSOR_ENABLE, DEFAULT.scissorEnable, enable, _values.scissorEnable); }
bool isScissorEnable() const { return _values.scissorEnable; } bool isScissorEnable() const { return _values.scissorEnable; }
@ -341,6 +341,7 @@ public:
// Color write mask // Color write mask
void setColorWriteMask(uint8 mask) { SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, mask, _values.colorWriteMask); } void setColorWriteMask(uint8 mask) { SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, mask, _values.colorWriteMask); }
void setColorWriteMask(bool red, bool green, bool blue, bool alpha) { uint32 value = ((WRITE_RED * red) | (WRITE_GREEN * green) | (WRITE_BLUE * blue) | (WRITE_ALPHA * alpha)); SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, value, _values.colorWriteMask); }
uint8 getColorWriteMask() const { return _values.colorWriteMask; } uint8 getColorWriteMask() const { return _values.colorWriteMask; }
// All the possible fields // All the possible fields
@ -348,7 +349,7 @@ public:
FILL_MODE, FILL_MODE,
CULL_MODE, CULL_MODE,
FRONT_FACE_CLOCKWISE, FRONT_FACE_CLOCKWISE,
DEPTH_CLIP_ENABLE, DEPTH_CLAMP_ENABLE,
SCISSOR_ENABLE, SCISSOR_ENABLE,
MULTISAMPLE_ENABLE, MULTISAMPLE_ENABLE,
ANTIALISED_LINE_ENABLE, ANTIALISED_LINE_ENABLE,

View file

@ -86,6 +86,7 @@ TransformCamera getTransformCamera() {
return camera; return camera;
} }
uniform mat4 transformObject_model;
uniform mat4 transformCamera_viewInverse; uniform mat4 transformCamera_viewInverse;
uniform vec4 transformCamera_viewport; uniform vec4 transformCamera_viewport;
@ -130,6 +131,16 @@ uniform vec4 transformCamera_viewport;
<@endif@> <@endif@>
<@endfunc@> <@endfunc@>
<@func transformModelToWorldPos(objectTransform, modelPos, worldPos)@>
<@if GPU_TRANSFORM_PROFILE == GPU_CORE@>
{ // transformModelToWorldPos
<$worldPos$> = (<$objectTransform$>._model * <$modelPos$>);
}
<@else@>
<$worldPos$> = (transformObject_model * <$modelPos$>);
<@endif@>
<@endfunc@>
<@func transformModelToEyeDir(cameraTransform, objectTransform, modelDir, eyeDir)@> <@func transformModelToEyeDir(cameraTransform, objectTransform, modelDir, eyeDir)@>
<@if GPU_TRANSFORM_PROFILE == GPU_CORE@> <@if GPU_TRANSFORM_PROFILE == GPU_CORE@>
{ // transformModelToEyeDir { // transformModelToEyeDir

View file

@ -113,6 +113,8 @@ public:
// Generate a BufferStream on the mesh vertices and attributes // Generate a BufferStream on the mesh vertices and attributes
const gpu::BufferStream makeBufferStream() const; const gpu::BufferStream makeBufferStream() const;
static gpu::Primitive topologyToPrimitive(Topology topo) { return static_cast<gpu::Primitive>(topo); }
protected: protected:
gpu::Stream::FormatPointer _vertexFormat; gpu::Stream::FormatPointer _vertexFormat;

View file

@ -77,18 +77,27 @@ void Light::setMaximumRadius(float radius) {
editSchema()._attenuation = Vec4(surfaceRadius, 1.0f/surfaceRadius, CutOffIntensityRatio, radius); editSchema()._attenuation = Vec4(surfaceRadius, 1.0f/surfaceRadius, CutOffIntensityRatio, radius);
} }
#include <math.h>
void Light::setSpotAngle(float angle) { void Light::setSpotAngle(float angle) {
if (angle <= 0.f) { double dangle = angle;
angle = 0.0f; if (dangle <= 0.0) {
dangle = 0.0;
} }
editSchema()._spot.x = cos(angle); if (dangle > glm::half_pi<double>()) {
editSchema()._spot.y = sin(angle); dangle = glm::half_pi<double>();
editSchema()._spot.z = angle; }
auto cosAngle = cos(dangle);
auto sinAngle = sin(dangle);
editSchema()._spot.x = (float) std::abs(cosAngle);
editSchema()._spot.y = (float) std::abs(sinAngle);
editSchema()._spot.z = (float) angle;
} }
void Light::setSpotExponent(float exponent) { void Light::setSpotExponent(float exponent) {
if (exponent <= 0.f) { if (exponent <= 0.f) {
exponent = 1.0f; exponent = 0.0f;
} }
editSchema()._spot.w = exponent; editSchema()._spot.w = exponent;
} }

View file

@ -81,6 +81,7 @@ public:
bool isSpot() const { return getType() == SPOT; } bool isSpot() const { return getType() == SPOT; }
void setSpotAngle(float angle); void setSpotAngle(float angle);
float getSpotAngle() const { return getSchema()._spot.z; } float getSpotAngle() const { return getSchema()._spot.z; }
glm::vec2 getSpotAngleCosSin() const { return glm::vec2(getSchema()._spot.x, getSchema()._spot.y); }
void setSpotExponent(float exponent); void setSpotExponent(float exponent);
float getSpotExponent() const { return getSchema()._spot.w; } float getSpotExponent() const { return getSchema()._spot.w; }
@ -107,7 +108,7 @@ public:
Color _color{1.0f}; Color _color{1.0f};
float _intensity{1.0f}; float _intensity{1.0f};
Vec4 _attenuation{1.0f}; Vec4 _attenuation{1.0f};
Vec4 _spot{0.0f, 0.0f, 0.0f, 3.0f}; Vec4 _spot{0.0f, 0.0f, 0.0f, 0.0f};
Vec4 _shadow{0.0f}; Vec4 _shadow{0.0f};
Vec4 _control{0.0f, 0.0f, 0.0f, 0.0f}; Vec4 _control{0.0f, 0.0f, 0.0f, 0.0f};

View file

@ -51,7 +51,7 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky
static gpu::BufferPointer theBuffer; static gpu::BufferPointer theBuffer;
static gpu::Stream::FormatPointer theFormat; static gpu::Stream::FormatPointer theFormat;
static gpu::BufferPointer theConstants; static gpu::BufferPointer theConstants;
int SKYBOX_CONSTANTS_SLOT = 0; // need to be defined by the compilation of the shader static int SKYBOX_CONSTANTS_SLOT = 0; // need to be defined by the compilation of the shader
if (!thePipeline) { if (!thePipeline) {
auto skyVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(Skybox_vert))); auto skyVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(Skybox_vert)));
auto skyFS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(Skybox_frag))); auto skyFS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(Skybox_frag)));

View file

@ -25,6 +25,7 @@
#include "gpu/Batch.h" #include "gpu/Batch.h"
#include "gpu/GLBackend.h" #include "gpu/GLBackend.h"
#include "gpu/StandardShaderLib.h"
#include "simple_vert.h" #include "simple_vert.h"
#include "simple_textured_frag.h" #include "simple_textured_frag.h"
@ -32,6 +33,7 @@
#include "deferred_light_vert.h" #include "deferred_light_vert.h"
#include "deferred_light_limited_vert.h" #include "deferred_light_limited_vert.h"
#include "deferred_light_spot_vert.h"
#include "directional_light_frag.h" #include "directional_light_frag.h"
#include "directional_light_shadow_map_frag.h" #include "directional_light_shadow_map_frag.h"
@ -90,26 +92,40 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) {
gpu::Shader::makeProgram(*_emissiveShader, slotBindings); gpu::Shader::makeProgram(*_emissiveShader, slotBindings);
_viewState = viewState; _viewState = viewState;
loadLightProgram(directional_light_frag, false, _directionalLight, _directionalLightLocations); loadLightProgram(deferred_light_vert, directional_light_frag, false, _directionalLight, _directionalLightLocations);
loadLightProgram(directional_light_shadow_map_frag, false, _directionalLightShadowMap, loadLightProgram(deferred_light_vert, directional_light_shadow_map_frag, false, _directionalLightShadowMap,
_directionalLightShadowMapLocations); _directionalLightShadowMapLocations);
loadLightProgram(directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap, loadLightProgram(deferred_light_vert, directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap,
_directionalLightCascadedShadowMapLocations); _directionalLightCascadedShadowMapLocations);
loadLightProgram(directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations); loadLightProgram(deferred_light_vert, directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations);
loadLightProgram(directional_ambient_light_shadow_map_frag, false, _directionalAmbientSphereLightShadowMap, loadLightProgram(deferred_light_vert, directional_ambient_light_shadow_map_frag, false, _directionalAmbientSphereLightShadowMap,
_directionalAmbientSphereLightShadowMapLocations); _directionalAmbientSphereLightShadowMapLocations);
loadLightProgram(directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap, loadLightProgram(deferred_light_vert, directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap,
_directionalAmbientSphereLightCascadedShadowMapLocations); _directionalAmbientSphereLightCascadedShadowMapLocations);
loadLightProgram(directional_skybox_light_frag, false, _directionalSkyboxLight, _directionalSkyboxLightLocations); loadLightProgram(deferred_light_vert, directional_skybox_light_frag, false, _directionalSkyboxLight, _directionalSkyboxLightLocations);
loadLightProgram(directional_skybox_light_shadow_map_frag, false, _directionalSkyboxLightShadowMap, loadLightProgram(deferred_light_vert, directional_skybox_light_shadow_map_frag, false, _directionalSkyboxLightShadowMap,
_directionalSkyboxLightShadowMapLocations); _directionalSkyboxLightShadowMapLocations);
loadLightProgram(directional_skybox_light_cascaded_shadow_map_frag, false, _directionalSkyboxLightCascadedShadowMap, loadLightProgram(deferred_light_vert, directional_skybox_light_cascaded_shadow_map_frag, false, _directionalSkyboxLightCascadedShadowMap,
_directionalSkyboxLightCascadedShadowMapLocations); _directionalSkyboxLightCascadedShadowMapLocations);
loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations);
loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations); loadLightProgram(deferred_light_limited_vert, point_light_frag, true, _pointLight, _pointLightLocations);
loadLightProgram(deferred_light_spot_vert, spot_light_frag, true, _spotLight, _spotLightLocations);
{
auto VSFS = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS();
auto PSBlit = gpu::StandardShaderLib::getDrawTexturePS();
auto blitProgram = gpu::ShaderPointer(gpu::Shader::createProgram(VSFS, PSBlit));
gpu::Shader::makeProgram(*blitProgram);
gpu::StatePointer blitState = gpu::StatePointer(new gpu::State());
blitState->setBlendFunction(true,
gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA,
gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
blitState->setColorWriteMask(true, true, true, false);
_blitLightBuffer = gpu::PipelinePointer(gpu::Pipeline::create(blitProgram, blitState));
}
// Allocate a global light representing the Global Directional light casting shadow (the sun) and the ambient light // Allocate a global light representing the Global Directional light casting shadow (the sun) and the ambient light
_globalLights.push_back(0); _globalLights.push_back(0);
@ -201,51 +217,40 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu
} }
void DeferredLightingEffect::prepare(RenderArgs* args) { void DeferredLightingEffect::prepare(RenderArgs* args) {
// clear the normal and specular buffers
auto textureCache = DependencyManager::get<TextureCache>(); auto textureCache = DependencyManager::get<TextureCache>();
textureCache->setPrimaryDrawBuffers(false, true, false); gpu::Batch batch;
glClear(GL_COLOR_BUFFER_BIT);
textureCache->setPrimaryDrawBuffers(false, false, true); // clear the normal and specular buffers
// clearing to zero alpha for specular causes problems on my Nvidia card; clear to lowest non-zero value instead batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLOR1, glm::vec4(0.0f, 0.0f, 0.0f, 0.0f));
const float MAX_SPECULAR_EXPONENT = 128.0f; const float MAX_SPECULAR_EXPONENT = 128.0f;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f / MAX_SPECULAR_EXPONENT); batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLOR2, glm::vec4(0.0f, 0.0f, 0.0f, 1.0f / MAX_SPECULAR_EXPONENT));
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); args->_context->syncCache();
textureCache->setPrimaryDrawBuffers(true, false, false); args->_context->render(batch);
} }
void DeferredLightingEffect::render(RenderArgs* args) { void DeferredLightingEffect::render(RenderArgs* args) {
// perform deferred lighting, rendering to free fbo gpu::Batch batch;
glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glDepthMask(false);
// perform deferred lighting, rendering to free fbo
auto textureCache = DependencyManager::get<TextureCache>(); auto textureCache = DependencyManager::get<TextureCache>();
glBindFramebuffer(GL_FRAMEBUFFER, 0 );
QSize framebufferSize = textureCache->getFrameBufferSize(); QSize framebufferSize = textureCache->getFrameBufferSize();
// binding the first framebuffer // binding the first framebuffer
auto freeFBO = DependencyManager::get<GlowEffect>()->getFreeFramebuffer(); auto freeFBO = DependencyManager::get<GlowEffect>()->getFreeFramebuffer();
glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(freeFBO)); batch.setFramebuffer(freeFBO);
glClear(GL_COLOR_BUFFER_BIT); batch.clearColorFramebuffer(freeFBO->getBufferMask(), glm::vec4(0.0f, 0.0f, 0.0f, 0.0f));
// glEnable(GL_FRAMEBUFFER_SRGB);
batch.setUniformTexture(0, textureCache->getPrimaryColorTexture());
// glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); batch.setUniformTexture(1, textureCache->getPrimaryNormalTexture());
glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryColorTextureID());
glActiveTexture(GL_TEXTURE1); batch.setUniformTexture(2, textureCache->getPrimarySpecularTexture());
glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryNormalTextureID());
glActiveTexture(GL_TEXTURE2); batch.setUniformTexture(3, textureCache->getPrimaryDepthTexture());
glBindTexture(GL_TEXTURE_2D, textureCache->getPrimarySpecularTextureID());
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryDepthTextureID());
// get the viewport side (left, right, both) // get the viewport side (left, right, both)
int viewport[4]; int viewport[4];
@ -266,53 +271,51 @@ void DeferredLightingEffect::render(RenderArgs* args) {
glm::mat4 invViewMat; glm::mat4 invViewMat;
_viewState->getViewTransform().getMatrix(invViewMat); _viewState->getViewTransform().getMatrix(invViewMat);
ProgramObject* program = &_directionalLight; auto& program = _directionalLight;
const LightLocations* locations = &_directionalLightLocations; const LightLocations* locations = &_directionalLightLocations;
bool shadowsEnabled = _viewState->getShadowsEnabled(); bool shadowsEnabled = _viewState->getShadowsEnabled();
if (shadowsEnabled) { if (shadowsEnabled) {
glActiveTexture(GL_TEXTURE4); batch.setUniformTexture(4, textureCache->getShadowFramebuffer()->getDepthStencilBuffer());
glBindTexture(GL_TEXTURE_2D, textureCache->getShadowDepthTextureID());
program = &_directionalLightShadowMap; program = _directionalLightShadowMap;
locations = &_directionalLightShadowMapLocations; locations = &_directionalLightShadowMapLocations;
if (_viewState->getCascadeShadowsEnabled()) { if (_viewState->getCascadeShadowsEnabled()) {
program = &_directionalLightCascadedShadowMap; program = _directionalLightCascadedShadowMap;
locations = &_directionalLightCascadedShadowMapLocations; locations = &_directionalLightCascadedShadowMapLocations;
if (useSkyboxCubemap) { if (useSkyboxCubemap) {
program = &_directionalSkyboxLightCascadedShadowMap; program = _directionalSkyboxLightCascadedShadowMap;
locations = &_directionalSkyboxLightCascadedShadowMapLocations; locations = &_directionalSkyboxLightCascadedShadowMapLocations;
} else if (_ambientLightMode > -1) { } else if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLightCascadedShadowMap; program = _directionalAmbientSphereLightCascadedShadowMap;
locations = &_directionalAmbientSphereLightCascadedShadowMapLocations; locations = &_directionalAmbientSphereLightCascadedShadowMapLocations;
} }
program->bind(); batch.setPipeline(program);
program->setUniform(locations->shadowDistances, _viewState->getShadowDistances()); batch._glUniform3fv(locations->shadowDistances, 1, (const GLfloat*) &_viewState->getShadowDistances());
} else { } else {
if (useSkyboxCubemap) { if (useSkyboxCubemap) {
program = &_directionalSkyboxLightShadowMap; program = _directionalSkyboxLightShadowMap;
locations = &_directionalSkyboxLightShadowMapLocations; locations = &_directionalSkyboxLightShadowMapLocations;
} else if (_ambientLightMode > -1) { } else if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLightShadowMap; program = _directionalAmbientSphereLightShadowMap;
locations = &_directionalAmbientSphereLightShadowMapLocations; locations = &_directionalAmbientSphereLightShadowMapLocations;
} }
program->bind(); batch.setPipeline(program);
} }
program->setUniformValue(locations->shadowScale, batch._glUniform1f(locations->shadowScale, 1.0f / textureCache->getShadowFramebuffer()->getWidth());
1.0f / textureCache->getShadowFramebuffer()->getWidth());
} else { } else {
if (useSkyboxCubemap) { if (useSkyboxCubemap) {
program = &_directionalSkyboxLight; program = _directionalSkyboxLight;
locations = &_directionalSkyboxLightLocations; locations = &_directionalSkyboxLightLocations;
} else if (_ambientLightMode > -1) { } else if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLight; program = _directionalAmbientSphereLight;
locations = &_directionalAmbientSphereLightLocations; locations = &_directionalAmbientSphereLightLocations;
} }
program->bind(); batch.setPipeline(program);
} }
{ { // Setup the global lighting
auto globalLight = _allocatedLights[_globalLights.front()]; auto globalLight = _allocatedLights[_globalLights.front()];
if (locations->ambientSphere >= 0) { if (locations->ambientSphere >= 0) {
@ -321,70 +324,74 @@ void DeferredLightingEffect::render(RenderArgs* args) {
sh = (*_skybox->getCubemap()->getIrradiance()); sh = (*_skybox->getCubemap()->getIrradiance());
} }
for (int i =0; i <gpu::SphericalHarmonics::NUM_COEFFICIENTS; i++) { for (int i =0; i <gpu::SphericalHarmonics::NUM_COEFFICIENTS; i++) {
program->setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i)); batch._glUniform4fv(locations->ambientSphere + i, 1, (const GLfloat*) (&sh) + i * 4);
} }
} }
if (useSkyboxCubemap) { if (useSkyboxCubemap) {
glActiveTexture(GL_TEXTURE5); batch.setUniformTexture(5, _skybox->getCubemap());
glBindTexture(GL_TEXTURE_CUBE_MAP, gpu::GLBackend::getTextureID(_skybox->getCubemap()));
} }
if (locations->lightBufferUnit >= 0) { if (locations->lightBufferUnit >= 0) {
gpu::Batch batch;
batch.setUniformBuffer(locations->lightBufferUnit, globalLight->getSchemaBuffer()); batch.setUniformBuffer(locations->lightBufferUnit, globalLight->getSchemaBuffer());
gpu::GLBackend::renderBatch(batch);
} }
if (_atmosphere && (locations->atmosphereBufferUnit >= 0)) { if (_atmosphere && (locations->atmosphereBufferUnit >= 0)) {
gpu::Batch batch;
batch.setUniformBuffer(locations->atmosphereBufferUnit, _atmosphere->getDataBuffer()); batch.setUniformBuffer(locations->atmosphereBufferUnit, _atmosphere->getDataBuffer());
gpu::GLBackend::renderBatch(batch);
} }
glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); batch._glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
} }
float left, right, bottom, top, nearVal, farVal; float left, right, bottom, top, nearVal, farVal;
glm::vec4 nearClipPlane, farClipPlane; glm::vec4 nearClipPlane, farClipPlane;
_viewState->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); _viewState->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane);
program->setUniformValue(locations->nearLocation, nearVal); batch._glUniform1f(locations->nearLocation, nearVal);
float depthScale = (farVal - nearVal) / farVal; float depthScale = (farVal - nearVal) / farVal;
program->setUniformValue(locations->depthScale, depthScale); batch._glUniform1f(locations->depthScale, depthScale);
float nearScale = -1.0f / nearVal; float nearScale = -1.0f / nearVal;
float depthTexCoordScaleS = (right - left) * nearScale / sWidth; float depthTexCoordScaleS = (right - left) * nearScale / sWidth;
float depthTexCoordScaleT = (top - bottom) * nearScale / tHeight; float depthTexCoordScaleT = (top - bottom) * nearScale / tHeight;
float depthTexCoordOffsetS = left * nearScale - sMin * depthTexCoordScaleS; float depthTexCoordOffsetS = left * nearScale - sMin * depthTexCoordScaleS;
float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT; float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT;
program->setUniformValue(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); batch._glUniform2f(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT);
program->setUniformValue(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); batch._glUniform2f(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT);
renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight); {
Transform model;
program->release(); model.setTranslation(glm::vec3(sMin, tMin, 0.0));
model.setScale(glm::vec3(sWidth, tHeight, 1.0));
batch.setModelTransform(model);
batch.setProjectionTransform(glm::mat4());
batch.setViewTransform(Transform());
glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
glm::vec2 topLeft(-1.0f, -1.0f);
glm::vec2 bottomRight(1.0f, 1.0f);
glm::vec2 texCoordTopLeft(sMin, tMin);
glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight);
DependencyManager::get<GeometryCache>()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color);
}
if (useSkyboxCubemap) { if (useSkyboxCubemap) {
glBindTexture(GL_TEXTURE_CUBE_MAP, 0); batch.setUniformTexture(5, nullptr);
if (!shadowsEnabled) {
glActiveTexture(GL_TEXTURE3);
}
} }
if (shadowsEnabled) { if (shadowsEnabled) {
glBindTexture(GL_TEXTURE_2D, 0); batch.setUniformTexture(4, nullptr);
glActiveTexture(GL_TEXTURE3);
} }
// additive blending
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_CULL_FACE);
glm::vec4 sCoefficients(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f); glm::vec4 sCoefficients(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f);
glm::vec4 tCoefficients(0.0f, tHeight / 2.0f, 0.0f, tMin + tHeight / 2.0f); glm::vec4 tCoefficients(0.0f, tHeight / 2.0f, 0.0f, tMin + tHeight / 2.0f);
glTexGenfv(GL_S, GL_OBJECT_PLANE, (const GLfloat*)&sCoefficients); auto texcoordMat = glm::mat4();
glTexGenfv(GL_T, GL_OBJECT_PLANE, (const GLfloat*)&tCoefficients); texcoordMat[0] = glm::vec4(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f);
texcoordMat[1] = glm::vec4(0.0f, tHeight / 2.0f, 0.0f, tMin + tHeight / 2.0f);
texcoordMat[2] = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f);
texcoordMat[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
// enlarge the scales slightly to account for tesselation // enlarge the scales slightly to account for tesselation
const float SCALE_EXPANSION = 0.05f; const float SCALE_EXPANSION = 0.05f;
@ -393,148 +400,162 @@ void DeferredLightingEffect::render(RenderArgs* args) {
auto geometryCache = DependencyManager::get<GeometryCache>(); auto geometryCache = DependencyManager::get<GeometryCache>();
glm::mat4 projMat;
Transform viewMat;
args->_viewFrustum->evalProjectionMatrix(projMat);
args->_viewFrustum->evalViewTransform(viewMat);
if (args->_renderMode == RenderArgs::MIRROR_RENDER_MODE) {
viewMat.postScale(glm::vec3(-1.0f, 1.0f, 1.0f));
}
batch.setProjectionTransform(projMat);
batch.setViewTransform(viewMat);
if (!_pointLights.empty()) { if (!_pointLights.empty()) {
_pointLight.bind(); batch.setPipeline(_pointLight);
_pointLight.setUniformValue(_pointLightLocations.nearLocation, nearVal); batch._glUniform1f(_pointLightLocations.nearLocation, nearVal);
_pointLight.setUniformValue(_pointLightLocations.depthScale, depthScale); batch._glUniform1f(_pointLightLocations.depthScale, depthScale);
_pointLight.setUniformValue(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); batch._glUniform2f(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT);
_pointLight.setUniformValue(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); batch._glUniform2f(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT);
batch._glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
batch._glUniformMatrix4fv(_pointLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat));
for (auto lightID : _pointLights) { for (auto lightID : _pointLights) {
auto light = _allocatedLights[lightID]; auto& light = _allocatedLights[lightID];
// IN DEBUG: light->setShowContour(true);
if (_pointLightLocations.lightBufferUnit >= 0) { if (_pointLightLocations.lightBufferUnit >= 0) {
gpu::Batch batch;
batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer());
gpu::GLBackend::renderBatch(batch);
} }
glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
glPushMatrix();
float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION);
// TODO: We shouldn;t have to do that test and use a different volume geometry for when inside the vlight volume,
// we should be able to draw thre same geometry use DepthClamp but for unknown reason it's s not working...
if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) {
glLoadIdentity(); Transform model;
glTranslatef(0.0f, 0.0f, -1.0f); model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f));
batch.setModelTransform(model);
glMatrixMode(GL_PROJECTION); batch.setViewTransform(Transform());
glPushMatrix(); batch.setProjectionTransform(glm::mat4());
glLoadIdentity();
glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
renderFullscreenQuad(); glm::vec2 topLeft(-1.0f, -1.0f);
glm::vec2 bottomRight(1.0f, 1.0f);
glPopMatrix(); glm::vec2 texCoordTopLeft(sMin, tMin);
glMatrixMode(GL_MODELVIEW); glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight);
DependencyManager::get<GeometryCache>()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color);
batch.setProjectionTransform(projMat);
batch.setViewTransform(viewMat);
} else { } else {
glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); Transform model;
geometryCache->renderSphere(expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z));
batch.setModelTransform(model);
geometryCache->renderSphere(batch, expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
} }
glPopMatrix();
} }
_pointLights.clear(); _pointLights.clear();
_pointLight.release();
} }
if (!_spotLights.empty()) { if (!_spotLights.empty()) {
_spotLight.bind(); batch.setPipeline(_spotLight);
_spotLight.setUniformValue(_spotLightLocations.nearLocation, nearVal); batch._glUniform1f(_spotLightLocations.nearLocation, nearVal);
_spotLight.setUniformValue(_spotLightLocations.depthScale, depthScale); batch._glUniform1f(_spotLightLocations.depthScale, depthScale);
_spotLight.setUniformValue(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); batch._glUniform2f(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT);
_spotLight.setUniformValue(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); batch._glUniform2f(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT);
batch._glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
batch._glUniformMatrix4fv(_spotLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat));
for (auto lightID : _spotLights) { for (auto lightID : _spotLights) {
auto light = _allocatedLights[lightID]; auto light = _allocatedLights[lightID];
// IN DEBUG: light->setShowContour(true);
if (_spotLightLocations.lightBufferUnit >= 0) { batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer());
gpu::Batch batch;
batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer());
gpu::GLBackend::renderBatch(batch);
}
glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
glPushMatrix();
auto eyeLightPos = eyePoint - light->getPosition();
auto eyeHalfPlaneDistance = glm::dot(eyeLightPos, light->getDirection());
const float TANGENT_LENGTH_SCALE = 0.666f;
glm::vec4 coneParam(light->getSpotAngleCosSin(), TANGENT_LENGTH_SCALE * tan(0.5 * light->getSpotAngle()), 1.0f);
float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION);
float edgeRadius = expandedRadius / glm::cos(light->getSpotAngle()); // TODO: We shouldn;t have to do that test and use a different volume geometry for when inside the vlight volume,
if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < edgeRadius + nearRadius) { // we should be able to draw thre same geometry use DepthClamp but for unknown reason it's s not working...
glLoadIdentity(); if ((eyeHalfPlaneDistance > -nearRadius) && (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius)) {
glTranslatef(0.0f, 0.0f, -1.0f); coneParam.w = 0.0f;
batch._glUniform4fv(_spotLightLocations.coneParam, 1, reinterpret_cast< const GLfloat* >(&coneParam));
Transform model;
model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f));
batch.setModelTransform(model);
batch.setViewTransform(Transform());
batch.setProjectionTransform(glm::mat4());
glMatrixMode(GL_PROJECTION); glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
glPushMatrix(); glm::vec2 topLeft(-1.0f, -1.0f);
glLoadIdentity(); glm::vec2 bottomRight(1.0f, 1.0f);
glm::vec2 texCoordTopLeft(sMin, tMin);
renderFullscreenQuad(); glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight);
glPopMatrix(); DependencyManager::get<GeometryCache>()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color);
glMatrixMode(GL_MODELVIEW);
batch.setProjectionTransform(projMat);
batch.setViewTransform(viewMat);
} else { } else {
glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); coneParam.w = 1.0f;
glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); batch._glUniform4fv(_spotLightLocations.coneParam, 1, reinterpret_cast< const GLfloat* >(&coneParam));
glm::vec3 axis = glm::axis(spotRotation);
glRotatef(glm::degrees(glm::angle(spotRotation)), axis.x, axis.y, axis.z); Transform model;
glTranslatef(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f)); model.setTranslation(light->getPosition());
geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotAngle()), model.postRotate(light->getOrientation());
expandedRadius, 32, 1); model.postScale(glm::vec3(expandedRadius, expandedRadius, expandedRadius));
batch.setModelTransform(model);
auto mesh = getSpotLightMesh();
batch.setIndexBuffer(mesh->getIndexBuffer());
batch.setInputBuffer(0, mesh->getVertexBuffer());
batch.setInputFormat(mesh->getVertexFormat());
auto& part = mesh->getPartBuffer().get<model::Mesh::Part>();
batch.drawIndexed(model::Mesh::topologyToPrimitive(part._topology), part._numIndices, part._startIndex);
} }
glPopMatrix();
} }
_spotLights.clear(); _spotLights.clear();
_spotLight.release();
} }
glBindTexture(GL_TEXTURE_2D, 0); // Probably not necessary in the long run because the gpu layer would unbound this texture if used as render target
batch.setUniformTexture(0, nullptr);
glActiveTexture(GL_TEXTURE2); batch.setUniformTexture(1, nullptr);
glBindTexture(GL_TEXTURE_2D, 0); batch.setUniformTexture(2, nullptr);
batch.setUniformTexture(3, nullptr);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0); args->_context->syncCache();
args->_context->render(batch);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
// glDisable(GL_FRAMEBUFFER_SRGB);
// End of the Lighting pass // End of the Lighting pass
} }
void DeferredLightingEffect::copyBack(RenderArgs* args) { void DeferredLightingEffect::copyBack(RenderArgs* args) {
gpu::Batch batch;
auto textureCache = DependencyManager::get<TextureCache>(); auto textureCache = DependencyManager::get<TextureCache>();
QSize framebufferSize = textureCache->getFrameBufferSize(); QSize framebufferSize = textureCache->getFrameBufferSize();
auto freeFBO = DependencyManager::get<GlowEffect>()->getFreeFramebuffer(); auto freeFBO = DependencyManager::get<GlowEffect>()->getFreeFramebuffer();
//freeFBO->release(); batch.setFramebuffer(textureCache->getPrimaryFramebuffer());
glBindFramebuffer(GL_FRAMEBUFFER, 0); batch.setPipeline(_blitLightBuffer);
batch.setUniformTexture(0, freeFBO->getRenderBuffer(0));
batch.setProjectionTransform(glm::mat4());
glDisable(GL_CULL_FACE); batch.setViewTransform(Transform());
// now transfer the lit region to the primary fbo
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);
glColorMask(true, true, true, false);
auto primaryFBO = gpu::GLBackend::getFramebufferID(textureCache->getPrimaryFramebuffer());
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, primaryFBO);
//primaryFBO->bind();
glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(freeFBO->getRenderBuffer(0)));
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
int viewport[4]; int viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport); glGetIntegerv(GL_VIEWPORT, viewport);
const int VIEWPORT_X_INDEX = 0; const int VIEWPORT_X_INDEX = 0;
@ -547,21 +568,19 @@ void DeferredLightingEffect::copyBack(RenderArgs* args) {
float tMin = viewport[VIEWPORT_Y_INDEX] / (float)framebufferSize.height(); float tMin = viewport[VIEWPORT_Y_INDEX] / (float)framebufferSize.height();
float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)framebufferSize.height(); float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)framebufferSize.height();
renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight); Transform model;
model.setTranslation(glm::vec3(sMin, tMin, 0.0));
glBindTexture(GL_TEXTURE_2D, 0); model.setScale(glm::vec3(sWidth, tHeight, 1.0));
glDisable(GL_TEXTURE_2D); batch.setModelTransform(model);
glColorMask(true, true, true, true);
glEnable(GL_LIGHTING); batch.setViewportTransform(glm::ivec4(viewport[0], viewport[1], viewport[2], viewport[3]));
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST); batch.draw(gpu::TRIANGLE_STRIP, 4);
glDepthMask(true);
glPopMatrix(); args->_context->syncCache();
args->_context->render(batch);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
} }
void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferUnit) { void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferUnit) {
@ -569,67 +588,62 @@ void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferU
args->_batch->setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer()); args->_batch->setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer());
} }
void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations) { void DeferredLightingEffect::loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocations& locations) {
program.addShaderFromSourceCode(QGLShader::Vertex, (limited ? deferred_light_limited_vert : deferred_light_vert)); auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(vertSource)));
program.addShaderFromSourceCode(QGLShader::Fragment, fragSource); auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(fragSource)));
program.link();
program.bind(); gpu::ShaderPointer program = gpu::ShaderPointer(gpu::Shader::createProgram(VS, PS));
program.setUniformValue("diffuseMap", 0);
program.setUniformValue("normalMap", 1);
program.setUniformValue("specularMap", 2);
program.setUniformValue("depthMap", 3);
program.setUniformValue("shadowMap", 4);
program.setUniformValue("skyboxMap", 5);
locations.shadowDistances = program.uniformLocation("shadowDistances");
locations.shadowScale = program.uniformLocation("shadowScale");
locations.nearLocation = program.uniformLocation("near");
locations.depthScale = program.uniformLocation("depthScale");
locations.depthTexCoordOffset = program.uniformLocation("depthTexCoordOffset");
locations.depthTexCoordScale = program.uniformLocation("depthTexCoordScale");
locations.radius = program.uniformLocation("radius");
locations.ambientSphere = program.uniformLocation("ambientSphere.L00");
locations.invViewMat = program.uniformLocation("invViewMat");
GLint loc = -1; gpu::Shader::BindingSet slotBindings;
slotBindings.insert(gpu::Shader::Binding(std::string("diffuseMap"), 0));
#if (GPU_FEATURE_PROFILE == GPU_CORE) slotBindings.insert(gpu::Shader::Binding(std::string("normalMap"), 1));
slotBindings.insert(gpu::Shader::Binding(std::string("specularMap"), 2));
slotBindings.insert(gpu::Shader::Binding(std::string("depthMap"), 3));
slotBindings.insert(gpu::Shader::Binding(std::string("shadowMap"), 4));
slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), 5));
const GLint LIGHT_GPU_SLOT = 3; const GLint LIGHT_GPU_SLOT = 3;
loc = glGetUniformBlockIndex(program.programId(), "lightBuffer"); slotBindings.insert(gpu::Shader::Binding(std::string("lightBuffer"), LIGHT_GPU_SLOT));
if (loc >= 0) { const GLint ATMOSPHERE_GPU_SLOT = 4;
glUniformBlockBinding(program.programId(), loc, LIGHT_GPU_SLOT); slotBindings.insert(gpu::Shader::Binding(std::string("atmosphereBufferUnit"), ATMOSPHERE_GPU_SLOT));
locations.lightBufferUnit = LIGHT_GPU_SLOT;
} else { gpu::Shader::makeProgram(*program, slotBindings);
locations.lightBufferUnit = -1;
} locations.shadowDistances = program->getUniforms().findLocation("shadowDistances");
#else locations.shadowScale = program->getUniforms().findLocation("shadowScale");
loc = program.uniformLocation("lightBuffer"); locations.nearLocation = program->getUniforms().findLocation("near");
if (loc >= 0) { locations.depthScale = program->getUniforms().findLocation("depthScale");
locations.lightBufferUnit = loc; locations.depthTexCoordOffset = program->getUniforms().findLocation("depthTexCoordOffset");
} else { locations.depthTexCoordScale = program->getUniforms().findLocation("depthTexCoordScale");
locations.lightBufferUnit = -1; locations.radius = program->getUniforms().findLocation("radius");
} locations.ambientSphere = program->getUniforms().findLocation("ambientSphere.L00");
#endif locations.invViewMat = program->getUniforms().findLocation("invViewMat");
locations.texcoordMat = program->getUniforms().findLocation("texcoordMat");
locations.coneParam = program->getUniforms().findLocation("coneParam");
#if (GPU_FEATURE_PROFILE == GPU_CORE) #if (GPU_FEATURE_PROFILE == GPU_CORE)
const GLint ATMOSPHERE_GPU_SLOT = 4; locations.lightBufferUnit = program->getBuffers().findLocation("lightBuffer");
loc = glGetUniformBlockIndex(program.programId(), "atmosphereBufferUnit"); locations.atmosphereBufferUnit = program->getBuffers().findLocation("atmosphereBufferUnit");
if (loc >= 0) {
glUniformBlockBinding(program.programId(), loc, ATMOSPHERE_GPU_SLOT);
locations.atmosphereBufferUnit = ATMOSPHERE_GPU_SLOT;
} else {
locations.atmosphereBufferUnit = -1;
}
#else #else
loc = program.uniformLocation("atmosphereBufferUnit"); locations.lightBufferUnit = program->getUniforms().findLocation("lightBuffer");
if (loc >= 0) { locations.atmosphereBufferUnit = program->getUniforms().findLocation("atmosphereBufferUnit");
locations.atmosphereBufferUnit = loc;
} else {
locations.atmosphereBufferUnit = -1;
}
#endif #endif
program.release(); gpu::StatePointer state = gpu::StatePointer(new gpu::State());
if (lightVolume) {
state->setCullMode(gpu::State::CULL_BACK);
// No need for z test since the depth buffer is not bound state->setDepthTest(true, false, gpu::LESS_EQUAL);
// TODO: We should bind the true depth buffer both as RT and texture for the depth test
// TODO: We should use DepthClamp and avoid changing geometry for inside /outside cases
state->setDepthClampEnable(true);
// additive blending
state->setBlendFunction(true, gpu::State::ONE, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
} else {
state->setCullMode(gpu::State::CULL_BACK);
}
pipeline.reset(gpu::Pipeline::create(program, state));
} }
void DeferredLightingEffect::setAmbientLightMode(int preset) { void DeferredLightingEffect::setAmbientLightMode(int preset) {
@ -654,3 +668,100 @@ void DeferredLightingEffect::setGlobalLight(const glm::vec3& direction, const gl
void DeferredLightingEffect::setGlobalSkybox(const model::SkyboxPointer& skybox) { void DeferredLightingEffect::setGlobalSkybox(const model::SkyboxPointer& skybox) {
_skybox = skybox; _skybox = skybox;
} }
model::MeshPointer DeferredLightingEffect::getSpotLightMesh() {
if (!_spotLightMesh) {
_spotLightMesh.reset(new model::Mesh());
int slices = 32;
int rings = 3;
int vertices = 2 + rings * slices;
int originVertex = vertices - 2;
int capVertex = vertices - 1;
int verticesSize = vertices * 3 * sizeof(float);
int indices = 3 * slices * (1 + 1 + 2 * (rings -1));
int ringFloatOffset = slices * 3;
GLfloat* vertexData = new GLfloat[verticesSize];
GLfloat* vertexRing0 = vertexData;
GLfloat* vertexRing1 = vertexRing0 + ringFloatOffset;
GLfloat* vertexRing2 = vertexRing1 + ringFloatOffset;
for (int i = 0; i < slices; i++) {
float theta = TWO_PI * i / slices;
auto cosin = glm::vec2(cosf(theta), sinf(theta));
*(vertexRing0++) = cosin.x;
*(vertexRing0++) = cosin.y;
*(vertexRing0++) = 0.0f;
*(vertexRing1++) = cosin.x;
*(vertexRing1++) = cosin.y;
*(vertexRing1++) = 0.33f;
*(vertexRing2++) = cosin.x;
*(vertexRing2++) = cosin.y;
*(vertexRing2++) = 0.66f;
}
*(vertexRing2++) = 0.0f;
*(vertexRing2++) = 0.0f;
*(vertexRing2++) = -1.0f;
*(vertexRing2++) = 0.0f;
*(vertexRing2++) = 0.0f;
*(vertexRing2++) = 1.0f;
_spotLightMesh->setVertexBuffer(gpu::BufferView(new gpu::Buffer(verticesSize, (gpu::Byte*) vertexData), gpu::Element::VEC3F_XYZ));
delete[] vertexData;
gpu::uint16* indexData = new gpu::uint16[indices];
gpu::uint16* index = indexData;
for (int i = 0; i < slices; i++) {
*(index++) = originVertex;
int s0 = i;
int s1 = ((i + 1) % slices);
*(index++) = s0;
*(index++) = s1;
int s2 = s0 + slices;
int s3 = s1 + slices;
*(index++) = s1;
*(index++) = s0;
*(index++) = s2;
*(index++) = s1;
*(index++) = s2;
*(index++) = s3;
int s4 = s2 + slices;
int s5 = s3 + slices;
*(index++) = s3;
*(index++) = s2;
*(index++) = s4;
*(index++) = s3;
*(index++) = s4;
*(index++) = s5;
*(index++) = s5;
*(index++) = s4;
*(index++) = capVertex;
}
_spotLightMesh->setIndexBuffer(gpu::BufferView(new gpu::Buffer(sizeof(GLushort) * indices, (gpu::Byte*) indexData), gpu::Element::INDEX_UINT16));
delete[] indexData;
model::Mesh::Part part(0, indices, 0, model::Mesh::TRIANGLES);
//DEBUG: model::Mesh::Part part(0, indices, 0, model::Mesh::LINE_STRIP);
_spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(sizeof(part), (gpu::Byte*) &part), gpu::Element::PART_DRAWCALL));
_spotLightMesh->makeBufferStream();
}
return _spotLightMesh;
}

View file

@ -21,6 +21,7 @@
#include "model/Light.h" #include "model/Light.h"
#include "model/Stage.h" #include "model/Stage.h"
#include "model/Geometry.h"
class AbstractViewStateInterface; class AbstractViewStateInterface;
class RenderArgs; class RenderArgs;
@ -94,41 +95,49 @@ private:
int lightBufferUnit; int lightBufferUnit;
int atmosphereBufferUnit; int atmosphereBufferUnit;
int invViewMat; int invViewMat;
int texcoordMat;
int coneParam;
}; };
static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations); model::MeshPointer _spotLightMesh;
model::MeshPointer getSpotLightMesh();
static void loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& program, LightLocations& locations);
gpu::PipelinePointer getPipeline(SimpleProgramKey config); gpu::PipelinePointer getPipeline(SimpleProgramKey config);
gpu::ShaderPointer _simpleShader; gpu::ShaderPointer _simpleShader;
gpu::ShaderPointer _emissiveShader; gpu::ShaderPointer _emissiveShader;
QHash<SimpleProgramKey, gpu::PipelinePointer> _simplePrograms; QHash<SimpleProgramKey, gpu::PipelinePointer> _simplePrograms;
ProgramObject _directionalSkyboxLight; gpu::PipelinePointer _blitLightBuffer;
gpu::PipelinePointer _directionalSkyboxLight;
LightLocations _directionalSkyboxLightLocations; LightLocations _directionalSkyboxLightLocations;
ProgramObject _directionalSkyboxLightShadowMap; gpu::PipelinePointer _directionalSkyboxLightShadowMap;
LightLocations _directionalSkyboxLightShadowMapLocations; LightLocations _directionalSkyboxLightShadowMapLocations;
ProgramObject _directionalSkyboxLightCascadedShadowMap; gpu::PipelinePointer _directionalSkyboxLightCascadedShadowMap;
LightLocations _directionalSkyboxLightCascadedShadowMapLocations; LightLocations _directionalSkyboxLightCascadedShadowMapLocations;
ProgramObject _directionalAmbientSphereLight; gpu::PipelinePointer _directionalAmbientSphereLight;
LightLocations _directionalAmbientSphereLightLocations; LightLocations _directionalAmbientSphereLightLocations;
ProgramObject _directionalAmbientSphereLightShadowMap; gpu::PipelinePointer _directionalAmbientSphereLightShadowMap;
LightLocations _directionalAmbientSphereLightShadowMapLocations; LightLocations _directionalAmbientSphereLightShadowMapLocations;
ProgramObject _directionalAmbientSphereLightCascadedShadowMap; gpu::PipelinePointer _directionalAmbientSphereLightCascadedShadowMap;
LightLocations _directionalAmbientSphereLightCascadedShadowMapLocations; LightLocations _directionalAmbientSphereLightCascadedShadowMapLocations;
ProgramObject _directionalLight; gpu::PipelinePointer _directionalLight;
LightLocations _directionalLightLocations; LightLocations _directionalLightLocations;
ProgramObject _directionalLightShadowMap; gpu::PipelinePointer _directionalLightShadowMap;
LightLocations _directionalLightShadowMapLocations; LightLocations _directionalLightShadowMapLocations;
ProgramObject _directionalLightCascadedShadowMap; gpu::PipelinePointer _directionalLightCascadedShadowMap;
LightLocations _directionalLightCascadedShadowMapLocations; LightLocations _directionalLightCascadedShadowMapLocations;
ProgramObject _pointLight; gpu::PipelinePointer _pointLight;
LightLocations _pointLightLocations; LightLocations _pointLightLocations;
ProgramObject _spotLight; gpu::PipelinePointer _spotLight;
LightLocations _spotLightLocations; LightLocations _spotLightLocations;
class PointLight { class PointLight {
public: public:
glm::vec4 position; glm::vec4 position;

View file

@ -401,7 +401,6 @@ void GeometryCache::renderCone(float base, float height, int slices, int stacks)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
void GeometryCache::renderGrid(int xDivisions, int yDivisions, const glm::vec4& color) { void GeometryCache::renderGrid(int xDivisions, int yDivisions, const glm::vec4& color) {
gpu::Batch batch; gpu::Batch batch;
renderGrid(batch, xDivisions, yDivisions, color); renderGrid(batch, xDivisions, yDivisions, color);

View file

@ -301,6 +301,7 @@ private:
}; };
QHash<IntPair, VerticesIndices> _coneVBOs; QHash<IntPair, VerticesIndices> _coneVBOs;
int _nextID; int _nextID;
QHash<int, Vec3PairVec4Pair> _lastRegisteredQuad3DTexture; QHash<int, Vec3PairVec4Pair> _lastRegisteredQuad3DTexture;

View file

@ -40,8 +40,9 @@ void ResolveDeferred::run(const SceneContextPointer& sceneContext, const RenderC
} }
RenderDeferredTask::RenderDeferredTask() : Task() { RenderDeferredTask::RenderDeferredTask() : Task() {
_jobs.push_back(Job(new PrepareDeferred::JobModel("PrepareDeferred")));
_jobs.push_back(Job(new DrawBackground::JobModel("DrawBackground"))); _jobs.push_back(Job(new DrawBackground::JobModel("DrawBackground")));
_jobs.push_back(Job(new PrepareDeferred::JobModel("PrepareDeferred")));
_jobs.push_back(Job(new FetchItems::JobModel("FetchOpaque", _jobs.push_back(Job(new FetchItems::JobModel("FetchOpaque",
FetchItems( FetchItems(
[] (const RenderContextPointer& context, int count) { [] (const RenderContextPointer& context, int count) {

View file

@ -12,9 +12,19 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
uniform mat4 texcoordMat;
void main(void) { void main(void) {
gl_Position = ftransform(); // standard transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
<$transformModelToClipPos(cam, obj, gl_Vertex, gl_Position)$>;
vec4 projected = gl_Position / gl_Position.w; vec4 projected = gl_Position / gl_Position.w;
gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]) * gl_Position.w, gl_TexCoord[0] = vec4(dot(projected, texcoordMat[0]) * gl_Position.w,
dot(projected, gl_ObjectPlaneT[3]) * gl_Position.w, 0.0, gl_Position.w); dot(projected, texcoordMat[1]) * gl_Position.w, 0.0, gl_Position.w);
} }

View file

@ -0,0 +1,47 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// deferred_light_spot.vert
// vertex shader
//
// Created by Sam Gateau on 7/8/15.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
uniform mat4 texcoordMat;
uniform vec4 coneParam;
void main(void) {
vec4 coneVertex = gl_Vertex;
if (coneParam.w != 0.0) {
if(coneVertex.z >= 0.0) {
// Evaluate the true position of the spot volume
vec2 dir = float(coneVertex.z < 0.5f) * (coneParam.xy
+ vec2(coneParam.y, -coneParam.x) * coneParam.z * float(coneVertex.z > 0.0f))
+ float(coneVertex.z > 0.5f) * (vec2(1.0, 0.0)
+ vec2(0.0, coneParam.z) * float(coneVertex.z < 1.0f));
coneVertex.xy *= dir.y;
coneVertex.z = -dir.x;
} else {
coneVertex.z = 0.0;
}
}
// standard transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
<$transformModelToClipPos(cam, obj, coneVertex, gl_Position)$>;
vec4 projected = gl_Position / gl_Position.w;
gl_TexCoord[0] = vec4(dot(projected, texcoordMat[0]) * gl_Position.w,
dot(projected, texcoordMat[1]) * gl_Position.w, 0.0, gl_Position.w);
}

View file

@ -28,7 +28,7 @@ void main(void) {
// Grab the fragment data from the uv // Grab the fragment data from the uv
vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q;
DeferredFragment frag = unpackDeferredFragment(texCoord); DeferredFragment frag = unpackDeferredFragment(texCoord);
// Kill if in front of the light volume // Kill if in front of the light volume
float depth = frag.depthVal; float depth = frag.depthVal;
if (depth < gl_FragCoord.z) { if (depth < gl_FragCoord.z) {