fix macOS build (following Projucer changes made in Windows, which removed /Applications/JUCE/modules from its headers). move JUCE headers under source control, so that Windows and macOS can both build against same version of JUCE. remove AUv3 target (I think it's an iOS thing, so it will never work with this macOS fluidsynth dylib).
This commit is contained in:
1258
modules/juce_opengl/opengl/juce_OpenGLContext.cpp
Normal file
1258
modules/juce_opengl/opengl/juce_OpenGLContext.cpp
Normal file
File diff suppressed because it is too large
Load Diff
369
modules/juce_opengl/opengl/juce_OpenGLContext.h
Normal file
369
modules/juce_opengl/opengl/juce_OpenGLContext.h
Normal file
@ -0,0 +1,369 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLTexture;
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Creates an OpenGL context, which can be attached to a component.
|
||||
|
||||
To render some OpenGL, you should create an instance of an OpenGLContext,
|
||||
and call attachTo() to make it use a component as its render target.
|
||||
|
||||
To provide threaded rendering, you can supply an OpenGLRenderer object that
|
||||
will be used to render each frame.
|
||||
|
||||
Before your target component or OpenGLRenderer is deleted, you MUST call
|
||||
detach() or delete the OpenGLContext to allow the background thread to
|
||||
stop and the native resources to be freed safely.
|
||||
|
||||
@see OpenGLRenderer
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLContext
|
||||
{
|
||||
public:
|
||||
OpenGLContext();
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLContext();
|
||||
|
||||
//==============================================================================
|
||||
/** Gives the context an OpenGLRenderer to use to do the drawing.
|
||||
The object that you give it will not be owned by the context, so it's the caller's
|
||||
responsibility to manage its lifetime and make sure that it doesn't get deleted
|
||||
while the context may be using it. To stop the context using a renderer, just call
|
||||
this method with a null pointer.
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setRenderer (OpenGLRenderer*) noexcept;
|
||||
|
||||
/** Attaches the context to a target component.
|
||||
|
||||
If the component is not fully visible, this call will wait until the component
|
||||
is shown before actually creating a native context for it.
|
||||
|
||||
When a native context is created, a thread is started, and will be used to call
|
||||
the OpenGLRenderer methods. The context will be floated above the target component,
|
||||
and when the target moves, it will track it. If the component is hidden/shown, the
|
||||
context may be deleted and re-created.
|
||||
*/
|
||||
void attachTo (Component&);
|
||||
|
||||
/** Detaches the context from its target component and deletes any native resources.
|
||||
If the context has not been attached, this will do nothing. Otherwise, it will block
|
||||
until the context and its thread have been cleaned up.
|
||||
*/
|
||||
void detach();
|
||||
|
||||
/** Returns true if the context is attached to a component and is on-screen.
|
||||
Note that if you call attachTo() for a non-visible component, this method will
|
||||
return false until the component is made visible.
|
||||
*/
|
||||
bool isAttached() const noexcept;
|
||||
|
||||
/** Returns the component to which this context is currently attached, or nullptr. */
|
||||
Component* getTargetComponent() const noexcept;
|
||||
|
||||
/** If the given component has an OpenGLContext attached, then this will return it. */
|
||||
static OpenGLContext* getContextAttachedTo (Component& component) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Sets the pixel format which you'd like to use for the target GL surface.
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept;
|
||||
|
||||
/** Texture magnification filters, used by setTextureMagnificationFilter(). */
|
||||
enum TextureMagnificationFilter
|
||||
{
|
||||
nearest,
|
||||
linear
|
||||
};
|
||||
|
||||
/** Sets the texture magnification filter. By default the texture magnification
|
||||
filter is linear. However, for faster rendering you may want to use the
|
||||
'nearest' magnification filter. This option will not affect any textures
|
||||
created before this function was called. */
|
||||
void setTextureMagnificationFilter (TextureMagnificationFilter magFilterMode) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Provides a context with which you'd like this context's resources to be shared.
|
||||
The object passed-in here is a platform-dependent native context object, and
|
||||
must not be deleted while this context may still be using it! To turn off sharing,
|
||||
you can call this method with a null pointer.
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setNativeSharedContext (void* nativeContextToShareWith) noexcept;
|
||||
|
||||
/** Enables multisampling on platforms where this is implemented.
|
||||
If enabling this, you must call this method before attachTo().
|
||||
*/
|
||||
void setMultisamplingEnabled (bool) noexcept;
|
||||
|
||||
/** Returns true if shaders can be used in this context. */
|
||||
bool areShadersAvailable() const;
|
||||
|
||||
/** OpenGL versions, used by setOpenGLVersionRequired(). */
|
||||
enum OpenGLVersion
|
||||
{
|
||||
defaultGLVersion = 0,
|
||||
openGL3_2
|
||||
};
|
||||
|
||||
/** Sets a preference for the version of GL that this context should use, if possible.
|
||||
Some platforms may ignore this value.
|
||||
*/
|
||||
void setOpenGLVersionRequired (OpenGLVersion) noexcept;
|
||||
|
||||
/** Enables or disables the use of the GL context to perform 2D rendering
|
||||
of the component to which it is attached.
|
||||
If this is false, then only your OpenGLRenderer will be used to perform
|
||||
any rendering. If true, then each time your target's paint() method needs
|
||||
to be called, an OpenGLGraphicsContext will be used to render it, (after
|
||||
calling your OpenGLRenderer if there is one).
|
||||
|
||||
By default this is set to true. If you're not using any paint() method functionality
|
||||
and are doing all your rendering in an OpenGLRenderer, you should disable it
|
||||
to improve performance.
|
||||
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setComponentPaintingEnabled (bool shouldPaintComponent) noexcept;
|
||||
|
||||
/** Enables or disables continuous repainting.
|
||||
If set to true, the context will run a loop, re-rendering itself without waiting
|
||||
for triggerRepaint() to be called, at a frequency determined by the swap interval
|
||||
(see setSwapInterval). If false, then after each render callback, it will wait for
|
||||
another call to triggerRepaint() before rendering again.
|
||||
This is disabled by default.
|
||||
@see setSwapInterval
|
||||
*/
|
||||
void setContinuousRepainting (bool shouldContinuouslyRepaint) noexcept;
|
||||
|
||||
/** Asynchronously causes a repaint to be made. */
|
||||
void triggerRepaint();
|
||||
|
||||
//==============================================================================
|
||||
/** This retrieves an object that was previously stored with setAssociatedObject().
|
||||
If no object is found with the given name, this will return nullptr.
|
||||
This method must only be called from within the GL rendering methods.
|
||||
@see setAssociatedObject
|
||||
*/
|
||||
ReferenceCountedObject* getAssociatedObject (const char* name) const;
|
||||
|
||||
/** Attaches a named object to the context, which will be deleted when the context is
|
||||
destroyed.
|
||||
|
||||
This allows you to store an object which will be released before the context is
|
||||
deleted. The main purpose is for caching GL objects such as shader programs, which
|
||||
will become invalid when the context is deleted.
|
||||
|
||||
This method must only be called from within the GL rendering methods.
|
||||
*/
|
||||
void setAssociatedObject (const char* name, ReferenceCountedObject* newObject);
|
||||
|
||||
//==============================================================================
|
||||
/** Makes this context the currently active one.
|
||||
You should never need to call this in normal use - the context will already be
|
||||
active when OpenGLRenderer::renderOpenGL() is invoked.
|
||||
*/
|
||||
bool makeActive() const noexcept;
|
||||
|
||||
/** Returns true if this context is currently active for the calling thread. */
|
||||
bool isActive() const noexcept;
|
||||
|
||||
/** If any context is active on the current thread, this deactivates it.
|
||||
Note that on some platforms, like Android, this isn't possible.
|
||||
*/
|
||||
static void deactivateCurrentContext();
|
||||
|
||||
/** Returns the context that's currently in active use by the calling thread, or
|
||||
nullptr if no context is active.
|
||||
*/
|
||||
static OpenGLContext* getCurrentContext();
|
||||
|
||||
//==============================================================================
|
||||
/** Swaps the buffers (if the context can do this).
|
||||
There's normally no need to call this directly - the buffers will be swapped
|
||||
automatically after your OpenGLRenderer::renderOpenGL() method has been called.
|
||||
*/
|
||||
void swapBuffers();
|
||||
|
||||
/** Sets whether the context checks the vertical sync before swapping.
|
||||
|
||||
The value is the number of frames to allow between buffer-swapping. This is
|
||||
fairly system-dependent, but 0 turns off syncing, 1 makes it swap on frame-boundaries,
|
||||
and greater numbers indicate that it should swap less often.
|
||||
|
||||
By default, this will be set to 1.
|
||||
|
||||
Returns true if it sets the value successfully - some platforms won't support
|
||||
this setting.
|
||||
|
||||
@see setContinuousRepainting
|
||||
*/
|
||||
bool setSwapInterval (int numFramesPerSwap);
|
||||
|
||||
/** Returns the current swap-sync interval.
|
||||
See setSwapInterval() for info about the value returned.
|
||||
*/
|
||||
int getSwapInterval() const;
|
||||
|
||||
//==============================================================================
|
||||
/** Execute a lambda, function or functor on the OpenGL thread with an active
|
||||
context.
|
||||
|
||||
This method will attempt to execute functor on the OpenGL thread. If
|
||||
blockUntilFinished is true then the method will block until the functor
|
||||
has finished executing.
|
||||
|
||||
This function can only be called if the context is attached to a component.
|
||||
Otherwise, this function will assert.
|
||||
|
||||
This function is useful when you need to excute house-keeping tasks such
|
||||
as allocating, deallocating textures or framebuffers. As such, the functor
|
||||
will execute without locking the message thread. Therefore, it is not
|
||||
intended for any drawing commands or GUI code. Any GUI code should be
|
||||
executed in the OpenGLRenderer::renderOpenGL callback instead.
|
||||
*/
|
||||
template <typename T>
|
||||
void executeOnGLThread (T&& functor, bool blockUntilFinished);
|
||||
|
||||
//==============================================================================
|
||||
/** Returns the scale factor used by the display that is being rendered.
|
||||
|
||||
The scale is that of the display - see Desktop::Displays::Display::scale
|
||||
|
||||
Note that this should only be called during an OpenGLRenderer::renderOpenGL()
|
||||
callback - at other times the value it returns is undefined.
|
||||
*/
|
||||
double getRenderingScale() const noexcept { return currentRenderScale; }
|
||||
|
||||
//==============================================================================
|
||||
/** If this context is backed by a frame buffer, this returns its ID number,
|
||||
or 0 if the context does not use a framebuffer.
|
||||
*/
|
||||
unsigned int getFrameBufferID() const noexcept;
|
||||
|
||||
/** Returns an OS-dependent handle to some kind of underlting OS-provided GL context.
|
||||
|
||||
The exact type of the value returned will depend on the OS and may change
|
||||
if the implementation changes. If you want to use this, digging around in the
|
||||
native code is probably the best way to find out what it is.
|
||||
*/
|
||||
void* getRawContext() const noexcept;
|
||||
|
||||
/** This structure holds a set of dynamically loaded GL functions for use on this context. */
|
||||
OpenGLExtensionFunctions extensions;
|
||||
|
||||
//==============================================================================
|
||||
/** Draws the currently selected texture into this context at its original size.
|
||||
|
||||
@param targetClipArea the target area to draw into (in top-left origin coords)
|
||||
@param anchorPosAndTextureSize the position of this rectangle is the texture's top-left
|
||||
anchor position in the target space, and the size must be
|
||||
the total size of the texture.
|
||||
@param contextWidth the width of the context or framebuffer that is being drawn into,
|
||||
used for scaling of the coordinates.
|
||||
@param contextHeight the height of the context or framebuffer that is being drawn into,
|
||||
used for vertical flipping of the y coordinates.
|
||||
@param textureOriginIsBottomLeft if true, the texture's origin is treated as being at
|
||||
(0, 0). If false, it is assumed to be (0, 1)
|
||||
*/
|
||||
void copyTexture (const Rectangle<int>& targetClipArea,
|
||||
const Rectangle<int>& anchorPosAndTextureSize,
|
||||
int contextWidth, int contextHeight,
|
||||
bool textureOriginIsBottomLeft);
|
||||
|
||||
|
||||
/** Changes the amount of GPU memory that the internal cache for Images is allowed to use. */
|
||||
void setImageCacheSize (size_t cacheSizeBytes) noexcept;
|
||||
|
||||
/** Returns the amount of GPU memory that the internal cache for Images is allowed to use. */
|
||||
size_t getImageCacheSize() const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
#ifndef DOXYGEN
|
||||
class NativeContext;
|
||||
#endif
|
||||
|
||||
private:
|
||||
friend class OpenGLTexture;
|
||||
|
||||
class CachedImage;
|
||||
class Attachment;
|
||||
NativeContext* nativeContext = nullptr;
|
||||
OpenGLRenderer* renderer = nullptr;
|
||||
double currentRenderScale = 1.0;
|
||||
std::unique_ptr<Attachment> attachment;
|
||||
OpenGLPixelFormat openGLPixelFormat;
|
||||
void* contextToShareWith = nullptr;
|
||||
OpenGLVersion versionRequired = defaultGLVersion;
|
||||
size_t imageCacheMaxSize = 8 * 1024 * 1024;
|
||||
bool renderComponents = true, useMultisampling = false, continuousRepaint = false, overrideCanAttach = false;
|
||||
TextureMagnificationFilter texMagFilter = linear;
|
||||
|
||||
//==============================================================================
|
||||
struct AsyncWorker : public ReferenceCountedObject
|
||||
{
|
||||
using Ptr = ReferenceCountedObjectPtr<AsyncWorker>;
|
||||
virtual void operator() (OpenGLContext&) = 0;
|
||||
virtual ~AsyncWorker() {}
|
||||
};
|
||||
|
||||
template <typename FunctionType>
|
||||
struct AsyncWorkerFunctor : public AsyncWorker
|
||||
{
|
||||
AsyncWorkerFunctor (FunctionType functorToUse) : functor (functorToUse) {}
|
||||
void operator() (OpenGLContext& callerContext) override { functor (callerContext); }
|
||||
FunctionType functor;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (AsyncWorkerFunctor)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
friend void componentPeerAboutToChange (Component&, bool);
|
||||
void overrideCanBeAttached (bool);
|
||||
|
||||
//==============================================================================
|
||||
CachedImage* getCachedImage() const noexcept;
|
||||
void execute (AsyncWorker::Ptr, bool);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
#ifndef DOXYGEN
|
||||
template <typename FunctionType>
|
||||
void OpenGLContext::executeOnGLThread (FunctionType&& f, bool shouldBlock) { execute (new AsyncWorkerFunctor<FunctionType> (f), shouldBlock); }
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
355
modules/juce_opengl/opengl/juce_OpenGLFrameBuffer.cpp
Normal file
355
modules/juce_opengl/opengl/juce_OpenGLFrameBuffer.cpp
Normal file
@ -0,0 +1,355 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLFrameBuffer::Pimpl
|
||||
{
|
||||
public:
|
||||
Pimpl (OpenGLContext& c, const int w, const int h,
|
||||
const bool wantsDepthBuffer, const bool wantsStencilBuffer)
|
||||
: context (c), width (w), height (h),
|
||||
textureID (0), frameBufferID (0), depthOrStencilBuffer (0),
|
||||
hasDepthBuffer (false), hasStencilBuffer (false)
|
||||
{
|
||||
// Framebuffer objects can only be created when the current thread has an active OpenGL
|
||||
// context. You'll need to create this object in one of the OpenGLContext's callbacks.
|
||||
jassert (OpenGLHelpers::isContextActive());
|
||||
|
||||
#if JUCE_WINDOWS || JUCE_LINUX
|
||||
if (context.extensions.glGenFramebuffers == nullptr)
|
||||
return;
|
||||
#endif
|
||||
|
||||
context.extensions.glGenFramebuffers (1, &frameBufferID);
|
||||
bind();
|
||||
|
||||
glGenTextures (1, &textureID);
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
context.extensions.glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
|
||||
|
||||
if (wantsDepthBuffer || wantsStencilBuffer)
|
||||
{
|
||||
context.extensions.glGenRenderbuffers (1, &depthOrStencilBuffer);
|
||||
context.extensions.glBindRenderbuffer (GL_RENDERBUFFER, depthOrStencilBuffer);
|
||||
jassert (context.extensions.glIsRenderbuffer (depthOrStencilBuffer));
|
||||
|
||||
context.extensions.glRenderbufferStorage (GL_RENDERBUFFER,
|
||||
(wantsDepthBuffer && wantsStencilBuffer) ? GL_DEPTH24_STENCIL8
|
||||
#if JUCE_OPENGL_ES
|
||||
: GL_DEPTH_COMPONENT16,
|
||||
#else
|
||||
: GL_DEPTH_COMPONENT,
|
||||
#endif
|
||||
width, height);
|
||||
|
||||
GLint params = 0;
|
||||
context.extensions.glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_DEPTH_SIZE, ¶ms);
|
||||
context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
|
||||
|
||||
if (wantsStencilBuffer)
|
||||
context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
|
||||
|
||||
hasDepthBuffer = wantsDepthBuffer;
|
||||
hasStencilBuffer = wantsStencilBuffer;
|
||||
}
|
||||
|
||||
unbind();
|
||||
}
|
||||
|
||||
~Pimpl()
|
||||
{
|
||||
if (OpenGLHelpers::isContextActive())
|
||||
{
|
||||
if (textureID != 0)
|
||||
glDeleteTextures (1, &textureID);
|
||||
|
||||
if (depthOrStencilBuffer != 0)
|
||||
context.extensions.glDeleteRenderbuffers (1, &depthOrStencilBuffer);
|
||||
|
||||
if (frameBufferID != 0)
|
||||
context.extensions.glDeleteFramebuffers (1, &frameBufferID);
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
}
|
||||
|
||||
bool createdOk() const
|
||||
{
|
||||
return frameBufferID != 0 && textureID != 0;
|
||||
}
|
||||
|
||||
void bind()
|
||||
{
|
||||
context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, frameBufferID);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
void unbind()
|
||||
{
|
||||
context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, context.getFrameBufferID());
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
OpenGLContext& context;
|
||||
const int width, height;
|
||||
GLuint textureID, frameBufferID, depthOrStencilBuffer;
|
||||
bool hasDepthBuffer, hasStencilBuffer;
|
||||
|
||||
private:
|
||||
bool checkStatus() noexcept
|
||||
{
|
||||
const GLenum status = context.extensions.glCheckFramebufferStatus (GL_FRAMEBUFFER);
|
||||
|
||||
return status == GL_NO_ERROR
|
||||
|| status == GL_FRAMEBUFFER_COMPLETE;
|
||||
}
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (Pimpl)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class OpenGLFrameBuffer::SavedState
|
||||
{
|
||||
public:
|
||||
SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
|
||||
: width (w), height (h),
|
||||
data ((size_t) (w * h))
|
||||
{
|
||||
buffer.readPixels (data, Rectangle<int> (w, h));
|
||||
}
|
||||
|
||||
bool restore (OpenGLContext& context, OpenGLFrameBuffer& buffer)
|
||||
{
|
||||
if (buffer.initialise (context, width, height))
|
||||
{
|
||||
buffer.writePixels (data, Rectangle<int> (width, height));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
const int width, height;
|
||||
HeapBlock<PixelARGB> data;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
OpenGLFrameBuffer::OpenGLFrameBuffer() {}
|
||||
OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
|
||||
|
||||
bool OpenGLFrameBuffer::initialise (OpenGLContext& context, int width, int height)
|
||||
{
|
||||
jassert (context.isActive()); // The context must be active when creating a framebuffer!
|
||||
|
||||
pimpl.reset();
|
||||
pimpl.reset (new Pimpl (context, width, height, false, false));
|
||||
|
||||
if (! pimpl->createdOk())
|
||||
pimpl.reset();
|
||||
|
||||
return pimpl != nullptr;
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::initialise (OpenGLContext& context, const Image& image)
|
||||
{
|
||||
if (! image.isARGB())
|
||||
return initialise (context, image.convertedToFormat (Image::ARGB));
|
||||
|
||||
Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
|
||||
|
||||
return initialise (context, bitmap.width, bitmap.height)
|
||||
&& writePixels ((const PixelARGB*) bitmap.data, image.getBounds());
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::initialise (OpenGLFrameBuffer& other)
|
||||
{
|
||||
auto* p = other.pimpl.get();
|
||||
|
||||
if (p == nullptr)
|
||||
{
|
||||
pimpl.reset();
|
||||
return true;
|
||||
}
|
||||
|
||||
const Rectangle<int> area (pimpl->width, pimpl->height);
|
||||
|
||||
if (initialise (p->context, area.getWidth(), area.getHeight()))
|
||||
{
|
||||
pimpl->bind();
|
||||
|
||||
#if ! JUCE_ANDROID
|
||||
glEnable (GL_TEXTURE_2D);
|
||||
clearGLError();
|
||||
#endif
|
||||
glBindTexture (GL_TEXTURE_2D, p->textureID);
|
||||
pimpl->context.copyTexture (area, area, area.getWidth(), area.getHeight(), false);
|
||||
glBindTexture (GL_TEXTURE_2D, 0);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
pimpl->unbind();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::release()
|
||||
{
|
||||
pimpl.reset();
|
||||
savedState.reset();
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::saveAndRelease()
|
||||
{
|
||||
if (pimpl != nullptr)
|
||||
{
|
||||
savedState.reset (new SavedState (*this, pimpl->width, pimpl->height));
|
||||
pimpl.reset();
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::reloadSavedCopy (OpenGLContext& context)
|
||||
{
|
||||
if (savedState != nullptr)
|
||||
{
|
||||
std::unique_ptr<SavedState> state;
|
||||
std::swap (state, savedState);
|
||||
|
||||
if (state->restore (context, *this))
|
||||
return true;
|
||||
|
||||
std::swap (state, savedState);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
|
||||
int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
|
||||
GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
|
||||
|
||||
bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
|
||||
{
|
||||
// trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
|
||||
// reloadSavedCopy() to put it back into GPU memory before using it..
|
||||
jassert (savedState == nullptr);
|
||||
|
||||
if (pimpl == nullptr)
|
||||
return false;
|
||||
|
||||
pimpl->bind();
|
||||
return true;
|
||||
}
|
||||
|
||||
GLuint OpenGLFrameBuffer::getFrameBufferID() const noexcept
|
||||
{
|
||||
return pimpl != nullptr ? pimpl->frameBufferID : 0;
|
||||
}
|
||||
|
||||
GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget() noexcept
|
||||
{
|
||||
GLint fb;
|
||||
glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
|
||||
return (GLuint) fb;
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::releaseAsRenderingTarget()
|
||||
{
|
||||
if (pimpl != nullptr)
|
||||
pimpl->unbind();
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::clear (Colour colour)
|
||||
{
|
||||
if (makeCurrentRenderingTarget())
|
||||
{
|
||||
OpenGLHelpers::clear (colour);
|
||||
releaseAsRenderingTarget();
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::makeCurrentAndClear()
|
||||
{
|
||||
if (makeCurrentRenderingTarget())
|
||||
{
|
||||
glClearColor (0, 0, 0, 0);
|
||||
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
|
||||
{
|
||||
if (! makeCurrentRenderingTarget())
|
||||
return false;
|
||||
|
||||
glPixelStorei (GL_PACK_ALIGNMENT, 4);
|
||||
glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
|
||||
JUCE_RGBA_FORMAT, GL_UNSIGNED_BYTE, target);
|
||||
|
||||
pimpl->unbind();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
|
||||
{
|
||||
OpenGLTargetSaver ts (pimpl->context);
|
||||
|
||||
if (! makeCurrentRenderingTarget())
|
||||
return false;
|
||||
|
||||
glDisable (GL_DEPTH_TEST);
|
||||
glDisable (GL_BLEND);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
OpenGLTexture tex;
|
||||
tex.loadARGB (data, area.getWidth(), area.getHeight());
|
||||
|
||||
glViewport (0, 0, pimpl->width, pimpl->height);
|
||||
pimpl->context.copyTexture (area, Rectangle<int> (area.getX(), area.getY(),
|
||||
tex.getWidth(), tex.getHeight()),
|
||||
pimpl->width, pimpl->height, true);
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace juce
|
136
modules/juce_opengl/opengl/juce_OpenGLFrameBuffer.h
Normal file
136
modules/juce_opengl/opengl/juce_OpenGLFrameBuffer.h
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Creates an openGL frame buffer.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLFrameBuffer
|
||||
{
|
||||
public:
|
||||
/** Creates an uninitialised buffer.
|
||||
To actually allocate the buffer, use initialise().
|
||||
*/
|
||||
OpenGLFrameBuffer();
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLFrameBuffer();
|
||||
|
||||
//==============================================================================
|
||||
/** Tries to allocates a buffer of the given size.
|
||||
Note that a valid openGL context must be selected when you call this method,
|
||||
or it will fail.
|
||||
*/
|
||||
bool initialise (OpenGLContext& context, int width, int height);
|
||||
|
||||
/** Tries to allocates a buffer containing a copy of a given image.
|
||||
Note that a valid openGL context must be selected when you call this method,
|
||||
or it will fail.
|
||||
*/
|
||||
bool initialise (OpenGLContext& context, const Image& content);
|
||||
|
||||
/** Tries to allocate a copy of another framebuffer.
|
||||
*/
|
||||
bool initialise (OpenGLFrameBuffer& other);
|
||||
|
||||
/** Releases the buffer, if one has been allocated.
|
||||
Any saved state that was created with saveAndRelease() will also be freed by this call.
|
||||
*/
|
||||
void release();
|
||||
|
||||
/** If the framebuffer is active, this will save a stashed copy of its contents in main memory,
|
||||
and will release the GL buffer.
|
||||
After saving, the original state can be restored again by calling reloadSavedCopy().
|
||||
*/
|
||||
void saveAndRelease();
|
||||
|
||||
/** Restores the framebuffer content that was previously saved using saveAndRelease().
|
||||
After saving to main memory, the original state can be restored by calling restoreToGPUMemory().
|
||||
*/
|
||||
bool reloadSavedCopy (OpenGLContext& context);
|
||||
|
||||
//==============================================================================
|
||||
/** Returns true if a valid buffer has been allocated. */
|
||||
bool isValid() const noexcept { return pimpl != nullptr; }
|
||||
|
||||
/** Returns the width of the buffer. */
|
||||
int getWidth() const noexcept;
|
||||
|
||||
/** Returns the height of the buffer. */
|
||||
int getHeight() const noexcept;
|
||||
|
||||
/** Returns the texture ID number for using this buffer as a texture. */
|
||||
GLuint getTextureID() const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Selects this buffer as the current OpenGL rendering target. */
|
||||
bool makeCurrentRenderingTarget();
|
||||
|
||||
/** Deselects this buffer as the current OpenGL rendering target. */
|
||||
void releaseAsRenderingTarget();
|
||||
|
||||
/** Returns the ID of this framebuffer, or 0 if it isn't initialised. */
|
||||
GLuint getFrameBufferID() const noexcept;
|
||||
|
||||
/** Returns the current frame buffer ID for the current context. */
|
||||
static GLuint getCurrentFrameBufferTarget() noexcept;
|
||||
|
||||
/** Clears the framebuffer with the specified colour. */
|
||||
void clear (Colour colour);
|
||||
|
||||
/** Selects the framebuffer as the current target, and clears it to transparent. */
|
||||
void makeCurrentAndClear();
|
||||
|
||||
/** Reads an area of pixels from the framebuffer into a 32-bit ARGB pixel array.
|
||||
The lineStride is measured as a number of pixels, not bytes - pass a stride
|
||||
of 0 to indicate a packed array.
|
||||
*/
|
||||
bool readPixels (PixelARGB* targetData, const Rectangle<int>& sourceArea);
|
||||
|
||||
/** Writes an area of pixels into the framebuffer from a specified pixel array.
|
||||
The lineStride is measured as a number of pixels, not bytes - pass a stride
|
||||
of 0 to indicate a packed array.
|
||||
*/
|
||||
bool writePixels (const PixelARGB* srcData, const Rectangle<int>& targetArea);
|
||||
|
||||
private:
|
||||
class Pimpl;
|
||||
friend struct ContainerDeletePolicy<Pimpl>;
|
||||
std::unique_ptr<Pimpl> pimpl;
|
||||
|
||||
class SavedState;
|
||||
friend struct ContainerDeletePolicy<SavedState>;
|
||||
std::unique_ptr<SavedState> savedState;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBuffer)
|
||||
};
|
||||
|
||||
} // namespace juce
|
1928
modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp
Normal file
1928
modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp
Normal file
File diff suppressed because it is too large
Load Diff
102
modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.h
Normal file
102
modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.h
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/** Creates a graphics context object that will render into the given OpenGL target.
|
||||
The caller is responsible for deleting this object when no longer needed.
|
||||
*/
|
||||
LowLevelGraphicsContext* createOpenGLGraphicsContext (OpenGLContext&, int width, int height);
|
||||
|
||||
/** Creates a graphics context object that will render into the given OpenGL framebuffer.
|
||||
The caller is responsible for deleting this object when no longer needed.
|
||||
*/
|
||||
LowLevelGraphicsContext* createOpenGLGraphicsContext (OpenGLContext&, OpenGLFrameBuffer&);
|
||||
|
||||
/** Creates a graphics context object that will render into the given OpenGL framebuffer,
|
||||
with the given size.
|
||||
The caller is responsible for deleting this object when no longer needed.
|
||||
*/
|
||||
LowLevelGraphicsContext* createOpenGLGraphicsContext (OpenGLContext&,
|
||||
unsigned int frameBufferID,
|
||||
int width, int height);
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Used to create custom shaders for use with an openGL 2D rendering context.
|
||||
|
||||
Given a GL-based rendering context, you can write a fragment shader that applies some
|
||||
kind of per-pixel effect.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
struct JUCE_API OpenGLGraphicsContextCustomShader
|
||||
{
|
||||
/** Creates a custom shader.
|
||||
|
||||
The shader code will not be compiled until actually needed, so it's OK to call this
|
||||
constructor when no GL context is active.
|
||||
|
||||
The code should be a normal fragment shader. As well as the usual GLSL variables, there is
|
||||
also an automatically declared varying vec2 called "pixelPos", which indicates the pixel
|
||||
position within the graphics context of the pixel being drawn. There is also a varying value
|
||||
"pixelAlpha", which indicates the alpha by which the pixel should be multiplied, so that the
|
||||
edges of any clip-region masks are anti-aliased correctly.
|
||||
*/
|
||||
OpenGLGraphicsContextCustomShader (const String& fragmentShaderCode);
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLGraphicsContextCustomShader();
|
||||
|
||||
/** Returns the program, if it has been linked and is active.
|
||||
This can be called when you're about to use fillRect, to set up any uniforms/textures that
|
||||
the program may require.
|
||||
*/
|
||||
OpenGLShaderProgram* getProgram (LowLevelGraphicsContext&) const;
|
||||
|
||||
/** Applies the shader to a rectangle within the graphics context. */
|
||||
void fillRect (LowLevelGraphicsContext&, Rectangle<int> area) const;
|
||||
|
||||
/** Attempts to compile the program if necessary, and returns an error message if it fails. */
|
||||
Result checkCompilation (LowLevelGraphicsContext&);
|
||||
|
||||
/** Returns the code that was used to create this object. */
|
||||
const String& getFragmentShaderCode() const noexcept { return code; }
|
||||
|
||||
/** Optional lambda that will be called when the shader is activated, to allow
|
||||
user code to do setup tasks.
|
||||
*/
|
||||
std::function<void(OpenGLShaderProgram&)> onShaderActivated;
|
||||
|
||||
private:
|
||||
String code, hashName;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLGraphicsContextCustomShader)
|
||||
};
|
||||
|
||||
} // namespace juce
|
135
modules/juce_opengl/opengl/juce_OpenGLHelpers.cpp
Normal file
135
modules/juce_opengl/opengl/juce_OpenGLHelpers.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
void OpenGLHelpers::resetErrorState()
|
||||
{
|
||||
while (glGetError() != GL_NO_ERROR) {}
|
||||
}
|
||||
|
||||
void* OpenGLHelpers::getExtensionFunction (const char* functionName)
|
||||
{
|
||||
#if JUCE_WINDOWS
|
||||
return (void*) wglGetProcAddress (functionName);
|
||||
#elif JUCE_LINUX
|
||||
return (void*) glXGetProcAddress ((const GLubyte*) functionName);
|
||||
#else
|
||||
static void* handle = dlopen (nullptr, RTLD_LAZY);
|
||||
return dlsym (handle, functionName);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
|
||||
{
|
||||
jassert (extensionName != nullptr); // you must supply a genuine string for this.
|
||||
jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
|
||||
|
||||
const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
|
||||
jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
|
||||
|
||||
for (;;)
|
||||
{
|
||||
const char* found = strstr (extensions, extensionName);
|
||||
|
||||
if (found == nullptr)
|
||||
break;
|
||||
|
||||
extensions = found + strlen (extensionName);
|
||||
|
||||
if (extensions[0] == ' ' || extensions[0] == 0)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void OpenGLHelpers::clear (Colour colour)
|
||||
{
|
||||
glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
|
||||
colour.getFloatBlue(), colour.getFloatAlpha());
|
||||
|
||||
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void OpenGLHelpers::enableScissorTest (Rectangle<int> clip)
|
||||
{
|
||||
glEnable (GL_SCISSOR_TEST);
|
||||
glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
|
||||
}
|
||||
|
||||
String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
|
||||
{
|
||||
#if JUCE_OPENGL3
|
||||
if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
|
||||
{
|
||||
String output;
|
||||
|
||||
#if JUCE_ANDROID
|
||||
{
|
||||
int numAttributes = 0;
|
||||
|
||||
for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
|
||||
numAttributes++;
|
||||
|
||||
int last = 0;
|
||||
|
||||
for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
|
||||
{
|
||||
output += code.substring (last, p) + "layout(location=" + String (--numAttributes) + ") in ";
|
||||
|
||||
last = p + 10;
|
||||
}
|
||||
|
||||
output += code.substring (last);
|
||||
}
|
||||
#else
|
||||
output = code.replace ("attribute", "in");
|
||||
#endif
|
||||
|
||||
return JUCE_GLSL_VERSION "\n" + output.replace ("varying", "out");
|
||||
}
|
||||
#endif
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
|
||||
{
|
||||
#if JUCE_OPENGL3
|
||||
if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
|
||||
return JUCE_GLSL_VERSION "\n"
|
||||
"out " JUCE_MEDIUMP " vec4 fragColor;\n"
|
||||
+ code.replace ("varying", "in")
|
||||
.replace ("texture2D", "texture")
|
||||
.replace ("gl_FragColor", "fragColor");
|
||||
#endif
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
} // namespace juce
|
75
modules/juce_opengl/opengl/juce_OpenGLHelpers.h
Normal file
75
modules/juce_opengl/opengl/juce_OpenGLHelpers.h
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A set of miscellaneous openGL helper functions.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLHelpers
|
||||
{
|
||||
public:
|
||||
/** Clears the GL error state. */
|
||||
static void resetErrorState();
|
||||
|
||||
/** Returns true if the current thread has an active OpenGL context. */
|
||||
static bool isContextActive();
|
||||
|
||||
/** Clears the current context using the given colour. */
|
||||
static void clear (Colour colour);
|
||||
|
||||
static void enableScissorTest (Rectangle<int> clip);
|
||||
|
||||
/** Checks whether the current context supports the specified extension. */
|
||||
static bool isExtensionSupported (const char* extensionName);
|
||||
|
||||
/** Returns the address of a named GL extension function */
|
||||
static void* getExtensionFunction (const char* functionName);
|
||||
|
||||
/** Makes some simple textual changes to a shader program to try to convert old GLSL
|
||||
keywords to their v3 equivalents.
|
||||
|
||||
Before doing this, the function will check whether the current context is actually
|
||||
using a later version of the language, and if not it will not make any changes.
|
||||
Obviously this is not a real parser, so will only work on simple code!
|
||||
*/
|
||||
static String translateVertexShaderToV3 (const String&);
|
||||
|
||||
/** Makes some simple textual changes to a shader program to try to convert old GLSL
|
||||
keywords to their v3 equivalents.
|
||||
|
||||
Before doing this, the function will check whether the current context is actually
|
||||
using a later version of the language, and if not it will not make any changes.
|
||||
Obviously this is not a real parser, so will only work on simple code!
|
||||
*/
|
||||
static String translateFragmentShaderToV3 (const String&);
|
||||
};
|
||||
|
||||
} // namespace juce
|
207
modules/juce_opengl/opengl/juce_OpenGLImage.cpp
Normal file
207
modules/juce_opengl/opengl/juce_OpenGLImage.cpp
Normal file
@ -0,0 +1,207 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLFrameBufferImage : public ImagePixelData
|
||||
{
|
||||
public:
|
||||
OpenGLFrameBufferImage (OpenGLContext& c, int w, int h)
|
||||
: ImagePixelData (Image::ARGB, w, h),
|
||||
context (c),
|
||||
pixelStride (4),
|
||||
lineStride (width * pixelStride)
|
||||
{
|
||||
}
|
||||
|
||||
bool initialise()
|
||||
{
|
||||
return frameBuffer.initialise (context, width, height);
|
||||
}
|
||||
|
||||
LowLevelGraphicsContext* createLowLevelContext() override
|
||||
{
|
||||
sendDataChangeMessage();
|
||||
return createOpenGLGraphicsContext (context, frameBuffer);
|
||||
}
|
||||
|
||||
ImageType* createType() const override { return new OpenGLImageType(); }
|
||||
|
||||
ImagePixelData::Ptr clone() override
|
||||
{
|
||||
Image newImage (new OpenGLFrameBufferImage (context, width, height));
|
||||
Graphics g (newImage);
|
||||
g.drawImageAt (Image (this), 0, 0, false);
|
||||
|
||||
return newImage.getPixelData();
|
||||
}
|
||||
|
||||
void initialiseBitmapData (Image::BitmapData& bitmapData, int x, int y, Image::BitmapData::ReadWriteMode mode) override
|
||||
{
|
||||
bitmapData.pixelFormat = pixelFormat;
|
||||
bitmapData.lineStride = lineStride;
|
||||
bitmapData.pixelStride = pixelStride;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case Image::BitmapData::writeOnly: DataReleaser<Dummy, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
|
||||
case Image::BitmapData::readOnly: DataReleaser<Reader, Dummy> ::initialise (frameBuffer, bitmapData, x, y); break;
|
||||
case Image::BitmapData::readWrite: DataReleaser<Reader, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
|
||||
default: jassertfalse; break;
|
||||
}
|
||||
|
||||
if (mode != Image::BitmapData::readOnly)
|
||||
sendDataChangeMessage();
|
||||
}
|
||||
|
||||
OpenGLContext& context;
|
||||
OpenGLFrameBuffer frameBuffer;
|
||||
|
||||
private:
|
||||
int pixelStride, lineStride;
|
||||
|
||||
struct Dummy
|
||||
{
|
||||
Dummy (OpenGLFrameBuffer&, int, int, int, int) noexcept {}
|
||||
static void read (OpenGLFrameBuffer&, Image::BitmapData& , int, int) noexcept {}
|
||||
static void write (const PixelARGB*) noexcept {}
|
||||
};
|
||||
|
||||
struct Reader
|
||||
{
|
||||
static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
|
||||
{
|
||||
frameBuffer.readPixels ((PixelARGB*) bitmapData.data,
|
||||
Rectangle<int> (x, frameBuffer.getHeight() - (y + bitmapData.height), bitmapData.width, bitmapData.height));
|
||||
|
||||
verticalRowFlip ((PixelARGB*) bitmapData.data, bitmapData.width, bitmapData.height);
|
||||
}
|
||||
|
||||
static void verticalRowFlip (PixelARGB* const data, const int w, const int h)
|
||||
{
|
||||
HeapBlock<PixelARGB> tempRow (w);
|
||||
auto rowSize = sizeof (PixelARGB) * (size_t) w;
|
||||
|
||||
for (int y = 0; y < h / 2; ++y)
|
||||
{
|
||||
PixelARGB* const row1 = data + y * w;
|
||||
PixelARGB* const row2 = data + (h - 1 - y) * w;
|
||||
memcpy (tempRow, row1, rowSize);
|
||||
memcpy (row1, row2, rowSize);
|
||||
memcpy (row2, tempRow, rowSize);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct Writer
|
||||
{
|
||||
Writer (OpenGLFrameBuffer& fb, int x, int y, int w, int h) noexcept
|
||||
: frameBuffer (fb), area (x, y, w, h)
|
||||
{}
|
||||
|
||||
void write (const PixelARGB* const data) const noexcept
|
||||
{
|
||||
HeapBlock<PixelARGB> invertedCopy (area.getWidth() * area.getHeight());
|
||||
auto rowSize = sizeof (PixelARGB) * (size_t) area.getWidth();
|
||||
|
||||
for (int y = 0; y < area.getHeight(); ++y)
|
||||
memcpy (invertedCopy + area.getWidth() * y,
|
||||
data + area.getWidth() * (area.getHeight() - 1 - y), rowSize);
|
||||
|
||||
frameBuffer.writePixels (invertedCopy, area);
|
||||
}
|
||||
|
||||
OpenGLFrameBuffer& frameBuffer;
|
||||
const Rectangle<int> area;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (Writer)
|
||||
};
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
|
||||
{
|
||||
DataReleaser (OpenGLFrameBuffer& fb, int x, int y, int w, int h)
|
||||
: data ((size_t) (w * h)),
|
||||
writer (fb, x, y, w, h)
|
||||
{}
|
||||
|
||||
~DataReleaser()
|
||||
{
|
||||
writer.write (data);
|
||||
}
|
||||
|
||||
static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
|
||||
{
|
||||
auto* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
|
||||
bitmapData.dataReleaser.reset (r);
|
||||
|
||||
bitmapData.data = (uint8*) r->data.get();
|
||||
bitmapData.lineStride = (bitmapData.width * bitmapData.pixelStride + 3) & ~3;
|
||||
|
||||
ReaderType::read (frameBuffer, bitmapData, x, y);
|
||||
}
|
||||
|
||||
HeapBlock<PixelARGB> data;
|
||||
WriterType writer;
|
||||
};
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBufferImage)
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
OpenGLImageType::OpenGLImageType() {}
|
||||
OpenGLImageType::~OpenGLImageType() {}
|
||||
|
||||
int OpenGLImageType::getTypeID() const
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
|
||||
ImagePixelData::Ptr OpenGLImageType::create (Image::PixelFormat, int width, int height, bool /*shouldClearImage*/) const
|
||||
{
|
||||
OpenGLContext* currentContext = OpenGLContext::getCurrentContext();
|
||||
jassert (currentContext != nullptr); // an OpenGL image can only be created when a valid context is active!
|
||||
|
||||
std::unique_ptr<OpenGLFrameBufferImage> im (new OpenGLFrameBufferImage (*currentContext, width, height));
|
||||
|
||||
if (! im->initialise())
|
||||
return ImagePixelData::Ptr();
|
||||
|
||||
im->frameBuffer.clear (Colours::transparentBlack);
|
||||
return im.release();
|
||||
}
|
||||
|
||||
OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
|
||||
{
|
||||
if (OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData()))
|
||||
return &(glImage->frameBuffer);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace juce
|
55
modules/juce_opengl/opengl/juce_OpenGLImage.h
Normal file
55
modules/juce_opengl/opengl/juce_OpenGLImage.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A type of ImagePixelData that stores its image data in an OpenGL
|
||||
framebuffer, allowing a JUCE Image object to wrap a framebuffer.
|
||||
|
||||
By creating an Image from an instance of an OpenGLFrameBufferImage,
|
||||
you can then use a Graphics object to draw into the framebuffer using normal
|
||||
JUCE 2D operations.
|
||||
|
||||
@see Image, ImageType, ImagePixelData, OpenGLFrameBuffer
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLImageType : public ImageType
|
||||
{
|
||||
public:
|
||||
OpenGLImageType();
|
||||
~OpenGLImageType();
|
||||
|
||||
ImagePixelData::Ptr create (Image::PixelFormat, int width, int height, bool shouldClearImage) const override;
|
||||
int getTypeID() const override;
|
||||
|
||||
static OpenGLFrameBuffer* getFrameBufferFrom (const Image&);
|
||||
};
|
||||
|
||||
} // namespace juce
|
68
modules/juce_opengl/opengl/juce_OpenGLPixelFormat.cpp
Normal file
68
modules/juce_opengl/opengl/juce_OpenGLPixelFormat.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLPixelFormat::OpenGLPixelFormat (const int bitsPerRGBComponent,
|
||||
const int alphaBits_,
|
||||
const int depthBufferBits_,
|
||||
const int stencilBufferBits_) noexcept
|
||||
: redBits (bitsPerRGBComponent),
|
||||
greenBits (bitsPerRGBComponent),
|
||||
blueBits (bitsPerRGBComponent),
|
||||
alphaBits (alphaBits_),
|
||||
depthBufferBits (depthBufferBits_),
|
||||
stencilBufferBits (stencilBufferBits_),
|
||||
accumulationBufferRedBits (0),
|
||||
accumulationBufferGreenBits (0),
|
||||
accumulationBufferBlueBits (0),
|
||||
accumulationBufferAlphaBits (0),
|
||||
multisamplingLevel (0)
|
||||
{
|
||||
}
|
||||
|
||||
bool OpenGLPixelFormat::operator== (const OpenGLPixelFormat& other) const noexcept
|
||||
{
|
||||
return redBits == other.redBits
|
||||
&& greenBits == other.greenBits
|
||||
&& blueBits == other.blueBits
|
||||
&& alphaBits == other.alphaBits
|
||||
&& depthBufferBits == other.depthBufferBits
|
||||
&& stencilBufferBits == other.stencilBufferBits
|
||||
&& accumulationBufferRedBits == other.accumulationBufferRedBits
|
||||
&& accumulationBufferGreenBits == other.accumulationBufferGreenBits
|
||||
&& accumulationBufferBlueBits == other.accumulationBufferBlueBits
|
||||
&& accumulationBufferAlphaBits == other.accumulationBufferAlphaBits
|
||||
&& multisamplingLevel == other.multisamplingLevel;
|
||||
}
|
||||
|
||||
bool OpenGLPixelFormat::operator!= (const OpenGLPixelFormat& other) const noexcept
|
||||
{
|
||||
return ! operator== (other);
|
||||
}
|
||||
|
||||
} // namespace juce
|
72
modules/juce_opengl/opengl/juce_OpenGLPixelFormat.h
Normal file
72
modules/juce_opengl/opengl/juce_OpenGLPixelFormat.h
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Represents the various properties of an OpenGL pixel format.
|
||||
|
||||
@see OpenGLContext::setPixelFormat
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLPixelFormat
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates an OpenGLPixelFormat.
|
||||
|
||||
The default constructor just initialises the object as a simple 8-bit
|
||||
RGBA format.
|
||||
*/
|
||||
OpenGLPixelFormat (int bitsPerRGBComponent = 8,
|
||||
int alphaBits = 8,
|
||||
int depthBufferBits = 16,
|
||||
int stencilBufferBits = 0) noexcept;
|
||||
|
||||
bool operator== (const OpenGLPixelFormat&) const noexcept;
|
||||
bool operator!= (const OpenGLPixelFormat&) const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
int redBits; /**< The number of bits per pixel to use for the red channel. */
|
||||
int greenBits; /**< The number of bits per pixel to use for the green channel. */
|
||||
int blueBits; /**< The number of bits per pixel to use for the blue channel. */
|
||||
int alphaBits; /**< The number of bits per pixel to use for the alpha channel. */
|
||||
|
||||
int depthBufferBits; /**< The number of bits per pixel to use for a depth buffer. */
|
||||
int stencilBufferBits; /**< The number of bits per pixel to use for a stencil buffer. */
|
||||
|
||||
int accumulationBufferRedBits; /**< The number of bits per pixel to use for an accumulation buffer's red channel. */
|
||||
int accumulationBufferGreenBits; /**< The number of bits per pixel to use for an accumulation buffer's green channel. */
|
||||
int accumulationBufferBlueBits; /**< The number of bits per pixel to use for an accumulation buffer's blue channel. */
|
||||
int accumulationBufferAlphaBits; /**< The number of bits per pixel to use for an accumulation buffer's alpha channel. */
|
||||
|
||||
uint8 multisamplingLevel; /**< The number of samples to use for full-scene multisampled anti-aliasing (if available). */
|
||||
};
|
||||
|
||||
} // namespace juce
|
82
modules/juce_opengl/opengl/juce_OpenGLRenderer.h
Normal file
82
modules/juce_opengl/opengl/juce_OpenGLRenderer.h
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A base class that should be implemented by classes which want to render openGL
|
||||
on a background thread.
|
||||
|
||||
@see OpenGLContext
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLRenderer
|
||||
{
|
||||
public:
|
||||
OpenGLRenderer() {}
|
||||
virtual ~OpenGLRenderer() {}
|
||||
|
||||
/** Called when a new GL context has been created.
|
||||
You can use this as an opportunity to create your textures, shaders, etc.
|
||||
When the method is invoked, the new GL context will be active.
|
||||
Note that this callback will be made on a background thread, so make sure
|
||||
that your implementation is thread-safe.
|
||||
*/
|
||||
virtual void newOpenGLContextCreated() = 0;
|
||||
|
||||
/** Called when you should render the next openGL frame.
|
||||
|
||||
Note that this callback will be made on a background thread.
|
||||
|
||||
If the context is attached to a component in order to do component rendering,
|
||||
then the MessageManager will be locked when this callback is made.
|
||||
|
||||
If no component rendering is being done, then the MessageManager will not be
|
||||
locked, and you'll need to make sure your code is thread-safe in any
|
||||
interactions it has with your GUI classes.
|
||||
|
||||
For information about how to trigger a render callback, see
|
||||
OpenGLContext::triggerRepaint() and OpenGLContext::setContinuousRepainting().
|
||||
*/
|
||||
virtual void renderOpenGL() = 0;
|
||||
|
||||
/** Called when the current openGL context is about to close.
|
||||
You can use this opportunity to release any GL resources that you may have
|
||||
created.
|
||||
|
||||
Note that this callback will be made on a background thread, so make sure
|
||||
that your implementation is thread-safe.
|
||||
|
||||
(Also note that on Android, this callback won't happen, because there's currently
|
||||
no way to implement it..)
|
||||
*/
|
||||
virtual void openGLContextClosing() = 0;
|
||||
};
|
||||
|
||||
} // namespace juce
|
193
modules/juce_opengl/opengl/juce_OpenGLShaderProgram.cpp
Normal file
193
modules/juce_opengl/opengl/juce_OpenGLShaderProgram.cpp
Normal file
@ -0,0 +1,193 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLShaderProgram::OpenGLShaderProgram (const OpenGLContext& c) noexcept : context (c)
|
||||
{
|
||||
}
|
||||
|
||||
OpenGLShaderProgram::~OpenGLShaderProgram() noexcept
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
GLuint OpenGLShaderProgram::getProgramID() const noexcept
|
||||
{
|
||||
if (programID == 0)
|
||||
{
|
||||
// This method should only be called when the current thread has an active OpenGL context.
|
||||
jassert (OpenGLHelpers::isContextActive());
|
||||
|
||||
programID = context.extensions.glCreateProgram();
|
||||
}
|
||||
|
||||
return programID;
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::release() noexcept
|
||||
{
|
||||
if (programID != 0)
|
||||
{
|
||||
context.extensions.glDeleteProgram (programID);
|
||||
programID = 0;
|
||||
}
|
||||
}
|
||||
|
||||
double OpenGLShaderProgram::getLanguageVersion()
|
||||
{
|
||||
return String::fromUTF8 ((const char*) glGetString (GL_SHADING_LANGUAGE_VERSION))
|
||||
.retainCharacters("1234567890.").getDoubleValue();
|
||||
}
|
||||
|
||||
bool OpenGLShaderProgram::addShader (const String& code, GLenum type)
|
||||
{
|
||||
GLuint shaderID = context.extensions.glCreateShader (type);
|
||||
|
||||
const GLchar* c = code.toRawUTF8();
|
||||
context.extensions.glShaderSource (shaderID, 1, &c, nullptr);
|
||||
|
||||
context.extensions.glCompileShader (shaderID);
|
||||
|
||||
GLint status = GL_FALSE;
|
||||
context.extensions.glGetShaderiv (shaderID, GL_COMPILE_STATUS, &status);
|
||||
|
||||
if (status == GL_FALSE)
|
||||
{
|
||||
GLchar infoLog [16384];
|
||||
GLsizei infoLogLength = 0;
|
||||
context.extensions.glGetShaderInfoLog (shaderID, sizeof (infoLog), &infoLogLength, infoLog);
|
||||
errorLog = String (infoLog, (size_t) infoLogLength);
|
||||
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
// Your GLSL code contained compile errors!
|
||||
// Hopefully this compile log should help to explain what went wrong.
|
||||
DBG (errorLog);
|
||||
jassertfalse;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
context.extensions.glAttachShader (getProgramID(), shaderID);
|
||||
context.extensions.glDeleteShader (shaderID);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenGLShaderProgram::addVertexShader (const String& code) { return addShader (code, GL_VERTEX_SHADER); }
|
||||
bool OpenGLShaderProgram::addFragmentShader (const String& code) { return addShader (code, GL_FRAGMENT_SHADER); }
|
||||
|
||||
bool OpenGLShaderProgram::link() noexcept
|
||||
{
|
||||
// This method can only be used when the current thread has an active OpenGL context.
|
||||
jassert (OpenGLHelpers::isContextActive());
|
||||
|
||||
GLuint progID = getProgramID();
|
||||
|
||||
context.extensions.glLinkProgram (progID);
|
||||
|
||||
GLint status = GL_FALSE;
|
||||
context.extensions.glGetProgramiv (progID, GL_LINK_STATUS, &status);
|
||||
|
||||
if (status == GL_FALSE)
|
||||
{
|
||||
GLchar infoLog [16384];
|
||||
GLsizei infoLogLength = 0;
|
||||
context.extensions.glGetProgramInfoLog (progID, sizeof (infoLog), &infoLogLength, infoLog);
|
||||
errorLog = String (infoLog, (size_t) infoLogLength);
|
||||
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
// Your GLSL code contained link errors!
|
||||
// Hopefully this compile log should help to explain what went wrong.
|
||||
DBG (errorLog);
|
||||
jassertfalse;
|
||||
#endif
|
||||
}
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
return status != GL_FALSE;
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::use() const noexcept
|
||||
{
|
||||
// The shader program must have been successfully linked when this method is called!
|
||||
jassert (programID != 0);
|
||||
|
||||
context.extensions.glUseProgram (programID);
|
||||
}
|
||||
|
||||
GLint OpenGLShaderProgram::getUniformIDFromName (const char* uniformName) const noexcept
|
||||
{
|
||||
// The shader program must be active when this method is called!
|
||||
jassert (programID != 0);
|
||||
|
||||
return (GLint) context.extensions.glGetUniformLocation (programID, uniformName);
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1) noexcept { context.extensions.glUniform1f (getUniformIDFromName (name), n1); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLint n1) noexcept { context.extensions.glUniform1i (getUniformIDFromName (name), n1); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1, GLfloat n2) noexcept { context.extensions.glUniform2f (getUniformIDFromName (name), n1, n2); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1, GLfloat n2, GLfloat n3) noexcept { context.extensions.glUniform3f (getUniformIDFromName (name), n1, n2, n3); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1, GLfloat n2, GLfloat n3, float n4) noexcept { context.extensions.glUniform4f (getUniformIDFromName (name), n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLint n1, GLint n2, GLint n3, GLint n4) noexcept { context.extensions.glUniform4i (getUniformIDFromName (name), n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, const GLfloat* values, GLsizei numValues) noexcept { context.extensions.glUniform1fv (getUniformIDFromName (name), numValues, values); }
|
||||
void OpenGLShaderProgram::setUniformMat2 (const char* name, const GLfloat* v, GLint num, GLboolean trns) noexcept { context.extensions.glUniformMatrix2fv (getUniformIDFromName (name), num, trns, v); }
|
||||
void OpenGLShaderProgram::setUniformMat3 (const char* name, const GLfloat* v, GLint num, GLboolean trns) noexcept { context.extensions.glUniformMatrix3fv (getUniformIDFromName (name), num, trns, v); }
|
||||
void OpenGLShaderProgram::setUniformMat4 (const char* name, const GLfloat* v, GLint num, GLboolean trns) noexcept { context.extensions.glUniformMatrix4fv (getUniformIDFromName (name), num, trns, v); }
|
||||
|
||||
//==============================================================================
|
||||
OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
|
||||
: attributeID ((GLuint) program.context.extensions.glGetAttribLocation (program.getProgramID(), name))
|
||||
{
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
jassert ((GLint) attributeID >= 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
|
||||
: uniformID (program.context.extensions.glGetUniformLocation (program.getProgramID(), name)), context (program.context)
|
||||
{
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
jassert (uniformID >= 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
|
||||
|
||||
void OpenGLShaderProgram::Uniform::setMatrix2 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix2fv (uniformID, num, trns, v); }
|
||||
void OpenGLShaderProgram::Uniform::setMatrix3 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix3fv (uniformID, num, trns, v); }
|
||||
void OpenGLShaderProgram::Uniform::setMatrix4 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix4fv (uniformID, num, trns, v); }
|
||||
|
||||
} // namespace juce
|
205
modules/juce_opengl/opengl/juce_OpenGLShaderProgram.h
Normal file
205
modules/juce_opengl/opengl/juce_OpenGLShaderProgram.h
Normal file
@ -0,0 +1,205 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Manages an OpenGL shader program.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLShaderProgram
|
||||
{
|
||||
public:
|
||||
/** Creates a shader for use in a particular GL context. */
|
||||
OpenGLShaderProgram (const OpenGLContext&) noexcept;
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLShaderProgram() noexcept;
|
||||
|
||||
/** Returns the version of GLSL that the current context supports.
|
||||
E.g.
|
||||
@code
|
||||
if (OpenGLShaderProgram::getLanguageVersion() > 1.199)
|
||||
{
|
||||
// ..do something that requires GLSL 1.2 or above..
|
||||
}
|
||||
@endcode
|
||||
*/
|
||||
static double getLanguageVersion();
|
||||
|
||||
/** Compiles and adds a shader to this program.
|
||||
|
||||
After adding all your shaders, remember to call link() to link them into
|
||||
a usable program.
|
||||
|
||||
If your app is built in debug mode, this method will assert if the program
|
||||
fails to compile correctly.
|
||||
|
||||
The shaderType parameter could be GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, etc.
|
||||
|
||||
@returns true if the shader compiled successfully. If not, you can call
|
||||
getLastError() to find out what happened.
|
||||
*/
|
||||
bool addShader (const String& shaderSourceCode, GLenum shaderType);
|
||||
|
||||
/** Compiles and adds a fragment shader to this program.
|
||||
This is equivalent to calling addShader() with a type of GL_VERTEX_SHADER.
|
||||
*/
|
||||
bool addVertexShader (const String& shaderSourceCode);
|
||||
|
||||
/** Compiles and adds a fragment shader to this program.
|
||||
This is equivalent to calling addShader() with a type of GL_FRAGMENT_SHADER.
|
||||
*/
|
||||
bool addFragmentShader (const String& shaderSourceCode);
|
||||
|
||||
/** Links all the compiled shaders into a usable program.
|
||||
If your app is built in debug mode, this method will assert if the program
|
||||
fails to link correctly.
|
||||
@returns true if the program linked successfully. If not, you can call
|
||||
getLastError() to find out what happened.
|
||||
*/
|
||||
bool link() noexcept;
|
||||
|
||||
/** Get the output for the last shader compilation or link that failed. */
|
||||
const String& getLastError() const noexcept { return errorLog; }
|
||||
|
||||
/** Selects this program into the current context. */
|
||||
void use() const noexcept;
|
||||
|
||||
/** Deletes the program. */
|
||||
void release() noexcept;
|
||||
|
||||
//==============================================================================
|
||||
// Methods for setting shader uniforms without using a Uniform object (see below).
|
||||
// You must make sure this shader is the currently bound one before setting uniforms
|
||||
// with these functions.
|
||||
|
||||
/** Get the uniform ID from the variable name */
|
||||
GLint getUniformIDFromName (const char* uniformName) const noexcept;
|
||||
|
||||
/** Sets a float uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat value) noexcept;
|
||||
/** Sets an int uniform. */
|
||||
void setUniform (const char* uniformName, GLint value) noexcept;
|
||||
/** Sets a vec2 uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat x, GLfloat y) noexcept;
|
||||
/** Sets a vec3 uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat x, GLfloat y, GLfloat z) noexcept;
|
||||
/** Sets a vec4 uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat x, GLfloat y, GLfloat z, GLfloat w) noexcept;
|
||||
/** Sets a vec4 uniform. */
|
||||
void setUniform (const char* uniformName, GLint x, GLint y, GLint z, GLint w) noexcept;
|
||||
/** Sets a vector float uniform. */
|
||||
void setUniform (const char* uniformName, const GLfloat* values, GLsizei numValues) noexcept;
|
||||
/** Sets a 2x2 matrix float uniform. */
|
||||
void setUniformMat2 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
|
||||
/** Sets a 3x3 matrix float uniform. */
|
||||
void setUniformMat3 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
|
||||
/** Sets a 4x4 matrix float uniform. */
|
||||
void setUniformMat4 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Represents an openGL uniform value.
|
||||
After a program has been linked, you can create Uniform objects to let you
|
||||
set the uniforms that your shaders use.
|
||||
|
||||
Be careful not to call the set() functions unless the appropriate program
|
||||
is loaded into the current context.
|
||||
*/
|
||||
struct JUCE_API Uniform
|
||||
{
|
||||
/** Initialises a uniform.
|
||||
The program must have been successfully linked when this
|
||||
constructor is called.
|
||||
*/
|
||||
Uniform (const OpenGLShaderProgram& program, const char* uniformName);
|
||||
|
||||
/** Sets a float uniform. */
|
||||
void set (GLfloat n1) const noexcept;
|
||||
/** Sets an int uniform. */
|
||||
void set (GLint n1) const noexcept;
|
||||
/** Sets a vec2 uniform. */
|
||||
void set (GLfloat n1, GLfloat n2) const noexcept;
|
||||
/** Sets a vec3 uniform. */
|
||||
void set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept;
|
||||
/** Sets a vec4 uniform. */
|
||||
void set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept;
|
||||
/** Sets an ivec4 uniform. */
|
||||
void set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept;
|
||||
/** Sets a vector float uniform. */
|
||||
void set (const GLfloat* values, int numValues) const noexcept;
|
||||
/** Sets a 2x2 matrix float uniform. */
|
||||
void setMatrix2 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
|
||||
/** Sets a 3x3 matrix float uniform. */
|
||||
void setMatrix3 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
|
||||
/** Sets a 4x4 matrix float uniform. */
|
||||
void setMatrix4 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
|
||||
|
||||
/** The uniform's ID number.
|
||||
If the uniform couldn't be found, this value will be < 0.
|
||||
*/
|
||||
GLint uniformID;
|
||||
|
||||
private:
|
||||
const OpenGLContext& context;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Uniform)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** Represents an openGL vertex attribute value.
|
||||
After a program has been linked, you can create Attribute objects to let you
|
||||
set the attributes that your vertex shaders use.
|
||||
*/
|
||||
struct JUCE_API Attribute
|
||||
{
|
||||
/** Initialises an attribute.
|
||||
The program must have been successfully linked when this
|
||||
constructor is called.
|
||||
*/
|
||||
Attribute (const OpenGLShaderProgram&, const char* attributeName);
|
||||
|
||||
/** The attribute's ID number.
|
||||
If the uniform couldn't be found, this value will be < 0.
|
||||
*/
|
||||
GLuint attributeID;
|
||||
};
|
||||
|
||||
/** The ID number of the compiled program. */
|
||||
GLuint getProgramID() const noexcept;
|
||||
|
||||
private:
|
||||
const OpenGLContext& context;
|
||||
mutable GLuint programID = 0;
|
||||
String errorLog;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLShaderProgram)
|
||||
};
|
||||
|
||||
} // namespace juce
|
194
modules/juce_opengl/opengl/juce_OpenGLTexture.cpp
Normal file
194
modules/juce_opengl/opengl/juce_OpenGLTexture.cpp
Normal file
@ -0,0 +1,194 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
static int getAllowedTextureSize (int x)
|
||||
{
|
||||
#if JUCE_OPENGL_ALLOW_NON_POWER_OF_TWO_TEXTURES
|
||||
return x;
|
||||
#else
|
||||
return nextPowerOfTwo (x);
|
||||
#endif
|
||||
}
|
||||
|
||||
OpenGLTexture::OpenGLTexture()
|
||||
: textureID (0), width (0), height (0), ownerContext (nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
OpenGLTexture::~OpenGLTexture()
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
bool OpenGLTexture::isValidSize (int width, int height)
|
||||
{
|
||||
return isPowerOfTwo (width) && isPowerOfTwo (height);
|
||||
}
|
||||
|
||||
void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
|
||||
{
|
||||
ownerContext = OpenGLContext::getCurrentContext();
|
||||
|
||||
// Texture objects can only be created when the current thread has an active OpenGL
|
||||
// context. You'll need to create this object in one of the OpenGLContext's callbacks.
|
||||
jassert (ownerContext != nullptr);
|
||||
|
||||
if (textureID == 0)
|
||||
{
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
glGenTextures (1, &textureID);
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
|
||||
auto glMagFilter = (ownerContext->texMagFilter == OpenGLContext::linear ? GL_LINEAR : GL_NEAREST);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glMagFilter);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
else
|
||||
{
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
JUCE_CHECK_OPENGL_ERROR;
|
||||
}
|
||||
|
||||
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
width = getAllowedTextureSize (w);
|
||||
height = getAllowedTextureSize (h);
|
||||
|
||||
const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
|
||||
|
||||
if (width != w || height != h)
|
||||
{
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
|
||||
width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
|
||||
|
||||
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
|
||||
type, GL_UNSIGNED_BYTE, pixels);
|
||||
}
|
||||
else
|
||||
{
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
|
||||
w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
|
||||
}
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
template <class PixelType>
|
||||
struct Flipper
|
||||
{
|
||||
static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
|
||||
const int w, const int h)
|
||||
{
|
||||
dataCopy.malloc (w * h);
|
||||
|
||||
for (int y = 0; y < h; ++y)
|
||||
{
|
||||
auto* src = (const PixelType*) srcData;
|
||||
auto* dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
|
||||
|
||||
for (int x = 0; x < w; ++x)
|
||||
dst[x].set (src[x]);
|
||||
|
||||
srcData += lineStride;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void OpenGLTexture::loadImage (const Image& image)
|
||||
{
|
||||
const int imageW = image.getWidth();
|
||||
const int imageH = image.getHeight();
|
||||
|
||||
HeapBlock<PixelARGB> dataCopy;
|
||||
Image::BitmapData srcData (image, Image::BitmapData::readOnly);
|
||||
|
||||
switch (srcData.pixelFormat)
|
||||
{
|
||||
case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
|
||||
case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
|
||||
case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
create (imageW, imageH, dataCopy, JUCE_RGBA_FORMAT, true);
|
||||
}
|
||||
|
||||
void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
|
||||
{
|
||||
create (w, h, pixels, JUCE_RGBA_FORMAT, false);
|
||||
}
|
||||
|
||||
void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
|
||||
{
|
||||
create (w, h, pixels, GL_ALPHA, false);
|
||||
}
|
||||
|
||||
void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
|
||||
{
|
||||
HeapBlock<PixelARGB> flippedCopy;
|
||||
Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h);
|
||||
|
||||
create (w, h, flippedCopy, JUCE_RGBA_FORMAT, true);
|
||||
}
|
||||
|
||||
void OpenGLTexture::release()
|
||||
{
|
||||
if (textureID != 0)
|
||||
{
|
||||
// If the texture is deleted while the owner context is not active, it's
|
||||
// impossible to delete it, so this will be a leak until the context itself
|
||||
// is deleted.
|
||||
jassert (ownerContext == OpenGLContext::getCurrentContext());
|
||||
|
||||
if (ownerContext == OpenGLContext::getCurrentContext())
|
||||
{
|
||||
glDeleteTextures (1, &textureID);
|
||||
|
||||
textureID = 0;
|
||||
width = 0;
|
||||
height = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLTexture::bind() const
|
||||
{
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
}
|
||||
|
||||
void OpenGLTexture::unbind() const
|
||||
{
|
||||
glBindTexture (GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
} // namespace juce
|
105
modules/juce_opengl/opengl/juce_OpenGLTexture.h
Normal file
105
modules/juce_opengl/opengl/juce_OpenGLTexture.h
Normal file
@ -0,0 +1,105 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2017 - ROLI Ltd.
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
|
||||
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
|
||||
27th April 2017).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-5-licence
|
||||
Privacy Policy: www.juce.com/juce-5-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Creates an openGL texture from an Image.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLTexture
|
||||
{
|
||||
public:
|
||||
OpenGLTexture();
|
||||
~OpenGLTexture();
|
||||
|
||||
/** Creates a texture from the given image.
|
||||
|
||||
Note that if the image's dimensions aren't a power-of-two, the texture may
|
||||
be created with a larger size.
|
||||
|
||||
The image will be arranged so that its top-left corner is at texture
|
||||
coordinate (0, 1).
|
||||
*/
|
||||
void loadImage (const Image& image);
|
||||
|
||||
/** Creates a texture from a raw array of pixels.
|
||||
If width and height are not powers-of-two, the texture will be created with a
|
||||
larger size, and only the subsection (0, 0, width, height) will be initialised.
|
||||
The data is sent directly to the OpenGL driver without being flipped vertically,
|
||||
so the first pixel will be mapped onto texture coordinate (0, 0).
|
||||
*/
|
||||
void loadARGB (const PixelARGB* pixels, int width, int height);
|
||||
|
||||
/** Creates a texture from a raw array of pixels.
|
||||
This is like loadARGB, but will vertically flip the data so that the first
|
||||
pixel ends up at texture coordinate (0, 1), and if the width and height are
|
||||
not powers-of-two, it will compensate by using a larger texture size.
|
||||
*/
|
||||
void loadARGBFlipped (const PixelARGB* pixels, int width, int height);
|
||||
|
||||
/** Creates an alpha-channel texture from an array of alpha values.
|
||||
If width and height are not powers-of-two, the texture will be created with a
|
||||
larger size, and only the subsection (0, 0, width, height) will be initialised.
|
||||
The data is sent directly to the OpenGL driver without being flipped vertically,
|
||||
so the first pixel will be mapped onto texture coordinate (0, 0).
|
||||
*/
|
||||
void loadAlpha (const uint8* pixels, int width, int height);
|
||||
|
||||
/** Frees the texture, if there is one. */
|
||||
void release();
|
||||
|
||||
/** Binds the texture to the currently active openGL context. */
|
||||
void bind() const;
|
||||
|
||||
/** Unbinds the texture to the currently active openGL context. */
|
||||
void unbind() const;
|
||||
|
||||
/** Returns the GL texture ID number. */
|
||||
GLuint getTextureID() const noexcept { return textureID; }
|
||||
|
||||
int getWidth() const noexcept { return width; }
|
||||
int getHeight() const noexcept { return height; }
|
||||
|
||||
/** Returns true if a texture can be created with the given size.
|
||||
Some systems may require that the sizes are powers-of-two.
|
||||
*/
|
||||
static bool isValidSize (int width, int height);
|
||||
|
||||
private:
|
||||
GLuint textureID;
|
||||
int width, height;
|
||||
OpenGLContext* ownerContext;
|
||||
|
||||
void create (int w, int h, const void*, GLenum, bool topLeft);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLTexture)
|
||||
};
|
||||
|
||||
} // namespace juce
|
Reference in New Issue
Block a user