| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /*
- Copyright (C) 2015 by Ivan Safrin
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- #include "PolyRenderer.h"
- #include "PolyCoreServices.h"
- #include "PolyCore.h"
- #include "PolyTexture.h"
- using namespace Polycode;
- GPUDrawBuffer::GPUDrawBuffer() {
-
- }
- GPUDrawBuffer::~GPUDrawBuffer() {
-
- }
- GraphicsInterface::GraphicsInterface() {
- }
- RenderThread::RenderThread() : interface(NULL) {
-
- options.filteringMode = Renderer::TEX_FILTERING_LINEAR;
- options.anisotropy = 0;
- options.createMipmaps = true;
-
- jobQueueMutex = Services()->getCore()->createMutex();
- }
- void RenderThread::runThread() {
- while(threadRunning) {
- Services()->getCore()->lockMutex(jobQueueMutex);
- if(jobQueue.size() > 0) {
- RendererThreadJob nextJob = jobQueue.front();
- jobQueue.pop();
- processJob(nextJob);
- }
- Services()->getCore()->unlockMutex(jobQueueMutex);
- }
- }
- void RenderThread::processDrawBuffer(GPUDrawBuffer *buffer) {
- interface->setViewport(buffer->viewport.x, buffer->viewport.y, buffer->viewport.w, buffer->viewport.h);
- interface->clearBuffers(true, true, true);
-
- for(int i=0; i < buffer->drawCalls.size(); i++) {
- if(buffer->drawCalls[i].material) {
-
- ShaderBinding *localShaderBinding = buffer->drawCalls[i].shaderBinding;
-
- for(int s=0; s < buffer->drawCalls[i].material->getNumShaders(); s++) {
-
- Shader *shader = buffer->drawCalls[i].material->getShader(s);
- ShaderBinding *materialShaderBinding = buffer->drawCalls[i].material->getShaderBinding(s);
-
-
-
- // !!!!!!!!!!!!!!!!!!!!!!!!
- // TODO: Don't do string lookups on every frame for all this stuff, find a better way!!
- // !!!!!!!!!!!!!!!!!!!!!!!!
-
- // set shader uniforms
-
- for(int p=0; p < shader->expectedParams.size(); p++) {
- ProgramParam param = shader->expectedParams[p];
- LocalShaderParam *localParam = NULL;
- localParam = materialShaderBinding->getLocalParamByName(param.name);
-
- // local options override material options
-
- LocalShaderParam *localOptionsParam = localShaderBinding->getLocalParamByName(param.name);
- if(localOptionsParam) {
- localParam = localOptionsParam;
- }
-
- interface->setParamInShader(shader, param, localParam);
- }
-
- for(int a=0; a < shader->expectedAttributes.size(); a++) {
- ProgramAttribute attribute = shader->expectedAttributes[a];
- }
-
- // set shader attributes
-
- // glVertexAttribPointer(texCoordAttribute, 2, GL_FLOAT, false, 0, uvs);
- // glEnableVertexAttribArray(texCoordAttribute);
-
- // render with shader
- }
- }
- }
- }
- void RenderThread::processJob(const RendererThreadJob &job) {
- switch(job.jobType) {
- case JOB_REQUEST_CONTEXT_CHANGE:
- {
- VideoModeChangeInfo *modeInfo = (VideoModeChangeInfo*) job.data;
- core->handleVideoModeChange(modeInfo);
- delete modeInfo;
- }
- break;
- case JOB_CREATE_TEXTURE:
- {
- Texture *texture = (Texture*) job.data;
- interface->createTexture(texture, options.filteringMode, options.anisotropy, options.createMipmaps);
- }
- break;
- case JOB_PROCESS_DRAW_BUFFER:
- {
- GPUDrawBuffer *buffer = (GPUDrawBuffer*) job.data;
- processDrawBuffer(buffer);
- delete buffer;
- }
- break;
- case JOB_FLUSH_CONTEXT:
- {
- core->flushRenderContext();
- }
- break;
- case JOB_CREATE_PROGRAM:
- {
- ShaderProgram *program = (ShaderProgram*) job.data;
- interface->createProgram(program);
- }
- break;
- case JOB_CREATE_SHADER:
- {
- Shader *shader = (Shader*) job.data;
- interface->createShader(shader);
- }
- break;
- }
- }
- void RenderThread::enqueueJob(int jobType, void *data) {
- Services()->getCore()->lockMutex(jobQueueMutex);
- RendererThreadJob job;
- job.jobType = jobType;
- job.data = data;
- jobQueue.push(job);
- Services()->getCore()->unlockMutex(jobQueueMutex);
- }
- void RenderThread::setGraphicsInterface(Core *core, GraphicsInterface *interface) {
- this->interface = interface;
- this->core = core;
- }
- Renderer::Renderer() {
- renderThread = new RenderThread();
- Services()->getCore()->createThread(renderThread);
-
- cpuBufferIndex = 0;
- gpuBufferIndex = 1;
- }
- Renderer::~Renderer() {
-
- }
- void Renderer::setGraphicsInterface(Core *core, GraphicsInterface *interface) {
- renderThread->setGraphicsInterface(core, interface);
- }
- RenderThread *Renderer::getRenderThread() {
- return renderThread;
- }
- void Renderer::setBackingResolutionScale(Number xScale, Number yScale) {
- backingResolutionScaleX = xScale;
- backingResolutionScaleY = yScale;
- }
- Number Renderer::getBackingResolutionScaleX() {
- return backingResolutionScaleX;
- }
- Number Renderer::getBackingResolutionScaleY() {
- return backingResolutionScaleY;
- }
- Number Renderer::getAnisotropyAmount() {
- return anisotropy;
- }
- void Renderer::setAnisotropyAmount(Number amount) {
- anisotropy = amount;
- }
- Cubemap *Renderer::createCubemap(Texture *t0, Texture *t1, Texture *t2, Texture *t3, Texture *t4, Texture *t5) {
- return NULL;
- }
- void Renderer::processDrawBuffer(GPUDrawBuffer *buffer) {
- renderThread->enqueueJob(RenderThread::JOB_PROCESS_DRAW_BUFFER, buffer);
- }
- void Renderer::flushContext() {
- renderThread->enqueueJob(RenderThread::JOB_FLUSH_CONTEXT, NULL);
- }
- Texture *Renderer::createTexture(unsigned int width, unsigned int height, char *textureData, bool clamp, bool createMipmaps, int type) {
- Texture *texture = new Texture(width, height, textureData, clamp, createMipmaps, type);
- renderThread->enqueueJob(RenderThread::JOB_CREATE_TEXTURE, (void*)texture);
- return texture;
- }
- Shader *Renderer::createShader(ShaderProgram *vertexProgram, ShaderProgram *fragmentProgram) {
- Shader *shader = new Shader();
- shader->vertexProgram = vertexProgram;
- shader->fragmentProgram = fragmentProgram;
- renderThread->enqueueJob(RenderThread::JOB_CREATE_SHADER, (void*)shader);
- return shader;
- }
- ShaderProgram *Renderer::createProgram(const String &fileName) {
- ShaderProgram *program = new ShaderProgram(fileName);
-
- OSFileEntry fileEntry(program->getResourcePath(), OSFileEntry::TYPE_FILE);
-
- if(fileEntry.extension == "vert" ) {
- program->type = ShaderProgram::TYPE_VERT;
- } else {
- program->type = ShaderProgram::TYPE_FRAG;
- }
-
- renderThread->enqueueJob(RenderThread::JOB_CREATE_PROGRAM, (void*)program);
- return program;
- }
- void Renderer::destroyTexture(Texture *texture) {
-
- }
- void createRenderTextures(Texture **colorBuffer, Texture **depthBuffer, int width, int height, bool floatingPointBuffer) {
-
- }
|