Skip to content

Commit 002ca98

Browse files
author
powertomato
committed
Added vertex_buffer/multitexturing testcase, implemented triangle-stripe rendering
1 parent 62d1835 commit 002ca98

37 files changed

+1058
-342
lines changed

Diff for: linux.mk

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ DEFINES+=-DSDL_SHADER_OPENGL \
22
-DSDL_SHADER_OPENGLES2 \
33
-DGL_GLEXT_PROTOTYPES
44

5-
TEST_CASES=color_mode context_switch all_the_shaders
5+
TEST_CASES=color_mode \
6+
context_switch \
7+
all_the_shaders \
8+
vertex_buffer
9+
610
GCC=gcc
711
GPP=g++
812
AR=ar
@@ -11,7 +15,7 @@ MKDIR_LIST=mkdir -p
1115
CPPFLAGS=-g3 --pedantic -Wall -std=c++11 $(DEFINES) -fno-rtti
1216
CFLAGS=-g3 --pedantic -Wall -std=c99 $(DEFINES)
1317
LDFLAGS=-lSDL2 -lSDL2_image \
14-
-ldl -lGL
18+
-ldl -lGL -lm
1519
EXE_EXT=
1620
RUN=#optirun
1721

Diff for: src/SDL_shader.c

+93-24
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ SDL_Shader* SDL_createShader_RW( SDL_Renderer *renderer,
7373

7474
}
7575

