blob: 54484dfced3a8f1f8eb4c51af77d348d548d654a [file] [log] [blame]
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -07001/*
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"
Jason Samseb4fe182011-05-26 16:33:01 -070019#include "rsdAllocation.h"
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070020#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
Jason Samseb4fe182011-05-26 16:33:01 -070044static void SyncProgramConstants(const Context *rsc, const Program *p) {
45 for (uint32_t ct=0; ct < p->mHal.state.texturesCount; ct++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070046 const Allocation *a = p->mHal.state.textures[ct];
Alex Sakhartchoukba157302011-08-05 15:27:25 -070047 if (!a) {
48 continue;
49 }
Jason Samseb4fe182011-05-26 16:33:01 -070050 DrvAllocation *drvAlloc = (DrvAllocation *)a->mHal.drv;
51 if (drvAlloc->uploadDeferred) {
52 rsdAllocationSyncAll(rsc, a, RS_ALLOCATION_USAGE_SCRIPT);
53 }
54 }
55}
56
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070057void rsdProgramVertexSetActive(const Context *rsc, const ProgramVertex *pv) {
58 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
59
Jason Samseb4fe182011-05-26 16:33:01 -070060 SyncProgramConstants(rsc, pv);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070061 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) {
Steve Block65982012011-10-20 11:56:00 +010071 ALOGV("Destroying vertex shader with ID %u", drv->getShaderID());
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070072 }
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
Jason Samseb4fe182011-05-26 16:33:01 -070091 SyncProgramConstants(rsc, pf);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070092 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) {
Steve Block65982012011-10-20 11:56:00 +0100102 ALOGV("Destroying fragment shader with ID %u", drv->getShaderID());
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700103 }
104 if (drv->getShaderID()) {
105 dc->gl.shaderCache->cleanupFragment(drv->getShaderID());
106 }
107 delete drv;
108 }
109}
110
111