rsdProgram.cpp revision eb4fe18dd88634330f9566cbb9e785d8c7ec5813
1/*
2 * Copyright (C) 2011 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
33using namespace android;
34using namespace android::renderscript;
35
36bool rsdProgramVertexInit(const Context *rsc, const ProgramVertex *pv,
37                          const char* shader, uint32_t shaderLen) {
38    RsdShader *drv = new RsdShader(pv, GL_VERTEX_SHADER, shader, shaderLen);
39    pv->mHal.drv = drv;
40
41    return drv->createShader();
42}
43
44static void SyncProgramConstants(const Context *rsc, const Program *p) {
45    for (uint32_t ct=0; ct < p->mHal.state.texturesCount; ct++) {
46        const Allocation *a = p->mHal.state.textures[ct].get();
47        DrvAllocation *drvAlloc = (DrvAllocation *)a->mHal.drv;
48        if (drvAlloc->uploadDeferred) {
49            rsdAllocationSyncAll(rsc, a, RS_ALLOCATION_USAGE_SCRIPT);
50        }
51    }
52}
53
54void rsdProgramVertexSetActive(const Context *rsc, const ProgramVertex *pv) {
55    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
56
57    SyncProgramConstants(rsc, pv);
58    dc->gl.shaderCache->setActiveVertex((RsdShader*)pv->mHal.drv);
59}
60
61void rsdProgramVertexDestroy(const Context *rsc, const ProgramVertex *pv) {
62    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
63
64    RsdShader *drv = NULL;
65    if(pv->mHal.drv) {
66        drv = (RsdShader*)pv->mHal.drv;
67        if (rsc->props.mLogShaders) {
68            LOGV("Destroying vertex shader with ID %u", drv->getShaderID());
69        }
70        if (drv->getShaderID()) {
71            dc->gl.shaderCache->cleanupVertex(drv->getShaderID());
72        }
73        delete drv;
74    }
75}
76
77bool rsdProgramFragmentInit(const Context *rsc, const ProgramFragment *pf,
78                          const char* shader, uint32_t shaderLen) {
79    RsdShader *drv = new RsdShader(pf, GL_FRAGMENT_SHADER, shader, shaderLen);
80    pf->mHal.drv = drv;
81
82    return drv->createShader();
83}
84
85void rsdProgramFragmentSetActive(const Context *rsc, const ProgramFragment *pf) {
86    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
87
88    SyncProgramConstants(rsc, pf);
89    dc->gl.shaderCache->setActiveFragment((RsdShader*)pf->mHal.drv);
90}
91
92void rsdProgramFragmentDestroy(const Context *rsc, const ProgramFragment *pf) {
93    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
94
95    RsdShader *drv = NULL;
96    if(pf->mHal.drv) {
97        drv = (RsdShader*)pf->mHal.drv;
98        if (rsc->props.mLogShaders) {
99            LOGV("Destroying fragment shader with ID %u", drv->getShaderID());
100        }
101        if (drv->getShaderID()) {
102            dc->gl.shaderCache->cleanupFragment(drv->getShaderID());
103        }
104        delete drv;
105    }
106}
107
108
109