Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 1 | /* |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011-2012 The Android Open Source Project |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "rsdCore.h" |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 19 | #include "rsdAllocation.h" |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 20 | #include "rsdProgramVertex.h" |
| 21 | #include "rsdShader.h" |
| 22 | #include "rsdShaderCache.h" |
| 23 | |
| 24 | #include "rsContext.h" |
| 25 | #include "rsProgramVertex.h" |
| 26 | #include "rsProgramFragment.h" |
| 27 | |
| 28 | #include <GLES/gl.h> |
| 29 | #include <GLES/glext.h> |
| 30 | #include <GLES2/gl2.h> |
| 31 | #include <GLES2/gl2ext.h> |
| 32 | |
| 33 | using namespace android; |
| 34 | using namespace android::renderscript; |
| 35 | |
| 36 | bool rsdProgramVertexInit(const Context *rsc, const ProgramVertex *pv, |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 37 | const char* shader, size_t shaderLen, |
| 38 | const char** textureNames, size_t textureNamesCount, |
| 39 | const size_t *textureNamesLength) { |
| 40 | RsdShader *drv = new RsdShader(pv, GL_VERTEX_SHADER, shader, shaderLen, |
| 41 | textureNames, textureNamesCount, textureNamesLength); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 42 | pv->mHal.drv = drv; |
| 43 | |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 44 | return true; |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 47 | static void SyncProgramConstants(const Context *rsc, const Program *p) { |
| 48 | for (uint32_t ct=0; ct < p->mHal.state.texturesCount; ct++) { |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 49 | const Allocation *a = p->mHal.state.textures[ct]; |
Alex Sakhartchouk | bbc41c0 | 2011-08-05 15:27:25 -0700 | [diff] [blame] | 50 | if (!a) { |
| 51 | continue; |
| 52 | } |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 53 | DrvAllocation *drvAlloc = (DrvAllocation *)a->mHal.drv; |
| 54 | if (drvAlloc->uploadDeferred) { |
| 55 | rsdAllocationSyncAll(rsc, a, RS_ALLOCATION_USAGE_SCRIPT); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 60 | void rsdProgramVertexSetActive(const Context *rsc, const ProgramVertex *pv) { |
| 61 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 62 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 63 | SyncProgramConstants(rsc, pv); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 64 | dc->gl.shaderCache->setActiveVertex((RsdShader*)pv->mHal.drv); |
| 65 | } |
| 66 | |
| 67 | void rsdProgramVertexDestroy(const Context *rsc, const ProgramVertex *pv) { |
| 68 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 69 | |
| 70 | RsdShader *drv = NULL; |
| 71 | if(pv->mHal.drv) { |
| 72 | drv = (RsdShader*)pv->mHal.drv; |
| 73 | if (rsc->props.mLogShaders) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 74 | ALOGV("Destroying vertex shader with ID %u", drv->getShaderID()); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 75 | } |
| 76 | if (drv->getShaderID()) { |
| 77 | dc->gl.shaderCache->cleanupVertex(drv->getShaderID()); |
| 78 | } |
| 79 | delete drv; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | bool rsdProgramFragmentInit(const Context *rsc, const ProgramFragment *pf, |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 84 | const char* shader, size_t shaderLen, |
| 85 | const char** textureNames, size_t textureNamesCount, |
| 86 | const size_t *textureNamesLength) { |
| 87 | RsdShader *drv = new RsdShader(pf, GL_FRAGMENT_SHADER, shader, shaderLen, |
| 88 | textureNames, textureNamesCount, textureNamesLength); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 89 | pf->mHal.drv = drv; |
| 90 | |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 91 | return true; |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void rsdProgramFragmentSetActive(const Context *rsc, const ProgramFragment *pf) { |
| 95 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 96 | |
Jason Sams | 7e8aae7 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 97 | SyncProgramConstants(rsc, pf); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 98 | dc->gl.shaderCache->setActiveFragment((RsdShader*)pf->mHal.drv); |
| 99 | } |
| 100 | |
| 101 | void rsdProgramFragmentDestroy(const Context *rsc, const ProgramFragment *pf) { |
| 102 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 103 | |
| 104 | RsdShader *drv = NULL; |
| 105 | if(pf->mHal.drv) { |
| 106 | drv = (RsdShader*)pf->mHal.drv; |
| 107 | if (rsc->props.mLogShaders) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 108 | ALOGV("Destroying fragment shader with ID %u", drv->getShaderID()); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 109 | } |
| 110 | if (drv->getShaderID()) { |
| 111 | dc->gl.shaderCache->cleanupFragment(drv->getShaderID()); |
| 112 | } |
| 113 | delete drv; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |