rsdProgram.cpp revision 6598201f1c4f409defac9a5af789fb53a7cc00f8
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];
47        if (!a) {
48            continue;
49        }
50        DrvAllocation *drvAlloc = (DrvAllocation *)a->mHal.drv;
51        if (drvAlloc->uploadDeferred) {
52            rsdAllocationSyncAll(rsc, a, RS_ALLOCATION_USAGE_SCRIPT);
53        }
54    }
55}
56
57void rsdProgramVertexSetActive(const Context *rsc, const ProgramVertex *pv) {
58    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
59
60    SyncProgramConstants(rsc, pv);
61    dc->gl.shaderCache->setActiveVertex((RsdShader*)pv->mHal.drv);
62}
63
64void rsdProgramVertexDestroy(const Context *rsc, const ProgramVertex *pv) {
65    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
66
67    RsdShader *drv = NULL;
68    if(pv->mHal.drv) {
69        drv = (RsdShader*)pv->mHal.drv;
70        if (rsc->props.mLogShaders) {
71            ALOGV("Destroying vertex shader with ID %u", drv->getShaderID());
72        }
73        if (drv->getShaderID()) {
74            dc->gl.shaderCache->cleanupVertex(drv->getShaderID());
75        }
76        delete drv;
77    }
78}
79
80bool rsdProgramFragmentInit(const Context *rsc, const ProgramFragment *pf,
81                          const char* shader, uint32_t shaderLen) {
82    RsdShader *drv = new RsdShader(pf, GL_FRAGMENT_SHADER, shader, shaderLen);
83    pf->mHal.drv = drv;
84
85    return drv->createShader();
86}
87
88void rsdProgramFragmentSetActive(const Context *rsc, const ProgramFragment *pf) {
89    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
90
91    SyncProgramConstants(rsc, pf);
92    dc->gl.shaderCache->setActiveFragment((RsdShader*)pf->mHal.drv);
93}
94
95void rsdProgramFragmentDestroy(const Context *rsc, const ProgramFragment *pf) {
96    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
97
98    RsdShader *drv = NULL;
99    if(pf->mHal.drv) {
100        drv = (RsdShader*)pf->mHal.drv;
101        if (rsc->props.mLogShaders) {
102            ALOGV("Destroying fragment shader with ID %u", drv->getShaderID());
103        }
104        if (drv->getShaderID()) {
105            dc->gl.shaderCache->cleanupFragment(drv->getShaderID());
106        }
107        delete drv;
108    }
109}
110
111
112