76-
void SDL_hint( sdl_shader_hint flag, void* value ){
76+
void SDL_shader_hint( sdl_shader_hint flag, void* value ){
7777
#ifdef SDL_SHADER_OPENGL
7878
SDL_GL_hint( flag, value );
7979
#endif
@@ -181,52 +181,121 @@ int SDL_destroyShader( SDL_Shader* shader ){
181181
int SDL_bindShader( SDL_Shader* shader ){
182182
return shader->bindShader( shader );
183183
}
184+
void SDL_updateViewport( SDL_Shader *shader ){
185+
shader->updateViewport( shader );
186+
}
187+
int SDL_renderVertexBuffer( SDL_Shader* shader, SDL_Vertex *vertices,
188+
SDL_Texture** textures, unsigned num_of_tex)
189+
{
190+
unsigned i;
191+
SDL_Renderer* renderer = shader->renderer;
192+
193+
if ( num_of_tex==0 )
194+
return 0;
195+
if ( num_of_tex >= 8 )
196+
return SDL_SetError("SDL_Shader: only 8 textures are supported");
197+
for ( i=0; i<num_of_tex; i++ ) {
198+
if (( textures[i]->w != textures[0]->w ||
199+
textures[i]->h != textures[0]->h ) && i!=0 )
200+
{
201+
return SDL_SetError("SDL_Shader: Textures must have the same size");
202+
}
203+
if (( textures[i]->format != textures[0]->format) && i!=0 ) {
204+
return SDL_SetError("SDL_Shader: Textures must have the same format");
205+
}
206+
if (renderer != textures[i]->renderer) {
207+
return SDL_SetError("SDL_Shader: Texture was not created with this renderer");
208+
}
209+
}
184210

185-
int SDL_renderCopyShd( SDL_Shader* shader, SDL_Texture* texture,
186-
const SDL_Rect * srcrect, const SDL_Rect * dstrect){
211+
return shader->renderCopyShd( shader, textures, num_of_tex,
212+
vertices, vertices->size );
213+
}
214+
215+
int SDL_renderCopyShdArray( SDL_Shader* shader, SDL_Texture** textures,
216+
const SDL_Rect * srcrect, const SDL_Rect * dstrect, unsigned num_of_tex)
217+
{
187218

188219
SDL_Renderer *renderer = shader->renderer;
220+
221+
if (renderer->hidden) {
222+
return 0;
223+
}
224+
189225
SDL_Rect real_srcrect = { 0, 0, 0, 0 };
190226
SDL_Rect real_dstrect = { 0, 0, 0, 0 };
191-
192-
if (renderer != texture->renderer) {
193-
return SDL_SetError("Texture was not created with this renderer");
194-
}
227+
unsigned i;
195228

196229
real_srcrect.x = 0;
197230
real_srcrect.y = 0;
198-
real_srcrect.w = texture->w;
199-
real_srcrect.h = texture->h;
231+
real_srcrect.w = textures[0]->w;
232+
real_srcrect.h = textures[0]->h;
200233
if (srcrect) {
201234
if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
202235
return 0;
203236
}
204237
}
205238

206-
SDL_RenderGetViewport(renderer, &real_dstrect);
207-
real_dstrect.x = 0;
208-
real_dstrect.y = 0;
239+
//XXX bug here
240+
//SDL_RenderGetViewport(renderer, &real_dstrect);
209241
if (dstrect) {
210-
if (!SDL_HasIntersection(dstrect, &real_dstrect)) {
242+
/*if (!SDL_HasIntersection(dstrect, &real_dstrect)) {
211243
return 0;
212-
}
244+
}*/
213245
real_dstrect = *dstrect;
214246
}
215247

216-
if (texture->native) {
217-
texture = texture->native;
218-
}
219-
220-
if (renderer->hidden) {
221-
return 0;
222-
}
248+
for( i=0; i<num_of_tex; i++ ) {
249+
if (textures[i]->native) {
250+
textures[i] = textures[i]->native;
251+
}
252+
}
223253

224-
real_dstrect.x *= renderer->scale.x;
254+
/*real_dstrect.x *= renderer->scale.x;
225255
real_dstrect.y *= renderer->scale.y;
226256
real_dstrect.w *= renderer->scale.x;
227-
real_dstrect.h *= renderer->scale.y;
257+
real_dstrect.h *= renderer->scale.y;*/
258+
259+
SDL_Vertex* vertices = shader->createVertexBuffer(4);
260+
261+
int r,g,b,a;
262+
r = textures[0]->r;
263+
g = textures[0]->g;
264+
b = textures[0]->b;
265+
a = textures[0]->a;
266+
vertices->setVertexColor( vertices, 0,4, r,g,b,a);
267+
268+
vertices->setVertexPosition( vertices, 0,1,
269+
real_dstrect.x, real_dstrect.y, 0.0f);
270+
vertices->setVertexPosition( vertices, 1,1,
271+
(real_dstrect.x + real_dstrect.w), real_dstrect.y, 0.0f);
272+
vertices->setVertexPosition( vertices, 2,1,
273+
real_dstrect.x, (real_dstrect.y + real_dstrect.h), 0.0f);
274+
vertices->setVertexPosition( vertices, 3,1,
275+
(real_dstrect.x + real_dstrect.w),
276+
(real_dstrect.y + real_dstrect.h), 0.0f);
277+
278+
float minu, maxu, minv, maxv;
279+
minu = real_srcrect.x / textures[0]->w;
280+
maxu = (real_srcrect.x + real_srcrect.w) / textures[0]->w;
281+
minv = real_srcrect.y / textures[0]->h;
282+
maxv = (real_srcrect.y + real_srcrect.h) / textures[0]->h;
283+
vertices->setVertexTexCoord( vertices, 0,1, minu, minv);
284+
vertices->setVertexTexCoord( vertices, 1,1, maxu, minv);
285+
vertices->setVertexTexCoord( vertices, 2,1, minu, maxv);
286+
vertices->setVertexTexCoord( vertices, 3,1, maxu, maxv);
287+
288+
int res = SDL_renderVertexBuffer( shader, vertices, textures, num_of_tex );
289+
shader->destroyVertexBuffer( vertices );
290+
return res;
291+
}
228292

229-
return shader->renderCopyShd( shader, texture, &real_srcrect, &real_dstrect );
293+
294+
int SDL_renderCopyShd( SDL_Shader* shader, SDL_Texture* texture,
295+
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
296+
{
297+
SDL_Texture *textures[] = {texture};
298+
return SDL_renderCopyShdArray( shader, textures, srcrect, dstrect, 1 );
230299
}
231300

232301
char* SDL_Shader_readRW( SDL_RWops* rwop ) {

Diff for: src/SDL_shader.h

+38-9
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ typedef enum {
3434
SDL_D3D_PS_MAJOR_VERSION,
3535
SDL_D3D_PS_MINOR_VERSION,
3636

37-
SDL_D3D11_VS_MAJOR_VERSION,
38-
SDL_D3D11_VS_MINOR_VERSION,
39-
SDL_D3D11_PS_MAJOR_VERSION,
40-
SDL_D3D11_PS_MINOR_VERSION,
37+
SDL_GL_TEX0_NAME,
38+
SDL_GL_TEX1_NAME,
39+
SDL_GL_TEX2_NAME,
40+
SDL_GL_TEX3_NAME,
41+
SDL_GL_TEX4_NAME,
42+
SDL_GL_TEX5_NAME,
43+
SDL_GL_TEX6_NAME,
44+
SDL_GL_TEX7_NAME,
4145
} sdl_shader_hint;
4246

4347
typedef struct {
@@ -71,6 +75,22 @@ struct SDL_Uniform_t{
7175
void* driver_data;
7276
};
7377

78+
typedef struct SDL_Vertex_t SDL_Vertex;
79+
struct SDL_Vertex_t {
80+
unsigned size;
81+
void* vertexBuffer;
82+
void (*setVertexColor)(SDL_Vertex* vertices, unsigned from,
83+
unsigned num, uint8_t r, uint8_t g, uint8_t b, uint8_t a );
84+
void (*setVertexPosition)(SDL_Vertex* vertices, unsigned from,
85+
unsigned num, float x, float y, float z );
86+
void (*setVertexTexCoord)(SDL_Vertex* vertices, unsigned from,
87+
unsigned num, float s, float t );
88+
void (*getVertex)( SDL_Vertex* vertices, unsigned num,
89+
uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a,
90+
float *x, float *y, float *z,
91+
float *tex_s, float *tex_t );
92+
};
93+
7494
struct SDL_Shader_t {
7595
SDL_Renderer* renderer;
7696

@@ -79,19 +99,23 @@ struct SDL_Shader_t {
7999
int (*bindShader)( SDL_Shader* shader );
80100
int (*unbindShader)( SDL_Shader* shader );
81101
int (*destroyShader)( SDL_Shader* shader );
82-
int (*renderCopyShd)( SDL_Shader* shader, SDL_Texture* texture,
83-
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
102+
103+
int (*renderCopyShd)(SDL_Shader* shader, SDL_Texture** textures,
104+
unsigned num_of_tex, SDL_Vertex* vertices, unsigned num_of_vert);
105+
void (*updateViewport)( SDL_Shader *shader );
84106

85107
SDL_Uniform* (*createUniform)( SDL_Shader* shader, const char* name );
86108
int (*destroyUniform)( SDL_Shader* shader, SDL_Uniform* uniform );
87109

110+
SDL_Vertex* (*createVertexBuffer)( unsigned size );
111+
int (*destroyVertexBuffer)( SDL_Vertex* buff );
112+
88113
void* driver_data;
89114
};
90115

91116

92117

93-
void SDL_hint(sdl_shader_hint flag, void* value);
94-
118+
void SDL_shader_hint(sdl_shader_hint flag, void* value);
95119

96120
SDL_ShaderFileNames SDL_getShaderFileNames( SDL_Renderer* renderer,
97121
const char* name );
@@ -103,7 +127,12 @@ SDL_Shader* SDL_createShader_RW( SDL_Renderer *renderer,
103127
int SDL_destroyShader( SDL_Shader* shader );
104128
int SDL_bindShader( SDL_Shader* shader );
105129
int SDL_renderCopyShd( SDL_Shader* shader, SDL_Texture* texture,
106-
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
130+
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
131+
int SDL_renderCopyShdArray( SDL_Shader* shader, SDL_Texture** textures,
132+
const SDL_Rect * srcrect, const SDL_Rect * dstrect, unsigned num);
133+
int SDL_renderVertexBuffer( SDL_Shader* shader, SDL_Vertex *vertices,
134+
SDL_Texture** textures, unsigned num_of_tex);
135+
void SDL_updateViewport( SDL_Shader *shader );
107136

108137
SDL_Uniform* SDL_createUniform( SDL_Shader* shader, const char* name );
109138
int SDL_destroyUniform( SDL_Shader* shader, SDL_Uniform* uniform );

0 commit comments

Comments
 (0)