1 /*
2  * Copyright (C) 2011-2012 The Android Open Source Project
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"
19 #include "rsdAllocation.h"
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 android::renderscript::Allocation;
34 using android::renderscript::Context;
35 using android::renderscript::Program;
36 using android::renderscript::ProgramFragment;
37 using android::renderscript::ProgramVertex;
38 
rsdProgramVertexInit(const Context * rsc,const ProgramVertex * pv,const char * shader,size_t shaderLen,const char ** textureNames,size_t textureNamesCount,const size_t * textureNamesLength)39 bool rsdProgramVertexInit(const Context *rsc, const ProgramVertex *pv,
40                           const char* shader, size_t shaderLen,
41                           const char** textureNames, size_t textureNamesCount,
42                           const size_t *textureNamesLength) {
43     RsdShader *drv = new RsdShader(pv, GL_VERTEX_SHADER, shader, shaderLen,
44                                    textureNames, textureNamesCount, textureNamesLength);
45     pv->mHal.drv = drv;
46 
47     return true;
48 }
49 
SyncProgramConstants(const Context * rsc,const Program * p)50 static void SyncProgramConstants(const Context *rsc, const Program *p) {
51     for (uint32_t ct=0; ct < p->mHal.state.texturesCount; ct++) {
52         const Allocation *a = p->mHal.state.textures[ct];
53         if (!a) {
54             continue;
55         }
56         DrvAllocation *drvAlloc = (DrvAllocation *)a->mHal.drv;
57         if (drvAlloc->uploadDeferred) {
58             rsdAllocationSyncAll(rsc, a, RS_ALLOCATION_USAGE_SCRIPT);
59         }
60     }
61 }
62 
rsdProgramVertexSetActive(const Context * rsc,const ProgramVertex * pv)63 void rsdProgramVertexSetActive(const Context *rsc, const ProgramVertex *pv) {
64     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
65 
66     SyncProgramConstants(rsc, pv);
67     dc->gl.shaderCache->setActiveVertex((RsdShader*)pv->mHal.drv);
68 }
69 
rsdProgramVertexDestroy(const Context * rsc,const ProgramVertex * pv)70 void rsdProgramVertexDestroy(const Context *rsc, const ProgramVertex *pv) {
71     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
72 
73     RsdShader *drv = nullptr;
74     if(pv->mHal.drv) {
75         drv = (RsdShader*)pv->mHal.drv;
76         if (rsc->props.mLogShaders) {
77             ALOGV("Destroying vertex shader with ID %p", (void*)pv);
78         }
79         if (drv->getStateBasedIDCount()) {
80             dc->gl.shaderCache->cleanupVertex(drv);
81         }
82         delete drv;
83     }
84 }
85 
rsdProgramFragmentInit(const Context * rsc,const ProgramFragment * pf,const char * shader,size_t shaderLen,const char ** textureNames,size_t textureNamesCount,const size_t * textureNamesLength)86 bool rsdProgramFragmentInit(const Context *rsc, const ProgramFragment *pf,
87                             const char* shader, size_t shaderLen,
88                             const char** textureNames, size_t textureNamesCount,
89                             const size_t *textureNamesLength) {
90     RsdShader *drv = new RsdShader(pf, GL_FRAGMENT_SHADER, shader, shaderLen,
91                                    textureNames, textureNamesCount, textureNamesLength);
92     pf->mHal.drv = drv;
93 
94     return true;
95 }
96 
rsdProgramFragmentSetActive(const Context * rsc,const ProgramFragment * pf)97 void rsdProgramFragmentSetActive(const Context *rsc, const ProgramFragment *pf) {
98     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
99 
100     SyncProgramConstants(rsc, pf);
101     dc->gl.shaderCache->setActiveFragment((RsdShader*)pf->mHal.drv);
102 }
103 
rsdProgramFragmentDestroy(const Context * rsc,const ProgramFragment * pf)104 void rsdProgramFragmentDestroy(const Context *rsc, const ProgramFragment *pf) {
105     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
106 
107     RsdShader *drv = nullptr;
108     if(pf->mHal.drv) {
109         drv = (RsdShader*)pf->mHal.drv;
110         if (rsc->props.mLogShaders) {
111             ALOGV("Destroying fragment shader with ID %p", (void*)pf);
112         }
113         if (drv->getStateBasedIDCount()) {
114             dc->gl.shaderCache->cleanupFragment(drv);
115         }
116         delete drv;
117     }
118 }
119 
120 
121