rsdProgram.cpp revision 6d6e114f8bea8db08bf1decb673d3eb9a8f258ae
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
33using namespace android;
34using namespace android::renderscript;
35
36bool rsdProgramVertexInit(const Context *rsc, const ProgramVertex *pv,
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);
42    pv->mHal.drv = drv;
43
44    return true;
45}
46
47static void SyncProgramConstants(const Context *rsc, const Program *p) {
48    for (uint32_t ct=0; ct < p->mHal.state.texturesCount; ct++) {
49        const Allocation *a = p->mHal.state.textures[ct];
50        if (!a) {
51            continue;
52        }
53        DrvAllocation *drvAlloc = (DrvAllocation *)a->mHal.drv;
54        if (drvAlloc->uploadDeferred) {
55            rsdAllocationSyncAll(rsc, a, RS_ALLOCATION_USAGE_SCRIPT);
56        }
57    }
58}
59
60void rsdProgramVertexSetActive(const Context *rsc, const ProgramVertex *pv) {
61    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
62
63    SyncProgramConstants(rsc, pv);
64    dc->gl.shaderCache->setActiveVertex((RsdShader*)pv->mHal.drv);
65}
66
67void 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) {
74            ALOGV("Destroying vertex shader with ID %u", (uint32_t)pv);
75        }
76        if (drv->getStateBasedIDCount()) {
77            dc->gl.shaderCache->cleanupVertex(drv);
78        }
79        delete drv;
80    }
81}
82
83bool rsdProgramFragmentInit(const Context *rsc, const ProgramFragment *pf,
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);
89    pf->mHal.drv = drv;
90
91    return true;
92}
93
94void rsdProgramFragmentSetActive(const Context *rsc, const ProgramFragment *pf) {
95    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
96
97    SyncProgramConstants(rsc, pf);
98    dc->gl.shaderCache->setActiveFragment((RsdShader*)pf->mHal.drv);
99}
100
101void 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) {
108            ALOGV("Destroying fragment shader with ID %u", (uint32_t)pf);
109        }
110        if (drv->getStateBasedIDCount()) {
111            dc->gl.shaderCache->cleanupFragment(drv);
112        }
113        delete drv;
114    }
115}
116
117
118